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