]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
BUG: Fix previously instroduce cmake bug, now should compile fine even without VTK...
[gdcm.git] / src / gdcmDict.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDict.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/05/11 15:01:47 $
7   Version:   $Revision: 1.75 $
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   group   group of the entry to be found
177  * @param   elem element of the entry to be found
178  * @return  the corresponding dictionary entry when existing, NULL otherwise
179  */
180 DictEntry *Dict::GetEntry(TagKey const &key)
181 {
182    TagKeyHT::iterator it = KeyHt.find(key);
183    if ( it == KeyHt.end() )
184    {
185       return 0;
186    }
187    return &(it->second);
188 }
189
190 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
191 {
192    TagKey key = DictEntry::TranslateToKey(group, elem);
193    TagKeyHT::iterator it = KeyHt.find(key);
194    if ( it == KeyHt.end() )
195    {
196       return 0;
197    }
198    return &(it->second);
199 }
200
201 /**
202  * \brief   Get the first entry while visiting the Dict entries
203  * \return  The first DicEntry if found, otherwhise NULL
204  */
205 DictEntry *Dict::GetFirstEntry()
206 {
207    ItKeyHt = KeyHt.begin();
208    if( ItKeyHt != KeyHt.end() )
209       return &(ItKeyHt->second);
210    return NULL;
211 }
212
213 /**
214  * \brief   Get the next entry while visiting the Hash table (KeyHt)
215  * \note : meaningfull only if GetFirstEntry already called
216  * \return  The next DictEntry if found, otherwhise NULL
217  */
218 DictEntry *Dict::GetNextEntry()
219 {
220    gdcmAssertMacro (ItKeyHt != KeyHt.end());
221
222    ++ItKeyHt;
223    if (ItKeyHt != KeyHt.end())
224       return &(ItKeyHt->second);
225    return NULL;
226 }
227
228 //-----------------------------------------------------------------------------
229 // Protected
230
231 //-----------------------------------------------------------------------------
232 // Private
233
234 //-----------------------------------------------------------------------------
235 // Print
236 /**
237  * \brief   Print all the dictionary entries contained in this dictionary.
238  *          Entries will be sorted by tag i.e. the couple (group, element).
239  * @param   os The output stream to be written to.
240  * @param indent Indentation string to be prepended during printing
241  */
242 void Dict::Print(std::ostream &os, std::string const & )
243 {
244    os << "Dict file name : " << Filename << std::endl;
245    std::ostringstream s;
246
247    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
248    {
249       s << "Entry : ";
250       s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
251       s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
252         << std::dec;
253       s << tag->second.GetVR() << ", ";
254       s << tag->second.GetVM() << ", ";
255       s << tag->second.GetName() << "."  << std::endl;
256    }
257    os << s.str();
258 }
259
260 //-----------------------------------------------------------------------------
261 } // end namespace gdcm