]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
* src/gdcmUtil.[cxx|h] split in two. Additional file gdcmDebug.[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 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
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 gdcmVR *gdcmGlobal::GetVR(void) {
66    return VR;
67 }
68
69 gdcmTS *gdcmGlobal::GetTS(void) {
70    return TS;
71 }
72
73 gdcmDictSet *gdcmGlobal::GetDicts(void) {
74    return Dicts;
75 }
76
77 gdcmDicomDirElement *gdcmGlobal::GetDicomDirElements(void) {
78    return ddElem;
79 }
80
81 /**
82  * \defgroup Globals Utility functions
83  * \brief    Here are some utility functions, belonging to NO class,
84  *           dealing with strings, file names... that can be called
85  *           from anywhere by whomsoever they can help.
86  */
87
88 /**
89  * \ingroup Globals
90  * \brief   Because is not yet available in g++2.96
91  */
92 std::istream& eatwhite(std::istream& is) {
93    char c;
94    while (is.get(c)) {
95       if (!isspace(c)) {
96          is.putback(c);
97          break;
98       }
99    }
100    return is;
101 }
102
103 /**
104  * \ingroup Globals
105  * \brief Because not available in C++ (?)
106  */
107 void Tokenize (const std::string& str,
108                std::vector<std::string>& tokens,
109                const std::string& delimiters) {
110    std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
111    std::string::size_type pos     = str.find_first_of    (delimiters,lastPos);
112    while (std::string::npos != pos || std::string::npos != lastPos) {
113       tokens.push_back(str.substr(lastPos, pos - lastPos));
114       lastPos = str.find_first_not_of(delimiters, pos);
115       pos     = str.find_first_of    (delimiters, lastPos);
116    }
117 }
118
119 /**
120  * \ingroup Globals
121  * \brief  Weed out a string from the non-printable characters (in order
122  *         to avoid corrupting the terminal of invocation when printing)
123  * @param v characters array to remove non printable characters from
124  */
125 char *_cleanString(char *v) {
126    char *d;
127    int i, l;
128    l = strlen(v);
129    for (i=0,d=v; 
130       i<l ; 
131       i++,d++) {
132          if (!isprint(*d))
133          *d = '.';
134    }
135    return v;
136 }
137
138 /**
139  * \ingroup Globals
140  * \brief   to prevent a flashing screen when non-printable character
141  * @param s string to remove non printable characters from
142  */
143 std::string _CreateCleanString(std::string s) {
144    std::string str=s;
145
146    for(int i=0;i<str.size();i++)
147    {
148       if(!isprint(str[i]))
149          str[i]='.';
150    }
151
152    if(str.size()>0)
153       if(!isprint(s[str.size()-1]))
154          if(s[str.size()-1]==0)
155             str[str.size()-1]=' ';
156
157    return(str);
158 }
159
160 /**
161  * \ingroup Globals
162  * \brief   Add a SEPARATOR to the end of the name is necessary
163  * @param name file/directory name to normalize 
164  */
165 void NormalizePath(std::string &name)
166 {
167    const char SEPARATOR_X      = '/';
168    const char SEPARATOR_WIN    = '\\';
169    const std::string SEPARATOR = "/";
170    int size=name.size();
171
172    if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
173    {
174       name+=SEPARATOR;
175    }
176 }
177
178 /**
179  * \ingroup Globals
180  * \brief   Get the (directory) path from a full path file name
181  * @param   fullName file/directory name to extract Path from
182  */
183 std::string GetPath(std::string &fullName)
184 {
185    int pos1=fullName.rfind("/");
186    int pos2=fullName.rfind("\\");
187    if(pos1>pos2)
188       fullName.resize(pos1);
189    else
190       fullName.resize(pos2);
191    return(fullName);
192 }
193
194 /**
195  * \ingroup Globals
196  * \brief   Get the (last) name of a full path file name
197  * @param   fullName file/directory name to extract end name from
198  */
199 std::string GetName(std::string &fullName)
200 {   
201   int fin=fullName.length()-1;
202   char a =fullName.c_str()[fin];
203   if (a == '/' || a == '\\') {
204      fin--;
205   }
206   int deb;
207   for (int i=fin;i!=0;i--) {
208      if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')  
209         break;
210       deb = i;
211   }    
212
213   std::string lastName;
214   for (int j=deb;j<fin+1;j++)
215     lastName=lastName+fullName.c_str()[j];
216
217   return(lastName);
218