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