]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
* src/*.[h|cxx] : coding style
[gdcm.git] / src / gdcmDict.cxx
1 // gdcmDict.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmDict.h"
4 #include "gdcmUtil.h"
5 #include <fstream>
6 #ifdef GDCM_NO_ANSI_STRING_STREAM
7 #  include <strstream>
8 #  define  ostringstream ostrstream
9 # else
10 #  include <sstream>
11 #endif
12
13 //-----------------------------------------------------------------------------
14 // Constructor / Destructor
15 /**
16  * \ingroup gdcmDict
17  * \brief   Construtor
18  * @param   FileName from which to build the dictionary.
19  */
20 gdcmDict::gdcmDict(std::string & FileName) {
21    std::ifstream from(FileName.c_str());
22    dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary",
23                     FileName.c_str());
24    guint16 group, element;
25         // CLEANME : use defines for all those constants
26    char buff[1024];
27    TagKey key;
28    TagName vr;
29    TagName fourth;
30    TagName name;
31
32    while (!from.eof()) {
33       from >> std::hex >> group >> element;
34       eatwhite(from);
35       from.getline(buff, 256, ' ');
36       vr = buff;
37       eatwhite(from);
38       from.getline(buff, 256, ' ');
39       fourth = buff;
40       from.getline(buff, 256, '\n');
41       name = buff;
42       gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
43                                                    vr, fourth, name);
44       // FIXME: use AddNewEntry
45       NameHt[name] = newEntry;
46       KeyHt[gdcmDictEntry::TranslateToKey(group, element)] = newEntry;
47    }
48    from.close();
49 }
50
51 /**
52  * \ingroup gdcmDict
53  * \brief  Destructor 
54  */
55 gdcmDict::~gdcmDict() {
56    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
57       gdcmDictEntry* EntryToDelete = tag->second;
58       if ( EntryToDelete )
59          delete EntryToDelete;
60    }
61    KeyHt.clear();
62    // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
63    // assume all the pointed gdcmDictEntries are already cleaned-up when
64    // we cleaned KeyHt.
65    NameHt.clear();
66 }
67
68 //-----------------------------------------------------------------------------
69 // Print
70 /**
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::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    std::ostringstream s;
87
88    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
89       s << "Tag : ";
90       s << "(" << std::hex << tag->second->GetGroup() << ',';
91       s << std::hex << tag->second->GetElement() << ") = " << std::dec;
92       s << tag->second->GetVR() << ", ";
93       s << tag->second->GetFourth() << ", ";
94       s << tag->second->GetName() << "."  << std::endl;
95    }
96    os << s.str();
97 }
98
99 /**
100  * \ingroup gdcmDict
101  * \brief   Print all the dictionary entries contained in this dictionary.
102  *          Entries will be sorted by the name of the dictionary entries.
103  * \warning AVOID USING IT : the name IS NOT an identifier
104  *                           unpredictable result
105  * @param   os The output stream to be written to.
106  */
107 void gdcmDict::PrintByName(std::ostream& os) {
108    std::ostringstream s;
109
110    for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
111       s << "Tag : ";
112       s << tag->second->GetName() << ",";
113       s << tag->second->GetVR() << ", ";
114       s << tag->second->GetFourth() << ", ";
115       s << "(" << std::hex << tag->second->GetGroup() << ',';
116       s << std::hex << tag->second->GetElement() << ") = " << std::dec << std::endl;
117    }
118    os << s.str();
119 }
120
121 //-----------------------------------------------------------------------------
122 // Public
123 /**
124  * \ingroup gdcmDict
125  * \brief  adds a new Dicom Dictionary Entry 
126  * @param   NewEntry 
127  * @return  false if Dicom Element already existed
128  */
129  bool gdcmDict::AddNewEntry(gdcmDictEntry* NewEntry) {
130    TagKey key;
131    key = NewEntry->GetKey();
132         
133    if(KeyHt.count(key) == 1) {
134       dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
135       return(false);
136    } else {
137       KeyHt[NewEntry->GetKey()] = NewEntry;
138       return(true);
139    }
140 }
141
142 /**
143  * \ingroup gdcmDict
144  * \brief  replaces an already existing Dicom Element by a new one
145  * @param   NewEntry
146  * @return  false if Dicom Element doesn't exist
147  */
148 bool gdcmDict::ReplaceEntry(gdcmDictEntry* NewEntry) {
149    if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
150        KeyHt[ NewEntry->GetKey()] = NewEntry;
151        return (true);
152    } 
153    return (false);
154 }
155
156 /**
157  * \ingroup gdcmDict
158  * \brief  removes an already existing Dicom Dictionary Entry,
159  *         identified by its Tag
160  * @param   key (group|element)
161  * @return  false if Dicom Dictionary Entry doesn't exist
162  */
163 bool gdcmDict::RemoveEntry(TagKey key) {
164    if(KeyHt.count(key) == 1) {
165       gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
166       if ( EntryToDelete )
167          delete EntryToDelete;
168       KeyHt.erase(key);
169       return (true);
170    } else {
171       dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
172       return (false);
173   }
174 }
175
176 /**
177  * \ingroup gdcmDict
178  * \brief  removes an already existing Dicom Dictionary Entry, 
179  *          identified by its group,element
180  number
181  * @param   group   Dicom group number of the Dicom Element
182  * @param   element Dicom element number of the Dicom Element
183  * @return  false if Dicom Dictionary Entry doesn't exist
184  */
185 bool gdcmDict::RemoveEntry (guint16 group, guint16 element) {
186         return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
187 }
188
189 /**
190  * \ingroup gdcmDict
191  * \brief   Get the dictionnary entry identified by a given tag (group,element)
192  * @param   group   group of the entry to be found
193  * @param   element element of the entry to be found
194  * @return  the corresponding dictionnary entry when existing, NULL otherwise
195  */
196 gdcmDictEntry * gdcmDict::GetTagByNumber(guint16 group, guint16 element) {
197    TagKey key = gdcmDictEntry::TranslateToKey(group, element);
198    if ( ! KeyHt.count(key))
199       return (gdcmDictEntry*)0; 
200    return KeyHt.find(key)->second;
201 }
202
203 /**
204  * \ingroup gdcmDict
205  * \brief   Get the dictionnary entry identified by it's name.
206  * @param   name element of the ElVal to modify
207  * \warning : NEVER use it !
208  *            the 'name' IS NOT an identifier within the Dicom Dicom Dictionary
209  *            the name MAY CHANGE between two versions !
210  * @return  the corresponding dictionnary entry when existing, NULL otherwise
211  */
212 gdcmDictEntry * gdcmDict::GetTagByName(TagName name) {
213    if ( ! NameHt.count(name))
214       return (gdcmDictEntry*)0; 
215    return NameHt.find(name)->second;
216 }
217
218 //-----------------------------------------------------------------------------
219 // Protected
220
221 //-----------------------------------------------------------------------------
222 // Private
223
224 //-----------------------------------------------------------------------------