1 /*=========================================================================
4 Module: $RCSfile: gdcmElementSet.cxx,v $
6 Date: $Date: 2005/01/24 16:10:52 $
7 Version: $Revision: 1.49 $
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 for 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) from the H Table
59 * @param os ostream to write to
60 * @param indent Indentation string to be prepended during printing
62 void ElementSet::Print(std::ostream &os, std::string const & )
64 for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
66 DocEntry* entry = i->second;
68 entry->SetPrintLevel(PrintLevel);
71 if ( dynamic_cast<SeqEntry*>(entry) )
73 // Avoid the newline for a sequence:
80 //-----------------------------------------------------------------------------
83 * \brief Writes the Header Entries (Dicom Elements)
85 * @param fp ofstream to write to
86 * @param filetype filetype
88 void ElementSet::WriteContent(std::ofstream *fp, FileType filetype)
90 for (TagDocEntryHT::const_iterator i = TagHT.begin();
94 i->second->WriteContent(fp, filetype);
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
104 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem)
106 TagKey key = DictEntry::TranslateToKey(group, elem);
107 if ( !TagHT.count(key))
111 return TagHT.find(key)->second;
115 * \brief Same as \ref Document::GetDocEntry except it only
116 * returns a result when the corresponding entry is of type
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.
122 ValEntry *ElementSet::GetValEntry(uint16_t group, uint16_t elem)
124 DocEntry *currentEntry = GetDocEntry(group, elem);
129 if ( ValEntry *entry = dynamic_cast<ValEntry*>(currentEntry) )
133 gdcmVerboseMacro( "Unfound ValEntry.");
139 * \brief Same as \ref Document::GetDocEntry except it only
140 * returns a result when the corresponding entry is of type
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.
146 BinEntry *ElementSet::GetBinEntry(uint16_t group, uint16_t elem)
148 DocEntry *currentEntry = GetDocEntry(group, elem);
153 if ( BinEntry *entry = dynamic_cast<BinEntry*>(currentEntry) )
157 gdcmVerboseMacro( "Unfound BinEntry.");
163 * \brief Same as \ref Document::GetDocEntry except it only
164 * returns a result when the corresponding entry is of type
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.
170 SeqEntry *ElementSet::GetSeqEntry(uint16_t group, uint16_t elem)
172 DocEntry *currentEntry = GetDocEntry(group, elem);
177 if ( SeqEntry *entry = dynamic_cast<SeqEntry*>(currentEntry) )
181 gdcmVerboseMacro( "Unfound SeqEntry.");
187 //-----------------------------------------------------------------------------
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
196 bool ElementSet::CheckIfEntryExist(uint16_t group, uint16_t elem )
198 const std::string &key = DictEntry::TranslateToKey(group, elem );
199 return TagHT.count(key) != 0;
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.
209 std::string ElementSet::GetEntryValue(uint16_t group, uint16_t elem)
211 TagKey key = DictEntry::TranslateToKey(group, elem);
212 if ( !TagHT.count(key))
217 return ((ValEntry *)TagHT.find(key)->second)->GetValue();
221 //-----------------------------------------------------------------------------
225 * \brief add a new Dicom Element pointer to the H Table
226 * @param newEntry entry to add
228 bool ElementSet::AddEntry(DocEntry *newEntry)
230 const TagKey &key = newEntry->GetKey();
232 if( TagHT.count(key) == 1 )
234 gdcmVerboseMacro( "Key already present: " << key.c_str());
239 TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
245 * \brief Clear the hash table from given entry AND delete the entry.
246 * @param entryToRemove Entry to remove AND delete.
248 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
250 const TagKey &key = entryToRemove->GetKey();
251 if( TagHT.count(key) == 1 )
254 //gdcmVerboseMacro( "One element erased.");
255 delete entryToRemove;
259 gdcmVerboseMacro( "Key not present");
264 * \brief Clear the hash table from given entry BUT keep the entry.
265 * @param entryToRemove Entry to remove.
267 bool ElementSet::RemoveEntryNoDestroy(DocEntry *entryToRemove)
269 const TagKey &key = entryToRemove->GetKey();
270 if( TagHT.count(key) == 1 )
273 //gdcmVerboseMacro( "One element erased.");
277 gdcmVerboseMacro( "Key not present");
282 * \brief Get the first entry while visiting the DocEntrySet
283 * \return The first DocEntry if found, otherwhise NULL
285 DocEntry *ElementSet::GetFirstEntry()
287 ItTagHT = TagHT.begin();
288 if (ItTagHT != TagHT.end())
289 return ItTagHT->second;
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
298 DocEntry *ElementSet::GetNextEntry()
300 gdcmAssertMacro (ItTagHT != TagHT.end());
303 if (ItTagHT != TagHT.end())
304 return ItTagHT->second;
310 * \brief Get the larst entry while visiting the DocEntrySet
311 * \return The last DocEntry if found, otherwhise NULL
313 DocEntry *ElementSet::GetLastEntry()
315 ItTagHT = TagHT.end();
316 if ( ItTagHT != TagHT.begin() )
317 return ItTagHT->second;
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
326 DocEntry *ElementSet::GetPreviousEntry()
328 gdcmAssertMacro (ItTagHT != TagHT.begin());
331 if (ItTagHT != TagHT.begin())
332 return ItTagHT->second;
337 //-----------------------------------------------------------------------------
338 } // end namespace gdcm