]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
some more doxygenation
[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 v characters array to remove non printable characters from
137  */
138 char *_cleanString(char *v) {
139    char *d;
140    int i, l;
141    l = strlen(v);
142    for (i=0,d=v; 
143       i<l ; 
144       i++,d++) {
145          if (!isprint(*d))
146          *d = '.';
147    }
148    return v;
149 }
150
151 /**
152  * \ingroup Globals
153  * \brief   to prevent a flashing screen when non-printable character
154  * @param s string to remove non printable characters from
155  */
156 std::string _CreateCleanString(std::string s) {
157    std::string str=s;
158
159    for(int i=0;i<str.size();i++)
160    {
161       if(!isprint(str[i]))
162          str[i]='.';
163    }
164
165    if(str.size()>0)
166       if(!isprint(s[str.size()-1]))
167          if(s[str.size()-1]==0)
168             str[str.size()-1]=' ';
169
170    return(str);
171 }
172
173 /**
174  * \ingroup Globals
175  * \brief   Add a SEPARATOR to the end of the name is necessary
176  * @param name file/directory name to normalize 
177  */
178 void NormalizePath(std::string &name)
179 {
180    const char SEPARATOR_X      = '/';
181    const char SEPARATOR_WIN    = '\\';
182    const std::string SEPARATOR = "/";
183    int size=name.size();
184
185    if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
186    {
187       name+=SEPARATOR;
188    }
189 }
190
191 /**
192  * \ingroup Globals
193  * \brief   Get the (directory) path from a full path file name
194  * @param   fullName file/directory name to extract Path from
195  */
196 std::string GetPath(std::string &fullName)
197 {
198    int pos1=fullName.rfind("/");
199    int pos2=fullName.rfind("\\");
200    if(pos1>pos2)
201       fullName.resize(pos1);
202    else
203       fullName.resize(pos2);
204    return(fullName);
205 }
206
207 /**
208  * \ingroup Globals
209  * \brief   Get the (last) name of a full path file name
210  * @param   fullName file/directory name to extract end name from
211  */
212 std::string GetName(std::string &fullName)
213 {   
214   int fin=fullName.length()-1;
215   char a =fullName.c_str()[fin];
216   if (a == '/' || a == '\\') {
217      fin--;
218   }
219   int deb;
220   for (int i=fin;i!=0;i--) {
221      if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')  
222         break;
223       deb = i;
224   }    
225
226   std::string lastName;
227   for (int j=deb;j<fin+1;j++)
228     lastName=lastName+fullName.c_str()[j];
229
230   return(lastName);
231