]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
Comments
[gdcm.git] / src / gdcmDict.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDict.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/05 13:25:26 $
7   Version:   $Revision: 1.83 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmDict.h"
20 #include "gdcmUtil.h"
21 #include "gdcmDebug.h"
22
23 #include <fstream>
24 #include <iostream>
25 #include <iomanip>
26
27 namespace gdcm 
28 {
29 //-----------------------------------------------------------------------------
30 /// \brief auto generated function, to fill up the Dicom Dictionnary,
31 ///       if relevant file is not found on user's disk
32 void FillDefaultDataDict(Dict *d);
33
34 //-----------------------------------------------------------------------------
35 // Constructor / Destructor
36 /**
37  * \brief   Constructor
38  */
39 Dict::Dict( )
40 {
41    Filename="";
42 }
43
44 /**
45  * \brief   Constructor
46  * @param   filename from which to build the dictionary.
47  */
48 Dict::Dict(std::string const &filename)
49 {
50
51    std::ifstream from( filename.c_str() );
52    if ( !from )
53    {
54       gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
55       // Using default embeded one:
56       FillDefaultDataDict( this );
57    }
58    else
59    {
60       DoTheLoadingJob(from);
61       Filename = filename;
62    }
63 }
64
65 /**
66  * \brief  Destructor 
67  */
68 Dict::~Dict()
69 {
70    ClearEntry();
71 }
72
73 //-----------------------------------------------------------------------------
74 // Public
75
76 /**
77  * \brief   Add all the entries held in a source dictionary
78  * \note it concerns only Private Dictionnary
79  * @param   filename from which to build the dictionary.
80  */
81 bool Dict::AddDict(std::string const &filename)
82 {
83
84    std::ifstream from( filename.c_str() );
85    if ( !from )
86    {
87       gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
88       return false;
89    }
90    else
91    {
92       DoTheLoadingJob(from);
93       return true;
94    }
95 }
96
97
98 /**
99  * \brief   Removes from the current Dicom Dict all the entries held in a source dictionary
100  * \note it concerns only Private Dictionnary
101  * @param   filename from which we read the entries to remove.
102  */
103 bool Dict::RemoveDict(std::string const &filename)
104 {
105    std::ifstream from( filename.c_str() );
106    if ( !from )
107    {
108       gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
109       return false;
110    }
111    else
112    {
113       uint16_t group;
114       uint16_t elem;
115       TagName vr;
116       TagName vm;
117       TagName name;
118
119       while (!from.eof() && from)
120       {
121          from >> std::hex;
122          from >> group;
123          from >> elem;
124          from >> vr;
125          from >> vm;
126         // from >> std::ws;  //remove white space
127          std::getline(from, name);
128  
129          RemoveEntry(group,elem);
130       }
131       from.close();
132       return true;
133    }
134 }
135
136 /**
137  * \brief  adds a new Dicom Dictionary Entry 
138  * @param   newEntry entry to add 
139  * @return  false if Dicom Element already exists
140  */
141 bool Dict::AddEntry(DictEntry *newEntry) 
142 {
143    const TagKey &key = newEntry->GetKey();
144
145    if ( KeyHt.count(key) == 1 )
146    {
147       gdcmErrorMacro( "Already present:" << key );
148       return false;
149    } 
150    else 
151    {
152       newEntry->Register();
153       KeyHt.insert( TagKeyHT::value_type(key, newEntry));
154       return true;
155    }
156 }
157
158 /**
159  * \brief  replaces an already existing Dicom Element by a new one
160  * @param   newEntry new entry (overwrites any previous one with same tag)
161  * @return  false if Dicom Element doesn't exist
162  */
163 bool Dict::ReplaceEntry(DictEntry *newEntry)
164 {
165    const TagKey &key = newEntry->GetKey();
166    if ( RemoveEntry(key) )
167    {
168       newEntry->Register();
169       KeyHt.insert( TagKeyHT::value_type(key, newEntry));
170       return true;
171    } 
172    return false;
173 }
174
175 /**
176  * \brief  removes an already existing Dicom Dictionary Entry,
177  *         identified by its Tag
178  * @param   key (group|element)
179  * @return  false if Dicom Dictionary Entry doesn't exist
180  */
181 bool Dict::RemoveEntry(TagKey const &key) 
182 {
183    TagKeyHT::const_iterator it = KeyHt.find(key);
184    if ( it != KeyHt.end() ) 
185    {
186       it->second->Unregister();
187       KeyHt.erase(key);
188
189       return true;
190    } 
191    else 
192    {
193       gdcmWarningMacro( "Unfound entry" << key );
194       return false;
195   }
196 }
197
198 /**
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   elem Dicom element number of the Dicom Element
203  * @return  false if Dicom Dictionary Entry doesn't exist
204  */
205 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
206 {
207    return RemoveEntry(DictEntry::TranslateToKey(group, elem));
208 }
209
210 /**
211  * \brief   Remove all Dicom Dictionary Entries
212  */
213 void Dict::ClearEntry()
214 {
215    // we assume all the pointed DictEntries are already cleaned-up
216    // when we clean KeyHt.
217    TagKeyHT::const_iterator it;
218    for(it = KeyHt.begin();it!=KeyHt.end();++it)
219       it->second->Unregister();
220    KeyHt.clear();
221 }
222
223 /**
224  * \brief   Get the dictionary entry identified by a given tag ("group|element")
225  * @param   key   tag of the searched entry 
226  * @return  the corresponding dictionary entry when existing, NULL otherwise
227  */
228 DictEntry *Dict::GetEntry(TagKey const &key)
229 {
230    TagKeyHT::iterator it = KeyHt.find(key);
231    if ( it == KeyHt.end() )
232    {
233       return 0;
234    }
235    return it->second;
236 }
237 /**
238  * \brief   Get the dictionary entry identified by it's "group" and "element")
239  * @param   group  Group number of the searched entry.
240  * @param   elem Element number of the searched entry.
241  * @return  the corresponding dictionary entry when existing, NULL otherwise
242  */
243 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
244 {
245    TagKey key = DictEntry::TranslateToKey(group, elem);
246    TagKeyHT::iterator it = KeyHt.find(key);
247    if ( it == KeyHt.end() )
248    {
249       return 0;
250    }
251    return it->second;
252 }
253
254 /**
255  * \brief   Get the first entry while visiting the Dict entries
256  * \return  The first DicEntry if found, otherwhise NULL
257  */
258 DictEntry *Dict::GetFirstEntry()
259 {
260    ItKeyHt = KeyHt.begin();
261    if ( ItKeyHt != KeyHt.end() )
262       return ItKeyHt->second;
263    return NULL;
264 }
265
266 /**
267  * \brief   Get the next entry while visiting the Hash table (KeyHt)
268  * \note : meaningfull only if GetFirstEntry already called
269  * \return  The next DictEntry if found, otherwhise NULL
270  */
271 DictEntry *Dict::GetNextEntry()
272 {
273    gdcmAssertMacro (ItKeyHt != KeyHt.end());
274
275    ++ItKeyHt;
276    if (ItKeyHt != KeyHt.end())
277       return ItKeyHt->second;
278    return NULL;
279 }
280
281 //-----------------------------------------------------------------------------
282 // Protected
283
284 //-----------------------------------------------------------------------------
285 // Private
286 /**
287  * \brief Add all the dictionary entries from an already open source file 
288  * @param from input stream to read from.
289  */
290 void Dict::DoTheLoadingJob(std::ifstream &from)
291 {
292    uint16_t group;
293    uint16_t elem;
294    VRKey vr;
295    TagName vm;
296    TagName name;
297
298    DictEntry *newEntry;
299    while (!from.eof() && from)
300    {
301       from >> std::hex;
302       from >> group;
303       from >> elem;
304       from >> vr;
305       from >> vm;
306       from >> std::ws;  //remove white space
307       std::getline(from, name);
308
309       newEntry = DictEntry::New(group, elem, vr, vm, name);
310       AddEntry(newEntry);
311       newEntry->Delete();
312    }
313    from.close();
314 }
315 //-----------------------------------------------------------------------------
316 // Print
317 /**
318  * \brief   Print all the dictionary entries contained in this dictionary.
319  *          Entries will be sorted by tag i.e. the couple (group, element).
320  * @param   os The output stream to be written to.
321  * @param indent Indentation string to be prepended during printing
322  */
323 void Dict::Print(std::ostream &os, std::string const & )
324 {
325    os << "Dict file name : " << Filename << std::endl;
326    std::ostringstream s;
327
328    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
329    {
330       s << "Entry : ";
331       s << "(" << tag->second->GetKey() << ") = "
332         << std::dec;
333       s << tag->second->GetVR() << ", ";
334       s << tag->second->GetVM() << ", ";
335       s << tag->second->GetName() << "."  << std::endl;
336    }
337    os << s.str();
338 }
339
340 //-----------------------------------------------------------------------------
341 } // end namespace gdcm