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