]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
Brutal commit. Changelog to come.... Frog
[gdcm.git] / src / gdcmDict.cxx
1 // gdcmDict.cxx
2
3 #include <fstream>
4 #include "gdcmDict.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         return KeyHt.find(key)->second;
85 }
86
87 /**
88  * \ingroup gdcmHeader
89  * \brief   Get the dictionnary entry identified by it's name.
90  * @param   name element of the ElVal to modify
91  * @return  the corresponding dictionnary entry when existing, NULL otherwise
92  */
93 gdcmDictEntry * gdcmDict::GetTagByName(TagName name) {
94         if ( ! NameHt.count(name))
95                 return (gdcmDictEntry*)0; 
96         return NameHt.find(name)->second;
97 }
98
99 int gdcmDict::ReplaceEntry(gdcmDictEntry* NewEntry) {
100    if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
101        KeyHt[ NewEntry->GetKey()] = NewEntry;
102        return (1);
103    } 
104    return (0);
105 }
106
107 int gdcmDict::AddNewEntry(gdcmDictEntry* NewEntry) {
108
109         TagKey key;
110         key = NewEntry->GetKey();
111         
112         if(KeyHt.count(key) == 1) {
113                 dbg.Verbose(1, "gdcmDict::AddNewEntry allready present", key.c_str());
114                 return(0);
115         } else {
116                 KeyHt[NewEntry->GetKey()] = NewEntry;
117                 return(1);
118         }
119 }
120
121
122 int gdcmDict::RemoveEntry(TagKey key) {
123    if(KeyHt.count(key) == 1) {
124       KeyHt.erase(key);
125       return (1);
126    } else {
127       dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
128       return (0);
129   }
130 }
131
132
133 int gdcmDict::RemoveEntry (guint16 group, guint16 element) {
134         return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
135 }
136