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