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