]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
istream replaced by std::istream
[gdcm.git] / src / gdcmUtil.cxx
1 // $Header: /cvs/public/gdcm/src/gdcmUtil.cxx,v 1.21 2003/09/24 13:21:46 jpr Exp $
2
3 //This is needed when compiling in debug mode
4 #ifdef _MSC_VER
5 // 'identifier' : class 'type' needs to have dll-interface to be used by
6 // clients of class 'type2'
7 #pragma warning ( disable : 4251 )
8 // 'identifier' : identifier was truncated to 'number' characters in the
9 // debug information
10 #pragma warning ( disable : 4786 )
11 #endif //_MSC_VER
12
13 #include <stdio.h>
14 #include <ctype.h>   // For isspace
15 #include <string.h>
16 #include "gdcmUtil.h"
17
18 // Library globals.
19 gdcmDebug dbg;
20
21 gdcmDebug::gdcmDebug(int level) {
22    DebugLevel = level;
23 }
24
25 void gdcmDebug::Verbose(int Level, const char * Msg1, const char * Msg2) {
26    if (Level > DebugLevel)
27       return ;
28    std::cerr << Msg1 << ' ' << Msg2 << std::endl;
29 }
30
31 void gdcmDebug::Assert(int Level, bool Test,
32                  const char * Msg1, const char * Msg2) {
33    if (Level > DebugLevel)
34       return ;
35    if (!Test)
36       std::cerr << Msg1 << ' ' << Msg2 << std::endl;
37 }
38
39 void gdcmDebug::Error( bool Test, const char * Msg1, const char * Msg2) {
40    if (!Test)
41       return;
42    std::cerr << Msg1 << ' ' << Msg2 << std::endl;
43    Exit(1);
44 }
45
46 void gdcmDebug::Error(const char* Msg1, const char* Msg2,
47                       const char* Msg3) {
48    std::cerr << Msg1 << ' ' << Msg2 << ' ' << Msg3 << std::endl;
49    Exit(1);
50 }
51
52 void gdcmDebug::Exit(int a) {
53 #ifdef __GNUC__
54    std::exit(a);
55 #endif
56 #ifdef _MSC_VER
57    exit(a);    // Found in #include <stdlib.h>
58 #endif
59 }
60
61 ///////////////////////////////////////////////////////////////////////////
62 gdcmVR      * gdcmGlobal::VR    = (gdcmVR*)0;
63 gdcmTS      * gdcmGlobal::TS    = (gdcmTS*)0;
64 gdcmDictSet * gdcmGlobal::Dicts = (gdcmDictSet*)0;
65 gdcmGlobal gdcmGlob;
66
67 gdcmGlobal::gdcmGlobal(void) {
68    if (VR || TS || Dicts)
69       dbg.Verbose(0, "gdcmGlobal::gdcmGlobal : VR or TS or Dicts already allocated");
70    VR = new gdcmVR();
71    TS = new gdcmTS();
72    Dicts = new gdcmDictSet();
73 }
74
75 gdcmGlobal::~gdcmGlobal() {
76    delete VR;
77    delete TS;
78    delete Dicts;
79 }
80
81 gdcmVR * gdcmGlobal::GetVR(void) {
82    return VR;
83 }
84
85 gdcmTS * gdcmGlobal::GetTS(void) {
86    return TS;
87 }
88 gdcmDictSet * gdcmGlobal::GetDicts(void) {
89    return Dicts;
90 }
91
92 ///////////////////////////////////////////////////////////////////////////
93 // Because is not yet available in g++2.96
94 std::istream& eatwhite(std::istream& is) {
95    char c;
96    while (is.get(c)) {
97       if (!isspace(c)) {
98          is.putback(c);
99          break;
100       }
101    }
102    return is;
103 }
104
105 ///////////////////////////////////////////////////////////////////////////
106 // Because is not  available in C++ (?)
107
108 void Tokenize (const std::string& str,
109                std::vector<std::string>& tokens,
110                const std::string& delimiters) {
111    string::size_type lastPos = str.find_first_not_of(delimiters,0);
112    string::size_type pos     = str.find_first_of    (delimiters,lastPos);
113    while (string::npos != pos || string::npos != lastPos) {
114       tokens.push_back(str.substr(lastPos, pos - lastPos));
115       lastPos = str.find_first_not_of(delimiters, pos);
116       pos     = str.find_first_of    (delimiters, lastPos);
117    }
118 }
119
120
121 ///////////////////////////////////////////////////////////////////////////
122 // to prevent a flashing screen when non-printable character
123
124 char * _cleanString(char *v) {
125    char *d;
126    int i, l;
127    l = strlen(v);
128    for (i=0,d=v; 
129         i<l ; 
130         i++,d++) {
131       if (!isprint(*d))
132          *d = '.';
133       } 
134    return v;
135 }
136
137
138 ///////////////////////////////////////////////////////////////////////////
139 // to prevent a flashing screen when non-printable character
140
141 std::string _CreateCleanString(std::string s) {
142 /*   char *d, *di, *v;
143    int i, l;
144    v=(char*)s.c_str();
145    l = strlen(v);
146    d = di = strdup(v);
147    for (i=0; 
148         i<l ; 
149         i++,di++,v++) {
150       if (!isprint(*v))
151          *di = '.';
152       } 
153    return d;*/
154   string str=s;
155   for(int i=0;i<str.size();i++)
156   {
157     if(!isprint(str[i]))
158       str[i]='.';
159   }
160
161   return(str);
162 }
163
164 ///////////////////////////////////////////////////////////////////////////
165 //
166 // because it may not be associated to a dictionary ...
167
168 std::string TranslateToKey(guint16 group, guint16 element) {
169         char trash[10];
170         string key;
171         // CLEAN ME: better call the iostream<< with the hex manipulator on.
172         // This requires some reading of the stdlibC++ sources to make the
173         // proper call (or copy).
174         sprintf(trash, "%04x|%04x", group , element);
175         key = trash;  // Convertion through assignement
176         return key;
177 }
178
179
180