1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2007/05/23 14:18:09 $
7 Version: $Revision: 1.87 $
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.
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.
17 =========================================================================*/
21 #include "gdcmDebug.h"
27 namespace GDCM_NAME_SPACE
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);
34 //-----------------------------------------------------------------------------
35 // Constructor / Destructor
46 * @param filename from which to build the dictionary.
48 Dict::Dict(std::string const &filename)
50 gdcmDebugMacro( "in Dict::Dict, filename =[" << filename << "]" );
51 std::ifstream from( filename.c_str() );
54 gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
55 // Using default embeded one:
56 FillDefaultDataDict( this );
60 gdcmDebugMacro( "in Dict::Dict, DoTheLoadingJob filename =["
62 DoTheLoadingJob(from);
75 //-----------------------------------------------------------------------------
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.
83 bool Dict::AddDict(std::string const &filename)
85 std::ifstream from( filename.c_str() );
88 gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
93 DoTheLoadingJob(from);
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.
103 bool Dict::RemoveDict(std::string const &filename)
105 std::ifstream from( filename.c_str() );
108 gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
130 // from >> std::ws; //remove white space
131 std::getline(from, name);
133 RemoveEntry(group,elem);
141 * \brief adds a new Dicom Dictionary Entry
142 * @param newEntry entry to add
143 * @return false if Dicom Element already exists
145 bool Dict::AddEntry(DictEntry *newEntry)
147 const TagKey &key = newEntry->GetKey();
149 if ( KeyHt.count(key) == 1 )
151 gdcmErrorMacro( "Already present:" << key );
156 newEntry->Register();
157 KeyHt.insert( TagKeyHT::value_type(key, newEntry));
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
168 /* seems to be useless
170 bool Dict::ReplaceEntry(DictEntry *newEntry) // seems to be useless
172 const TagKey &key = newEntry->GetKey();
173 if ( RemoveEntry(key) )
175 newEntry->Register();
176 KeyHt.insert( TagKeyHT::value_type(key, newEntry));
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
189 bool Dict::RemoveEntry(TagKey const &key)
191 TagKeyHT::const_iterator it = KeyHt.find(key);
192 if ( it != KeyHt.end() )
194 it->second->Unregister(); // delete the entry
195 KeyHt.erase(key); // remove pointer from HTable
201 gdcmWarningMacro( "Unfound entry" << key );
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
213 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
215 return RemoveEntry(DictEntry::TranslateToKey(group, elem));
219 * \brief Remove all Dicom Dictionary Entries
221 void Dict::ClearEntry()
223 // we assume all the pointed DictEntries are already cleaned-up
224 // when we clean KeyHt.
225 TagKeyHT::const_iterator it;
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
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
238 DictEntry *Dict::GetEntry(TagKey const &key)
240 TagKeyHT::iterator it = KeyHt.find(key);
241 if ( it == KeyHt.end() )
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
253 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
255 TagKey key = DictEntry::TranslateToKey(group, elem);
256 TagKeyHT::iterator it = KeyHt.find(key);
257 if ( it == KeyHt.end() )
265 * \brief Get the first entry while visiting the Dict entries
266 * \return The first DicEntry if found, otherwhise NULL
268 DictEntry *Dict::GetFirstEntry()
270 ItKeyHt = KeyHt.begin();
271 if ( ItKeyHt != KeyHt.end() )
272 return ItKeyHt->second;
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
281 DictEntry *Dict::GetNextEntry()
283 gdcmAssertMacro (ItKeyHt != KeyHt.end());
286 if (ItKeyHt != KeyHt.end())
287 return ItKeyHt->second;
291 //-----------------------------------------------------------------------------
294 //-----------------------------------------------------------------------------
297 * \brief Add all the dictionary entries from an already open source file
298 * @param from input stream to read from.
300 void Dict::DoTheLoadingJob(std::ifstream &from)
319 from >> std::ws; //remove white space
320 std::getline(from, name);
326 newEntry = DictEntry::New(group, elem, vr, vm, name);
332 //-----------------------------------------------------------------------------
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
340 void Dict::Print(std::ostream &os, std::string const & )
342 os << "Dict file name : [" << Filename << "]" << std::endl;
343 std::ostringstream s;
345 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
347 std::cout << tag->second->GetKey() << " " << tag->second->GetName()
350 s << "(" << tag->second->GetKey() << ") = "
352 s << tag->second->GetVR() << ", ";
353 s << tag->second->GetVM() << ", ";
354 s << tag->second->GetName() << "." << std::endl;
361 //-----------------------------------------------------------------------------
362 } // end namespace gdcm