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