1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2005/09/02 07:00:04 $
7 Version: $Revision: 1.79 $
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(DictEntry::TranslateToKey(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 const &newEntry)
143 const TagKey &key = newEntry.GetKey();
145 if ( KeyHt.count(key) == 1 )
147 gdcmErrorMacro( "Already present:" << key );
152 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
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
162 bool Dict::ReplaceEntry(DictEntry const &newEntry)
164 if ( RemoveEntry(newEntry.GetKey()) )
166 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
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
178 bool Dict::RemoveEntry(TagKey const &key)
180 TagKeyHT::const_iterator it = KeyHt.find(key);
181 if ( it != KeyHt.end() )
189 gdcmWarningMacro( "Unfound entry" << key );
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
201 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
203 return RemoveEntry(DictEntry::TranslateToKey(group, elem));
207 * \brief Remove all Dicom Dictionary Entries
209 void Dict::ClearEntry()
211 // we assume all the pointed DictEntries are already cleaned-up
212 // when we clean KeyHt.
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
221 DictEntry *Dict::GetEntry(TagKey const &key)
223 TagKeyHT::iterator it = KeyHt.find(key);
224 if ( it == KeyHt.end() )
228 return &(it->second);
231 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
233 TagKey key = DictEntry::TranslateToKey(group, elem);
234 TagKeyHT::iterator it = KeyHt.find(key);
235 if ( it == KeyHt.end() )
239 return &(it->second);
243 * \brief Get the first entry while visiting the Dict entries
244 * \return The first DicEntry if found, otherwhise NULL
246 DictEntry *Dict::GetFirstEntry()
248 ItKeyHt = KeyHt.begin();
249 if ( ItKeyHt != KeyHt.end() )
250 return &(ItKeyHt->second);
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
259 DictEntry *Dict::GetNextEntry()
261 gdcmAssertMacro (ItKeyHt != KeyHt.end());
264 if (ItKeyHt != KeyHt.end())
265 return &(ItKeyHt->second);
269 //-----------------------------------------------------------------------------
272 //-----------------------------------------------------------------------------
275 * \brief Add all the dictionary entries from an already open source file
276 * @param from input stream to read from.
278 void Dict::DoTheLoadingJob(std::ifstream &from)
286 while (!from.eof() && from)
293 from >> std::ws; //remove white space
294 std::getline(from, name);
296 const DictEntry newEntry(group, elem, vr, vm, name);
301 //-----------------------------------------------------------------------------
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
309 void Dict::Print(std::ostream &os, std::string const & )
311 os << "Dict file name : " << Filename << std::endl;
312 std::ostringstream s;
314 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
317 s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
318 s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
320 s << tag->second.GetVR() << ", ";
321 s << tag->second.GetVM() << ", ";
322 s << tag->second.GetName() << "." << std::endl;
327 //-----------------------------------------------------------------------------
328 } // end namespace gdcm