]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
* src/*.[h] all occurences of stl classes are now prefixed with
[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 gdcmDict::~gdcmDict() {
44    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
45       gdcmDictEntry* EntryToDelete = tag->second;
46       if ( EntryToDelete )
47          delete EntryToDelete;
48    }
49    KeyHt.clear();
50    // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
51    // assume all the pointed gdcmDictEntries are allready cleaned-up when
52    // we cleaned KeyHt.
53    NameHt.clear();
54 }
55
56 void gdcmDict::Print(std::ostream& os) {
57         PrintByKey(os);
58 }
59
60 /**
61  * \ingroup gdcmDict
62  * \brief   Print all the dictionary entries contained in this dictionary.
63  *          Entries will be sorted by tag i.e. the couple (group, element).
64  * @param   os The output stream to be written to.
65  */
66 void gdcmDict::PrintByKey(std::ostream& os) {
67         for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
68                 os << "Tag : ";
69                 os << "(" << hex << tag->second->GetGroup() << ',';
70                 os << hex << tag->second->GetElement() << ") = " << dec;
71                 os << tag->second->GetVR() << ", ";
72                 os << tag->second->GetFourth() << ", ";
73                 os << tag->second->GetName() << "."  << endl;
74         }
75 }
76
77 /**
78  * \ingroup gdcmDict
79  * \brief   Print all the dictionary entries contained in this dictionary.
80  *          Entries will be sorted by the name of the dictionary entries.
81  * @param   os The output stream to be written to.
82  */
83 void gdcmDict::PrintByName(std::ostream& os) {
84         for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
85                 os << "Tag : ";
86                 os << tag->second->GetName() << ",";
87                 os << tag->second->GetVR() << ", ";
88                 os << tag->second->GetFourth() << ", ";
89                 os << "(" << std::hex << tag->second->GetGroup() << ',';
90                 os << std::hex << tag->second->GetElement() << ") = " << dec << std::endl;
91         }
92 }
93
94 /**
95  * \ingroup gdcmDict
96  * \brief   Get the dictionnary entry identified by a given tag (group,element)
97  * @param   group   group of the entry to be found
98  * @param   element element of the entry to be found
99  * @return  the corresponding dictionnary entry when existing, NULL otherwise
100  */
101 gdcmDictEntry * gdcmDict::GetTagByNumber(guint16 group, guint16 element) {
102         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
103         if ( ! KeyHt.count(key))
104                 return (gdcmDictEntry*)0; 
105         return KeyHt.find(key)->second;
106 }
107
108 /**
109  * \ingroup gdcmDict
110  * \brief   Get the dictionnary entry identified by it's name.
111  * @param   name element of the ElVal to modify
112  * @return  the corresponding dictionnary entry when existing, NULL otherwise
113  */
114 gdcmDictEntry * gdcmDict::GetTagByName(TagName name) {
115         if ( ! NameHt.count(name))
116                 return (gdcmDictEntry*)0; 
117         return NameHt.find(name)->second;
118 }
119
120 int gdcmDict::ReplaceEntry(gdcmDictEntry* NewEntry) {
121    if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
122        KeyHt[ NewEntry->GetKey()] = NewEntry;
123        return (1);
124    } 
125    return (0);
126 }
127
128 int gdcmDict::AddNewEntry(gdcmDictEntry* NewEntry) {
129
130         TagKey key;
131         key = NewEntry->GetKey();
132         
133         if(KeyHt.count(key) == 1) {
134                 dbg.Verbose(1, "gdcmDict::AddNewEntry allready present", key.c_str());
135                 return(0);
136         } else {
137                 KeyHt[NewEntry->GetKey()] = NewEntry;
138                 return(1);
139         }
140 }
141
142
143 int gdcmDict::RemoveEntry(TagKey key) {
144    if(KeyHt.count(key) == 1) {
145       gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
146       if ( EntryToDelete )
147          delete EntryToDelete;
148       KeyHt.erase(key);
149       return (1);
150    } else {
151       dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
152       return (0);
153   }
154 }
155
156
157 int gdcmDict::RemoveEntry (guint16 group, guint16 element) {
158         return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
159 }
160