1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2005/10/23 15:32:30 $
7 Version: $Revision: 1.82 $
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 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 searched entry
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 * \brief Get the dictionary entry identified by it's "group" and "element")
239 * @param group Group number of the searched entry.
240 * @param elem Element number of the searched entry.
241 * @return the corresponding dictionary entry when existing, NULL otherwise
243 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
245 TagKey key = DictEntry::TranslateToKey(group, elem);
246 TagKeyHT::iterator it = KeyHt.find(key);
247 if ( it == KeyHt.end() )
255 * \brief Get the first entry while visiting the Dict entries
256 * \return The first DicEntry if found, otherwhise NULL
258 DictEntry *Dict::GetFirstEntry()
260 ItKeyHt = KeyHt.begin();
261 if ( ItKeyHt != KeyHt.end() )
262 return ItKeyHt->second;
267 * \brief Get the next entry while visiting the Hash table (KeyHt)
268 * \note : meaningfull only if GetFirstEntry already called
269 * \return The next DictEntry if found, otherwhise NULL
271 DictEntry *Dict::GetNextEntry()
273 gdcmAssertMacro (ItKeyHt != KeyHt.end());
276 if (ItKeyHt != KeyHt.end())
277 return ItKeyHt->second;
281 //-----------------------------------------------------------------------------
284 //-----------------------------------------------------------------------------
287 * \brief Add all the dictionary entries from an already open source file
288 * @param from input stream to read from.
290 void Dict::DoTheLoadingJob(std::ifstream &from)
299 while (!from.eof() && from)
306 from >> std::ws; //remove white space
307 std::getline(from, name);
309 newEntry = DictEntry::New(group, elem, vr, vm, name);
315 //-----------------------------------------------------------------------------
318 * \brief Print all the dictionary entries contained in this dictionary.
319 * Entries will be sorted by tag i.e. the couple (group, element).
320 * @param os The output stream to be written to.
321 * @param indent Indentation string to be prepended during printing
323 void Dict::Print(std::ostream &os, std::string const & )
325 os << "Dict file name : " << Filename << std::endl;
326 std::ostringstream s;
328 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
331 s << "(" << tag->second->GetKey() << ") = "
333 s << tag->second->GetVR() << ", ";
334 s << tag->second->GetVM() << ", ";
335 s << tag->second->GetName() << "." << std::endl;
340 //-----------------------------------------------------------------------------
341 } // end namespace gdcm