1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2005/01/18 11:39:59 $
7 Version: $Revision: 1.67 $
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 void FillDefaultDataDict(Dict *d);
30 //-----------------------------------------------------------------------------
31 // Constructor / Destructor
42 * @param filename from which to build the dictionary.
44 Dict::Dict(std::string const &filename)
52 std::ifstream from( filename.c_str() );
55 gdcmVerboseMacro( "Can't open dictionary" << filename.c_str());
56 // Using default embeded one:
57 FillDefaultDataDict( this );
68 from >> std::ws; //remove white space
69 std::getline(from, name);
71 const DictEntry newEntry(group, element, vr, vm, name);
72 AddNewEntry(newEntry);
88 //-----------------------------------------------------------------------------
91 * \brief Print all the dictionary entries contained in this dictionary.
92 * Entries will be sorted by tag i.e. the couple (group, element).
93 * @param os The output stream to be written to.
95 void Dict::Print(std::ostream &os, std::string const & )
97 os << "Dict file name : " << Filename << std::endl;
100 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
103 s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
104 s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
106 s << tag->second.GetVR() << ", ";
107 s << tag->second.GetVM() << ", ";
108 s << tag->second.GetName() << "." << std::endl;
114 //-----------------------------------------------------------------------------
117 * \brief Remove all Dicom Dictionary Entries
119 void Dict::ClearEntry()
121 // we assume all the pointed DictEntries are already cleaned-up
122 // when we clean KeyHt.
127 * \brief adds a new Dicom Dictionary Entry
128 * @param newEntry entry to add
129 * @return false if Dicom Element already exists
131 bool Dict::AddNewEntry(DictEntry const &newEntry)
133 const TagKey &key = newEntry.GetKey();
135 if(KeyHt.count(key) == 1)
137 gdcmVerboseMacro( "Already present" << key.c_str());
142 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
148 * \brief replaces an already existing Dicom Element by a new one
149 * @param newEntry new entry (overwrites any previous one with same tag)
150 * @return false if Dicom Element doesn't exist
152 bool Dict::ReplaceEntry(DictEntry const &newEntry)
154 if ( RemoveEntry(newEntry.GetKey()) )
156 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
163 * \brief removes an already existing Dicom Dictionary Entry,
164 * identified by its Tag
165 * @param key (group|element)
166 * @return false if Dicom Dictionary Entry doesn't exist
168 bool Dict::RemoveEntry (TagKey const &key)
170 TagKeyHT::const_iterator it = KeyHt.find(key);
171 if(it != KeyHt.end())
179 gdcmVerboseMacro( "Unfound entry" << key.c_str());
185 * \brief removes an already existing Dicom Dictionary Entry,
186 * identified by its group,element number
187 * @param group Dicom group number of the Dicom Element
188 * @param elem Dicom element number of the Dicom Element
189 * @return false if Dicom Dictionary Entry doesn't exist
191 bool Dict::RemoveEntry (uint16_t group, uint16_t elem)
193 return RemoveEntry(DictEntry::TranslateToKey(group, elem));
197 * \brief Get the dictionary entry identified by a given tag (group,element)
198 * @param group group of the entry to be found
199 * @param elem element of the entry to be found
200 * @return the corresponding dictionary entry when existing, NULL otherwise
202 DictEntry *Dict::GetDictEntry(uint16_t group, uint16_t elem)
204 TagKey key = DictEntry::TranslateToKey(group, elem);
205 TagKeyHT::iterator it = KeyHt.find(key);
206 if ( it == KeyHt.end() )
210 return &(it->second);
214 * \brief Consider all the entries of the public dicom dictionary.
215 * Build all list of all the tag names of all those entries.
216 * \sa DictSet::GetPubDictTagNamesByCategory
217 * @return A list of all entries of the public dicom dictionary.
224 //EntryNamesList *Dict::GetDictEntryNames()
226 // EntryNamesList *result = new EntryNamesList;
227 // for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
229 // result->push_back( tag->second.GetName() );
235 * \brief Consider all the entries of the public dicom dictionary.
236 * Build an hashtable whose keys are the names of the groups
237 * (fourth field in each line of dictionary) and whose corresponding
238 * values are lists of all the dictionary entries among that
239 * group. Note that apparently the Dicom standard doesn't explicitely
240 * define a name (as a string) for each group.
241 * A typical usage of this method would be to enable a dynamic
242 * configuration of a Dicom file browser: the admin/user can
243 * select in the interface which Dicom tags should be displayed.
244 * \warning Dicom *doesn't* define any name for any 'categorie'
245 * (the dictionary fourth field was formerly NIH defined
246 * - and no longer he is-
247 * and will be removed when Dicom provides us a text file
248 * with the 'official' Dictionary, that would be more friendly
249 * than asking us to perform a line by line check of the dictionary
250 * at the beginning of each year to -try to- guess the changes)
251 * Therefore : please NEVER use that fourth field :-(
253 * @return An hashtable: whose keys are the names of the groups and whose
254 * corresponding values are lists of all the dictionary entries
260 //EntryNamesByCatMap *Dict::GetDictEntryNamesByCategory()
262 // EntryNamesByCatMap *result = new EntryNamesByCatMap;
264 // for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
266 // (*result)[tag->second.GetFourth()].push_back(tag->second.GetName());
273 * \brief Get the first entry while visiting the Dict entries
274 * \return The first DicEntry if found, otherwhise NULL
276 DictEntry *Dict::GetFirstEntry()
278 ItKeyHt = KeyHt.begin();
279 if( ItKeyHt != KeyHt.end() )
280 return &(ItKeyHt->second);
285 * \brief Get the next entry while visiting the Hash table (KeyHt)
286 * \note : meaningfull only if GetFirstEntry already called
287 * \return The next DictEntry if found, otherwhise NULL
289 DictEntry *Dict::GetNextEntry()
291 gdcmAssertMacro (ItKeyHt != KeyHt.end());
295 if (ItKeyHt != KeyHt.end())
296 return &(ItKeyHt->second);
301 //-----------------------------------------------------------------------------
304 //-----------------------------------------------------------------------------
307 //-----------------------------------------------------------------------------
309 } // end namespace gdcm