]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
ENH: If possible move the stl include after the gdcm ones, to avoid vc warnings
[gdcm.git] / src / gdcmUtil.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmUtil.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/10/08 04:43:38 $
7   Version:   $Revision: 1.51 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmUtil.h"
20 #include "gdcmDebug.h"
21
22 /**
23  * \ingroup Globals
24  * \brief Because not available in C++ (?)
25  */
26 void Tokenize (const std::string& str,
27                std::vector<std::string>& tokens,
28                const std::string& delimiters)
29 {
30    std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
31    std::string::size_type pos     = str.find_first_of    (delimiters,lastPos);
32    while (std::string::npos != pos || std::string::npos != lastPos)
33    {
34       tokens.push_back(str.substr(lastPos, pos - lastPos));
35       lastPos = str.find_first_not_of(delimiters, pos);
36       pos     = str.find_first_of    (delimiters, lastPos);
37    }
38 }
39
40 /**
41  * \ingroup Globals
42  * \brief Because not available in C++ (?)
43  *        Counts the number of occurences of a substring within a string
44  */
45  
46 int CountSubstring (const std::string& str,
47                     const std::string& subStr)
48 {
49    int count = 0;   // counts how many times it appears
50    unsigned int x = 0;       // The index position in the string
51
52    do
53    {
54       x = str.find(subStr,x);       // Find the substring
55       if (x != std::string::npos)   // If present
56       {
57          count++;                  // increase the count
58          x += subStr.length();     // Skip this word
59       }
60    }
61    while (x != std::string::npos);  // Carry on until not present
62
63    return count;
64 }
65
66 /**
67  * \ingroup Globals
68  * \brief  Weed out a string from the non-printable characters (in order
69  *         to avoid corrupting the terminal of invocation when printing)
70  * @param s string to remove non printable characters from
71  */
72 std::string CreateCleanString(std::string s)
73 {
74    std::string str = s;
75
76    for(unsigned int i=0;i<str.size();i++)
77    {
78       if(!isprint(str[i]))
79       {
80          str[i]='.';
81       }
82    }
83
84    if(str.size()>0)
85    {
86       if(!isprint(s[str.size()-1]))
87       {
88          if(s[str.size()-1]==0)
89          {
90             str[str.size()-1]=' ';
91          }
92       }
93    }
94
95    return str;
96 }
97
98 /**
99  * \ingroup Globals
100  * \brief   Add a SEPARATOR to the end of the name is necessary
101  * @param name file/directory name to normalize 
102  */
103 void NormalizePath(std::string &name)
104 {
105    const char SEPARATOR_X      = '/';
106    const char SEPARATOR_WIN    = '\\';
107    const std::string SEPARATOR = "/";
108    int size = name.size();
109
110    if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
111    {
112       name+=SEPARATOR;
113    }
114 }
115
116 /**
117  * \ingroup Globals
118  * \brief   Get the (directory) path from a full path file name
119  * @param   fullName file/directory name to extract Path from
120  */
121 std::string GetPath(std::string &fullName)
122 {
123    int pos1 = fullName.rfind("/");
124    int pos2 = fullName.rfind("\\");
125    if( pos1 > pos2)
126    {
127       fullName.resize(pos1);
128    }
129    else
130    {
131       fullName.resize(pos2);
132    }
133
134    return fullName;
135 }
136
137 /**
138  * \ingroup Globals
139  * \brief   Get the (last) name of a full path file name
140  * @param   fullName file/directory name to extract end name from
141  */
142 std::string GetName(std::string &fullName)
143 {   
144    int fin = fullName.length()-1;
145    char a =fullName.c_str()[fin];
146    if (a == '/' || a == '\\')
147    {
148       fin--;
149    }
150    int deb = 0;
151    for (int i=fin;i!=0;i--)
152    {
153       if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')
154       {
155          break;
156       }
157       deb = i;
158    }
159
160    std::string lastName;
161    for (int j=deb;j<fin+1;j++)
162    {
163       lastName=lastName+fullName.c_str()[j];
164    }
165
166   return lastName;
167