1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2005/01/14 11:28:30 $
7 Version: $Revision: 1.63 $
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)
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 //-----------------------------------------------------------------------------
118 * \brief Remove all Dicom Dictionary Entries
120 void Dict::ClearEntry()
122 // we assume all the pointed DictEntries are already cleaned-up
123 // when we clean KeyHt.
129 * \brief adds a new Dicom Dictionary Entry
130 * @param newEntry entry to add
131 * @return false if Dicom Element already exists
133 bool Dict::AddNewEntry(DictEntry const &newEntry)
135 const TagKey &key = newEntry.GetKey();
137 if(KeyHt.count(key) == 1)
139 gdcmVerboseMacro( "Already present" << key.c_str());
144 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
151 * \brief replaces an already existing Dicom Element by a new one
152 * @param newEntry new entry (overwrites any previous one with same tag)
153 * @return false if Dicom Element doesn't exist
155 bool Dict::ReplaceEntry(DictEntry const &newEntry)
157 if ( RemoveEntry(newEntry.GetKey()) )
159 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
167 * \brief removes an already existing Dicom Dictionary Entry,
168 * identified by its Tag
169 * @param key (group|element)
170 * @return false if Dicom Dictionary Entry doesn't exist
172 bool Dict::RemoveEntry (TagKey const &key)
174 TagKeyHT::const_iterator it = KeyHt.find(key);
175 if(it != KeyHt.end())
183 gdcmVerboseMacro( "Unfound entry" << key.c_str());
189 * \brief removes an already existing Dicom Dictionary Entry,
190 * identified by its group,element number
191 * @param group Dicom group number of the Dicom Element
192 * @param elem Dicom element number of the Dicom Element
193 * @return false if Dicom Dictionary Entry doesn't exist
195 bool Dict::RemoveEntry (uint16_t group, uint16_t elem)
197 return RemoveEntry(DictEntry::TranslateToKey(group, elem));
201 * \brief Get the dictionnary entry identified by a given tag (group,element)
202 * @param group group of the entry to be found
203 * @param elem element of the entry to be found
204 * @return the corresponding dictionnary entry when existing, NULL otherwise
206 DictEntry *Dict::GetDictEntry(uint16_t group, uint16_t elem)
208 TagKey key = DictEntry::TranslateToKey(group, elem);
209 TagKeyHT::iterator it = KeyHt.find(key);
210 if ( it == KeyHt.end() )
214 return &(it->second);
218 * \brief Consider all the entries of the public dicom dictionnary.
219 * Build all list of all the tag names of all those entries.
220 * \sa DictSet::GetPubDictTagNamesByCategory
221 * @return A list of all entries of the public dicom dictionnary.
228 //EntryNamesList *Dict::GetDictEntryNames()
230 // EntryNamesList *result = new EntryNamesList;
231 // for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
233 // result->push_back( tag->second.GetName() );
240 * \brief Consider all the entries of the public dicom dictionnary.
241 * Build an hashtable whose keys are the names of the groups
242 * (fourth field in each line of dictionary) and whose corresponding
243 * values are lists of all the dictionnary entries among that
244 * group. Note that apparently the Dicom standard doesn't explicitely
245 * define a name (as a string) for each group.
246 * A typical usage of this method would be to enable a dynamic
247 * configuration of a Dicom file browser: the admin/user can
248 * select in the interface which Dicom tags should be displayed.
249 * \warning Dicom *doesn't* define any name for any 'categorie'
250 * (the dictionnary fourth field was formerly NIH defined
251 * - and no longer he is-
252 * and will be removed when Dicom provides us a text file
253 * with the 'official' Dictionnary, that would be more friendly
254 * than asking us to perform a line by line check of the dictionnary
255 * at the beginning of each year to -try to- guess the changes)
256 * Therefore : please NEVER use that fourth field :-(
258 * @return An hashtable: whose keys are the names of the groups and whose
259 * corresponding values are lists of all the dictionnary entries
265 //EntryNamesByCatMap *Dict::GetDictEntryNamesByCategory()
267 // EntryNamesByCatMap *result = new EntryNamesByCatMap;
269 // for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
271 // (*result)[tag->second.GetFourth()].push_back(tag->second.GetName());
278 * \brief Initialise the visit of the Hash table (KeyHt)
280 void Dict::InitTraversal()
282 ItKeyHt = KeyHt.begin();
286 * \brief Get the next entry whil visiting the Hash table (KeyHt)
287 * \return The next DictEntry if found, otherwhise NULL
289 DictEntry *Dict::GetNextEntry()
291 if (ItKeyHt != KeyHt.end())
293 DictEntry *tmp = &(ItKeyHt->second);
304 //-----------------------------------------------------------------------------
307 //-----------------------------------------------------------------------------
310 //-----------------------------------------------------------------------------
312 } // end namespace gdcm