1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2005/10/20 15:24:08 $
7 Version: $Revision: 1.81 $
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"
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)
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 DoTheLoadingJob(from);
73 //-----------------------------------------------------------------------------
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.
81 bool Dict::AddDict(std::string const &filename)
84 std::ifstream from( filename.c_str() );
87 gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
92 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());
119 while (!from.eof() && from)
126 // from >> std::ws; //remove white space
127 std::getline(from, name);
129 RemoveEntry(group,elem);
137 * \brief adds a new Dicom Dictionary Entry
138 * @param newEntry entry to add
139 * @return false if Dicom Element already exists
141 bool Dict::AddEntry(DictEntry *newEntry)
143 const TagKey &key = newEntry->GetKey();
145 if ( KeyHt.count(key) == 1 )
147 gdcmErrorMacro( "Already present:" << key );
152 newEntry->Register();
153 KeyHt.insert( TagKeyHT::value_type(key, newEntry));
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
163 bool Dict::ReplaceEntry(DictEntry *newEntry)
165 const TagKey &key = newEntry->GetKey();
166 if ( RemoveEntry(key) )
168 newEntry->Register();
169 KeyHt.insert( TagKeyHT::value_type(key, newEntry));
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
181 bool Dict::RemoveEntry(TagKey const &key)
183 TagKeyHT::const_iterator it = KeyHt.find(key);
184 if ( it != KeyHt.end() )
186 it->second->Unregister();
193 gdcmWarningMacro( "Unfound entry" << key );
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
205 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
207 return RemoveEntry(DictEntry::TranslateToKey(group, elem));
211 * \brief Remove all Dicom Dictionary Entries
213 void Dict::ClearEntry()
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();
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
228 DictEntry *Dict::GetEntry(TagKey const &key)
230 TagKeyHT::iterator it = KeyHt.find(key);
231 if ( it == KeyHt.end() )
238 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
240 TagKey key = DictEntry::TranslateToKey(group, elem);
241 TagKeyHT::iterator it = KeyHt.find(key);
242 if ( it == KeyHt.end() )
250 * \brief Get the first entry while visiting the Dict entries
251 * \return The first DicEntry if found, otherwhise NULL
253 DictEntry *Dict::GetFirstEntry()
255 ItKeyHt = KeyHt.begin();
256 if ( ItKeyHt != KeyHt.end() )
257 return ItKeyHt->second;
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
266 DictEntry *Dict::GetNextEntry()
268 gdcmAssertMacro (ItKeyHt != KeyHt.end());
271 if (ItKeyHt != KeyHt.end())
272 return ItKeyHt->second;
276 //-----------------------------------------------------------------------------
279 //-----------------------------------------------------------------------------
282 * \brief Add all the dictionary entries from an already open source file
283 * @param from input stream to read from.
285 void Dict::DoTheLoadingJob(std::ifstream &from)
294 while (!from.eof() && from)
301 from >> std::ws; //remove white space
302 std::getline(from, name);
304 newEntry = DictEntry::New(group, elem, vr, vm, name);
310 //-----------------------------------------------------------------------------
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
318 void Dict::Print(std::ostream &os, std::string const & )
320 os << "Dict file name : " << Filename << std::endl;
321 std::ostringstream s;
323 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
326 s << "(" << tag->second->GetKey() << ") = "
328 s << tag->second->GetVR() << ", ";
329 s << tag->second->GetVM() << ", ";
330 s << tag->second->GetName() << "." << std::endl;
335 //-----------------------------------------------------------------------------
336 } // end namespace gdcm