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