]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
doxygenation
[gdcm.git] / src / gdcmUtil.cxx
1 // gdcmUtil.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmUtil.h"
4
5 #include <stdio.h>
6 #include <ctype.h>   // For isspace
7 #include <string.h>
8
9 //-----------------------------------------------------------------------------
10 // Library globals.
11 gdcmDebug dbg;
12
13 //-----------------------------------------------------------------------------
14 /**
15  * \ingroup gdcmDebug
16  * \brief   constructor
17  * @param level debug level
18  */ 
19
20 gdcmDebug::gdcmDebug(int level) {
21    DebugLevel = level;
22 }
23
24 /**
25  * \ingroup gdcmDebug
26  * \brief   Verbose 
27  * @param Level level
28  * @param Msg1 first message part
29  * @param Msg2 second message part 
30  */
31 void gdcmDebug::Verbose(int Level, const char * Msg1, const char * Msg2) {
32    if (Level > DebugLevel)
33       return ;
34    std::cerr << Msg1 << ' ' << Msg2 << std::endl;
35 }
36
37 /**
38  * \ingroup gdcmDebug
39  * \brief   Error 
40  * @param Test test
41  * @param Msg1 first message part
42  * @param Msg2 second message part 
43  */
44 void gdcmDebug::Error( bool Test, const char * Msg1, const char * Msg2) {
45    if (!Test)
46       return;
47    std::cerr << Msg1 << ' ' << Msg2 << std::endl;
48    Exit(1);
49 }
50
51 /**
52  * \ingroup gdcmDebug
53  * \brief   Error 
54  * @param Msg1 first message part
55  * @param Msg2 second message part
56  * @param Msg3 Third message part  
57  */
58 void gdcmDebug::Error(const char* Msg1, const char* Msg2,
59                       const char* Msg3) {
60    std::cerr << Msg1 << ' ' << Msg2 << ' ' << Msg3 << std::endl;
61    Exit(1);
62 }
63
64 /**
65  * \ingroup gdcmDebug
66  * \brief   Assert 
67  * @param Level level 
68  * @param Test test
69  * @param Msg1 first message part
70  * @param Msg2 second message part
71  */
72  void gdcmDebug::Assert(int Level, bool Test,
73                  const char * Msg1, const char * Msg2) {
74    if (Level > DebugLevel)
75       return ;
76    if (!Test)
77       std::cerr << Msg1 << ' ' << Msg2 << std::endl;
78 }
79
80 /**
81  * \ingroup gdcmDebug
82  * \brief   Exit 
83  * @param a return code 
84  */
85 void gdcmDebug::Exit(int a) {
86 #ifdef __GNUC__
87    std::exit(a);
88 #endif
89 #ifdef _MSC_VER
90    exit(a);    // Found in #include <stdlib.h>
91 #endif
92 }
93
94 //-----------------------------------------------------------------------------
95 /// Pointer to a container, holding *all* the Dicom Dictionaries
96 gdcmDictSet         *gdcmGlobal::Dicts  = (gdcmDictSet *)0;
97 /// Pointer to a H table containing the 'Value Representations'
98 gdcmVR              *gdcmGlobal::VR     = (gdcmVR *)0;
99 /// Pointer to a H table containing the Transfer Syntax codes and their english description 
100 gdcmTS              *gdcmGlobal::TS     = (gdcmTS *)0;
101 /// Pointer to a H table containing the Dicom Elements necessary to describe each part of a DICOMDIR 
102 gdcmDicomDirElement *gdcmGlobal::ddElem = (gdcmDicomDirElement *)0;
103 /// gdcm Glob
104 gdcmGlobal gdcmGlob;
105
106
107 /**
108  * \ingroup gdcmGlobal
109  * \brief   constructor : populates the
110  */
111 gdcmGlobal::gdcmGlobal(void) {
112    if (VR || TS || Dicts || ddElem)
113       dbg.Verbose(0, "gdcmGlobal::gdcmGlobal : VR or TS or Dicts already allocated");
114    Dicts  = new gdcmDictSet();
115    VR     = new gdcmVR();
116    TS     = new gdcmTS();
117    ddElem = new gdcmDicomDirElement();
118 }
119
120 /**
121  * \ingroup gdcmGlobal
122  * \brief   canonical destructor 
123  */
124 gdcmGlobal::~gdcmGlobal() {
125    delete Dicts;
126    delete VR;
127    delete TS;
128    delete ddElem;
129 }
130
131 gdcmVR *gdcmGlobal::GetVR(void) {
132    return VR;
133 }
134
135 gdcmTS *gdcmGlobal::GetTS(void) {
136    return TS;
137 }
138
139 gdcmDictSet *gdcmGlobal::GetDicts(void) {
140    return Dicts;
141 }
142
143 gdcmDicomDirElement *gdcmGlobal::GetDicomDirElements(void) {
144    return ddElem;
145 }
146
147 //-----------------------------------------------------------------------------
148 // Here are some usefull functions, belonging to NO class,
149 // dealing with strings, file names, etc
150 // that can be called from anywhere
151 // by whomsoever they can help.
152 //-----------------------------------------------------------------------------
153
154 // Because is not yet available in g++2.96
155 std::istream& eatwhite(std::istream& is) {
156    char c;
157    while (is.get(c)) {
158       if (!isspace(c)) {
159          is.putback(c);
160          break;
161       }
162    }
163    return is;
164 }
165
166 ///////////////////////////////////////////////////////////////////////////
167 // Because is not  available in C++ (?)
168 void Tokenize (const std::string& str,
169                std::vector<std::string>& tokens,
170                const std::string& delimiters) {
171    std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
172    std::string::size_type pos     = str.find_first_of    (delimiters,lastPos);
173    while (std::string::npos != pos || std::string::npos != lastPos) {
174       tokens.push_back(str.substr(lastPos, pos - lastPos));
175       lastPos = str.find_first_not_of(delimiters, pos);
176       pos     = str.find_first_of    (delimiters, lastPos);
177    }
178 }
179
180
181 ///////////////////////////////////////////////////////////////////////////
182 /*
183  * \brief  to prevent a flashing screen when non-printable character
184  * @param 
185  */
186 char *_cleanString(char *v) {
187    char *d;
188    int i, l;
189    l = strlen(v);
190    for (i=0,d=v; 
191       i<l ; 
192       i++,d++) {
193          if (!isprint(*d))
194          *d = '.';
195    }    
196    return v;
197 }
198
199 ///////////////////////////////////////////////////////////////////////////
200 /*
201  * \brief   to prevent a flashing screen when non-printable character
202  * @param 
203  */
204 std::string _CreateCleanString(std::string s) {
205    std::string str=s;
206
207    for(int i=0;i<str.size();i++)
208    {
209       if(!isprint(str[i]))
210          str[i]='.';
211    }
212
213
214    if(str.size()>0)
215       if(!isprint(s[str.size()-1]))
216          if(s[str.size()-1]==0)
217             str[str.size()-1]=' ';
218
219    return(str);
220 }
221
222 ///////////////////////////////////////////////////////////////////////////
223 /*
224  * \brief   Add a SEPARATOR to the end of the name is necessary
225  * @param   
226  */
227 void NormalizePath(std::string &name)
228 {
229 const char SEPARATOR_X      = '/';
230 const char SEPARATOR_WIN    = '\\';
231 const std::string SEPARATOR = "/";
232    int size=name.size();
233    if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
234    {
235       name+=SEPARATOR;
236    }
237 }
238
239 ///////////////////////////////////////////////////////////////////////////
240 /*
241  * \brief   Get the (directory) path from a full path file name
242  * @param 
243  */
244 std::string GetPath(std::string &fullName)
245 {
246    int pos1=fullName.rfind("/");
247    int pos2=fullName.rfind("\\");
248    if(pos1>pos2)
249       fullName.resize(pos1);
250    else
251       fullName.resize(pos2);
252    return(fullName);
253 }
254
255 ///////////////////////////////////////////////////////////////////////////
256 /*
257  * \brief   Get the (last) name of a full path file name
258  * @param 
259  */
260 std::string GetName(std::string &fullName)
261 {   
262   int fin=fullName.length()-1;
263   char a =fullName.c_str()[fin];
264   if (a == '/' || a == '\\') {
265      fin--;
266   }
267   int deb;
268   for (int i=fin;i!=0;i--) {
269      if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')  
270         break;
271       deb = i;
272   }    
273
274   std::string lastName;
275   for (int j=deb;j<fin+1;j++)
276     lastName=lastName+fullName.c_str()[j];
277
278   return(lastName);
279