]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
321f168a5de35e8a33e553d43d6070853264977d
[gdcm.git] / src / gdcmDict.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDict.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/18 12:58:27 $
7   Version:   $Revision: 1.80 $
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(DictEntry::TranslateToKey(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 const &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       KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
153       return true;
154    }
155 }
156
157 /**
158  * \brief  replaces an already existing Dicom Element by a new one
159  * @param   newEntry new entry (overwrites any previous one with same tag)
160  * @return  false if Dicom Element doesn't exist
161  */
162 bool Dict::ReplaceEntry(DictEntry const &newEntry)
163 {
164    if ( RemoveEntry(newEntry.GetKey()) )
165    {
166        KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
167        return true;
168    } 
169    return false;
170 }
171
172 /**
173  * \brief  removes an already existing Dicom Dictionary Entry,
174  *         identified by its Tag
175  * @param   key (group|element)
176  * @return  false if Dicom Dictionary Entry doesn't exist
177  */
178 bool Dict::RemoveEntry(TagKey const &key) 
179 {
180    TagKeyHT::const_iterator it = KeyHt.find(key);
181    if ( it != KeyHt.end() ) 
182    {
183       KeyHt.erase(key);
184
185       return true;
186    } 
187    else 
188    {
189       gdcmWarningMacro( "Unfound entry" << key );
190       return false;
191   }
192 }
193
194 /**
195  * \brief  removes an already existing Dicom Dictionary Entry, 
196  *          identified by its group,element number
197  * @param   group   Dicom group number of the Dicom Element
198  * @param   elem Dicom element number of the Dicom Element
199  * @return  false if Dicom Dictionary Entry doesn't exist
200  */
201 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
202 {
203    return RemoveEntry(DictEntry::TranslateToKey(group, elem));
204 }
205
206 /**
207  * \brief   Remove all Dicom Dictionary Entries
208  */
209 void Dict::ClearEntry()
210 {
211    // we assume all the pointed DictEntries are already cleaned-up
212    // when we clean KeyHt.
213    KeyHt.clear();
214 }
215
216 /**
217  * \brief   Get the dictionary entry identified by a given tag ("group|element")
218  * @param   key   tag of the entry to be found
219  * @return  the corresponding dictionary entry when existing, NULL otherwise
220  */
221 DictEntry *Dict::GetEntry(TagKey const &key)
222 {
223    TagKeyHT::iterator it = KeyHt.find(key);
224    if ( it == KeyHt.end() )
225    {
226       return 0;
227    }
228    return &(it->second);
229 }
230
231 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
232 {
233    TagKey key = DictEntry::TranslateToKey(group, elem);
234    TagKeyHT::iterator it = KeyHt.find(key);
235    if ( it == KeyHt.end() )
236    {
237       return 0;
238    }
239    return &(it->second);
240 }
241
242 /**
243  * \brief   Get the first entry while visiting the Dict entries
244  * \return  The first DicEntry if found, otherwhise NULL
245  */
246 DictEntry *Dict::GetFirstEntry()
247 {
248    ItKeyHt = KeyHt.begin();
249    if ( ItKeyHt != KeyHt.end() )
250       return &(ItKeyHt->second);
251    return NULL;
252 }
253
254 /**
255  * \brief   Get the next entry while visiting the Hash table (KeyHt)
256  * \note : meaningfull only if GetFirstEntry already called
257  * \return  The next DictEntry if found, otherwhise NULL
258  */
259 DictEntry *Dict::GetNextEntry()
260 {
261    gdcmAssertMacro (ItKeyHt != KeyHt.end());
262
263    ++ItKeyHt;
264    if (ItKeyHt != KeyHt.end())
265       return &(ItKeyHt->second);
266    return NULL;
267 }
268
269 //-----------------------------------------------------------------------------
270 // Protected
271
272 //-----------------------------------------------------------------------------
273 // Private
274 /**
275  * \brief Add all the dictionary entries from an already open source file 
276  * @param from input stream to read from.
277  */
278 void Dict::DoTheLoadingJob(std::ifstream &from)
279 {
280    uint16_t group;
281    uint16_t elem;
282    VRKey vr;
283    TagName vm;
284    TagName name;
285
286    while (!from.eof() && from)
287    {
288       from >> std::hex;
289       from >> group;
290       from >> elem;
291       from >> vr;
292       from >> vm;
293       from >> std::ws;  //remove white space
294       std::getline(from, name);
295  
296       DictEntry newEntry(group, elem, vr, vm, name);
297       AddEntry(newEntry);
298    }
299    from.close();
300 }
301 //-----------------------------------------------------------------------------
302 // Print
303 /**
304  * \brief   Print all the dictionary entries contained in this dictionary.
305  *          Entries will be sorted by tag i.e. the couple (group, element).
306  * @param   os The output stream to be written to.
307  * @param indent Indentation string to be prepended during printing
308  */
309 void Dict::Print(std::ostream &os, std::string const & )
310 {
311    os << "Dict file name : " << Filename << std::endl;
312    std::ostringstream s;
313
314    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
315    {
316       s << "Entry : ";
317       s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
318       s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
319         << std::dec;
320       s << tag->second.GetVR() << ", ";
321       s << tag->second.GetVM() << ", ";
322       s << tag->second.GetName() << "."  << std::endl;
323    }
324    os << s.str();
325 }
326
327 //-----------------------------------------------------------------------------
328 } // end namespace gdcm