]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
Doxygenation
[gdcm.git] / src / gdcmDict.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDict.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/06/02 12:26:42 $
7   Version:   $Revision: 1.76 $
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 //-----------------------------------------------------------------------------
30 /// \brief auto generate function, to fill up the Dicom Dictionnary,
31 ///       if relevant file is not found on user's disk
32 void FillDefaultDataDict(Dict *d);
33
34 //-----------------------------------------------------------------------------
35 // Constructor / Destructor
36 /**
37  * \brief   Constructor
38  */
39 Dict::Dict( )
40 {
41    Filename="";
42 }
43
44 /**
45  * \brief   Constructor
46  * @param   filename from which to build the dictionary.
47  */
48 Dict::Dict(std::string const &filename)
49 {
50    uint16_t group;
51    uint16_t element;
52    TagName vr;
53    TagName vm;
54    TagName name;
55
56    std::ifstream from( filename.c_str() );
57    if( !from )
58    {
59       gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
60       // Using default embeded one:
61       FillDefaultDataDict( this );
62    }
63    else
64    {
65       while (!from.eof())
66       {
67          from >> std::hex;
68          from >> group;
69          from >> element;
70          from >> vr;
71          from >> vm;
72          from >> std::ws;  //remove white space
73          std::getline(from, name);
74    
75          const DictEntry newEntry(group, element, vr, vm, name);
76          AddEntry(newEntry);
77       }
78
79       Filename = filename;
80       from.close();
81    }
82 }
83
84 /**
85  * \brief  Destructor 
86  */
87 Dict::~Dict()
88 {
89    ClearEntry();
90 }
91
92 //-----------------------------------------------------------------------------
93 // Public
94 /**
95  * \brief  adds a new Dicom Dictionary Entry 
96  * @param   newEntry entry to add 
97  * @return  false if Dicom Element already exists
98  */
99 bool Dict::AddEntry(DictEntry const &newEntry) 
100 {
101    const TagKey &key = newEntry.GetKey();
102
103    if(KeyHt.count(key) == 1)
104    {
105       gdcmWarningMacro( "Already present" << key.c_str());
106       return false;
107    } 
108    else 
109    {
110       KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
111       return true;
112    }
113 }
114
115 /**
116  * \brief  replaces an already existing Dicom Element by a new one
117  * @param   newEntry new entry (overwrites any previous one with same tag)
118  * @return  false if Dicom Element doesn't exist
119  */
120 bool Dict::ReplaceEntry(DictEntry const &newEntry)
121 {
122    if ( RemoveEntry(newEntry.GetKey()) )
123    {
124        KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
125        return true;
126    } 
127    return false;
128 }
129
130 /**
131  * \brief  removes an already existing Dicom Dictionary Entry,
132  *         identified by its Tag
133  * @param   key (group|element)
134  * @return  false if Dicom Dictionary Entry doesn't exist
135  */
136 bool Dict::RemoveEntry(TagKey const &key) 
137 {
138    TagKeyHT::const_iterator it = KeyHt.find(key);
139    if(it != KeyHt.end()) 
140    {
141       KeyHt.erase(key);
142
143       return true;
144    } 
145    else 
146    {
147       gdcmWarningMacro( "Unfound entry" << key.c_str());
148       return false;
149   }
150 }
151
152 /**
153  * \brief  removes an already existing Dicom Dictionary Entry, 
154  *          identified by its group,element number
155  * @param   group   Dicom group number of the Dicom Element
156  * @param   elem Dicom element number of the Dicom Element
157  * @return  false if Dicom Dictionary Entry doesn't exist
158  */
159 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
160 {
161    return RemoveEntry(DictEntry::TranslateToKey(group, elem));
162 }
163
164 /**
165  * \brief   Remove all Dicom Dictionary Entries
166  */
167 void Dict::ClearEntry()
168 {
169    // we assume all the pointed DictEntries are already cleaned-up
170    // when we clean KeyHt.
171    KeyHt.clear();
172 }
173
174 /**
175  * \brief   Get the dictionary entry identified by a given tag ("group|element")
176  * @param   key   tag of the entry to be found
177  * @return  the corresponding dictionary entry when existing, NULL otherwise
178  */
179 DictEntry *Dict::GetEntry(TagKey const &key)
180 {
181    TagKeyHT::iterator it = KeyHt.find(key);
182    if ( it == KeyHt.end() )
183    {
184       return 0;
185    }
186    return &(it->second);
187 }
188
189 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
190 {
191    TagKey key = DictEntry::TranslateToKey(group, elem);
192    TagKeyHT::iterator it = KeyHt.find(key);
193    if ( it == KeyHt.end() )
194    {
195       return 0;
196    }
197    return &(it->second);
198 }
199
200 /**
201  * \brief   Get the first entry while visiting the Dict entries
202  * \return  The first DicEntry if found, otherwhise NULL
203  */
204 DictEntry *Dict::GetFirstEntry()
205 {
206    ItKeyHt = KeyHt.begin();
207    if( ItKeyHt != KeyHt.end() )
208       return &(ItKeyHt->second);
209    return NULL;
210 }
211
212 /**
213  * \brief   Get the next entry while visiting the Hash table (KeyHt)
214  * \note : meaningfull only if GetFirstEntry already called
215  * \return  The next DictEntry if found, otherwhise NULL
216  */
217 DictEntry *Dict::GetNextEntry()
218 {
219    gdcmAssertMacro (ItKeyHt != KeyHt.end());
220
221    ++ItKeyHt;
222    if (ItKeyHt != KeyHt.end())
223       return &(ItKeyHt->second);
224    return NULL;
225 }
226
227 //-----------------------------------------------------------------------------
228 // Protected
229
230 //-----------------------------------------------------------------------------
231 // Private
232
233 //-----------------------------------------------------------------------------
234 // Print
235 /**
236  * \brief   Print all the dictionary entries contained in this dictionary.
237  *          Entries will be sorted by tag i.e. the couple (group, element).
238  * @param   os The output stream to be written to.
239  * @param indent Indentation string to be prepended during printing
240  */
241 void Dict::Print(std::ostream &os, std::string const & )
242 {
243    os << "Dict file name : " << Filename << std::endl;
244    std::ostringstream s;
245
246    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
247    {
248       s << "Entry : ";
249       s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
250       s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
251         << std::dec;
252       s << tag->second.GetVR() << ", ";
253       s << tag->second.GetVM() << ", ";
254       s << tag->second.GetName() << "."  << std::endl;
255    }
256    os << s.str();
257 }
258
259 //-----------------------------------------------------------------------------
260 } // end namespace gdcm