]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
Remove using namespace std;
[gdcm.git] / src / gdcmDict.cxx
1 // gdcmDict.cxx
2
3 //This is needed when compiling in debug mode
4 #ifdef _MSC_VER
5 // 'identifier' : class 'type' needs to have dll-interface to be used by
6 // clients of class 'type2'
7 #pragma warning ( disable : 4251 )
8 // 'identifier' : identifier was truncated to 'number' characters in the
9 // debug information
10 #pragma warning ( disable : 4786 )
11 #endif //_MSC_VER
12
13 #include <fstream>
14 #include "gdcmDict.h"
15 #include "gdcmUtil.h"
16
17 /**
18  * \ingroup gdcmDict
19  * \brief   Construtor
20  * @param   FileName from which to build the dictionary.
21  */
22 gdcmDict::gdcmDict(std::string & FileName) {
23    std::ifstream from(FileName.c_str());
24    dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary",
25                     FileName.c_str());
26    guint16 group, element;
27         // CLEANME : use defines for all those constants
28    char buff[1024];
29    TagKey key;
30    TagName vr;
31    TagName fourth;
32    TagName name;
33    while (!from.eof()) {
34       from >> std::hex >> group >> element;
35       eatwhite(from);
36       from.getline(buff, 256, ' ');
37       vr = buff;
38       eatwhite(from);
39       from.getline(buff, 256, ' ');
40       fourth = buff;
41       from.getline(buff, 256, '\n');
42       name = buff;
43       gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
44                                                    vr, fourth, name);
45       // FIXME: use AddNewEntry
46       NameHt[name] = newEntry;
47       KeyHt[gdcmDictEntry::TranslateToKey(group, element)] = newEntry;
48    }
49    from.close();
50 }
51
52 /**
53  * \ingroup gdcmDict
54  * \brief   
55  */
56 gdcmDict::~gdcmDict() {
57    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
58       gdcmDictEntry* EntryToDelete = tag->second;
59       if ( EntryToDelete )
60          delete EntryToDelete;
61    }
62    KeyHt.clear();
63    // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
64    // assume all the pointed gdcmDictEntries are already cleaned-up when
65    // we cleaned KeyHt.
66    NameHt.clear();
67 }
68
69 /**
70  * \ingroup gdcmDict
71  * \brief   
72  * @param   os
73  */
74 void gdcmDict::Print(std::ostream& os) {
75    PrintByKey(os);
76 }
77
78 /**
79  * \ingroup gdcmDict
80  * \brief   Print all the dictionary entries contained in this dictionary.
81  *          Entries will be sorted by tag i.e. the couple (group, element).
82  * @param   os The output stream to be written to.
83  */
84 void gdcmDict::PrintByKey(std::ostream& os) {
85    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
86       os << "Tag : ";
87       os << "(" << hex << tag->second->GetGroup() << ',';
88       os << hex << tag->second->GetElement() << ") = " << dec;
89       os << tag->second->GetVR() << ", ";
90       os << tag->second->GetFourth() << ", ";
91       os << tag->second->GetName() << "."  << endl;
92    }
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  * @param   os The output stream to be written to.
100  */
101 void gdcmDict::PrintByName(std::ostream& os) {
102    for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
103       os << "Tag : ";
104       os << tag->second->GetName() << ",";
105       os << tag->second->GetVR() << ", ";
106       os << tag->second->GetFourth() << ", ";
107       os << "(" << std::hex << tag->second->GetGroup() << ',';
108       os << std::hex << tag->second->GetElement() << ") = " << dec << std::endl;
109    }
110 }
111
112 /**
113  * \ingroup gdcmDict
114  * \brief   Get the dictionnary entry identified by a given tag (group,element)
115  * @param   group   group of the entry to be found
116  * @param   element element of the entry to be found
117  * @return  the corresponding dictionnary entry when existing, NULL otherwise
118  */
119 gdcmDictEntry * gdcmDict::GetTagByNumber(guint16 group, guint16 element) {
120    TagKey key = gdcmDictEntry::TranslateToKey(group, element);
121    if ( ! KeyHt.count(key))
122       return (gdcmDictEntry*)0; 
123    return KeyHt.find(key)->second;
124 }
125
126 /**
127  * \ingroup gdcmDict
128  * \brief   Get the dictionnary entry identified by it's name.
129  * @param   name element of the ElVal to modify
130  * @return  the corresponding dictionnary entry when existing, NULL otherwise
131  */
132 gdcmDictEntry * gdcmDict::GetTagByName(TagName name) {
133    if ( ! NameHt.count(name))
134       return (gdcmDictEntry*)0; 
135    return NameHt.find(name)->second;
136 }
137
138 /**
139  * \ingroup gdcmDict
140  * \brief   
141  * @param   NewEntry
142  * @return  
143  */
144 int gdcmDict::ReplaceEntry(gdcmDictEntry* NewEntry) {
145    if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
146        KeyHt[ NewEntry->GetKey()] = NewEntry;
147        return (1);
148    } 
149    return (0);
150 }
151
152 /**
153  * \ingroup gdcmDict
154  * \brief   
155  * @param   NewEntry
156  * @return  
157  */
158  int gdcmDict::AddNewEntry(gdcmDictEntry* NewEntry) {
159    TagKey key;
160    key = NewEntry->GetKey();
161         
162    if(KeyHt.count(key) == 1) {
163       dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
164       return(0);
165    } else {
166       KeyHt[NewEntry->GetKey()] = NewEntry;
167       return(1);
168    }
169 }
170
171 /**
172  * \ingroup gdcmDict
173  * \brief   
174  * @param   key
175  * @return  
176  */
177 int gdcmDict::RemoveEntry(TagKey key) {
178    if(KeyHt.count(key) == 1) {
179       gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
180       if ( EntryToDelete )
181          delete EntryToDelete;
182       KeyHt.erase(key);
183       return (1);
184    } else {
185       dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
186       return (0);
187   }
188 }
189
190 /**
191  * \ingroup gdcmDict
192  * \brief   
193  * @param   group 
194  * @param   element
195  * @return  
196  */
197 int gdcmDict::RemoveEntry (guint16 group, guint16 element) {
198         return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
199 }
200