]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
Fix mistypings
[gdcm.git] / src / gdcmDict.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDict.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:09 $
7   Version:   $Revision: 1.87 $
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_NAME_SPACE
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    gdcmDebugMacro( "in Dict::Dict, filename =[" << filename << "]" );
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       gdcmDebugMacro( "in Dict::Dict, DoTheLoadingJob filename =[" 
61                     << filename << "]" );
62       DoTheLoadingJob(from);
63       Filename = filename;
64    }
65 }
66
67 /**
68  * \brief  Destructor 
69  */
70 Dict::~Dict()
71 {
72    ClearEntry();
73 }
74
75 //-----------------------------------------------------------------------------
76 // Public
77
78 /**
79  * \brief   Add all the entries held in a source dictionary
80  * \note it concerns only Private Dictionnary
81  * @param   filename from which to build the dictionary.
82  */
83 bool Dict::AddDict(std::string const &filename)
84 {
85    std::ifstream from( filename.c_str() );
86    if ( !from )
87    {
88       gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
89       return false;
90    }
91    else
92    {
93       DoTheLoadingJob(from);
94       return true;
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 ( true )
120       {
121          from >> std::hex;
122          from >> group;
123
124          if (from.eof())
125             break;
126
127          from >> elem;
128          from >> vr;
129          from >> vm;
130         // from >> std::ws;  //remove white space
131          std::getline(from, name);
132  
133          RemoveEntry(group,elem);
134       }
135       from.close();
136       return true;
137    }
138 }
139
140 /**
141  * \brief  adds a new Dicom Dictionary Entry 
142  * @param   newEntry entry to add 
143  * @return  false if Dicom Element already exists
144  */
145 bool Dict::AddEntry(DictEntry *newEntry) 
146 {
147    const TagKey &key = newEntry->GetKey();
148
149    if ( KeyHt.count(key) == 1 )
150    {
151       gdcmErrorMacro( "Already present:" << key );
152       return false;
153    } 
154    else 
155    {
156       newEntry->Register();
157       KeyHt.insert( TagKeyHT::value_type(key, newEntry));
158       return true;
159    }
160 }
161
162 /*
163  * \ brief  replaces an already existing Dicom Element by a new one
164  * @ param   newEntry new entry (overwrites any previous one with same tag)
165  * @ return  false if Dicom Element doesn't exist
166  */
167  
168  /* seems to be useless
169  
170 bool Dict::ReplaceEntry(DictEntry *newEntry) // seems to be useless
171 {
172    const TagKey &key = newEntry->GetKey();
173    if ( RemoveEntry(key) )
174    {
175       newEntry->Register();
176       KeyHt.insert( TagKeyHT::value_type(key, newEntry));
177       return true;
178    } 
179    return false;
180 }
181 */
182
183 /**
184  * \brief  removes an already existing Dicom Dictionary Entry,
185  *         identified by its Tag
186  * @param   key (group|element)
187  * @return  false if Dicom Dictionary Entry doesn't exist
188  */
189 bool Dict::RemoveEntry(TagKey const &key) 
190 {
191    TagKeyHT::const_iterator it = KeyHt.find(key);
192    if ( it != KeyHt.end() ) 
193    {
194       it->second->Unregister(); // delete the entry
195       KeyHt.erase(key);         // remove pointer from HTable
196
197       return true;
198    } 
199    else 
200    {
201       gdcmWarningMacro( "Unfound entry" << key );
202       return false;
203   }
204 }
205
206 /**
207  * \brief  removes an already existing Dicom Dictionary Entry, 
208  *          identified by its group,element number
209  * @param   group   Dicom group number of the Dicom Element
210  * @param   elem Dicom element number of the Dicom Element
211  * @return  false if Dicom Dictionary Entry doesn't exist
212  */
213 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
214 {
215    return RemoveEntry(DictEntry::TranslateToKey(group, elem));
216 }
217
218 /**
219  * \brief   Remove all Dicom Dictionary Entries
220  */
221 void Dict::ClearEntry()
222 {
223    // we assume all the pointed DictEntries are already cleaned-up
224    // when we clean KeyHt.
225    TagKeyHT::const_iterator it;
226
227    for(it = KeyHt.begin();it!=KeyHt.end();++it)
228       it->second->Unregister(); // delete the entry
229    KeyHt.clear();               // remove all the entries from HTable
230
231 }
232
233 /**
234  * \brief   Get the dictionary entry identified by a given tag ("group|element")
235  * @param   key   tag of the searched entry 
236  * @return  the corresponding dictionary entry when existing, NULL otherwise
237  */
238 DictEntry *Dict::GetEntry(TagKey const &key)
239 {
240    TagKeyHT::iterator it = KeyHt.find(key);
241    if ( it == KeyHt.end() )
242    {
243       return 0;
244    }
245    return it->second;
246 }
247 /**
248  * \brief   Get the dictionary entry identified by its "group" and "element")
249  * @param   group  Group number of the searched entry.
250  * @param   elem Element number of the searched entry.
251  * @return  the corresponding dictionary entry when existing, NULL otherwise
252  */
253 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
254 {
255    TagKey key = DictEntry::TranslateToKey(group, elem);
256    TagKeyHT::iterator it = KeyHt.find(key);
257    if ( it == KeyHt.end() )
258    {
259       return 0;
260    }
261    return it->second;
262 }
263
264 /**
265  * \brief   Get the first entry while visiting the Dict entries
266  * \return  The first DicEntry if found, otherwhise NULL
267  */
268 DictEntry *Dict::GetFirstEntry()
269 {
270    ItKeyHt = KeyHt.begin();
271    if ( ItKeyHt != KeyHt.end() )
272       return ItKeyHt->second;
273    return NULL;
274 }
275
276 /**
277  * \brief   Get the next entry while visiting the Hash table (KeyHt)
278  * \note : meaningfull only if GetFirstEntry already called
279  * \return  The next DictEntry if found, otherwhise NULL
280  */
281 DictEntry *Dict::GetNextEntry()
282 {
283    gdcmAssertMacro (ItKeyHt != KeyHt.end());
284
285    ++ItKeyHt;
286    if (ItKeyHt != KeyHt.end())
287       return ItKeyHt->second;
288    return NULL;
289 }
290
291 //-----------------------------------------------------------------------------
292 // Protected
293
294 //-----------------------------------------------------------------------------
295 // Private
296 /**
297  * \brief Add all the dictionary entries from an already open source file 
298  * @param from input stream to read from.
299  */
300 void Dict::DoTheLoadingJob(std::ifstream &from)
301 {
302    if (!from)
303       return;
304       
305    uint16_t group;
306    uint16_t elem;
307    VRKey vr;
308    TagName vm;
309    TagName name;
310
311    DictEntry *newEntry;
312    while ( true )
313    {
314       from >> std::hex;
315       from >> group;      
316       from >> elem;
317       from >> vr;
318       from >> vm;
319       from >> std::ws;  //remove white space
320       std::getline(from, name);
321       
322       if(from.eof()) {
323          break;
324       }
325       
326       newEntry = DictEntry::New(group, elem, vr, vm, name);
327       AddEntry(newEntry);
328       newEntry->Delete();
329    }
330    from.close();
331 }
332 //-----------------------------------------------------------------------------
333 // Print
334 /**
335  * \brief   Print all the dictionary entries contained in this dictionary.
336  *          Entries will be sorted by tag i.e. the couple (group, element).
337  * @param   os The output stream to be written to.
338  * @param indent Indentation string to be prepended during printing
339  */
340 void Dict::Print(std::ostream &os, std::string const & )
341 {
342    os << "Dict file name : [" << Filename << "]" << std::endl;
343    std::ostringstream s;
344
345    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
346    {  
347 std::cout << tag->second->GetKey() << " " << tag->second->GetName() 
348           << std::endl;
349       s << "Entry : ";
350       s << "(" << tag->second->GetKey() << ") = "
351         << std::dec;
352       s << tag->second->GetVR() << ", ";
353       s << tag->second->GetVM() << ", ";
354       s << tag->second->GetName() << "."  << std::endl;
355      
356    }
357    os << s.str();
358
359 }
360
361 //-----------------------------------------------------------------------------
362 } // end namespace gdcm