]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
* src/*.cxx : first parss to normalize file organisation
[gdcm.git] / src / gdcmDict.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDict.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/01 10:29:55 $
7   Version:   $Revision: 1.71 $
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   Remove all Dicom Dictionary Entries
94  */
95 void Dict::ClearEntry()
96 {
97    // we assume all the pointed DictEntries are already cleaned-up
98    // when we clean KeyHt.
99    KeyHt.clear();
100 }
101
102 /**
103  * \brief  adds a new Dicom Dictionary Entry 
104  * @param   newEntry entry to add 
105  * @return  false if Dicom Element already exists
106  */
107 bool Dict::AddEntry(DictEntry const &newEntry) 
108 {
109    const TagKey &key = newEntry.GetKey();
110
111    if(KeyHt.count(key) == 1)
112    {
113       gdcmVerboseMacro( "Already present" << key.c_str());
114       return false;
115    } 
116    else 
117    {
118       KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
119       return true;
120    }
121 }
122
123 /**
124  * \brief  replaces an already existing Dicom Element by a new one
125  * @param   newEntry new entry (overwrites any previous one with same tag)
126  * @return  false if Dicom Element doesn't exist
127  */
128 bool Dict::ReplaceEntry(DictEntry const &newEntry)
129 {
130    if ( RemoveEntry(newEntry.GetKey()) )
131    {
132        KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
133        return true;
134    } 
135    return false;
136 }
137
138 /**
139  * \brief  removes an already existing Dicom Dictionary Entry,
140  *         identified by its Tag
141  * @param   key (group|element)
142  * @return  false if Dicom Dictionary Entry doesn't exist
143  */
144 bool Dict::RemoveEntry (TagKey const &key) 
145 {
146    TagKeyHT::const_iterator it = KeyHt.find(key);
147    if(it != KeyHt.end()) 
148    {
149       KeyHt.erase(key);
150
151       return true;
152    } 
153    else 
154    {
155       gdcmVerboseMacro( "Unfound entry" << key.c_str());
156       return false;
157   }
158 }
159
160 /**
161  * \brief  removes an already existing Dicom Dictionary Entry, 
162  *          identified by its group,element number
163  * @param   group   Dicom group number of the Dicom Element
164  * @param   elem Dicom element number of the Dicom Element
165  * @return  false if Dicom Dictionary Entry doesn't exist
166  */
167 bool Dict::RemoveEntry (uint16_t group, uint16_t elem)
168 {
169    return RemoveEntry(DictEntry::TranslateToKey(group, elem));
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    {
211       ++ItKeyHt;
212       if (ItKeyHt != KeyHt.end())
213          return &(ItKeyHt->second);
214    }
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