1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2006/04/11 16:03:26 $
7 Version: $Revision: 1.84 $
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)
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)
86 std::ifstream from( filename.c_str() );
89 gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
94 DoTheLoadingJob(from);
101 * \brief Removes from the current Dicom Dict all the entries held in a source dictionary
102 * \note it concerns only Private Dictionnary
103 * @param filename from which we read the entries to remove.
105 bool Dict::RemoveDict(std::string const &filename)
107 std::ifstream from( filename.c_str() );
110 gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
121 while (!from.eof() && from)
128 // from >> std::ws; //remove white space
129 std::getline(from, name);
131 RemoveEntry(group,elem);
139 * \brief adds a new Dicom Dictionary Entry
140 * @param newEntry entry to add
141 * @return false if Dicom Element already exists
143 bool Dict::AddEntry(DictEntry *newEntry)
145 const TagKey &key = newEntry->GetKey();
147 if ( KeyHt.count(key) == 1 )
149 gdcmErrorMacro( "Already present:" << key );
154 newEntry->Register();
155 KeyHt.insert( TagKeyHT::value_type(key, newEntry));
161 * \brief replaces an already existing Dicom Element by a new one
162 * @param newEntry new entry (overwrites any previous one with same tag)
163 * @return false if Dicom Element doesn't exist
166 /* seems to be useless
168 bool Dict::ReplaceEntry(DictEntry *newEntry) // seems to be useless
170 const TagKey &key = newEntry->GetKey();
171 if ( RemoveEntry(key) )
173 newEntry->Register();
174 KeyHt.insert( TagKeyHT::value_type(key, newEntry));
181 * \brief removes an already existing Dicom Dictionary Entry,
182 * identified by its Tag
183 * @param key (group|element)
184 * @return false if Dicom Dictionary Entry doesn't exist
186 bool Dict::RemoveEntry(TagKey const &key)
188 TagKeyHT::const_iterator it = KeyHt.find(key);
189 if ( it != KeyHt.end() )
191 it->second->Unregister(); // delete the entry
192 KeyHt.erase(key); // remove pointer from HTable
198 gdcmWarningMacro( "Unfound entry" << key );
204 * \brief removes an already existing Dicom Dictionary Entry,
205 * identified by its group,element number
206 * @param group Dicom group number of the Dicom Element
207 * @param elem Dicom element number of the Dicom Element
208 * @return false if Dicom Dictionary Entry doesn't exist
210 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
212 return RemoveEntry(DictEntry::TranslateToKey(group, elem));
216 * \brief Remove all Dicom Dictionary Entries
218 void Dict::ClearEntry()
220 // we assume all the pointed DictEntries are already cleaned-up
221 // when we clean KeyHt.
222 TagKeyHT::const_iterator it;
224 for(it = KeyHt.begin();it!=KeyHt.end();++it)
225 it->second->Unregister(); // delete the entry
226 KeyHt.clear(); // remove all the entries from HTable
231 * \brief Get the dictionary entry identified by a given tag ("group|element")
232 * @param key tag of the searched entry
233 * @return the corresponding dictionary entry when existing, NULL otherwise
235 DictEntry *Dict::GetEntry(TagKey const &key)
237 TagKeyHT::iterator it = KeyHt.find(key);
238 if ( it == KeyHt.end() )
245 * \brief Get the dictionary entry identified by it's "group" and "element")
246 * @param group Group number of the searched entry.
247 * @param elem Element number of the searched entry.
248 * @return the corresponding dictionary entry when existing, NULL otherwise
250 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
252 TagKey key = DictEntry::TranslateToKey(group, elem);
253 TagKeyHT::iterator it = KeyHt.find(key);
254 if ( it == KeyHt.end() )
262 * \brief Get the first entry while visiting the Dict entries
263 * \return The first DicEntry if found, otherwhise NULL
265 DictEntry *Dict::GetFirstEntry()
267 ItKeyHt = KeyHt.begin();
268 if ( ItKeyHt != KeyHt.end() )
269 return ItKeyHt->second;
274 * \brief Get the next entry while visiting the Hash table (KeyHt)
275 * \note : meaningfull only if GetFirstEntry already called
276 * \return The next DictEntry if found, otherwhise NULL
278 DictEntry *Dict::GetNextEntry()
280 gdcmAssertMacro (ItKeyHt != KeyHt.end());
283 if (ItKeyHt != KeyHt.end())
284 return ItKeyHt->second;
288 //-----------------------------------------------------------------------------
291 //-----------------------------------------------------------------------------
294 * \brief Add all the dictionary entries from an already open source file
295 * @param from input stream to read from.
297 void Dict::DoTheLoadingJob(std::ifstream &from)
306 while (!from.eof() && from)
313 from >> std::ws; //remove white space
314 std::getline(from, name);
316 newEntry = DictEntry::New(group, elem, vr, vm, name);
322 //-----------------------------------------------------------------------------
325 * \brief Print all the dictionary entries contained in this dictionary.
326 * Entries will be sorted by tag i.e. the couple (group, element).
327 * @param os The output stream to be written to.
328 * @param indent Indentation string to be prepended during printing
330 void Dict::Print(std::ostream &os, std::string const & )
332 os << "Dict file name : [" << Filename << "]" << std::endl;
333 std::ostringstream s;
335 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
337 std::cout << tag->second->GetKey() << " " << tag->second->GetName()
340 s << "(" << tag->second->GetKey() << ") = "
342 s << tag->second->GetVR() << ", ";
343 s << tag->second->GetVM() << ", ";
344 s << tag->second->GetName() << "." << std::endl;
351 //-----------------------------------------------------------------------------
352 } // end namespace gdcm