1 /*=========================================================================
4 Module: $RCSfile: gdcmElementSet.cxx,v $
6 Date: $Date: 2005/01/23 10:12:33 $
7 Version: $Revision: 1.48 $
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.
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.
17 =========================================================================*/
19 #include "gdcmElementSet.h"
20 #include "gdcmDebug.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmSeqEntry.h"
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
31 * \brief Constructor from a given ElementSet
33 //BOZ depthLevel is not usefull anymore
34 ElementSet::ElementSet(int depthLevel)
41 * \brief Canonical destructor.
43 ElementSet::~ElementSet()
45 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
55 //-----------------------------------------------------------------------------
58 * \brief Prints the Header Entries (Dicom Elements)
60 * @param os ostream to write to
61 * @param indent Indentation string to be prepended during printing
63 void ElementSet::Print(std::ostream &os, std::string const & )
65 for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
67 DocEntry* entry = i->second;
69 entry->SetPrintLevel(PrintLevel);
72 if ( dynamic_cast<SeqEntry*>(entry) )
74 // Avoid the newline for a sequence:
81 //-----------------------------------------------------------------------------
84 * \brief Writes the Header Entries (Dicom Elements)
86 * @param fp ofstream to write to
87 * @param filetype filetype
89 void ElementSet::WriteContent(std::ofstream *fp, FileType filetype)
91 for (TagDocEntryHT::const_iterator i = TagHT.begin();
95 i->second->WriteContent(fp, filetype);
100 * \brief retrieves a Dicom Element using (group, element)
101 * @param group Group number of the searched Dicom Element
102 * @param elem Element number of the searched Dicom Element
105 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem)
107 TagKey key = DictEntry::TranslateToKey(group, elem);
108 if ( !TagHT.count(key))
112 return TagHT.find(key)->second;
116 * \brief Same as \ref Document::GetDocEntry except it only
117 * returns a result when the corresponding entry is of type
119 * @param group Group number of the searched Dicom Element
120 * @param elem Element number of the searched Dicom Element
121 * @return When present, the corresponding ValEntry.
123 ValEntry *ElementSet::GetValEntry(uint16_t group, uint16_t elem)
125 DocEntry *currentEntry = GetDocEntry(group, elem);
130 if ( ValEntry *entry = dynamic_cast<ValEntry*>(currentEntry) )
134 gdcmVerboseMacro( "Unfound ValEntry.");
140 * \brief Same as \ref Document::GetDocEntry except it only
141 * returns a result when the corresponding entry is of type
143 * @param group Group number of the searched Dicom Element
144 * @param elem Element number of the searched Dicom Element
145 * @return When present, the corresponding BinEntry.
147 BinEntry *ElementSet::GetBinEntry(uint16_t group, uint16_t elem)
149 DocEntry *currentEntry = GetDocEntry(group, elem);
154 if ( BinEntry *entry = dynamic_cast<BinEntry*>(currentEntry) )
158 gdcmVerboseMacro( "Unfound BinEntry.");
164 * \brief Same as \ref Document::GetDocEntry except it only
165 * returns a result when the corresponding entry is of type
167 * @param group Group number of the searched Dicom Element
168 * @param elem Element number of the searched Dicom Element
169 * @return When present, the corresponding SeqEntry.
171 SeqEntry *ElementSet::GetSeqEntry(uint16_t group, uint16_t elem)
173 DocEntry *currentEntry = GetDocEntry(group, elem);
178 if ( SeqEntry *entry = dynamic_cast<SeqEntry*>(currentEntry) )
182 gdcmVerboseMacro( "Unfound SeqEntry.");
188 //-----------------------------------------------------------------------------
192 * \brief Checks if a given Dicom Element exists within the H table
193 * @param group Group number of the searched Dicom Element
194 * @param elem Element number of the searched Dicom Element
195 * @return true is found
197 bool ElementSet::CheckIfEntryExist(uint16_t group, uint16_t elem )
199 const std::string &key = DictEntry::TranslateToKey(group, elem );
200 return TagHT.count(key) != 0;
204 * \brief Searches within Header Entries (Dicom Elements) parsed with
205 * the public and private dictionaries
206 * for the element value representation of a given tag.
207 * @param group Group number of the searched tag.
208 * @param elem Element number of the searched tag.
209 * @return Corresponding element value representation when it exists,
210 * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
212 std::string ElementSet::GetEntry(uint16_t group, uint16_t elem)
214 TagKey key = DictEntry::TranslateToKey(group, elem);
215 if ( !TagHT.count(key))
220 return ((ValEntry *)TagHT.find(key)->second)->GetValue();
224 //-----------------------------------------------------------------------------
228 * \brief add a new Dicom Element pointer to the H Table
229 * @param newEntry entry to add
231 bool ElementSet::AddEntry(DocEntry *newEntry)
233 const TagKey &key = newEntry->GetKey();
235 if( TagHT.count(key) == 1 )
237 gdcmVerboseMacro( "Key already present: " << key.c_str());
242 TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
248 * \brief Clear the hash table from given entry AND delete the entry.
249 * @param entryToRemove Entry to remove AND delete.
251 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
253 const TagKey &key = entryToRemove->GetKey();
254 if( TagHT.count(key) == 1 )
257 //gdcmVerboseMacro( "One element erased.");
258 delete entryToRemove;
262 gdcmVerboseMacro( "Key not present");
267 * \brief Clear the hash table from given entry BUT keep the entry.
268 * @param entryToRemove Entry to remove.
270 bool ElementSet::RemoveEntryNoDestroy(DocEntry *entryToRemove)
272 const TagKey &key = entryToRemove->GetKey();
273 if( TagHT.count(key) == 1 )
276 //gdcmVerboseMacro( "One element erased.");
280 gdcmVerboseMacro( "Key not present");
285 * \brief Get the first entry while visiting the DocEntrySet
286 * \return The first DocEntry if found, otherwhise NULL
288 DocEntry *ElementSet::GetFirstEntry()
290 ItTagHT = TagHT.begin();
291 if (ItTagHT != TagHT.end())
292 return ItTagHT->second;
297 * \brief Get the next entry while visiting the Hash table (TagHT)
298 * \note : meaningfull only if GetFirstEntry already called
299 * \return The next DocEntry if found, otherwhise NULL
301 DocEntry *ElementSet::GetNextEntry()
303 gdcmAssertMacro (ItTagHT != TagHT.end());
306 if (ItTagHT != TagHT.end())
307 return ItTagHT->second;
313 * \brief Get the larst entry while visiting the DocEntrySet
314 * \return The last DocEntry if found, otherwhise NULL
316 DocEntry *ElementSet::GetLastEntry()
318 ItTagHT = TagHT.end();
319 if ( ItTagHT != TagHT.begin() )
320 return ItTagHT->second;
325 * \brief Get the previous entry while visiting the Hash table (TagHT)
326 * \note : meaningfull only if GetFirstEntry already called
327 * \return The previous DocEntry if found, otherwhise NULL
329 DocEntry *ElementSet::GetPreviousEntry()
331 gdcmAssertMacro (ItTagHT != TagHT.begin());
334 if (ItTagHT != TagHT.begin())
335 return ItTagHT->second;
340 //-----------------------------------------------------------------------------
341 } // end namespace gdcm