1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2005/02/05 01:37:08 $
7 Version: $Revision: 1.73 $
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 void FillDefaultDataDict(Dict *d);
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
44 * @param filename from which to build the dictionary.
46 Dict::Dict(std::string const &filename)
54 std::ifstream from( filename.c_str() );
57 gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
58 // Using default embeded one:
59 FillDefaultDataDict( this );
70 from >> std::ws; //remove white space
71 std::getline(from, name);
73 const DictEntry newEntry(group, element, vr, vm, name);
90 //-----------------------------------------------------------------------------
93 * \brief adds a new Dicom Dictionary Entry
94 * @param newEntry entry to add
95 * @return false if Dicom Element already exists
97 bool Dict::AddEntry(DictEntry const &newEntry)
99 const TagKey &key = newEntry.GetKey();
101 if(KeyHt.count(key) == 1)
103 gdcmWarningMacro( "Already present" << key.c_str());
108 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
114 * \brief replaces an already existing Dicom Element by a new one
115 * @param newEntry new entry (overwrites any previous one with same tag)
116 * @return false if Dicom Element doesn't exist
118 bool Dict::ReplaceEntry(DictEntry const &newEntry)
120 if ( RemoveEntry(newEntry.GetKey()) )
122 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
129 * \brief removes an already existing Dicom Dictionary Entry,
130 * identified by its Tag
131 * @param key (group|element)
132 * @return false if Dicom Dictionary Entry doesn't exist
134 bool Dict::RemoveEntry(TagKey const &key)
136 TagKeyHT::const_iterator it = KeyHt.find(key);
137 if(it != KeyHt.end())
145 gdcmWarningMacro( "Unfound entry" << key.c_str());
151 * \brief removes an already existing Dicom Dictionary Entry,
152 * identified by its group,element number
153 * @param group Dicom group number of the Dicom Element
154 * @param elem Dicom element number of the Dicom Element
155 * @return false if Dicom Dictionary Entry doesn't exist
157 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
159 return RemoveEntry(DictEntry::TranslateToKey(group, elem));
163 * \brief Remove all Dicom Dictionary Entries
165 void Dict::ClearEntry()
167 // we assume all the pointed DictEntries are already cleaned-up
168 // when we clean KeyHt.
173 * \brief Get the dictionary entry identified by a given tag (group,element)
174 * @param group group of the entry to be found
175 * @param elem element of the entry to be found
176 * @return the corresponding dictionary entry when existing, NULL otherwise
178 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
180 TagKey key = DictEntry::TranslateToKey(group, elem);
181 TagKeyHT::iterator it = KeyHt.find(key);
182 if ( it == KeyHt.end() )
186 return &(it->second);
190 * \brief Get the first entry while visiting the Dict entries
191 * \return The first DicEntry if found, otherwhise NULL
193 DictEntry *Dict::GetFirstEntry()
195 ItKeyHt = KeyHt.begin();
196 if( ItKeyHt != KeyHt.end() )
197 return &(ItKeyHt->second);
202 * \brief Get the next entry while visiting the Hash table (KeyHt)
203 * \note : meaningfull only if GetFirstEntry already called
204 * \return The next DictEntry if found, otherwhise NULL
206 DictEntry *Dict::GetNextEntry()
208 gdcmAssertMacro (ItKeyHt != KeyHt.end());
211 if (ItKeyHt != KeyHt.end())
212 return &(ItKeyHt->second);
216 //-----------------------------------------------------------------------------
219 //-----------------------------------------------------------------------------
222 //-----------------------------------------------------------------------------
225 * \brief Print all the dictionary entries contained in this dictionary.
226 * Entries will be sorted by tag i.e. the couple (group, element).
227 * @param os The output stream to be written to.
228 * @param indent Indentation string to be prepended during printing
230 void Dict::Print(std::ostream &os, std::string const & )
232 os << "Dict file name : " << Filename << std::endl;
233 std::ostringstream s;
235 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
238 s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
239 s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
241 s << tag->second.GetVR() << ", ";
242 s << tag->second.GetVM() << ", ";
243 s << tag->second.GetName() << "." << std::endl;
248 //-----------------------------------------------------------------------------
249 } // end namespace gdcm