1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2005/02/11 15:22:18 $
7 Version: $Revision: 1.74 $
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 group group of the entry to be found
177 * @param elem element of the entry to be found
178 * @return the corresponding dictionary entry when existing, NULL otherwise
180 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
182 TagKey key = DictEntry::TranslateToKey(group, elem);
183 TagKeyHT::iterator it = KeyHt.find(key);
184 if ( it == KeyHt.end() )
188 return &(it->second);
192 * \brief Get the first entry while visiting the Dict entries
193 * \return The first DicEntry if found, otherwhise NULL
195 DictEntry *Dict::GetFirstEntry()
197 ItKeyHt = KeyHt.begin();
198 if( ItKeyHt != KeyHt.end() )
199 return &(ItKeyHt->second);
204 * \brief Get the next entry while visiting the Hash table (KeyHt)
205 * \note : meaningfull only if GetFirstEntry already called
206 * \return The next DictEntry if found, otherwhise NULL
208 DictEntry *Dict::GetNextEntry()
210 gdcmAssertMacro (ItKeyHt != KeyHt.end());
213 if (ItKeyHt != KeyHt.end())
214 return &(ItKeyHt->second);
218 //-----------------------------------------------------------------------------
221 //-----------------------------------------------------------------------------
224 //-----------------------------------------------------------------------------
227 * \brief Print all the dictionary entries contained in this dictionary.
228 * Entries will be sorted by tag i.e. the couple (group, element).
229 * @param os The output stream to be written to.
230 * @param indent Indentation string to be prepended during printing
232 void Dict::Print(std::ostream &os, std::string const & )
234 os << "Dict file name : " << Filename << std::endl;
235 std::ostringstream s;
237 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
240 s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
241 s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
243 s << tag->second.GetVR() << ", ";
244 s << tag->second.GetVM() << ", ";
245 s << tag->second.GetName() << "." << std::endl;
250 //-----------------------------------------------------------------------------
251 } // end namespace gdcm