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