]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
b642db190e39a3950a25123f7b6e4b27e2f6d39c
[gdcm.git] / src / gdcmUtil.cxx
1 // gdcmUtil.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmUtil.h"
4
5 #include <stdio.h>
6 #include <ctype.h>   // For isspace
7 #include <string.h>
8
9 //-----------------------------------------------------------------------------
10 // Library globals.
11 gdcmDebug dbg;
12
13 //-----------------------------------------------------------------------------
14 gdcmDebug::gdcmDebug(int level) {
15    DebugLevel = level;
16 }
17
18 void gdcmDebug::Verbose(int Level, const char * Msg1, const char * Msg2) {
19    if (Level > DebugLevel)
20       return ;
21    std::cerr << Msg1 << ' ' << Msg2 << std::endl;
22 }
23
24 void gdcmDebug::Error( bool Test, const char * Msg1, const char * Msg2) {
25    if (!Test)
26       return;
27    std::cerr << Msg1 << ' ' << Msg2 << std::endl;
28    Exit(1);
29 }
30
31 void gdcmDebug::Error(const char* Msg1, const char* Msg2,
32                       const char* Msg3) {
33    std::cerr << Msg1 << ' ' << Msg2 << ' ' << Msg3 << std::endl;
34    Exit(1);
35 }
36
37 void gdcmDebug::Assert(int Level, bool Test,
38                  const char * Msg1, const char * Msg2) {
39    if (Level > DebugLevel)
40       return ;
41    if (!Test)
42       std::cerr << Msg1 << ' ' << Msg2 << std::endl;
43 }
44
45 void gdcmDebug::Exit(int a) {
46 #ifdef __GNUC__
47    std::exit(a);
48 #endif
49 #ifdef _MSC_VER
50    exit(a);    // Found in #include <stdlib.h>
51 #endif
52 }
53
54 //-----------------------------------------------------------------------------
55 gdcmVR      *gdcmGlobal::VR    = (gdcmVR *)0;
56 gdcmTS      *gdcmGlobal::TS    = (gdcmTS *)0;
57 gdcmDictSet *gdcmGlobal::Dicts = (gdcmDictSet *)0;
58 gdcmGlobal gdcmGlob;
59
60 gdcmGlobal::gdcmGlobal(void) {
61    if (VR || TS || Dicts)
62       dbg.Verbose(0, "gdcmGlobal::gdcmGlobal : VR or TS or Dicts already allocated");
63    VR = new gdcmVR();
64    TS = new gdcmTS();
65    Dicts = new gdcmDictSet();
66 }
67
68 gdcmGlobal::~gdcmGlobal() {
69    delete VR;
70    delete TS;
71    delete Dicts;
72 }
73
74 gdcmVR *gdcmGlobal::GetVR(void) {
75    return VR;
76 }
77
78 gdcmTS *gdcmGlobal::GetTS(void) {
79    return TS;
80 }
81
82 gdcmDictSet *gdcmGlobal::GetDicts(void) {
83    return Dicts;
84 }
85
86 //-----------------------------------------------------------------------------
87 // Because is not yet available in g++2.96
88 std::istream& eatwhite(std::istream& is) {
89    char c;
90    while (is.get(c)) {
91       if (!isspace(c)) {
92          is.putback(c);
93          break;
94       }
95    }
96    return is;
97 }
98
99 ///////////////////////////////////////////////////////////////////////////
100 // Because is not  available in C++ (?)
101 void Tokenize (const std::string& str,
102                std::vector<std::string>& tokens,
103                const std::string& delimiters) {
104    std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
105    std::string::size_type pos     = str.find_first_of    (delimiters,lastPos);
106    while (std::string::npos != pos || std::string::npos != lastPos) {
107       tokens.push_back(str.substr(lastPos, pos - lastPos));
108       lastPos = str.find_first_not_of(delimiters, pos);
109       pos     = str.find_first_of    (delimiters, lastPos);
110    }
111 }
112
113
114 ///////////////////////////////////////////////////////////////////////////
115 // to prevent a flashing screen when non-printable character
116 char *_cleanString(char *v) {
117    char *d;
118    int i, l;
119    l = strlen(v);
120    for (i=0,d=v; 
121         i<l ; 
122         i++,d++) {
123       if (!isprint(*d))
124          *d = '.';
125       } 
126    return v;
127 }
128
129
130 ///////////////////////////////////////////////////////////////////////////
131 // to prevent a flashing screen when non-printable character
132 std::string _CreateCleanString(std::string s) {
133   std::string str=s;
134   for(int i=0;i<str.size();i++)
135   {
136     if(!isprint(str[i]))
137       str[i]='.';
138   }
139
140   return(str);
141 }
142