]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
6287e9aa522325a18510c310e401801b9ae41c3c
[gdcm.git] / src / gdcmDict.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDict.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/24 14:14:11 $
7   Version:   $Revision: 1.70 $
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          AddEntry(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  * @param indent Indentation string to be prepended during printing
95  */
96 void Dict::Print(std::ostream &os, std::string const & )
97 {
98    os << "Dict file name : " << Filename << std::endl;
99    std::ostringstream s;
100
101    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
102    {
103       s << "Entry : ";
104       s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
105       s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
106         << std::dec;
107       s << tag->second.GetVR() << ", ";
108       s << tag->second.GetVM() << ", ";
109       s << tag->second.GetName() << "."  << std::endl;
110    }
111    os << s.str();
112 }
113
114
115 //-----------------------------------------------------------------------------
116 // Public
117 /**
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  * \brief  adds a new Dicom Dictionary Entry 
129  * @param   newEntry entry to add 
130  * @return  false if Dicom Element already exists
131  */
132 bool Dict::AddEntry(DictEntry const &newEntry) 
133 {
134    const TagKey &key = newEntry.GetKey();
135
136    if(KeyHt.count(key) == 1)
137    {
138       gdcmVerboseMacro( "Already present" << key.c_str());
139       return false;
140    } 
141    else 
142    {
143       KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
144       return true;
145    }
146 }
147
148 /**
149  * \brief  replaces an already existing Dicom Element by a new one
150  * @param   newEntry new entry (overwrites any previous one with same tag)
151  * @return  false if Dicom Element doesn't exist
152  */
153 bool Dict::ReplaceEntry(DictEntry const &newEntry)
154 {
155    if ( RemoveEntry(newEntry.GetKey()) )
156    {
157        KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
158        return true;
159    } 
160    return false;
161 }
162
163 /**
164  * \brief  removes an already existing Dicom Dictionary Entry,
165  *         identified by its Tag
166  * @param   key (group|element)
167  * @return  false if Dicom Dictionary Entry doesn't exist
168  */
169 bool Dict::RemoveEntry (TagKey const &key) 
170 {
171    TagKeyHT::const_iterator it = KeyHt.find(key);
172    if(it != KeyHt.end()) 
173    {
174       KeyHt.erase(key);
175
176       return true;
177    } 
178    else 
179    {
180       gdcmVerboseMacro( "Unfound entry" << key.c_str());
181       return false;
182   }
183 }
184
185 /**
186  * \brief  removes an already existing Dicom Dictionary Entry, 
187  *          identified by its group,element number
188  * @param   group   Dicom group number of the Dicom Element
189  * @param   elem Dicom element number of the Dicom Element
190  * @return  false if Dicom Dictionary Entry doesn't exist
191  */
192 bool Dict::RemoveEntry (uint16_t group, uint16_t elem)
193 {
194    return RemoveEntry(DictEntry::TranslateToKey(group, elem));
195 }
196
197 /**
198  * \brief   Get the dictionary entry identified by a given tag (group,element)
199  * @param   group   group of the entry to be found
200  * @param   elem element of the entry to be found
201  * @return  the corresponding dictionary entry when existing, NULL otherwise
202  */
203 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
204 {
205    TagKey key = DictEntry::TranslateToKey(group, elem);
206    TagKeyHT::iterator it = KeyHt.find(key);
207    if ( it == KeyHt.end() )
208    {
209       return 0;
210    }
211    return &(it->second);
212 }
213
214 /**
215  * \brief   Get the first entry while visiting the Dict entries
216  * \return  The first DicEntry if found, otherwhise NULL
217  */
218 DictEntry *Dict::GetFirstEntry()
219 {
220    ItKeyHt = KeyHt.begin();
221    if( ItKeyHt != KeyHt.end() )
222       return &(ItKeyHt->second);
223    return NULL;
224 }
225
226 /**
227  * \brief   Get the next entry while visiting the Hash table (KeyHt)
228  * \note : meaningfull only if GetFirstEntry already called
229  * \return  The next DictEntry if found, otherwhise NULL
230  */
231 DictEntry *Dict::GetNextEntry()
232 {
233    gdcmAssertMacro (ItKeyHt != KeyHt.end());
234
235    {
236       ++ItKeyHt;
237       if (ItKeyHt != KeyHt.end())
238          return &(ItKeyHt->second);
239    }
240    return NULL;
241 }
242
243 //-----------------------------------------------------------------------------
244 // Protected
245
246 //-----------------------------------------------------------------------------
247 // Private
248
249 //-----------------------------------------------------------------------------
250
251 } // end namespace gdcm