]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
* FIX : Set the elements to create the DicomDir in a dictionary file
[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 gdcmDictSet         *gdcmGlobal::Dicts  = (gdcmDictSet *)0;
56 gdcmVR              *gdcmGlobal::VR     = (gdcmVR *)0;
57 gdcmTS              *gdcmGlobal::TS     = (gdcmTS *)0;
58 gdcmDicomDirElement *gdcmGlobal::ddElem = (gdcmDicomDirElement *)0;
59 gdcmGlobal gdcmGlob;
60
61 gdcmGlobal::gdcmGlobal(void) {
62    if (VR || TS || Dicts)
63       dbg.Verbose(0, "gdcmGlobal::gdcmGlobal : VR or TS or Dicts already allocated");
64    Dicts = new gdcmDictSet();
65    VR = new gdcmVR();
66    TS = new gdcmTS();
67    ddElem = new gdcmDicomDirElement();
68 }
69
70 gdcmGlobal::~gdcmGlobal() {
71    delete Dicts;
72    delete VR;
73    delete TS;
74    delete ddElem;
75 }
76
77 gdcmVR *gdcmGlobal::GetVR(void) {
78    return VR;
79 }
80
81 gdcmTS *gdcmGlobal::GetTS(void) {
82    return TS;
83 }
84
85 gdcmDictSet *gdcmGlobal::GetDicts(void) {
86    return Dicts;
87 }
88
89 gdcmDicomDirElement *gdcmGlobal::GetDicomDirElements(void) {
90    return ddElem;
91 }
92
93 //-----------------------------------------------------------------------------
94 // Because is not yet available in g++2.96
95 std::istream& eatwhite(std::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 void Tokenize (const std::string& str,
109                std::vector<std::string>& tokens,
110                const std::string& delimiters) {
111    std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
112    std::string::size_type pos     = str.find_first_of    (delimiters,lastPos);
113    while (std::string::npos != pos || std::string::npos != lastPos) {
114       tokens.push_back(str.substr(lastPos, pos - lastPos));
115       lastPos = str.find_first_not_of(delimiters, pos);
116       pos     = str.find_first_of    (delimiters, lastPos);
117    }
118 }
119
120
121 ///////////////////////////////////////////////////////////////////////////
122 // to prevent a flashing screen when non-printable character
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 std::string _CreateCleanString(std::string s) {
140    std::string str=s;
141
142    for(int i=0;i<str.size();i++)
143    {
144       if(!isprint(str[i]))
145          str[i]='.';
146    }
147
148
149    if(str.size()>0)
150       if(!isprint(s[str.size()-1]))
151          if(s[str.size()-1]==0)
152             str[str.size()-1]=' ';
153
154    return(str);
155 }