]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
* Add a RefCounter object that is deleted only when it's reference count is
[gdcm.git] / src / gdcmDict.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDict.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/20 15:24:08 $
7   Version:   $Revision: 1.81 $
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 a 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 entry to be found
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 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
239 {
240    TagKey key = DictEntry::TranslateToKey(group, elem);
241    TagKeyHT::iterator it = KeyHt.find(key);
242    if ( it == KeyHt.end() )
243    {
244       return 0;
245    }
246    return it->second;
247 }
248
249 /**
250  * \brief   Get the first entry while visiting the Dict entries
251  * \return  The first DicEntry if found, otherwhise NULL
252  */
253 DictEntry *Dict::GetFirstEntry()
254 {
255    ItKeyHt = KeyHt.begin();
256    if ( ItKeyHt != KeyHt.end() )
257       return ItKeyHt->second;
258    return NULL;
259 }
260
261 /**
262  * \brief   Get the next entry while visiting the Hash table (KeyHt)
263  * \note : meaningfull only if GetFirstEntry already called
264  * \return  The next DictEntry if found, otherwhise NULL
265  */
266 DictEntry *Dict::GetNextEntry()
267 {
268    gdcmAssertMacro (ItKeyHt != KeyHt.end());
269
270    ++ItKeyHt;
271    if (ItKeyHt != KeyHt.end())
272       return ItKeyHt->second;
273    return NULL;
274 }
275
276 //-----------------------------------------------------------------------------
277 // Protected
278
279 //-----------------------------------------------------------------------------
280 // Private
281 /**
282  * \brief Add all the dictionary entries from an already open source file 
283  * @param from input stream to read from.
284  */
285 void Dict::DoTheLoadingJob(std::ifstream &from)
286 {
287    uint16_t group;
288    uint16_t elem;
289    VRKey vr;
290    TagName vm;
291    TagName name;
292
293    DictEntry *newEntry;
294    while (!from.eof() && from)
295    {
296       from >> std::hex;
297       from >> group;
298       from >> elem;
299       from >> vr;
300       from >> vm;
301       from >> std::ws;  //remove white space
302       std::getline(from, name);
303  
304       newEntry = DictEntry::New(group, elem, vr, vm, name);
305       AddEntry(newEntry);
306       newEntry->Delete();
307    }
308    from.close();
309 }
310 //-----------------------------------------------------------------------------
311 // Print
312 /**
313  * \brief   Print all the dictionary entries contained in this dictionary.
314  *          Entries will be sorted by tag i.e. the couple (group, element).
315  * @param   os The output stream to be written to.
316  * @param indent Indentation string to be prepended during printing
317  */
318 void Dict::Print(std::ostream &os, std::string const & )
319 {
320    os << "Dict file name : " << Filename << std::endl;
321    std::ostringstream s;
322
323    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
324    {
325       s << "Entry : ";
326       s << "(" << tag->second->GetKey() << ") = "
327         << std::dec;
328       s << tag->second->GetVR() << ", ";
329       s << tag->second->GetVM() << ", ";
330       s << tag->second->GetName() << "."  << std::endl;
331    }
332    os << s.str();
333 }
334
335 //-----------------------------------------------------------------------------
336 } // end namespace gdcm