1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2005/06/24 10:55:58 $
7 Version: $Revision: 1.77 $
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 generate 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)
56 std::ifstream from( filename.c_str() );
59 gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
60 // Using default embeded one:
61 FillDefaultDataDict( this );
72 from >> std::ws; //remove white space
73 std::getline(from, name);
75 const DictEntry newEntry(group, element, vr, vm, name);
92 //-----------------------------------------------------------------------------
95 * \brief adds a new Dicom Dictionary Entry
96 * @param newEntry entry to add
97 * @return false if Dicom Element already exists
99 bool Dict::AddEntry(DictEntry const &newEntry)
101 const TagKey &key = newEntry.GetKey();
103 if ( KeyHt.count(key) == 1 )
105 gdcmWarningMacro( "Already present" << key.c_str());
110 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
116 * \brief replaces an already existing Dicom Element by a new one
117 * @param newEntry new entry (overwrites any previous one with same tag)
118 * @return false if Dicom Element doesn't exist
120 bool Dict::ReplaceEntry(DictEntry const &newEntry)
122 if ( RemoveEntry(newEntry.GetKey()) )
124 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
131 * \brief removes an already existing Dicom Dictionary Entry,
132 * identified by its Tag
133 * @param key (group|element)
134 * @return false if Dicom Dictionary Entry doesn't exist
136 bool Dict::RemoveEntry(TagKey const &key)
138 TagKeyHT::const_iterator it = KeyHt.find(key);
139 if ( it != KeyHt.end() )
147 gdcmWarningMacro( "Unfound entry" << key.c_str());
153 * \brief removes an already existing Dicom Dictionary Entry,
154 * identified by its group,element number
155 * @param group Dicom group number of the Dicom Element
156 * @param elem Dicom element number of the Dicom Element
157 * @return false if Dicom Dictionary Entry doesn't exist
159 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
161 return RemoveEntry(DictEntry::TranslateToKey(group, elem));
165 * \brief Remove all Dicom Dictionary Entries
167 void Dict::ClearEntry()
169 // we assume all the pointed DictEntries are already cleaned-up
170 // when we clean KeyHt.
175 * \brief Get the dictionary entry identified by a given tag ("group|element")
176 * @param key tag of the entry to be found
177 * @return the corresponding dictionary entry when existing, NULL otherwise
179 DictEntry *Dict::GetEntry(TagKey const &key)
181 TagKeyHT::iterator it = KeyHt.find(key);
182 if ( it == KeyHt.end() )
186 return &(it->second);
189 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
191 TagKey key = DictEntry::TranslateToKey(group, elem);
192 TagKeyHT::iterator it = KeyHt.find(key);
193 if ( it == KeyHt.end() )
197 return &(it->second);
201 * \brief Get the first entry while visiting the Dict entries
202 * \return The first DicEntry if found, otherwhise NULL
204 DictEntry *Dict::GetFirstEntry()
206 ItKeyHt = KeyHt.begin();
207 if ( ItKeyHt != KeyHt.end() )
208 return &(ItKeyHt->second);
213 * \brief Get the next entry while visiting the Hash table (KeyHt)
214 * \note : meaningfull only if GetFirstEntry already called
215 * \return The next DictEntry if found, otherwhise NULL
217 DictEntry *Dict::GetNextEntry()
219 gdcmAssertMacro (ItKeyHt != KeyHt.end());
222 if (ItKeyHt != KeyHt.end())
223 return &(ItKeyHt->second);
227 //-----------------------------------------------------------------------------
230 //-----------------------------------------------------------------------------
233 //-----------------------------------------------------------------------------
236 * \brief Print all the dictionary entries contained in this dictionary.
237 * Entries will be sorted by tag i.e. the couple (group, element).
238 * @param os The output stream to be written to.
239 * @param indent Indentation string to be prepended during printing
241 void Dict::Print(std::ostream &os, std::string const & )
243 os << "Dict file name : " << Filename << std::endl;
244 std::ostringstream s;
246 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
249 s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
250 s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
252 s << tag->second.GetVR() << ", ";
253 s << tag->second.GetVM() << ", ";
254 s << tag->second.GetName() << "." << std::endl;
259 //-----------------------------------------------------------------------------
260 } // end namespace gdcm