]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
600ce145316b8d10577cb98e740abf94e2aee9ea
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/24 14:14:11 $
7   Version:   $Revision: 1.56 $
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->GetEntry(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 std::list 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: " << entryToRemove->GetKey() );
240          delete entryToRemove;
241          return true;
242       }
243    }
244    gdcmVerboseMacro( "Entry not found: " << entryToRemove->GetKey() );
245    return false ;
246 }
247
248 /**
249  * \brief   Clear the std::list 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, no destroyed: "
263                             << entryToRemove->GetKey() );
264          return true;
265       }
266    }
267                                                                                 
268    gdcmVerboseMacro( "Entry not found:" << entryToRemove->GetKey() );
269    return false ;
270 }
271                                                                                 
272 /**
273  * \brief   Get the first entry while visiting the SQItem
274  * \return  The first DocEntry if found, otherwhise 0
275  */
276 DocEntry * SQItem::GetFirstEntry()
277 {
278    ItDocEntries = DocEntries.begin();
279    if (ItDocEntries != DocEntries.end())
280       return *ItDocEntries;
281    return 0;   
282 }
283                                                                                 
284 /**
285  * \brief   Get the next entry while visiting the chained list
286  * \return  The next DocEntry if found, otherwhise NULL
287  */
288 DocEntry *SQItem::GetNextEntry()
289 {
290   // gdcmAssertMacro (ItDocEntries != DocEntries.end());
291    {
292       ++ItDocEntries;
293       if (ItDocEntries != DocEntries.end())
294          return  *ItDocEntries;
295       return NULL;
296    }
297 }
298
299 //-----------------------------------------------------------------------------
300 // Protected
301 /**
302  * \brief   Gets a Dicom Element inside a SQ Item Entry
303  * @param   group   Group number of the Entry
304  * @param   elem  Element number of the Entry
305  * @return Entry whose (group,elem) was passed. 0 if not found
306  */
307 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
308 {
309    for(ListDocEntry::iterator i = DocEntries.begin();
310                               i != DocEntries.end(); ++i)
311    {
312       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
313       {
314          return *i;
315       }
316    }
317    return 0;
318 }
319
320 /**
321  * \brief   Gets a Dicom Element inside a SQ Item Entry
322  * @param   group   Group number of the Entry
323  * @param   elem  Element number of the Entry
324  * @return Entry whose (group,elem) was passed. 0 if not found
325  */
326 ValEntry* SQItem::GetValEntry(uint16_t group, uint16_t elem)
327 {
328    DocEntry *d = GetDocEntry(group, elem);
329    if ( ValEntry *e = dynamic_cast<ValEntry*>(d) )
330       return e;
331    return 0;
332 }
333
334 /**
335  * \brief   Gets a Dicom Element inside a SQ Item Entry
336  * @param   group   Group number of the Entry
337  * @param   elem  Element number of the Entry
338  * @return Entry whose (group,elem) was passed. 0 if not found
339  */
340 BinEntry* SQItem::GetBinEntry(uint16_t group, uint16_t elem)
341 {
342    DocEntry *d = GetDocEntry(group, elem);
343    if ( BinEntry *e = dynamic_cast<BinEntry*>(d) )
344       return e;
345    return 0;
346 }
347
348 /**
349  * \brief   Gets a Dicom Element inside a SQ Item Entry
350  * @param   group   Group number of the Entry
351  * @param   elem  Element number of the Entry
352  * @return Entry whose (group,elem) was passed. 0 if not found
353  */
354 SeqEntry* SQItem::GetSeqEntry(uint16_t group, uint16_t elem)
355 {
356    DocEntry *d = GetDocEntry(group, elem);
357    if ( SeqEntry *e = dynamic_cast<SeqEntry*>(d) )
358       return e;
359    return 0;
360 }
361
362
363 /**
364  * \brief   Get the value of a Dicom Element inside a SQ Item Entry
365  * \note : meaningfull only if the required entry is NEITHER a SeqEntry 
366  *                                                   NOR a BinEntry
367  * @param   group   Group number of the Entry
368  * @param   elem  Element number of the Entry 
369  * @return  'string value' of the entry whose (group,elem) was passed.
370  *           GDCM_UNFOUND if not found
371  */ 
372
373 std::string SQItem::GetEntry(uint16_t group, uint16_t elem)
374 {
375
376 /*
377    DocEntry *e = GetFirstEntry();
378    while (e)
379    {
380       if ( e->GetGroup() == group && e->GetElement() == elem)
381       {
382
383          if (ValEntry *ve = dynamic_cast<ValEntry*>(e))
384             return ve->GetValue();
385       }
386       e = GetNextEntry();
387    }   
388 */
389    for(ListDocEntry::iterator i = DocEntries.begin();
390                               i != DocEntries.end(); ++i)
391    {
392       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem)
393       {
394          if (ValEntry *ve = dynamic_cast<ValEntry*>(*i))
395            return ve->GetValue();
396       }
397    }
398    return GDCM_UNFOUND;
399 }
400 //-----------------------------------------------------------------------------
401 // Private
402
403
404 //-----------------------------------------------------------------------------
405
406 } // end namespace gdcm