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