1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2004/12/03 17:13:18 $
7 Version: $Revision: 1.39 $
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"
25 #include "gdcmDebug.h"
30 //-----------------------------------------------------------------------------
31 // Constructor / Destructor
34 * \brief Constructor from a given SQItem
36 SQItem::SQItem(int depthLevel )
39 SQDepthLevel = depthLevel;
43 * \brief Canonical destructor.
47 for(ListDocEntry::iterator cc = DocEntries.begin();
48 cc != DocEntries.end();
56 //-----------------------------------------------------------------------------
59 * \brief canonical Printer
61 void SQItem::Print(std::ostream& os)
68 for (int i = 0; i < SQDepthLevel; ++i)
73 //std::cout << 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(2);
84 if ( SeqEntry* seqEntry = dynamic_cast<SeqEntry*>(Entry) )
86 (void)seqEntry; //not used
98 * \brief canonical Writer
100 void SQItem::WriteContent(std::ofstream* fp, FileType filetype)
103 uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
104 uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
106 //we force the writting of an 'Item' Start Element
107 // because we want to write the Item as a 'no Length' item
110 binary_write( *fp, item[j]); // fffe e000 ffff ffff
113 for (ListDocEntry::iterator it = DocEntries.begin();
114 it != DocEntries.end();
117 // we skip delimitors (start and end one) because
118 // we force them as 'no length'
119 if ( (*it)->GetGroup() == 0xfffe )
124 // Fix in order to make some MR PHILIPS images e-film readable
125 // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
126 // we just *always* ignore spurious fffe|0000 tag !
127 if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
129 break; // FIXME : continue; ?!?
132 (*it)->WriteContent(fp, filetype);
135 //we force the writting of an 'Item Delimitation' item
136 // because we wrote the Item as a 'no Length' item
139 binary_write( *fp, itemt[j]); // fffe e000 ffff ffff
144 //-----------------------------------------------------------------------------
147 * \brief adds any Entry (Dicom Element) to the Sequence Item
149 bool SQItem::AddEntry(DocEntry* entry)
151 DocEntries.push_back(entry);
152 //TODO : check if it worked
157 * \brief Sets Entry (Dicom Element) value of an element,
158 * specified by it's tag (Group, Number)
159 * and the length, too ... inside a SQ Item
160 * If the Element is not found, it's just created !
161 * \warning we suppose, right now, the element belongs to a Public Group
163 * @param val string value to set
164 * @param group Group number of the searched tag.
165 * @param element Element number of the searched tag.
166 * @return true if element was found or created successfully
169 bool SQItem::SetEntryByNumber(std::string const & val, uint16_t group,
172 for(ListDocEntry::iterator i = DocEntries.begin();
173 i != DocEntries.end();
176 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 )
181 if ( ( group < (*i)->GetGroup() )
182 ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
184 // instead of ReplaceOrCreateByNumber
185 // that is a method of Document :-(
187 TagKey key = DictEntry::TranslateToKey(group, element);
189 if ( ! PtagHT->count(key))
191 // we assume a Public Dictionnary *is* loaded
192 Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
193 // if the invoked (group,elem) doesn't exist inside the Dictionary
194 // we create a VirtualDictEntry
195 DictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, element);
196 if (dictEntry == NULL)
199 Global::GetDicts()->NewVirtualDictEntry(group, element,
202 // we assume the constructor didn't fail
203 entry = new ValEntry(dictEntry);
206 /// better we don't assume too much !
207 /// SQItem is now used to describe any DICOMDIR related object
211 DocEntry* foundEntry = PtagHT->find(key)->second;
212 entry = dynamic_cast<ValEntry*>(foundEntry);
215 dbg.Verbose(0, "SQItem::SetEntryByNumber: docEntries"
216 " contains non ValEntry occurences");
221 entry->SetValue(val);
223 entry->SetLength(val.length());
224 DocEntries.insert(i,entry);
228 if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
230 if ( ValEntry* entry = dynamic_cast<ValEntry*>(*i) )
232 entry->SetValue(val);
234 (*i)->SetLength(val.length());
242 * \brief Clear the hash table from given entry AND delete the entry.
243 * @param entryToRemove Entry to remove AND delete.
244 * \warning Some problems when using under Windows... prefer the use of
245 * Initialize / GetNext methods
247 bool SQItem::RemoveEntry( DocEntry* entryToRemove)
249 for(ListDocEntry::iterator it = DocEntries.begin();
250 it != DocEntries.end();
253 if( *it == entryToRemove)
255 DocEntries.erase(it);
256 dbg.Verbose(0, "SQItem::RemoveEntry: one element erased.");
257 delete entryToRemove;
262 dbg.Verbose(0, "SQItem::RemoveEntry: value not present ");
267 * \brief Clear the hash table from given entry BUT keep the entry.
268 * @param entryToRemove Entry to remove.
270 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
272 for(ListDocEntry::iterator it = DocEntries.begin();
273 it != DocEntries.end();
276 if( *it == entryToRemove)
278 DocEntries.erase(it);
279 dbg.Verbose(0, "SQItem::RemoveEntry: one element erased.");
284 dbg.Verbose(0, "SQItem::RemoveEntry: value not present ");
289 * \brief Initialise the visit of the chained list
291 void SQItem::Initialize()
293 ItDocEntries = DocEntries.begin();
297 * \brief Get the next entry whil visiting the chained list
298 * \return The next DocEntry if found, otherwhise NULL
300 DocEntry *SQItem::GetNextEntry()
302 if (ItDocEntries != DocEntries.end())
304 DocEntry *tmp = *ItDocEntries;
315 //-----------------------------------------------------------------------------
318 * \brief Gets a Dicom Element inside a SQ Item Entry, by number
321 DocEntry* SQItem::GetDocEntryByNumber(uint16_t group, uint16_t element)
323 for(ListDocEntry::iterator i = DocEntries.begin();
324 i != DocEntries.end(); ++i)
326 if ( (*i)->GetGroup() == group && (*i)->GetElement() == element )
335 * \brief Get the value of a Dicom Element inside a SQ Item Entry, by number
339 std::string SQItem::GetEntryByNumber(uint16_t group, uint16_t element)
341 for(ListDocEntry::iterator i = DocEntries.begin();
342 i != DocEntries.end(); ++i)
344 if ( (*i)->GetGroup() == group && (*i)->GetElement() == element)
346 return ((ValEntry *)(*i))->GetValue(); //FIXME
351 //-----------------------------------------------------------------------------
355 //-----------------------------------------------------------------------------
357 } // end namespace gdcm