]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
Now the tree-like structure describing a DICOMDIR comming from an already
[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 gdcmDebug::gdcmDebug(int level) {
15    DebugLevel = level;
16 }
17
18 void gdcmDebug::Verbose(int Level, const char * Msg1, const char * Msg2) {
19    if (Level > DebugLevel)
20       return ;
21    std::cerr << Msg1 << ' ' << Msg2 << std::endl;
22 }
23
24 void gdcmDebug::Error( bool Test, const char * Msg1, const char * Msg2) {
25    if (!Test)
26       return;
27    std::cerr << Msg1 << ' ' << Msg2 << std::endl;
28    Exit(1);
29 }
30
31 void gdcmDebug::Error(const char* Msg1, const char* Msg2,
32                       const char* Msg3) {
33    std::cerr << Msg1 << ' ' << Msg2 << ' ' << Msg3 << std::endl;
34    Exit(1);
35 }
36
37 void gdcmDebug::Assert(int Level, bool Test,
38                  const char * Msg1, const char * Msg2) {
39    if (Level > DebugLevel)
40       return ;
41    if (!Test)
42       std::cerr << Msg1 << ' ' << Msg2 << std::endl;
43 }
44
45 void gdcmDebug::Exit(int a) {
46 #ifdef __GNUC__
47    std::exit(a);
48 #endif
49 #ifdef _MSC_VER
50    exit(a);    // Found in #include <stdlib.h>
51 #endif
52 }
53
54 //-----------------------------------------------------------------------------
55 gdcmDictSet         *gdcmGlobal::Dicts  = (gdcmDictSet *)0;
56 gdcmVR              *gdcmGlobal::VR     = (gdcmVR *)0;
57 gdcmTS              *gdcmGlobal::TS     = (gdcmTS *)0;
58 gdcmDicomDirElement *gdcmGlobal::ddElem = (gdcmDicomDirElement *)0;
59 gdcmGlobal gdcmGlob;
60
61 gdcmGlobal::gdcmGlobal(void) {
62    if (VR || TS || Dicts || ddElem)
63       dbg.Verbose(0, "gdcmGlobal::gdcmGlobal : VR or TS or Dicts already allocated");
64    Dicts  = new gdcmDictSet();
65    VR     = new gdcmVR();
66    TS     = new gdcmTS();
67    ddElem = new gdcmDicomDirElement();
68 }
69
70 gdcmGlobal::~gdcmGlobal() {
71    delete Dicts;
72    delete VR;
73    delete TS;
74    delete ddElem;
75 }
76
77 gdcmVR *gdcmGlobal::GetVR(void) {
78    return VR;
79 }
80
81 gdcmTS *gdcmGlobal::GetTS(void) {
82    return TS;
83 }
84
85 gdcmDictSet *gdcmGlobal::GetDicts(void) {
86    return Dicts;
87 }
88
89 gdcmDicomDirElement *gdcmGlobal::GetDicomDirElements(void) {
90    return ddElem;
91 }
92
93 //-----------------------------------------------------------------------------
94 // Here are some usefull functions, belonging to NO class,
95 // dealing with strings, file names, etc
96 // that can be called from anywhere
97 // by whomsoever they can help.
98 //-----------------------------------------------------------------------------
99
100 // Because is not yet available in g++2.96
101 std::istream& eatwhite(std::istream& is) {
102    char c;
103    while (is.get(c)) {
104       if (!isspace(c)) {
105          is.putback(c);
106          break;
107       }
108    }
109    return is;
110 }
111
112 ///////////////////////////////////////////////////////////////////////////
113 // Because is not  available in C++ (?)
114 void Tokenize (const std::string& str,
115                std::vector<std::string>& tokens,
116                const std::string& delimiters) {
117    std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
118    std::string::size_type pos     = str.find_first_of    (delimiters,lastPos);
119    while (std::string::npos != pos || std::string::npos != lastPos) {
120       tokens.push_back(str.substr(lastPos, pos - lastPos));
121       lastPos = str.find_first_not_of(delimiters, pos);
122       pos     = str.find_first_of    (delimiters, lastPos);
123    }
124 }
125
126
127 ///////////////////////////////////////////////////////////////////////////
128 // to prevent a flashing screen when non-printable character
129 char *_cleanString(char *v) {
130    char *d;
131    int i, l;
132    l = strlen(v);
133    for (i=0,d=v; 
134       i<l ; 
135       i++,d++) {
136          if (!isprint(*d))
137          *d = '.';
138    }    
139    return v;
140 }
141
142 ///////////////////////////////////////////////////////////////////////////
143 // to prevent a flashing screen when non-printable character
144 std::string _CreateCleanString(std::string s) {
145    std::string str=s;
146
147    for(int i=0;i<str.size();i++)
148    {
149       if(!isprint(str[i]))
150          str[i]='.';
151    }
152
153
154    if(str.size()>0)
155       if(!isprint(s[str.size()-1]))
156          if(s[str.size()-1]==0)
157             str[str.size()-1]=' ';
158
159    return(str);
160 }
161
162 ///////////////////////////////////////////////////////////////////////////
163 /*
164  * \brief   Add a SEPARATOR to the end of the name is necessary
165  * @param   
166  */
167 void NormalizePath(std::string &name)
168 {
169 const char SEPARATOR_X      = '/';
170 const char SEPARATOR_WIN    = '\\';
171 const std::string SEPARATOR = "/";
172    int size=name.size();
173    if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
174    {
175       name+=SEPARATOR;
176    }
177 }
178
179 ///////////////////////////////////////////////////////////////////////////
180 /*
181  * \brief   Get the (directory) path from a full path file name
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 /*
196  * \brief   Get the (last) name of a full path file name
197  */
198 std::string GetName(std::string &fullName)
199 {   
200   int fin=fullName.length()-1;
201   char a =fullName.c_str()[fin];
202   if (a == '/' || a == '\\') {
203      fin--;
204   }
205   int deb;
206   for (int i=fin;i!=0;i--) {
207      if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')  
208         break;
209       deb = i;
210   }    
211   std::string lastName;
212   for (int i=deb;i<fin+1;i++)
213     lastName=lastName+fullName.c_str()[i];
214   return(lastName);
215