1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2004/08/31 14:24:47 $
7 Version: $Revision: 1.42 $
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.htm 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"
27 //-----------------------------------------------------------------------------
28 // Constructor / Destructor
31 * @param filename from which to build the dictionary.
33 gdcmDict::gdcmDict(std::string const & filename)
41 std::ifstream from( filename.c_str() );
42 dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary",
48 from >> group; /// MEMORY LEAK in std::istream::operator>>
53 std::getline(from, name); /// MEMORY LEAK in std::getline<>
55 gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
57 AddNewEntry(newEntry);
69 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
71 gdcmDictEntry* entryToDelete = tag->second;
77 // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
78 // assume all the pointed gdcmDictEntries are already cleaned-up when
84 //-----------------------------------------------------------------------------
87 * \brief Print all the dictionary entries contained in this dictionary.
88 * Entries will be sorted by tag i.e. the couple (group, element).
89 * @param os The output stream to be written to.
91 void gdcmDict::Print(std::ostream &os)
93 os << "Dict file name : " << Filename << std::endl;
98 * \brief Print all the dictionary entries contained in this dictionary.
99 * Entries will be sorted by tag i.e. the couple (group, element).
100 * @param os The output stream to be written to.
102 void gdcmDict::PrintByKey(std::ostream &os)
104 std::ostringstream s;
106 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
109 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
110 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = "
112 s << tag->second->GetVR() << ", ";
113 s << tag->second->GetFourth() << ", ";
114 s << tag->second->GetName() << "." << std::endl;
120 * \brief Print all the dictionary entries contained in this dictionary.
121 * Entries will be sorted by the name of the dictionary entries.
122 * \warning AVOID USING IT : the name IS NOT an identifier;
123 * unpredictable result
124 * @param os The output stream to be written to.
126 void gdcmDict::PrintByName(std::ostream& os)
128 std::ostringstream s;
130 for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag)
133 s << tag->second->GetName() << ",";
134 s << tag->second->GetVR() << ", ";
135 s << tag->second->GetFourth() << ", ";
136 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
137 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = ";
138 s << std::dec << std::endl;
143 //-----------------------------------------------------------------------------
147 * \brief adds a new Dicom Dictionary Entry
148 * @param newEntry entry to add
149 * @return false if Dicom Element already exists
151 bool gdcmDict::AddNewEntry(gdcmDictEntry *newEntry)
153 gdcmTagKey key = newEntry->GetKey();
155 if(KeyHt.count(key) == 1)
157 dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
162 KeyHt[newEntry->GetKey()] = newEntry;
163 NameHt[newEntry->GetName()] = newEntry;
170 * \brief replaces an already existing Dicom Element by a new one
171 * @param newEntry new entry (overwrites any previous one with same tag)
172 * @return false if Dicom Element doesn't exist
174 bool gdcmDict::ReplaceEntry(gdcmDictEntry *newEntry)
176 if ( RemoveEntry(newEntry->gdcmDictEntry::GetKey()) )
178 KeyHt[newEntry->GetKey()] = newEntry;
179 NameHt[newEntry->GetName()] = newEntry;
187 * \brief removes an already existing Dicom Dictionary Entry,
188 * identified by its Tag
189 * @param key (group|element)
190 * @return false if Dicom Dictionary Entry doesn't exist
192 bool gdcmDict::RemoveEntry(gdcmTagKey key)
194 if(KeyHt.count(key) == 1)
196 gdcmDictEntry* entryToDelete = KeyHt.find(key)->second;
200 NameHt.erase(entryToDelete->GetName());
201 delete entryToDelete;
209 dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
215 * \brief removes an already existing Dicom Dictionary Entry,
216 * identified by its group,element number
217 * @param group Dicom group number of the Dicom Element
218 * @param element Dicom element number of the Dicom Element
219 * @return false if Dicom Dictionary Entry doesn't exist
221 bool gdcmDict::RemoveEntry (uint16_t group, uint16_t element)
223 return RemoveEntry(gdcmDictEntry::TranslateToKey(group, element));
227 * \brief Get the dictionnary entry identified by it's name.
228 * @param name element of the ElVal to modify
229 * \warning : NEVER use it !
230 * the 'name' IS NOT an identifier within the Dicom Dicom Dictionary
231 * the name MAY CHANGE between two versions !
232 * @return the corresponding dictionnary entry when existing, NULL otherwise
234 gdcmDictEntry* gdcmDict::GetDictEntryByName(TagName name)
236 if ( !NameHt.count(name))
240 return NameHt.find(name)->second;
244 * \brief Get the dictionnary entry identified by a given tag (group,element)
245 * @param group group of the entry to be found
246 * @param element element of the entry to be found
247 * @return the corresponding dictionnary entry when existing, NULL otherwise
249 gdcmDictEntry* gdcmDict::GetDictEntryByNumber(uint16_t group, uint16_t element)
251 gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, element);
252 if ( !KeyHt.count(key) )
256 return KeyHt.find(key)->second;
260 * \brief Consider all the entries of the public dicom dictionnary.
261 * Build all list of all the tag names of all those entries.
262 * \sa gdcmDictSet::GetPubDictTagNamesByCategory
263 * @return A list of all entries of the public dicom dictionnary.
265 std::list<std::string>* gdcmDict::GetDictEntryNames()
267 std::list<std::string> *result = new std::list<std::string>;
268 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
270 result->push_back( tag->second->GetName() );
277 * \brief Consider all the entries of the public dicom dictionnary.
278 * Build an hashtable whose keys are the names of the groups
279 * (fourth field in each line of dictionary) and whose corresponding
280 * values are lists of all the dictionnary entries among that
281 * group. Note that apparently the Dicom standard doesn't explicitely
282 * define a name (as a string) for each group.
283 * A typical usage of this method would be to enable a dynamic
284 * configuration of a Dicom file browser: the admin/user can
285 * select in the interface which Dicom tags should be displayed.
286 * \warning Dicom *doesn't* define any name for any 'categorie'
287 * (the dictionnary fourth field was formerly NIH defined
288 * - and no longer he is-
289 * and will be removed when Dicom provides us a text file
290 * with the 'official' Dictionnary, that would be more friendly
291 * than asking us to perform a line by line check of the dictionnary
292 * at the beginning of each year to -try to- guess the changes)
293 * Therefore : please NEVER use that fourth field :-(
295 * @return An hashtable: whose keys are the names of the groups and whose
296 * corresponding values are lists of all the dictionnary entries
299 std::map<std::string, std::list<std::string> > *gdcmDict::GetDictEntryNamesByCategory(void)
301 std::map<std::string, std::list<std::string> > *result = new std::map<std::string, std::list<std::string> >;
303 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
305 (*result)[tag->second->GetFourth()].push_back(tag->second->GetName());
311 //-----------------------------------------------------------------------------
314 //-----------------------------------------------------------------------------
317 //-----------------------------------------------------------------------------