]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
'Dictionnary' is now spelt 'Dictionary',
[gdcm.git] / src / gdcmDict.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDict.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/17 11:13:21 $
7   Version:   $Revision: 1.65 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmDict.h"
20 #include "gdcmUtil.h"
21 #include "gdcmDebug.h"
22
23 #include <fstream>
24 #include <iostream>
25 #include <iomanip>
26
27 namespace gdcm 
28 {
29 void FillDefaultDataDict(Dict *d);
30 //-----------------------------------------------------------------------------
31 // Constructor / Destructor
32 /**
33  * \brief   Constructor
34  */
35 Dict::Dict( )
36 {
37    Filename="";
38 }
39
40 /**
41  * \brief   Constructor
42  * @param   filename from which to build the dictionary.
43  */
44 Dict::Dict(std::string const &filename)
45 {
46    uint16_t group;
47    uint16_t element;
48    TagName vr;
49    TagName vm;
50    TagName name;
51
52    std::ifstream from( filename.c_str() );
53    if( !from )
54    {
55       gdcmVerboseMacro( "Can't open dictionary" << filename.c_str());
56       // Using default embeded one:
57       FillDefaultDataDict( this );
58    }
59    else
60    {
61       while (!from.eof())
62       {
63          from >> std::hex;
64          from >> group;
65          from >> element;
66          from >> vr;
67          from >> vm;
68          from >> std::ws;  //remove white space
69          std::getline(from, name);
70    
71          const DictEntry newEntry(group, element, vr, vm, name);
72          AddNewEntry(newEntry);
73       }
74
75       Filename = filename;
76       from.close();
77    }
78 }
79
80 /**
81  * \brief  Destructor 
82  */
83 Dict::~Dict()
84 {
85    ClearEntry();
86 }
87
88 //-----------------------------------------------------------------------------
89 // Print
90 /**
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.
94  */
95 void Dict::Print(std::ostream &os, std::string const & )
96 {
97    os << "Dict file name : " << Filename << std::endl;
98    std::ostringstream s;
99
100    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
101    {
102       s << "Entry : ";
103       s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
104       s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
105         << std::dec;
106       s << tag->second.GetVR() << ", ";
107       s << tag->second.GetVM() << ", ";
108       s << tag->second.GetName() << "."  << std::endl;
109    }
110    os << s.str();
111 }
112
113
114 //-----------------------------------------------------------------------------
115 // Public
116 /**
117  * \ingroup Dict
118  * \brief   Remove all Dicom Dictionary Entries
119  */
120 void Dict::ClearEntry()
121 {
122    // we assume all the pointed DictEntries are already cleaned-up
123    // when we clean KeyHt.
124    KeyHt.clear();
125 }
126
127 /**
128  * \ingroup Dict
129  * \brief  adds a new Dicom Dictionary Entry 
130  * @param   newEntry entry to add 
131  * @return  false if Dicom Element already exists
132  */
133 bool Dict::AddNewEntry(DictEntry const &newEntry) 
134 {
135    const TagKey &key = newEntry.GetKey();
136
137    if(KeyHt.count(key) == 1)
138    {
139       gdcmVerboseMacro( "Already present" << key.c_str());
140       return false;
141    } 
142    else 
143    {
144       KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
145       return true;
146    }
147 }
148
149 /**
150  * \ingroup Dict
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
154  */
155 bool Dict::ReplaceEntry(DictEntry const &newEntry)
156 {
157    if ( RemoveEntry(newEntry.GetKey()) )
158    {
159        KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
160        return true;
161    } 
162    return false;
163 }
164
165 /**
166  * \ingroup Dict
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
171  */
172 bool Dict::RemoveEntry (TagKey const &key) 
173 {
174    TagKeyHT::const_iterator it = KeyHt.find(key);
175    if(it != KeyHt.end()) 
176    {
177       KeyHt.erase(key);
178
179       return true;
180    } 
181    else 
182    {
183       gdcmVerboseMacro( "Unfound entry" << key.c_str());
184       return false;
185   }
186 }
187
188 /**
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
194  */
195 bool Dict::RemoveEntry (uint16_t group, uint16_t elem)
196 {
197    return RemoveEntry(DictEntry::TranslateToKey(group, elem));
198 }
199
200 /**
201  * \brief   Get the dictionary 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 dictionary entry when existing, NULL otherwise
205  */
206 DictEntry *Dict::GetDictEntry(uint16_t group, uint16_t elem)
207 {
208    TagKey key = DictEntry::TranslateToKey(group, elem);
209    TagKeyHT::iterator it = KeyHt.find(key);
210    if ( it == KeyHt.end() )
211    {
212       return 0;
213    }
214    return &(it->second);
215 }
216
217 /** 
218  * \brief   Consider all the entries of the public dicom dictionary. 
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 dictionary.
222  */
223
224  
225  // Probabely useless
226   
227  
228 //EntryNamesList *Dict::GetDictEntryNames() 
229 //{
230 //   EntryNamesList *result = new EntryNamesList;
231 //   for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
232 //   {
233 //      result->push_back( tag->second.GetName() );
234 //   }
235 //   return result;
236 //}
237
238 /** 
239  * \ingroup Dict
240  * \brief   Consider all the entries of the public dicom dictionary.
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 dictionary 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 dictionary 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' Dictionary, that would be more friendly
254  *           than asking us to perform a line by line check of the dictionary
255  *           at the beginning of each year to -try to- guess the changes)
256  *           Therefore : please NEVER use that fourth field :-(
257  *
258  * @return  An hashtable: whose keys are the names of the groups and whose
259  *          corresponding values are lists of all the dictionary entries
260  *          among that group.
261  */
262  
263  // Probabely useless
264  
265 //EntryNamesByCatMap *Dict::GetDictEntryNamesByCategory() 
266 //{
267 //   EntryNamesByCatMap *result = new EntryNamesByCatMap;
268 //
269 //   for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
270 //   {
271 //      (*result)[tag->second.GetFourth()].push_back(tag->second.GetName());
272 //   }
273 //
274 //   return result;
275 //}
276
277 /**
278  * \brief   Initialise the visit of the Hash table (KeyHt)
279  */
280 void Dict::InitTraversal()
281 {
282    ItKeyHt = KeyHt.begin();
283 }
284
285 /**
286  * \brief   Get the next entry while visiting the Hash table (KeyHt)
287  * \return  The next DictEntry if found, otherwhise NULL
288  */
289 DictEntry *Dict::GetNextEntry()
290 {
291    if (ItKeyHt != KeyHt.end())
292    {
293       DictEntry *tmp = &(ItKeyHt->second);
294       ++ItKeyHt;
295
296       return tmp;
297    }
298    else
299    {
300       return NULL;
301    }
302 }
303
304 //-----------------------------------------------------------------------------
305 // Protected
306
307 //-----------------------------------------------------------------------------
308 // Private
309
310 //-----------------------------------------------------------------------------
311
312 } // end namespace gdcm