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