]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
Add missing SQItem methods
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/19 08:55:09 $
7   Version:   $Revision: 1.54 $
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 "gdcmSQItem.h"
20 #include "gdcmSeqEntry.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmGlobal.h"
24 #include "gdcmDictSet.h"
25 #include "gdcmUtil.h"
26 #include "gdcmDebug.h"
27
28 #include <fstream>
29
30 namespace gdcm 
31 {
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 /**
35  * \brief   Constructor from a given SQItem
36  */
37 SQItem::SQItem(int depthLevel ) 
38           : DocEntrySet( )
39 {
40    SQDepthLevel = depthLevel;
41 }
42
43 /**
44  * \brief   Canonical destructor.
45  */
46 SQItem::~SQItem() 
47 {
48    for(ListDocEntry::iterator cc = DocEntries.begin();
49                              cc != DocEntries.end();
50                              ++cc)
51    {
52       delete *cc;
53    }
54    DocEntries.clear();
55 }
56
57 //-----------------------------------------------------------------------------
58 // Print
59 /*
60  * \brief   canonical Printer
61  * @param os     Stream to print to. 
62  * @param indent Indentation string to be prepended during printing.
63  */
64 void SQItem::Print(std::ostream &os, std::string const &)
65 {
66    std::ostringstream s;
67
68    if (SQDepthLevel > 0)
69    {
70       for (int i = 0; i < SQDepthLevel; ++i)
71       {
72          s << "   | " ;
73       }
74    }
75    os << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
76    for (ListDocEntry::iterator i  = DocEntries.begin();
77                                i != DocEntries.end();
78                              ++i)
79    {
80       DocEntry *Entry = *i;
81       bool PrintEndLine = true;
82
83       os << s.str();
84       Entry->SetPrintLevel(PrintLevel);
85       Entry->Print(os); 
86       if ( dynamic_cast<SeqEntry*>(Entry) )
87       {
88          PrintEndLine = false;
89       }
90       if (PrintEndLine)
91       {
92          os << std::endl;
93       }
94    } 
95 }
96
97 /*
98  * \brief   canonical Writer
99  */
100 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
101 {
102    int j;
103    uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
104    uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
105
106     //we force the writting of an 'Item' Start Element
107     // because we want to write the Item as a 'no Length' item
108    for(j=0;j<4;++j)
109    {
110       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
111    }
112      
113    for (ListDocEntry::iterator it = DocEntries.begin();  
114                               it != DocEntries.end();
115                              ++it)
116    {   
117       // we skip delimitors (start and end one) because 
118       // we force them as 'no length'
119       if ( (*it)->GetGroup() == 0xfffe )
120       {
121          continue;
122       }
123
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 )
128       {
129          break; // FIXME : continue; ?!?
130       }
131
132       (*it)->WriteContent(fp, filetype);
133    }
134       
135     //we force the writting of an 'Item Delimitation' item
136     // because we wrote the Item as a 'no Length' item
137    for(j=0;j<4;++j)
138    {
139       binary_write( *fp, itemt[j]);  // fffe e000 ffff ffff 
140    }
141  
142 }
143
144 //-----------------------------------------------------------------------------
145 // Public
146 /**
147  * \brief   adds any Entry (Dicom Element) to the Sequence Item
148  */
149 bool SQItem::AddEntry(DocEntry *entry)
150 {
151    DocEntries.push_back(entry);
152    //TODO : check if it worked
153    return true;
154 }   
155
156 /**
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
162  *          (NOT a shadow one)       
163  * @param   val string value to set
164  * @param   group Group number of the searched tag.
165  * @param   elem Element number of the searched tag.
166  * @return  true if element was found or created successfully
167  */
168
169 bool SQItem::SetEntry(std::string const &val, uint16_t group, 
170                       uint16_t elem)
171 {
172    for(ListDocEntry::iterator i = DocEntries.begin(); 
173                               i != DocEntries.end(); 
174                             ++i)
175    { 
176       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
177       {
178          continue;
179       }
180
181       if (  ( group  < (*i)->GetGroup() )
182           ||( group == (*i)->GetGroup() && elem < (*i)->GetElement()) )
183       {
184          // instead of ReplaceOrCreate 
185          // that is a method of Document :-( 
186          ValEntry* entry = 0;
187          TagKey key = DictEntry::TranslateToKey(group, elem);
188
189          // we assume a Public Dictionary *is* loaded
190          Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
191          // if the invoked (group,elem) doesn't exist inside the Dictionary
192          // we create a VirtualDictEntry
193          DictEntry *dictEntry = pubDict->GetDictEntry(group, elem);
194          if (dictEntry == NULL)
195          {
196             dictEntry = 
197                Global::GetDicts()->NewVirtualDictEntry(group, elem,
198                                                        "UN", GDCM_UNKNOWN, 
199                                                         GDCM_UNKNOWN);
200          } 
201          // we assume the constructor didn't fail
202          entry = new ValEntry(dictEntry);
203          if (entry)
204          {
205             entry->SetValue(val); 
206          }
207          DocEntries.insert(i,entry);
208
209          return true;
210       }   
211       if (group == (*i)->GetGroup() && elem == (*i)->GetElement() )
212       {
213          if ( ValEntry *entry = dynamic_cast<ValEntry*>(*i) )
214          {
215             entry->SetValue(val);
216          }
217          return true;    
218       }
219    }
220    return false;
221 }
222
223 /**
224  * \brief   Clear the hash table from given entry AND delete the entry.
225  * @param   entryToRemove Entry to remove AND delete.
226  * \warning Some problems when using under Windows... prefer the use of
227  *          Initialize / GetNext methods
228  * @return true if the entry was found and removed; false otherwise
229  */
230 bool SQItem::RemoveEntry( DocEntry* entryToRemove)
231 {
232    for(ListDocEntry::iterator it = DocEntries.begin();
233        it != DocEntries.end();
234        ++it)
235    {
236       if( *it == entryToRemove)
237       {
238          DocEntries.erase(it);
239          gdcmVerboseMacro( "One element erased.");
240          delete entryToRemove;
241          return true;
242       }
243    }
244    gdcmVerboseMacro( "Value not present.");
245    return false ;
246 }
247
248 /**
249  * \brief   Clear the hash table from given entry BUT keep the entry.
250  * @param   entryToRemove Entry to remove.
251  * @return true if the entry was found and removed; false otherwise
252  */
253 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
254 {
255    for(ListDocEntry::iterator it = DocEntries.begin();
256        it != DocEntries.end();
257        ++it)
258    {
259       if( *it == entryToRemove)
260       {
261          DocEntries.erase(it);
262          gdcmVerboseMacro( "One element erased.");
263          return true;
264       }
265    }
266                                                                                 
267    gdcmVerboseMacro( "Value not present.");
268    return false ;
269 }
270                                                                                 
271 /**
272  * \brief   Get the first entry while visiting the SQItem
273  * \return  The first DocEntry if found, otherwhise 0
274  */
275 DocEntry * SQItem::GetFirstEntry()
276 {
277    ItDocEntries = DocEntries.begin();
278    if (ItDocEntries != DocEntries.end())
279       return *ItDocEntries;
280    return 0;   
281 }
282                                                                                 
283 /**
284  * \brief   Get the next entry while visiting the chained list
285  * \return  The next DocEntry if found, otherwhise NULL
286  */
287 DocEntry *SQItem::GetNextEntry()
288 {
289    gdcmAssertMacro (ItDocEntries != DocEntries.end());
290    {
291       ++ItDocEntries;
292       if (ItDocEntries != DocEntries.end())
293          return  *ItDocEntries;
294       return NULL;
295    }
296 }
297
298 //-----------------------------------------------------------------------------
299 // Protected
300 /**
301  * \brief   Gets a Dicom Element inside a SQ Item Entry
302  * @param   group   Group number of the Entry
303  * @param   elem  Element number of the Entry
304  * @return Entry whose (group,elem) was passed. 0 if not found
305  */
306 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
307 {
308    for(ListDocEntry::iterator i = DocEntries.begin();
309                               i != DocEntries.end(); ++i)
310    {
311       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
312       {
313          return *i;
314       }
315    }
316    return 0;
317 }
318
319 /**
320  * \brief   Gets a Dicom Element inside a SQ Item Entry
321  * @param   group   Group number of the Entry
322  * @param   elem  Element number of the Entry
323  * @return Entry whose (group,elem) was passed. 0 if not found
324  */
325 ValEntry* SQItem::GetValEntry(uint16_t group, uint16_t elem)
326 {
327    DocEntry *d = GetDocEntry(group, elem);
328    if ( ValEntry *e = dynamic_cast<ValEntry*>(d) )
329       return e;
330    return 0;
331 }
332
333 /**
334  * \brief   Gets a Dicom Element inside a SQ Item Entry
335  * @param   group   Group number of the Entry
336  * @param   elem  Element number of the Entry
337  * @return Entry whose (group,elem) was passed. 0 if not found
338  */
339 BinEntry* SQItem::GetBinEntry(uint16_t group, uint16_t elem)
340 {
341    DocEntry *d = GetDocEntry(group, elem);
342    if ( BinEntry *e = dynamic_cast<BinEntry*>(d) )
343       return e;
344    return 0;
345 }
346
347 /**
348  * \brief   Gets a Dicom Element inside a SQ Item Entry
349  * @param   group   Group number of the Entry
350  * @param   elem  Element number of the Entry
351  * @return Entry whose (group,elem) was passed. 0 if not found
352  */
353 SeqEntry* SQItem::GetSeqEntry(uint16_t group, uint16_t elem)
354 {
355    DocEntry *d = GetDocEntry(group, elem);
356    if ( SeqEntry *e = dynamic_cast<SeqEntry*>(d) )
357       return e;
358    return 0;
359 }
360
361
362 /**
363  * \brief   Get the value of a Dicom Element inside a SQ Item Entry
364  * \note : meaningfull only if the required entry is NEITHER a SeqEntry 
365  *                                                   NOR a BinEntry
366  * @param   group   Group number of the Entry
367  * @param   elem  Element number of the Entry 
368  * @return  'string value' of the entry whose (group,elem) was passed.
369  *           GDCM_UNFOUND if not found
370  */ 
371
372 std::string SQItem::GetEntry(uint16_t group, uint16_t elem)
373 {
374    for(ListDocEntry::iterator i = DocEntries.begin();
375                               i != DocEntries.end(); ++i)
376    {
377       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem)
378       {
379          if (ValEntry *e = dynamic_cast<ValEntry*>(*i))
380             return e->GetValue();
381       }
382    }
383    return GDCM_UNFOUND;
384 }
385 //-----------------------------------------------------------------------------
386 // Private
387
388
389 //-----------------------------------------------------------------------------
390
391 } // end namespace gdcm