]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
Replace confusing names gdcmImage, etc by gdcmDicomDirImage etc, to avoid
[gdcm.git] / src / gdcmDict.cxx
1 // gdcmDict.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmDict.h"
4 #include "gdcmUtil.h"
5
6 #include <fstream>
7 #include <iostream>
8 #include <iomanip>
9
10 #ifdef GDCM_NO_ANSI_STRING_STREAM
11 #  include <strstream>
12 #  define  ostringstream ostrstream
13 # else
14 #  include <sstream>
15 #endif
16
17 //-----------------------------------------------------------------------------
18 // Constructor / Destructor
19 /**
20  * \ingroup gdcmDict
21  * \brief   Construtor
22  * @param   FileName from which to build the dictionary.
23  */
24 gdcmDict::gdcmDict(std::string & FileName) {
25    guint16 group, element;
26    char buff[1024];
27    TagName vr;
28    TagName fourth;
29    TagName name;
30
31    std::ifstream from(FileName.c_str());
32    dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary",
33                     FileName.c_str());
34
35    while (!from.eof()) {
36       from >> std::hex >> group >> element;
37       eatwhite(from);
38       from.getline(buff, 256, ' ');
39       vr = buff;
40       eatwhite(from);
41       from.getline(buff, 256, ' ');
42       fourth = buff;
43       from.getline(buff, 256, '\n');
44       name = buff;
45
46       gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
47                                                    vr, fourth, name);
48       AddNewEntry(newEntry);
49    }
50    from.close();
51
52    filename=FileName;
53 }
54
55 /**
56  * \ingroup gdcmDict
57  * \brief  Destructor 
58  */
59 gdcmDict::~gdcmDict() {
60    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
61       gdcmDictEntry* EntryToDelete = tag->second;
62       if ( EntryToDelete )
63          delete EntryToDelete;
64    }
65    // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
66    // assume all the pointed gdcmDictEntries are already cleaned-up when
67    // we cleaned KeyHt.
68    KeyHt.clear();
69    NameHt.clear();
70 }
71
72 //-----------------------------------------------------------------------------
73 // Print
74 /**
75  * \brief   Print all the dictionary entries contained in this dictionary.
76  *          Entries will be sorted by tag i.e. the couple (group, element).
77  * @param   os The output stream to be written to.
78  */
79 void gdcmDict::Print(std::ostream &os) {
80    os<<"Dict file name : "<<filename<<std::endl;
81    PrintByKey(os);
82 }
83
84 /**
85  * \ingroup gdcmDict
86  * \brief   Print all the dictionary entries contained in this dictionary.
87  *          Entries will be sorted by tag i.e. the couple (group, element).
88  * @param   os The output stream to be written to.
89  */
90 void gdcmDict::PrintByKey(std::ostream &os) {
91    std::ostringstream s;
92
93    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
94       s << "Entry : ";
95       s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
96       s << std::hex << std::setw(4) << tag->second->GetElement() << ") = " << std::dec;
97       s << tag->second->GetVR() << ", ";
98       s << tag->second->GetFourth() << ", ";
99       s << tag->second->GetName() << "."  << std::endl;
100    }
101    os << s.str();
102 }
103
104 /**
105  * \ingroup gdcmDict
106  * \brief   Print all the dictionary entries contained in this dictionary.
107  *          Entries will be sorted by the name of the dictionary entries.
108  * \warning AVOID USING IT : the name IS NOT an identifier; 
109  *                           unpredictable result
110  * @param   os The output stream to be written to.
111  */
112 void gdcmDict::PrintByName(std::ostream& os) {
113    std::ostringstream s;
114
115    for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
116       s << "Entry : ";
117       s << tag->second->GetName() << ",";
118       s << tag->second->GetVR() << ", ";
119       s << tag->second->GetFourth() << ", ";
120       s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
121       s << std::hex << std::setw(4) << tag->second->GetElement() << ") = ";
122       s << std::dec << std::endl;
123    }
124    os << s.str();
125 }
126
127 //-----------------------------------------------------------------------------
128 // Public
129 /**
130  * \ingroup gdcmDict
131  * \brief  adds a new Dicom Dictionary Entry 
132  * @param   NewEntry entry to add 
133  * @return  false if Dicom Element already exists
134  */
135 bool gdcmDict::AddNewEntry(gdcmDictEntry *NewEntry) 
136 {
137    TagKey key;
138    key = NewEntry->GetKey();
139         
140    if(KeyHt.count(key) == 1)
141    {
142       dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
143       return(false);
144    } 
145    else 
146    {
147       KeyHt[NewEntry->GetKey()] = NewEntry;
148       NameHt[NewEntry->GetName()] = NewEntry;
149       return(true);
150    }
151 }
152
153 /**
154  * \ingroup gdcmDict
155  * \brief  replaces an already existing Dicom Element by a new one
156  * @param   NewEntry new entry (overwrites any previous one with same tag)
157  * @return  false if Dicom Element doesn't exist
158  */
159 bool gdcmDict::ReplaceEntry(gdcmDictEntry *NewEntry) {
160    if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
161        KeyHt[NewEntry->GetKey()] = NewEntry;
162        NameHt[NewEntry->GetName()] = NewEntry;
163        return (true);
164    } 
165    return (false);
166 }
167
168 /**
169  * \ingroup gdcmDict
170  * \brief  removes an already existing Dicom Dictionary Entry,
171  *         identified by its Tag
172  * @param   key (group|element)
173  * @return  false if Dicom Dictionary Entry doesn't exist
174  */
175 bool gdcmDict::RemoveEntry(TagKey key) 
176 {
177    if(KeyHt.count(key) == 1) 
178    {
179       gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
180
181       if ( EntryToDelete )
182       {
183          NameHt.erase(EntryToDelete->GetName());
184          delete EntryToDelete;
185       }
186
187       KeyHt.erase(key);
188       return (true);
189    } 
190    else 
191    {
192       dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
193       return (false);
194   }
195 }
196
197 /**
198  * \ingroup gdcmDict
199  * \brief  removes an already existing Dicom Dictionary Entry, 
200  *          identified by its group,element number
201  * @param   group   Dicom group number of the Dicom Element
202  * @param   element Dicom element number of the Dicom Element
203  * @return  false if Dicom Dictionary Entry doesn't exist
204  */
205 bool gdcmDict::RemoveEntry (guint16 group, guint16 element) {
206         return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
207 }
208
209 /**
210  * \ingroup gdcmDict
211  * \brief   Get the dictionnary entry identified by it's name.
212  * @param   name element of the ElVal to modify
213  * \warning : NEVER use it !
214  *            the 'name' IS NOT an identifier within the Dicom Dicom Dictionary
215  *            the name MAY CHANGE between two versions !
216  * @return  the corresponding dictionnary entry when existing, NULL otherwise
217  */
218 gdcmDictEntry *gdcmDict::GetDictEntryByName(TagName name) {
219    if ( ! NameHt.count(name))
220       return NULL; 
221    return NameHt.find(name)->second;
222 }
223
224 /**
225  * \ingroup gdcmDict
226  * \brief   Get the dictionnary entry identified by a given tag (group,element)
227  * @param   group   group of the entry to be found
228  * @param   element element of the entry to be found
229  * @return  the corresponding dictionnary entry when existing, NULL otherwise
230  */
231 gdcmDictEntry *gdcmDict::GetDictEntryByNumber(guint16 group, guint16 element) {
232    TagKey key = gdcmDictEntry::TranslateToKey(group, element);
233    if ( ! KeyHt.count(key))
234       return NULL; 
235    return KeyHt.find(key)->second;
236 }
237
238 /** 
239  * \ingroup gdcmDict
240  * \brief   Consider all the entries of the public dicom dictionnary. 
241  *          Build all list of all the tag names of all those entries.
242  * \sa      gdcmDictSet::GetPubDictTagNamesByCategory
243  * @return  A list of all entries of the public dicom dictionnary.
244  */
245 std::list<std::string> *gdcmDict::GetDictEntryNames(void) 
246 {
247    std::list<std::string> *Result = new std::list<std::string>;
248    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
249    {
250       Result->push_back( tag->second->GetName() );
251    }
252    return Result;
253 }
254
255 /** 
256  * \ingroup gdcmDict
257  * \brief   Consider all the entries of the public dicom dictionnary.
258  *          Build an hashtable whose keys are the names of the groups
259  *          (fourth field in each line of dictionary) and whose corresponding
260  *          values are lists of all the dictionnary entries among that
261  *          group. Note that apparently the Dicom standard doesn't explicitely
262  *          define a name (as a string) for each group.
263  *          A typical usage of this method would be to enable a dynamic
264  *          configuration of a Dicom file browser: the admin/user can
265  *          select in the interface which Dicom tags should be displayed.
266  * \warning Dicom *doesn't* define any name for any 'categorie'
267  *          (the dictionnary fourth field was formerly NIH defined
268  *           - and no longer he is-
269  *           and will be removed when Dicom provides us a text file
270  *           with the 'official' Dictionnary, that would be more friendly
271  *           than asking us to perform a line by line check of the dictionnary
272  *           at the beginning of each year to -try to- guess the changes)
273  *           Therefore : please NEVER use that fourth field :-(
274  *
275  * @return  An hashtable: whose keys are the names of the groups and whose
276  *          corresponding values are lists of all the dictionnary entries
277  *          among that group.
278  */
279 std::map<std::string, std::list<std::string> > *gdcmDict::GetDictEntryNamesByCategory(void) 
280 {
281    std::map<std::string, std::list<std::string> > *Result = new std::map<std::string, std::list<std::string> >;
282
283    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
284    {
285       (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
286    }
287    return Result;
288 }
289
290 //-----------------------------------------------------------------------------
291 // Protected
292
293 //-----------------------------------------------------------------------------
294 // Private
295
296 //-----------------------------------------------------------------------------