1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2005/01/18 08:01:42 $
7 Version: $Revision: 1.53 $
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 "gdcmSQItem.h"
20 #include "gdcmSeqEntry.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmGlobal.h"
24 #include "gdcmDictSet.h"
26 #include "gdcmDebug.h"
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
35 * \brief Constructor from a given SQItem
37 SQItem::SQItem(int depthLevel )
40 SQDepthLevel = depthLevel;
44 * \brief Canonical destructor.
48 for(ListDocEntry::iterator cc = DocEntries.begin();
49 cc != DocEntries.end();
57 //-----------------------------------------------------------------------------
60 * \brief canonical Printer
62 void SQItem::Print(std::ostream &os, std::string const &)
68 for (int i = 0; i < SQDepthLevel; ++i)
73 os << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
74 for (ListDocEntry::iterator i = DocEntries.begin();
75 i != DocEntries.end();
79 bool PrintEndLine = true;
82 Entry->SetPrintLevel(PrintLevel);
84 if ( /* SeqEntry *seqEntry =*/ dynamic_cast<SeqEntry*>(Entry) )
86 //(void)seqEntry; //not used
97 * \brief canonical Writer
99 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
102 uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
103 uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
105 //we force the writting of an 'Item' Start Element
106 // because we want to write the Item as a 'no Length' item
109 binary_write( *fp, item[j]); // fffe e000 ffff ffff
112 for (ListDocEntry::iterator it = DocEntries.begin();
113 it != DocEntries.end();
116 // we skip delimitors (start and end one) because
117 // we force them as 'no length'
118 if ( (*it)->GetGroup() == 0xfffe )
123 // Fix in order to make some MR PHILIPS images e-film readable
124 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
125 // we just *always* ignore spurious fffe|0000 tag !
126 if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
128 break; // FIXME : continue; ?!?
131 (*it)->WriteContent(fp, filetype);
134 //we force the writting of an 'Item Delimitation' item
135 // because we wrote the Item as a 'no Length' item
138 binary_write( *fp, itemt[j]); // fffe e000 ffff ffff
143 //-----------------------------------------------------------------------------
146 * \brief adds any Entry (Dicom Element) to the Sequence Item
148 bool SQItem::AddEntry(DocEntry *entry)
150 DocEntries.push_back(entry);
151 //TODO : check if it worked
156 * \brief Sets Entry (Dicom Element) value of an element,
157 * specified by it's tag (Group, Number)
158 * and the length, too ... inside a SQ Item
159 * If the Element is not found, it's just created !
160 * \warning we suppose, right now, the element belongs to a Public Group
162 * @param val string value to set
163 * @param group Group number of the searched tag.
164 * @param elem Element number of the searched tag.
165 * @return true if element was found or created successfully
168 bool SQItem::SetEntry(std::string const &val, uint16_t group,
171 for(ListDocEntry::iterator i = DocEntries.begin();
172 i != DocEntries.end();
175 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 )
180 if ( ( group < (*i)->GetGroup() )
181 ||( group == (*i)->GetGroup() && elem < (*i)->GetElement()) )
183 // instead of ReplaceOrCreate
184 // that is a method of Document :-(
186 TagKey key = DictEntry::TranslateToKey(group, elem);
188 // we assume a Public Dictionary *is* loaded
189 Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
190 // if the invoked (group,elem) doesn't exist inside the Dictionary
191 // we create a VirtualDictEntry
192 DictEntry *dictEntry = pubDict->GetDictEntry(group, elem);
193 if (dictEntry == NULL)
196 Global::GetDicts()->NewVirtualDictEntry(group, elem,
200 // we assume the constructor didn't fail
201 entry = new ValEntry(dictEntry);
204 entry->SetValue(val);
206 DocEntries.insert(i,entry);
210 if (group == (*i)->GetGroup() && elem == (*i)->GetElement() )
212 if ( ValEntry* entry = dynamic_cast<ValEntry*>(*i) )
214 entry->SetValue(val);
223 * \brief Clear the hash table from given entry AND delete the entry.
224 * @param entryToRemove Entry to remove AND delete.
225 * \warning Some problems when using under Windows... prefer the use of
226 * Initialize / GetNext methods
228 bool SQItem::RemoveEntry( DocEntry* entryToRemove)
230 for(ListDocEntry::iterator it = DocEntries.begin();
231 it != DocEntries.end();
234 if( *it == entryToRemove)
236 DocEntries.erase(it);
237 gdcmVerboseMacro( "One element erased.");
238 delete entryToRemove;
242 gdcmVerboseMacro( "Value not present.");
247 * \brief Clear the hash table from given entry BUT keep the entry.
248 * @param entryToRemove Entry to remove.
250 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
252 for(ListDocEntry::iterator it = DocEntries.begin();
253 it != DocEntries.end();
256 if( *it == entryToRemove)
258 DocEntries.erase(it);
259 gdcmVerboseMacro( "One element erased.");
264 gdcmVerboseMacro( "Value not present.");
269 * \brief Initialise the visit of the chained list
271 void SQItem::Initialize()
273 ItDocEntries = DocEntries.begin();
277 * \brief Get the next entry while visiting the chained list
278 * \return The next DocEntry if found, otherwhise NULL
280 DocEntry *SQItem::GetNextEntry()
282 if (ItDocEntries != DocEntries.end())
284 DocEntry *tmp = *ItDocEntries;
295 //-----------------------------------------------------------------------------
298 * \brief Gets a Dicom Element inside a SQ Item Entry
299 * @param group Group number of the Entry
300 * @param elem Element number of the Entry
303 DocEntry* SQItem::GetDocEntry(uint16_t group, uint16_t elem)
305 for(ListDocEntry::iterator i = DocEntries.begin();
306 i != DocEntries.end(); ++i)
308 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
317 * \brief Get the value of a Dicom Element inside a SQ Item Entry
318 * @param group Group number of the Entry
319 * @param elem Element number of the Entry
323 std::string SQItem::GetEntry(uint16_t group, uint16_t elem)
325 for(ListDocEntry::iterator i = DocEntries.begin();
326 i != DocEntries.end(); ++i)
328 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem)
330 return ((ValEntry *)(*i))->GetValue(); //FIXME
335 //-----------------------------------------------------------------------------
339 //-----------------------------------------------------------------------------
341 } // end namespace gdcm