1 /*=========================================================================
4 Module: $RCSfile: gdcmDict.cxx,v $
6 Date: $Date: 2004/06/21 04:43:02 $
7 Version: $Revision: 1.36 $
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 & FileName) {
34 guint16 group, element;
40 std::ifstream from(FileName.c_str());
41 dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary",
46 from >> group; /// MEMORY LEAK in std::istream::operator>>
51 getline(from, name); /// MEMORY LEAK in std::getline<>
53 gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
55 AddNewEntry(newEntry);
65 gdcmDict::~gdcmDict() {
66 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
67 gdcmDictEntry* EntryToDelete = tag->second;
71 // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
72 // assume all the pointed gdcmDictEntries are already cleaned-up when
78 //-----------------------------------------------------------------------------
81 * \brief Print all the dictionary entries contained in this dictionary.
82 * Entries will be sorted by tag i.e. the couple (group, element).
83 * @param os The output stream to be written to.
85 void gdcmDict::Print(std::ostream &os) {
86 os<<"Dict file name : "<<filename<<std::endl;
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 gdcmDict::PrintByKey(std::ostream &os) {
98 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
100 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
101 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = " << std::dec;
102 s << tag->second->GetVR() << ", ";
103 s << tag->second->GetFourth() << ", ";
104 s << tag->second->GetName() << "." << std::endl;
110 * \brief Print all the dictionary entries contained in this dictionary.
111 * Entries will be sorted by the name of the dictionary entries.
112 * \warning AVOID USING IT : the name IS NOT an identifier;
113 * unpredictable result
114 * @param os The output stream to be written to.
116 void gdcmDict::PrintByName(std::ostream& os) {
117 std::ostringstream s;
119 for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
121 s << tag->second->GetName() << ",";
122 s << tag->second->GetVR() << ", ";
123 s << tag->second->GetFourth() << ", ";
124 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
125 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = ";
126 s << std::dec << std::endl;
131 //-----------------------------------------------------------------------------
135 * \brief adds a new Dicom Dictionary Entry
136 * @param NewEntry entry to add
137 * @return false if Dicom Element already exists
139 bool gdcmDict::AddNewEntry(gdcmDictEntry *NewEntry)
142 key = NewEntry->GetKey();
144 if(KeyHt.count(key) == 1)
146 dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
151 KeyHt[NewEntry->GetKey()] = NewEntry;
152 NameHt[NewEntry->GetName()] = NewEntry; /// MEMORY LEAK in
153 /// std::map<>::operator[]
160 * \brief replaces an already existing Dicom Element by a new one
161 * @param NewEntry new entry (overwrites any previous one with same tag)
162 * @return false if Dicom Element doesn't exist
164 bool gdcmDict::ReplaceEntry(gdcmDictEntry *NewEntry) {
165 if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
166 KeyHt[NewEntry->GetKey()] = NewEntry;
167 NameHt[NewEntry->GetName()] = NewEntry;
175 * \brief removes an already existing Dicom Dictionary Entry,
176 * identified by its Tag
177 * @param key (group|element)
178 * @return false if Dicom Dictionary Entry doesn't exist
180 bool gdcmDict::RemoveEntry(TagKey key)
182 if(KeyHt.count(key) == 1)
184 gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
188 NameHt.erase(EntryToDelete->GetName());
189 delete EntryToDelete;
197 dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
204 * \brief removes an already existing Dicom Dictionary Entry,
205 * identified by its group,element number
206 * @param group Dicom group number of the Dicom Element
207 * @param element Dicom element number of the Dicom Element
208 * @return false if Dicom Dictionary Entry doesn't exist
210 bool gdcmDict::RemoveEntry (guint16 group, guint16 element) {
211 return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
216 * \brief Get the dictionnary entry identified by it's name.
217 * @param name element of the ElVal to modify
218 * \warning : NEVER use it !
219 * the 'name' IS NOT an identifier within the Dicom Dicom Dictionary
220 * the name MAY CHANGE between two versions !
221 * @return the corresponding dictionnary entry when existing, NULL otherwise
223 gdcmDictEntry *gdcmDict::GetDictEntryByName(TagName name) {
224 if ( ! NameHt.count(name))
226 return NameHt.find(name)->second;
231 * \brief Get the dictionnary entry identified by a given tag (group,element)
232 * @param group group of the entry to be found
233 * @param element element of the entry to be found
234 * @return the corresponding dictionnary entry when existing, NULL otherwise
236 gdcmDictEntry *gdcmDict::GetDictEntryByNumber(guint16 group, guint16 element) {
237 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
238 if ( ! KeyHt.count(key))
240 return KeyHt.find(key)->second;
245 * \brief Consider all the entries of the public dicom dictionnary.
246 * Build all list of all the tag names of all those entries.
247 * \sa gdcmDictSet::GetPubDictTagNamesByCategory
248 * @return A list of all entries of the public dicom dictionnary.
250 std::list<std::string> *gdcmDict::GetDictEntryNames(void)
252 std::list<std::string> *Result = new std::list<std::string>;
253 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
255 Result->push_back( tag->second->GetName() );
262 * \brief Consider all the entries of the public dicom dictionnary.
263 * Build an hashtable whose keys are the names of the groups
264 * (fourth field in each line of dictionary) and whose corresponding
265 * values are lists of all the dictionnary entries among that
266 * group. Note that apparently the Dicom standard doesn't explicitely
267 * define a name (as a string) for each group.
268 * A typical usage of this method would be to enable a dynamic
269 * configuration of a Dicom file browser: the admin/user can
270 * select in the interface which Dicom tags should be displayed.
271 * \warning Dicom *doesn't* define any name for any 'categorie'
272 * (the dictionnary fourth field was formerly NIH defined
273 * - and no longer he is-
274 * and will be removed when Dicom provides us a text file
275 * with the 'official' Dictionnary, that would be more friendly
276 * than asking us to perform a line by line check of the dictionnary
277 * at the beginning of each year to -try to- guess the changes)
278 * Therefore : please NEVER use that fourth field :-(
280 * @return An hashtable: whose keys are the names of the groups and whose
281 * corresponding values are lists of all the dictionnary entries
284 std::map<std::string, std::list<std::string> > *gdcmDict::GetDictEntryNamesByCategory(void)
286 std::map<std::string, std::list<std::string> > *Result = new std::map<std::string, std::list<std::string> >;
288 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
290 (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
295 //-----------------------------------------------------------------------------
298 //-----------------------------------------------------------------------------
301 //-----------------------------------------------------------------------------