]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
* src/gdcmDict.cxx, gdcmElValSet.cxx : bug fix under windows for prints.
[gdcm.git] / src / gdcmDict.cxx
1 // gdcmDict.cxx
2
3 #include "gdcmDict.h"
4 #include "gdcmUtil.h"
5 #include <fstream>
6 #ifdef GDCM_NO_ANSI_STRING_STREAM
7 #  include <strstream>
8 #  define  ostringstream ostrstream
9 # else
10 #  include <sstream>
11 #endif
12
13 /**
14  * \ingroup gdcmDict
15  * \brief   Construtor
16  * @param   FileName from which to build the dictionary.
17  */
18 gdcmDict::gdcmDict(std::string & FileName) {
19    std::ifstream from(FileName.c_str());
20    dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary",
21                     FileName.c_str());
22    guint16 group, element;
23         // CLEANME : use defines for all those constants
24    char buff[1024];
25    TagKey key;
26    TagName vr;
27    TagName fourth;
28    TagName name;
29
30    while (!from.eof()) {
31       from >> std::hex >> group >> element;
32       eatwhite(from);
33       from.getline(buff, 256, ' ');
34       vr = buff;
35       eatwhite(from);
36       from.getline(buff, 256, ' ');
37       fourth = buff;
38       from.getline(buff, 256, '\n');
39       name = buff;
40       gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
41                                                    vr, fourth, name);
42       // FIXME: use AddNewEntry
43       NameHt[name] = newEntry;
44       KeyHt[gdcmDictEntry::TranslateToKey(group, element)] = newEntry;
45    }
46    from.close();
47 }
48
49 /**
50  * \ingroup gdcmDict
51  * \brief   
52  */
53 gdcmDict::~gdcmDict() {
54    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
55       gdcmDictEntry* EntryToDelete = tag->second;
56       if ( EntryToDelete )
57          delete EntryToDelete;
58    }
59    KeyHt.clear();
60    // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
61    // assume all the pointed gdcmDictEntries are already cleaned-up when
62    // we cleaned KeyHt.
63    NameHt.clear();
64 }
65
66 /**
67  * \ingroup gdcmDict
68  * \brief   
69  * @param   os
70  */
71 void gdcmDict::Print(std::ostream& os) {
72    PrintByKey(os);
73 }
74
75 /**
76  * \ingroup gdcmDict
77  * \brief   Print all the dictionary entries contained in this dictionary.
78  *          Entries will be sorted by tag i.e. the couple (group, element).
79  * @param   os The output stream to be written to.
80  */
81 void gdcmDict::PrintByKey(std::ostream& os) {
82    std::ostringstream s;
83
84    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
85       s << "Tag : ";
86       s << "(" << std::hex << tag->second->GetGroup() << ',';
87       s << std::hex << tag->second->GetElement() << ") = " << std::dec;
88       s << tag->second->GetVR() << ", ";
89       s << tag->second->GetFourth() << ", ";
90       s << tag->second->GetName() << "."  << std::endl;
91    }
92    os << s.str();
93 }
94
95 /**
96  * \ingroup gdcmDict
97  * \brief   Print all the dictionary entries contained in this dictionary.
98  *          Entries will be sorted by the name of the dictionary entries.
99  * @param   os The output stream to be written to.
100  */
101 void gdcmDict::PrintByName(std::ostream& os) {
102    std::ostringstream s;
103
104    for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
105       s << "Tag : ";
106       s << tag->second->GetName() << ",";
107       s << tag->second->GetVR() << ", ";
108       s << tag->second->GetFourth() << ", ";
109       s << "(" << std::hex << tag->second->GetGroup() << ',';
110       s << std::hex << tag->second->GetElement() << ") = " << std::dec << std::endl;
111    }
112    os << s.str();
113 }
114
115 /**
116  * \ingroup gdcmDict
117  * \brief   Get the dictionnary entry identified by a given tag (group,element)
118  * @param   group   group of the entry to be found
119  * @param   element element of the entry to be found
120  * @return  the corresponding dictionnary entry when existing, NULL otherwise
121  */
122 gdcmDictEntry * gdcmDict::GetTagByNumber(guint16 group, guint16 element) {
123    TagKey key = gdcmDictEntry::TranslateToKey(group, element);
124    if ( ! KeyHt.count(key))
125       return (gdcmDictEntry*)0; 
126    return KeyHt.find(key)->second;
127 }
128
129 /**
130  * \ingroup gdcmDict
131  * \brief   Get the dictionnary entry identified by it's name.
132  * @param   name element of the ElVal to modify
133  * @return  the corresponding dictionnary entry when existing, NULL otherwise
134  */
135 gdcmDictEntry * gdcmDict::GetTagByName(TagName name) {
136    if ( ! NameHt.count(name))
137       return (gdcmDictEntry*)0; 
138    return NameHt.find(name)->second;
139 }
140
141 /**
142  * \ingroup gdcmDict
143  * \brief   
144  * @param   NewEntry
145  * @return  
146  */
147 int gdcmDict::ReplaceEntry(gdcmDictEntry* NewEntry) {
148    if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
149        KeyHt[ NewEntry->GetKey()] = NewEntry;
150        return (1);
151    } 
152    return (0);
153 }
154
155 /**
156  * \ingroup gdcmDict
157  * \brief   
158  * @param   NewEntry
159  * @return  
160  */
161  int gdcmDict::AddNewEntry(gdcmDictEntry* NewEntry) {
162    TagKey key;
163    key = NewEntry->GetKey();
164         
165    if(KeyHt.count(key) == 1) {
166       dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
167       return(0);
168    } else {
169       KeyHt[NewEntry->GetKey()] = NewEntry;
170       return(1);
171    }
172 }
173
174 /**
175  * \ingroup gdcmDict
176  * \brief   
177  * @param   key
178  * @return  
179  */
180 int gdcmDict::RemoveEntry(TagKey key) {
181    if(KeyHt.count(key) == 1) {
182       gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
183       if ( EntryToDelete )
184          delete EntryToDelete;
185       KeyHt.erase(key);
186       return (1);
187    } else {
188       dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
189       return (0);
190   }
191 }
192
193 /**
194  * \ingroup gdcmDict
195  * \brief   
196  * @param   group 
197  * @param   element
198  * @return  
199  */
200 int gdcmDict::RemoveEntry (guint16 group, guint16 element) {
201         return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
202 }
203