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