]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
The following methods
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/20 11:37:37 $
7   Version:   $Revision: 1.47 $
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 from 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)
59   *          from the H Table
60   * @param os ostream to write to  
61   * @param indent Indentation string to be prepended during printing
62   */ 
63 void ElementSet::Print(std::ostream &os, std::string const & )
64 {
65    for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
66    {
67       DocEntry* entry = i->second;
68
69       entry->SetPrintLevel(PrintLevel);
70       entry->Print(os);   
71
72       if ( dynamic_cast<SeqEntry*>(entry) )
73       {
74          // Avoid the newline for a sequence:
75          continue;
76       }
77       os << std::endl;
78    }
79 }
80
81 //-----------------------------------------------------------------------------
82 // Public
83 /**
84   * \brief   Writes the Header Entries (Dicom Elements)
85   *          from the H Table
86   * @param fp ofstream to write to  
87   * @param filetype filetype
88   */ 
89 void ElementSet::WriteContent(std::ofstream *fp, FileType filetype)
90 {
91    for (TagDocEntryHT::const_iterator i = TagHT.begin(); 
92                                      i != TagHT.end(); 
93                                     ++i)
94    {
95       i->second->WriteContent(fp, filetype);
96    } 
97 }
98
99
100 /**
101  * \brief  retrieves a Dicom Element using (group, element)
102  * @param   group  Group number of the searched Dicom Element 
103  * @param   elem Element number of the searched Dicom Element 
104  * @return  
105  */
106 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem) 
107 {
108    TagKey key = DictEntry::TranslateToKey(group, elem);
109    if ( !TagHT.count(key))
110    {
111       return NULL;
112    }
113    return TagHT.find(key)->second;
114 }
115
116 /**
117  * \brief  Same as \ref Document::GetDocEntry except it only
118  *         returns a result when the corresponding entry is of type
119  *         ValEntry.
120  * @param   group  Group number of the searched Dicom Element 
121  * @param   elem Element number of the searched Dicom Element  
122  * @return When present, the corresponding ValEntry. 
123  */
124 ValEntry *ElementSet::GetValEntry(uint16_t group, uint16_t elem)
125 {
126    DocEntry *currentEntry = GetDocEntry(group, elem);
127    if ( !currentEntry )
128    {
129       return 0;
130    }
131    if ( ValEntry *entry = dynamic_cast<ValEntry*>(currentEntry) )
132    {
133       return entry;
134    }
135    gdcmVerboseMacro( "Unfound ValEntry.");
136
137    return 0;
138 }
139
140 /**
141  * \brief  Same as \ref Document::GetDocEntry except it only
142  *         returns a result when the corresponding entry is of type
143  *         BinEntry.
144  * @param   group  Group number of the searched Dicom Element 
145  * @param   elem Element number of the searched Dicom Element  
146  * @return When present, the corresponding BinEntry. 
147  */
148 BinEntry *ElementSet::GetBinEntry(uint16_t group, uint16_t elem)
149 {
150    DocEntry *currentEntry = GetDocEntry(group, elem);
151    if ( !currentEntry )
152    {
153       return 0;
154    }
155    if ( BinEntry *entry = dynamic_cast<BinEntry*>(currentEntry) )
156    {
157       return entry;
158    }
159    gdcmVerboseMacro( "Unfound BinEntry.");
160
161    return 0;
162 }
163
164 /**
165  * \brief  Same as \ref Document::GetDocEntry except it only
166  *         returns a result when the corresponding entry is of type
167  *         SeqEntry.
168  * @param   group  Group number of the searched Dicom Element 
169  * @param   elem Element number of the searched Dicom Element  
170  * @return When present, the corresponding SeqEntry. 
171  */
172 SeqEntry *ElementSet::GetSeqEntry(uint16_t group, uint16_t elem)
173 {
174    DocEntry *currentEntry = GetDocEntry(group, elem);
175    if ( !currentEntry )
176    {
177       return 0;
178    }
179    if ( SeqEntry *entry = dynamic_cast<SeqEntry*>(currentEntry) )
180    {
181       return entry;
182    }
183    gdcmVerboseMacro( "Unfound SeqEntry.");
184
185    return 0;
186 }
187
188
189 //-----------------------------------------------------------------------------
190 // Protected
191
192 //-----------------------------------------------------------------------------
193 // Private
194
195 /**
196  * \brief   add a new Dicom Element pointer to the H Table
197  * @param   newEntry entry to add
198  */
199 bool ElementSet::AddEntry(DocEntry *newEntry)
200 {
201    const TagKey &key = newEntry->GetKey();
202
203    if( TagHT.count(key) == 1 )
204    {
205       gdcmVerboseMacro( "Key already present: " << key.c_str());
206       return false;
207    }
208    else
209    {
210       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
211       return true;
212    }
213 }
214
215 /**
216  * \brief   Clear the hash table from given entry AND delete the entry.
217  * @param   entryToRemove Entry to remove AND delete.
218  */
219 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
220 {
221    const TagKey &key = entryToRemove->GetKey();
222    if( TagHT.count(key) == 1 )
223    {
224       TagHT.erase(key);
225       //gdcmVerboseMacro( "One element erased.");
226       delete entryToRemove;
227       return true;
228    }
229
230    gdcmVerboseMacro( "Key not present");
231    return false ;
232 }
233
234 /**
235  * \brief   Clear the hash table from given entry BUT keep the entry.
236  * @param   entryToRemove Entry to remove.
237  */
238 bool ElementSet::RemoveEntryNoDestroy(DocEntry *entryToRemove)
239 {
240    const TagKey &key = entryToRemove->GetKey();
241    if( TagHT.count(key) == 1 )
242    {
243       TagHT.erase(key);
244       //gdcmVerboseMacro( "One element erased.");
245       return true;
246    }
247
248    gdcmVerboseMacro( "Key not present");
249    return false ;
250 }
251
252 /**
253  * \brief   Get the first entry while visiting the DocEntrySet
254  * \return  The first DocEntry if found, otherwhise NULL
255  */
256 DocEntry *ElementSet::GetFirstEntry()
257 {
258    ItTagHT = TagHT.begin();
259    if (ItTagHT != TagHT.end())
260       return  ItTagHT->second;
261    return NULL;
262 }
263
264 /**
265  * \brief   Get the next entry while visiting the Hash table (TagHT)
266  * \note : meaningfull only if GetFirstEntry already called 
267  * \return  The next DocEntry if found, otherwhise NULL
268  */
269 DocEntry *ElementSet::GetNextEntry()
270 {
271    gdcmAssertMacro (ItTagHT != TagHT.end());
272
273    ++ItTagHT;
274    if (ItTagHT != TagHT.end())
275       return  ItTagHT->second;
276    return NULL;
277 }
278
279 //-----------------------------------------------------------------------------
280 } // end namespace gdcm