1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2005/01/07 19:20:38 $
7 Version: $Revision: 1.59 $
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
34 * @param filename from which to build the dictionary.
36 Dict::Dict(std::string const &filename)
44 std::ifstream from( filename.c_str() );
47 gdcmVerboseMacro("Dict::Dict: can't open dictionary" << filename.c_str());
48 // Using default embeded one:
49 FillDefaultDataDict( this );
60 from >> std::ws; //remove white space
61 std::getline(from, name);
63 const DictEntry newEntry(group, element, vr, vm, name);
64 AddNewEntry(newEntry);
77 // we assume all the pointed DictEntries are already cleaned-up
78 // when we clean KeyHt.
82 //-----------------------------------------------------------------------------
85 * \brief Print all the dictionary entries contained in this dictionary.
86 * Entries will be sorted by tag i.e. the couple (group, element).
87 * @param os The output stream to be written to.
89 void Dict::Print(std::ostream &os)
91 os << "Dict file name : " << Filename << std::endl;
96 * \brief Print all the dictionary entries contained in this dictionary.
97 * Entries will be sorted by tag i.e. the couple (group, element).
98 * @param os The output stream to be written to.
100 void Dict::PrintByKey(std::ostream &os)
102 std::ostringstream s;
104 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
107 s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
108 s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
110 s << tag->second.GetVR() << ", ";
111 s << tag->second.GetVM() << ", ";
112 s << tag->second.GetName() << "." << std::endl;
117 //-----------------------------------------------------------------------------
121 * \brief adds a new Dicom Dictionary Entry
122 * @param newEntry entry to add
123 * @return false if Dicom Element already exists
125 bool Dict::AddNewEntry(DictEntry const &newEntry)
127 const TagKey &key = newEntry.GetKey();
129 if(KeyHt.count(key) == 1)
131 gdcmVerboseMacro("Dict::AddNewEntry already present" << key.c_str());
136 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
143 * \brief replaces an already existing Dicom Element by a new one
144 * @param newEntry new entry (overwrites any previous one with same tag)
145 * @return false if Dicom Element doesn't exist
147 bool Dict::ReplaceEntry(DictEntry const &newEntry)
149 if ( RemoveEntry(newEntry.GetKey()) )
151 KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
159 * \brief removes an already existing Dicom Dictionary Entry,
160 * identified by its Tag
161 * @param key (group|element)
162 * @return false if Dicom Dictionary Entry doesn't exist
164 bool Dict::RemoveEntry (TagKey const &key)
166 TagKeyHT::const_iterator it = KeyHt.find(key);
167 if(it != KeyHt.end())
175 gdcmVerboseMacro("Dict::RemoveEntry unfound entry" << key.c_str());
181 * \brief removes an already existing Dicom Dictionary Entry,
182 * identified by its group,element number
183 * @param group Dicom group number of the Dicom Element
184 * @param elem Dicom element number of the Dicom Element
185 * @return false if Dicom Dictionary Entry doesn't exist
187 bool Dict::RemoveEntry (uint16_t group, uint16_t elem)
189 return RemoveEntry(DictEntry::TranslateToKey(group, elem));
193 * \brief Get the dictionnary entry identified by a given tag (group,element)
194 * @param group group of the entry to be found
195 * @param elem element of the entry to be found
196 * @return the corresponding dictionnary entry when existing, NULL otherwise
198 DictEntry *Dict::GetDictEntryByNumber(uint16_t group, uint16_t elem)
200 TagKey key = DictEntry::TranslateToKey(group, elem);
201 TagKeyHT::iterator it = KeyHt.find(key);
202 if ( it == KeyHt.end() )
206 return &(it->second);
210 * \brief Consider all the entries of the public dicom dictionnary.
211 * Build all list of all the tag names of all those entries.
212 * \sa DictSet::GetPubDictTagNamesByCategory
213 * @return A list of all entries of the public dicom dictionnary.
220 //EntryNamesList *Dict::GetDictEntryNames()
222 // EntryNamesList *result = new EntryNamesList;
223 // for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
225 // result->push_back( tag->second.GetName() );
232 * \brief Consider all the entries of the public dicom dictionnary.
233 * Build an hashtable whose keys are the names of the groups
234 * (fourth field in each line of dictionary) and whose corresponding
235 * values are lists of all the dictionnary entries among that
236 * group. Note that apparently the Dicom standard doesn't explicitely
237 * define a name (as a string) for each group.
238 * A typical usage of this method would be to enable a dynamic
239 * configuration of a Dicom file browser: the admin/user can
240 * select in the interface which Dicom tags should be displayed.
241 * \warning Dicom *doesn't* define any name for any 'categorie'
242 * (the dictionnary fourth field was formerly NIH defined
243 * - and no longer he is-
244 * and will be removed when Dicom provides us a text file
245 * with the 'official' Dictionnary, that would be more friendly
246 * than asking us to perform a line by line check of the dictionnary
247 * at the beginning of each year to -try to- guess the changes)
248 * Therefore : please NEVER use that fourth field :-(
250 * @return An hashtable: whose keys are the names of the groups and whose
251 * corresponding values are lists of all the dictionnary entries
257 //EntryNamesByCatMap *Dict::GetDictEntryNamesByCategory()
259 // EntryNamesByCatMap *result = new EntryNamesByCatMap;
261 // for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
263 // (*result)[tag->second.GetFourth()].push_back(tag->second.GetName());
269 //-----------------------------------------------------------------------------
272 //-----------------------------------------------------------------------------
275 //-----------------------------------------------------------------------------
277 } // end namespace gdcm