]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
2ad026e8f1c9dbc8732eb3d16b924148fa02b249
[gdcm.git] / src / gdcmDict.cxx
1 // gdcmDict.cxx
2
3 #include <fstream>
4 #include "gdcm.h"
5 #include "gdcmUtil.h"
6
7 gdcmDict::gdcmDict(const char* FileName) {
8         std::ifstream from(FileName);
9         dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary", FileName);
10         guint16 group, element;
11         // CLEANME : use defines for all those constants
12         char buff[1024];
13         TagKey key;
14         TagName vr;
15         TagName fourth;
16         TagName name;
17         while (!from.eof()) {
18                 from >> hex >> group >> element;
19                 eatwhite(from);
20                 from.getline(buff, 256, ' ');
21                 vr = buff;
22                 eatwhite(from);
23                 from.getline(buff, 256, ' ');
24                 fourth = buff;
25                 from.getline(buff, 256, '\n');
26                 name = buff;
27                 gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
28                                                          vr, fourth, name);
29                 NameHt[name] = newEntry;
30                 KeyHt[gdcmDictEntry::TranslateToKey(group, element)] = newEntry;
31         }
32         from.close();
33 }
34
35 void gdcmDict::Print(ostream& os) {
36         PrintByKey(os);
37 }
38
39 /**
40  * \ingroup gdcmHeader
41  * \brief   Print all the dictionary entries contained in this dictionary.
42  *          Entries will be sorted by tag i.e. the couple (group, element).
43  * @param   os The output stream to be written to.
44  */
45 void gdcmDict::PrintByKey(ostream& os) {
46         for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
47                 os << "Tag : ";
48                 os << "(" << hex << tag->second->GetGroup() << ',';
49                 os << hex << tag->second->GetElement() << ") = " << dec;
50                 os << tag->second->GetVR() << ", ";
51                 os << tag->second->GetFourth() << ", ";
52                 os << tag->second->GetName() << "."  << endl;
53         }
54 }
55
56 /**
57  * \ingroup gdcmHeader
58  * \brief   Print all the dictionary entries contained in this dictionary.
59  *          Entries will be sorted by the name of the dictionary entries.
60  * @param   os The output stream to be written to.
61  */
62 void gdcmDict::PrintByName(ostream& os) {
63         for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
64                 os << "Tag : ";
65                 os << tag->second->GetName() << ",";
66                 os << tag->second->GetVR() << ", ";
67                 os << tag->second->GetFourth() << ", ";
68                 os << "(" << hex << tag->second->GetGroup() << ',';
69                 os << hex << tag->second->GetElement() << ") = " << dec << endl;
70         }
71 }
72
73 /**
74  * \ingroup gdcmHeader
75  * \brief   Get the dictionnary entry identified by a given tag (group,element)
76  * @param   group   group of the entry to be found
77  * @param   element element of the entry to be found
78  * @return  the corresponding dictionnary entry when existing, NULL otherwise
79  */
80 gdcmDictEntry * gdcmDict::GetTagByKey(guint16 group, guint16 element) {
81         TagKey key = gdcmDictEntry::TranslateToKey(group, element);
82         if ( ! KeyHt.count(key))
83                 return (gdcmDictEntry*)0; 
84         if (KeyHt.count(key) > 1)
85                 dbg.Verbose(0, "gdcmDict::GetTagByName", 
86                             "multiple entries for this key (FIXME) !");
87         return KeyHt.find(key)->second;
88 }
89
90 /**
91  * \ingroup gdcmHeader
92  * \brief   Get the dictionnary entry identified by it's name.
93  * @param   name element of the ElVal to modify
94  * @return  the corresponding dictionnary entry when existing, NULL otherwise
95  */
96 gdcmDictEntry * gdcmDict::GetTagByName(TagName name) {
97         if ( ! NameHt.count(name))
98                 return (gdcmDictEntry*)0; 
99         if (NameHt.count(name) > 1)
100                 dbg.Verbose(0, "gdcmDict::GetTagByName", 
101                             "multiple entries for this key (FIXME) !");
102         return NameHt.find(name)->second;
103 }
104
105
106 int gdcmDict::ReplaceEntry(gdcmDictEntry* NewEntry) {
107         //JPRCLEAN
108         // au cas ou la NewEntry serait incomplete
109         // Question : cela peut-il se produire ?
110         //
111         // --> NON : voir constructeur
112         //TagKey key;
113         //key = NewEntry->GetKey();
114         //if (key =="") {
115         //      NewEntry->gdcmDictEntry::SetKey(
116         //                      gdcmDictEntry::TranslateToKey(NewEntry->GetGroup(), NewEntry->GetElement())
117         //                      );
118         //}
119         
120         KeyHt.erase (NewEntry->gdcmDictEntry::GetKey());
121         KeyHt[ NewEntry->GetKey()] = NewEntry;
122         return (1);
123         // Question(jpr): Dans quel cas ça peut planter ?
124         // Reponse(frog): dans les mauvais cas...
125 }
126  
127
128 int gdcmDict::AddNewEntry(gdcmDictEntry* NewEntry) {
129
130         TagKey key;
131         key = NewEntry->GetKey();
132         
133         if(KeyHt.count(key) >= 1) {
134                 printf("gdcmDict::AddNewEntry %s deja present\n", key.c_str());
135                 return(0);
136         } else {
137                 KeyHt[NewEntry->GetKey()] = NewEntry;
138                 return(1);
139         }
140         }
141
142
143 int gdcmDict::RemoveEntry(TagKey key) { 
144         if(KeyHt.count(key) == 1) {
145                 KeyHt.erase(key);
146                 return (1);
147         } else {
148                 printf("gdcmDict::RemoveEntry %s non trouve\n", key.c_str());
149                 return (0);
150         }
151 }
152
153
154 int gdcmDict::RemoveEntry (guint16 group, guint16 element) {
155
156         return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
157 }
158