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