]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
* src/gdcmUtil.[cxx|h] split in two. Additional file gdcmGlobal.[cxx|h]
[gdcm.git] / src / gdcmUtil.cxx
1 // gdcmUtil.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmUtil.h"
4 #include "gdcmDebug.h"
5 #include <stdio.h>
6 #include <ctype.h>   // For isspace
7 #include <string.h>  // CLEANME: could this be only string ? Related to Win32 ?
8
9 /**
10  * \ingroup Globals
11  * \brief   Because is not yet available in g++2.96
12  */
13 std::istream& eatwhite(std::istream& is) {
14    char c;
15    while (is.get(c)) {
16       if (!isspace(c)) {
17          is.putback(c);
18          break;
19       }
20    }
21    return is;
22 }
23
24 /**
25  * \ingroup Globals
26  * \brief Because not available in C++ (?)
27  */
28 void Tokenize (const std::string& str,
29                std::vector<std::string>& tokens,
30                const std::string& delimiters) {
31    std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
32    std::string::size_type pos     = str.find_first_of    (delimiters,lastPos);
33    while (std::string::npos != pos || std::string::npos != lastPos) {
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  Weed out a string from the non-printable characters (in order
43  *         to avoid corrupting the terminal of invocation when printing)
44  * @param s string to remove non printable characters from
45  */
46 std::string CreateCleanString(std::string s) {
47    std::string str=s;
48
49    for(int i=0;i<str.size();i++)
50    {
51       if(!isprint(str[i]))
52          str[i]='.';
53    }
54
55    if(str.size()>0)
56       if(!isprint(s[str.size()-1]))
57          if(s[str.size()-1]==0)
58             str[str.size()-1]=' ';
59
60    return(str);
61 }
62
63 /**
64  * \ingroup Globals
65  * \brief   Add a SEPARATOR to the end of the name is necessary
66  * @param name file/directory name to normalize 
67  */
68 void NormalizePath(std::string &name)
69 {
70    const char SEPARATOR_X      = '/';
71    const char SEPARATOR_WIN    = '\\';
72    const std::string SEPARATOR = "/";
73    int size=name.size();
74
75    if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
76    {
77       name+=SEPARATOR;
78    }
79 }
80
81 /**
82  * \ingroup Globals
83  * \brief   Get the (directory) path from a full path file name
84  * @param   fullName file/directory name to extract Path from
85  */
86 std::string GetPath(std::string &fullName)
87 {
88    int pos1=fullName.rfind("/");
89    int pos2=fullName.rfind("\\");
90    if(pos1>pos2)
91       fullName.resize(pos1);
92    else
93       fullName.resize(pos2);
94    return(fullName);
95 }
96
97 /**
98  * \ingroup Globals
99  * \brief   Get the (last) name of a full path file name
100  * @param   fullName file/directory name to extract end name from
101  */
102 std::string GetName(std::string &fullName)
103 {   
104   int fin=fullName.length()-1;
105   char a =fullName.c_str()[fin];
106   if (a == '/' || a == '\\') {
107      fin--;
108   }
109   int deb;
110   for (int i=fin;i!=0;i--) {
111      if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')  
112         break;
113       deb = i;
114   }    
115
116   std::string lastName;
117   for (int j=deb;j<fin+1;j++)
118     lastName=lastName+fullName.c_str()[j];
119
120   return(lastName);
121