]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
src/*.cxx removed pragma thingies to src/gdcmCommon.h
[gdcm.git] / src / gdcmUtil.cxx
1 // $Header: /cvs/public/gdcm/src/gdcmUtil.cxx,v 1.25 2003/10/02 11:26:16 malaterre Exp $
2
3 #include "gdcmUtil.h"
4
5 #include <stdio.h>
6 #include <ctype.h>   // For isspace
7 #include <string.h>
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 << std::endl;
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 << std::endl;
28 }
29
30 void gdcmDebug::Error( bool Test, const char * Msg1, const char * Msg2) {
31    if (!Test)
32       return;
33    std::cerr << Msg1 << ' ' << Msg2 << std::endl;
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 << std::endl;
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 std::istream& eatwhite(std::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 std::string& str,
100                std::vector<std::string>& tokens,
101                const std::string& delimiters) {
102    std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
103    std::string::size_type pos     = str.find_first_of    (delimiters,lastPos);
104    while (std::string::npos != pos || std::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 std::string _CreateCleanString(std::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   std::string str=s;
146   for(int i=0;i<str.size();i++)
147   {
148     if(!isprint(str[i]))
149       str[i]='.';
150   }
151
152   return(str);
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         std::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