1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2004/10/12 04:35:45 $
7 Version: $Revision: 1.45 $
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 // Constructor / Destructor
33 * @param filename from which to build the dictionary.
35 Dict::Dict(std::string const & filename)
43 std::ifstream from( filename.c_str() );
44 dbg.Error(!from, "Dict::Dict: can't open dictionary",
50 from >> group; /// MEMORY LEAK in std::istream::operator>>
54 from >> std::ws; // used to be eatwhite(from);
55 std::getline(from, name); /// MEMORY LEAK in std::getline<>
57 DictEntry * newEntry = new DictEntry(group, element,
59 AddNewEntry(newEntry);
71 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
73 DictEntry* entryToDelete = tag->second;
79 // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
80 // assume all the pointed DictEntries are already cleaned-up when
86 //-----------------------------------------------------------------------------
89 * \brief Print all the dictionary entries contained in this dictionary.
90 * Entries will be sorted by tag i.e. the couple (group, element).
91 * @param os The output stream to be written to.
93 void Dict::Print(std::ostream &os)
95 os << "Dict file name : " << Filename << std::endl;
100 * \brief Print all the dictionary entries contained in this dictionary.
101 * Entries will be sorted by tag i.e. the couple (group, element).
102 * @param os The output stream to be written to.
104 void Dict::PrintByKey(std::ostream &os)
106 std::ostringstream s;
108 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
111 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
112 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = "
114 s << tag->second->GetVR() << ", ";
115 s << tag->second->GetFourth() << ", ";
116 s << tag->second->GetName() << "." << std::endl;
122 * \brief Print all the dictionary entries contained in this dictionary.
123 * Entries will be sorted by the name of the dictionary entries.
124 * \warning AVOID USING IT : the name IS NOT an identifier;
125 * unpredictable result
126 * @param os The output stream to be written to.
128 void Dict::PrintByName(std::ostream& os)
130 std::ostringstream s;
132 for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag)
135 s << tag->second->GetName() << ",";
136 s << tag->second->GetVR() << ", ";
137 s << tag->second->GetFourth() << ", ";
138 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
139 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = ";
140 s << std::dec << std::endl;
145 //-----------------------------------------------------------------------------
149 * \brief adds a new Dicom Dictionary Entry
150 * @param newEntry entry to add
151 * @return false if Dicom Element already exists
153 bool Dict::AddNewEntry(DictEntry *newEntry)
155 TagKey key = newEntry->GetKey();
157 if(KeyHt.count(key) == 1)
159 dbg.Verbose(1, "Dict::AddNewEntry already present", key.c_str());
164 KeyHt[newEntry->GetKey()] = newEntry;
165 NameHt[newEntry->GetName()] = newEntry;
172 * \brief replaces an already existing Dicom Element by a new one
173 * @param newEntry new entry (overwrites any previous one with same tag)
174 * @return false if Dicom Element doesn't exist
176 bool Dict::ReplaceEntry(DictEntry *newEntry)
178 if ( RemoveEntry(newEntry->DictEntry::GetKey()) )
180 KeyHt[newEntry->GetKey()] = newEntry;
181 NameHt[newEntry->GetName()] = newEntry;
189 * \brief removes an already existing Dicom Dictionary Entry,
190 * identified by its Tag
191 * @param key (group|element)
192 * @return false if Dicom Dictionary Entry doesn't exist
194 bool Dict::RemoveEntry(TagKey key)
196 if(KeyHt.count(key) == 1)
198 DictEntry* entryToDelete = KeyHt.find(key)->second;
202 NameHt.erase(entryToDelete->GetName());
203 delete entryToDelete;
211 dbg.Verbose(1, "Dict::RemoveEntry unfound entry", key.c_str());
217 * \brief removes an already existing Dicom Dictionary Entry,
218 * identified by its group,element number
219 * @param group Dicom group number of the Dicom Element
220 * @param element Dicom element number of the Dicom Element
221 * @return false if Dicom Dictionary Entry doesn't exist
223 bool Dict::RemoveEntry (uint16_t group, uint16_t element)
225 return RemoveEntry(DictEntry::TranslateToKey(group, element));
229 * \brief Get the dictionnary entry identified by it's name.
230 * @param name element of the ElVal to modify
231 * \warning : NEVER use it !
232 * the 'name' IS NOT an identifier within the Dicom Dicom Dictionary
233 * the name MAY CHANGE between two versions !
234 * @return the corresponding dictionnary entry when existing, NULL otherwise
236 DictEntry* Dict::GetDictEntryByName(TagName name)
238 if ( !NameHt.count(name))
242 return NameHt.find(name)->second;
246 * \brief Get the dictionnary entry identified by a given tag (group,element)
247 * @param group group of the entry to be found
248 * @param element element of the entry to be found
249 * @return the corresponding dictionnary entry when existing, NULL otherwise
251 DictEntry* Dict::GetDictEntryByNumber(uint16_t group, uint16_t element)
253 TagKey key = DictEntry::TranslateToKey(group, element);
254 if ( !KeyHt.count(key) )
258 return KeyHt.find(key)->second;
262 * \brief Consider all the entries of the public dicom dictionnary.
263 * Build all list of all the tag names of all those entries.
264 * \sa DictSet::GetPubDictTagNamesByCategory
265 * @return A list of all entries of the public dicom dictionnary.
267 std::list<std::string>* Dict::GetDictEntryNames()
269 std::list<std::string> *result = new std::list<std::string>;
270 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
272 result->push_back( tag->second->GetName() );
279 * \brief Consider all the entries of the public dicom dictionnary.
280 * Build an hashtable whose keys are the names of the groups
281 * (fourth field in each line of dictionary) and whose corresponding
282 * values are lists of all the dictionnary entries among that
283 * group. Note that apparently the Dicom standard doesn't explicitely
284 * define a name (as a string) for each group.
285 * A typical usage of this method would be to enable a dynamic
286 * configuration of a Dicom file browser: the admin/user can
287 * select in the interface which Dicom tags should be displayed.
288 * \warning Dicom *doesn't* define any name for any 'categorie'
289 * (the dictionnary fourth field was formerly NIH defined
290 * - and no longer he is-
291 * and will be removed when Dicom provides us a text file
292 * with the 'official' Dictionnary, that would be more friendly
293 * than asking us to perform a line by line check of the dictionnary
294 * at the beginning of each year to -try to- guess the changes)
295 * Therefore : please NEVER use that fourth field :-(
297 * @return An hashtable: whose keys are the names of the groups and whose
298 * corresponding values are lists of all the dictionnary entries
301 std::map<std::string, std::list<std::string> > *Dict::GetDictEntryNamesByCategory(void)
303 std::map<std::string, std::list<std::string> > *result = new std::map<std::string, std::list<std::string> >;
305 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
307 (*result)[tag->second->GetFourth()].push_back(tag->second->GetName());
313 //-----------------------------------------------------------------------------
316 //-----------------------------------------------------------------------------
319 //-----------------------------------------------------------------------------
321 } // end namespace gdcm