]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
* src/gdcmUtil.[cxx|h] _cleanString C style function (replaced with
[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 Pointer to a container, holding _all_ the Dicom Dictionaries.
12  */
13 gdcmDictSet         *gdcmGlobal::Dicts  = (gdcmDictSet *)0;
14
15 /**
16  * \ingroup Globals
17  * \brief   Pointer to a hash table containing the 'Value Representations'.
18  */
19 gdcmVR              *gdcmGlobal::VR     = (gdcmVR *)0;
20
21 /**
22  * \ingroup Globals
23  * \brief   Pointer to a hash table containing the Transfer Syntax codes
24  *          and their english description 
25  */
26 gdcmTS              *gdcmGlobal::TS     = (gdcmTS *)0;
27
28 /**
29  * \ingroup Globals
30  * \brief   Pointer to the hash table containing the Dicom Elements
31  *          necessary to describe each part of a DICOMDIR 
32  */
33 gdcmDicomDirElement *gdcmGlobal::ddElem = (gdcmDicomDirElement *)0;
34
35 /**
36  * \ingroup Globals
37  * \brief   Global container
38  */
39 gdcmGlobal gdcmGlob;
40
41 /**
42  * \ingroup gdcmGlobal
43  * \brief   constructor : populates the various H Tables
44  */
45 gdcmGlobal::gdcmGlobal(void) {
46    if (VR || TS || Dicts || ddElem)
47       dbg.Verbose(0, "gdcmGlobal::gdcmGlobal : VR or TS or Dicts already allocated");
48    Dicts  = new gdcmDictSet();
49    VR     = new gdcmVR();
50    TS     = new gdcmTS();
51    ddElem = new gdcmDicomDirElement();
52 }
53
54 /**
55  * \ingroup gdcmGlobal
56  * \brief   canonical destructor 
57  */
58 gdcmGlobal::~gdcmGlobal() {
59    delete Dicts;
60    delete VR;
61    delete TS;
62    delete ddElem;
63 }
64 /**
65  * \ingroup gdcmGlobal
66  * \brief   returns a pointer to the 'Value Representation Table' 
67  */
68 gdcmVR *gdcmGlobal::GetVR(void) {
69    return VR;
70 }
71 /**
72  * \ingroup gdcmGlobal
73  * \brief   returns a pointer to the 'Transfert Syntax Table' 
74  */
75 gdcmTS *gdcmGlobal::GetTS(void) {
76    return TS;
77 }
78 /**
79  * \ingroup gdcmGlobal
80  * \brief   returns a pointer to Dictionaries Table 
81  */
82 gdcmDictSet *gdcmGlobal::GetDicts(void) {
83    return Dicts;
84 }
85 /**
86  * \ingroup gdcmGlobal
87  * \brief   returns a pointer to the DicomDir related elements Table 
88  */
89 gdcmDicomDirElement *gdcmGlobal::GetDicomDirElements(void) {
90    return ddElem;
91 }
92
93 /**
94  * \defgroup Globals Utility functions
95  * \brief    Here are some utility functions, belonging to NO class,
96  *           dealing with strings, file names... that can be called
97  *           from anywhere by whomsoever they can help.
98  */
99
100
101 /**
102  * \ingroup Globals
103  * \brief   Because is not yet available in g++2.96
104  */
105 std::istream& eatwhite(std::istream& is) {
106    char c;
107    while (is.get(c)) {
108       if (!isspace(c)) {
109          is.putback(c);
110          break;
111       }
112    }
113    return is;
114 }
115
116 /**
117  * \ingroup Globals
118  * \brief Because not available in C++ (?)
119  */
120 void Tokenize (const std::string& str,
121                std::vector<std::string>& tokens,
122                const std::string& delimiters) {
123    std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
124    std::string::size_type pos     = str.find_first_of    (delimiters,lastPos);
125    while (std::string::npos != pos || std::string::npos != lastPos) {
126       tokens.push_back(str.substr(lastPos, pos - lastPos));
127       lastPos = str.find_first_not_of(delimiters, pos);
128       pos     = str.find_first_of    (delimiters, lastPos);
129    }
130 }
131
132 /**
133  * \ingroup Globals
134  * \brief  Weed out a string from the non-printable characters (in order
135  *         to avoid corrupting the terminal of invocation when printing)
136  * @param s string to remove non printable characters from
137  */
138 std::string CreateCleanString(std::string s) {
139    std::string str=s;
140
141    for(int i=0;i<str.size();i++)
142    {
143       if(!isprint(str[i]))
144          str[i]='.';
145    }
146
147    if(str.size()>0)
148       if(!isprint(s[str.size()-1]))
149          if(s[str.size()-1]==0)
150             str[str.size()-1]=' ';
151
152    return(str);
153 }
154
155 /**
156  * \ingroup Globals
157  * \brief   Add a SEPARATOR to the end of the name is necessary
158  * @param name file/directory name to normalize 
159  */
160 void NormalizePath(std::string &name)
161 {
162    const char SEPARATOR_X      = '/';
163    const char SEPARATOR_WIN    = '\\';
164    const std::string SEPARATOR = "/";
165    int size=name.size();
166
167    if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
168    {
169       name+=SEPARATOR;
170    }
171 }
172
173 /**
174  * \ingroup Globals
175  * \brief   Get the (directory) path from a full path file name
176  * @param   fullName file/directory name to extract Path from
177  */
178 std::string GetPath(std::string &fullName)
179 {
180    int pos1=fullName.rfind("/");
181    int pos2=fullName.rfind("\\");
182    if(pos1>pos2)
183       fullName.resize(pos1);
184    else
185       fullName.resize(pos2);
186    return(fullName);
187 }
188
189 /**
190  * \ingroup Globals
191  * \brief   Get the (last) name of a full path file name
192  * @param   fullName file/directory name to extract end name from
193  */
194 std::string GetName(std::string &fullName)
195 {   
196   int fin=fullName.length()-1;
197   char a =fullName.c_str()[fin];
198   if (a == '/' || a == '\\') {
199      fin--;
200   }
201   int deb;
202   for (int i=fin;i!=0;i--) {
203      if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')  
204         break;
205       deb = i;
206   }    
207
208   std::string lastName;
209   for (int j=deb;j<fin+1;j++)
210     lastName=lastName+fullName.c_str()[j];
211
212   return(lastName);
213