]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
Stage 3 of normalization :
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/24 16:10:52 $
7   Version:   $Revision: 1.49 $
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 "gdcmElementSet.h"
20 #include "gdcmDebug.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmSeqEntry.h"
24
25 namespace gdcm 
26 {
27
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
30 /**
31  * \brief   Constructor for a given ElementSet
32  */
33 //BOZ depthLevel is not usefull anymore
34 ElementSet::ElementSet(int depthLevel) 
35               : DocEntrySet()
36 {
37   (void)depthLevel;
38 }
39
40 /**
41  * \brief   Canonical destructor.
42  */
43 ElementSet::~ElementSet() 
44 {
45    for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
46    {
47       if ( cc->second )
48       {
49          delete cc->second;
50       }
51    }
52    TagHT.clear();
53 }
54
55 //-----------------------------------------------------------------------------
56 // Print
57 /**
58   * \brief   Prints the Header Entries (Dicom Elements) from the H Table
59   * @param os ostream to write to  
60   * @param indent Indentation string to be prepended during printing
61   */ 
62 void ElementSet::Print(std::ostream &os, std::string const & )
63 {
64    for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
65    {
66       DocEntry* entry = i->second;
67
68       entry->SetPrintLevel(PrintLevel);
69       entry->Print(os);   
70
71       if ( dynamic_cast<SeqEntry*>(entry) )
72       {
73          // Avoid the newline for a sequence:
74          continue;
75       }
76       os << std::endl;
77    }
78 }
79
80 //-----------------------------------------------------------------------------
81 // Public
82 /**
83   * \brief   Writes the Header Entries (Dicom Elements)
84   *          from the H Table
85   * @param fp ofstream to write to  
86   * @param filetype filetype
87   */ 
88 void ElementSet::WriteContent(std::ofstream *fp, FileType filetype)
89 {
90    for (TagDocEntryHT::const_iterator i = TagHT.begin(); 
91                                      i != TagHT.end(); 
92                                     ++i)
93    {
94       i->second->WriteContent(fp, filetype);
95    } 
96 }
97
98 /**
99  * \brief  retrieves a Dicom Element using (group, element)
100  * @param   group  Group number of the searched Dicom Element 
101  * @param   elem Element number of the searched Dicom Element 
102  * @return  
103  */
104 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem) 
105 {
106    TagKey key = DictEntry::TranslateToKey(group, elem);
107    if ( !TagHT.count(key))
108    {
109       return NULL;
110    }
111    return TagHT.find(key)->second;
112 }
113
114 /**
115  * \brief  Same as \ref Document::GetDocEntry except it only
116  *         returns a result when the corresponding entry is of type
117  *         ValEntry.
118  * @param   group  Group number of the searched Dicom Element 
119  * @param   elem Element number of the searched Dicom Element  
120  * @return When present, the corresponding ValEntry. 
121  */
122 ValEntry *ElementSet::GetValEntry(uint16_t group, uint16_t elem)
123 {
124    DocEntry *currentEntry = GetDocEntry(group, elem);
125    if ( !currentEntry )
126    {
127       return 0;
128    }
129    if ( ValEntry *entry = dynamic_cast<ValEntry*>(currentEntry) )
130    {
131       return entry;
132    }
133    gdcmVerboseMacro( "Unfound ValEntry.");
134
135    return 0;
136 }
137
138 /**
139  * \brief  Same as \ref Document::GetDocEntry except it only
140  *         returns a result when the corresponding entry is of type
141  *         BinEntry.
142  * @param   group  Group number of the searched Dicom Element 
143  * @param   elem Element number of the searched Dicom Element  
144  * @return When present, the corresponding BinEntry. 
145  */
146 BinEntry *ElementSet::GetBinEntry(uint16_t group, uint16_t elem)
147 {
148    DocEntry *currentEntry = GetDocEntry(group, elem);
149    if ( !currentEntry )
150    {
151       return 0;
152    }
153    if ( BinEntry *entry = dynamic_cast<BinEntry*>(currentEntry) )
154    {
155       return entry;
156    }
157    gdcmVerboseMacro( "Unfound BinEntry.");
158
159    return 0;
160 }
161
162 /**
163  * \brief  Same as \ref Document::GetDocEntry except it only
164  *         returns a result when the corresponding entry is of type
165  *         SeqEntry.
166  * @param   group  Group number of the searched Dicom Element 
167  * @param   elem Element number of the searched Dicom Element  
168  * @return When present, the corresponding SeqEntry. 
169  */
170 SeqEntry *ElementSet::GetSeqEntry(uint16_t group, uint16_t elem)
171 {
172    DocEntry *currentEntry = GetDocEntry(group, elem);
173    if ( !currentEntry )
174    {
175       return 0;
176    }
177    if ( SeqEntry *entry = dynamic_cast<SeqEntry*>(currentEntry) )
178    {
179       return entry;
180    }
181    gdcmVerboseMacro( "Unfound SeqEntry.");
182
183    return 0;
184 }
185
186
187 //-----------------------------------------------------------------------------
188 // Protected
189
190 /**
191  * \brief   Checks if a given Dicom Element exists within the H table
192  * @param   group   Group number of the searched Dicom Element 
193  * @param   elem  Element number of the searched Dicom Element 
194  * @return true is found
195  */
196 bool ElementSet::CheckIfEntryExist(uint16_t group, uint16_t elem )
197 {
198    const std::string &key = DictEntry::TranslateToKey(group, elem );
199    return TagHT.count(key) != 0;
200 }
201
202 /**
203  * \brief   Get the (std::string representable) value of the Dicom entry
204  * @param   group  Group number of the searched tag.
205  * @param   elem Element number of the searched tag.
206  * @return  Corresponding element value when it exists,
207  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
208  */
209 std::string ElementSet::GetEntryValue(uint16_t group, uint16_t elem)
210 {
211    TagKey key = DictEntry::TranslateToKey(group, elem);
212    if ( !TagHT.count(key))
213    {
214       return GDCM_UNFOUND;
215    }
216
217    return ((ValEntry *)TagHT.find(key)->second)->GetValue();
218 }
219
220
221 //-----------------------------------------------------------------------------
222 // Private
223
224 /**
225  * \brief   add a new Dicom Element pointer to the H Table
226  * @param   newEntry entry to add
227  */
228 bool ElementSet::AddEntry(DocEntry *newEntry)
229 {
230    const TagKey &key = newEntry->GetKey();
231
232    if( TagHT.count(key) == 1 )
233    {
234       gdcmVerboseMacro( "Key already present: " << key.c_str());
235       return false;
236    }
237    else
238    {
239       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
240       return true;
241    }
242 }
243
244 /**
245  * \brief   Clear the hash table from given entry AND delete the entry.
246  * @param   entryToRemove Entry to remove AND delete.
247  */
248 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
249 {
250    const TagKey &key = entryToRemove->GetKey();
251    if( TagHT.count(key) == 1 )
252    {
253       TagHT.erase(key);
254       //gdcmVerboseMacro( "One element erased.");
255       delete entryToRemove;
256       return true;
257    }
258
259    gdcmVerboseMacro( "Key not present");
260    return false ;
261 }
262
263 /**
264  * \brief   Clear the hash table from given entry BUT keep the entry.
265  * @param   entryToRemove Entry to remove.
266  */
267 bool ElementSet::RemoveEntryNoDestroy(DocEntry *entryToRemove)
268 {
269    const TagKey &key = entryToRemove->GetKey();
270    if( TagHT.count(key) == 1 )
271    {
272       TagHT.erase(key);
273       //gdcmVerboseMacro( "One element erased.");
274       return true;
275    }
276
277    gdcmVerboseMacro( "Key not present");
278    return false ;
279 }
280
281 /**
282  * \brief   Get the first entry while visiting the DocEntrySet
283  * \return  The first DocEntry if found, otherwhise NULL
284  */
285 DocEntry *ElementSet::GetFirstEntry()
286 {
287    ItTagHT = TagHT.begin();
288    if (ItTagHT != TagHT.end())
289       return  ItTagHT->second;
290    return NULL;
291 }
292
293 /**
294  * \brief   Get the next entry while visiting the Hash table (TagHT)
295  * \note : meaningfull only if GetFirstEntry already called 
296  * \return  The next DocEntry if found, otherwhise NULL
297  */
298 DocEntry *ElementSet::GetNextEntry()
299 {
300    gdcmAssertMacro (ItTagHT != TagHT.end());
301
302    ++ItTagHT;
303    if (ItTagHT != TagHT.end())
304       return  ItTagHT->second;
305    return NULL;
306 }
307
308
309 /**
310  * \brief   Get the larst entry while visiting the DocEntrySet
311  * \return  The last DocEntry if found, otherwhise NULL
312  */
313 DocEntry *ElementSet::GetLastEntry()
314 {
315    ItTagHT = TagHT.end();
316    if ( ItTagHT != TagHT.begin() )
317       return  ItTagHT->second;
318    return NULL;
319 }
320
321 /**
322  * \brief   Get the previous entry while visiting the Hash table (TagHT)
323  * \note : meaningfull only if GetFirstEntry already called 
324  * \return  The previous DocEntry if found, otherwhise NULL
325  */
326 DocEntry *ElementSet::GetPreviousEntry()
327 {
328    gdcmAssertMacro (ItTagHT != TagHT.begin());
329
330    --ItTagHT;
331    if (ItTagHT != TagHT.begin())
332       return  ItTagHT->second;
333    return NULL;
334 }
335
336
337 //-----------------------------------------------------------------------------
338 } // end namespace gdcm