]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
Stage 3 of normalization :
[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 16:10:53 $
7   Version:   $Revision: 1.57 $
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  * @param fp     file pointer to an already open file. 
100  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
101  */
102 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
103 {
104    int j;
105    uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
106    uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
107
108     //we force the writting of an 'Item' Start Element
109     // because we want to write the Item as a 'no Length' item
110    for(j=0;j<4;++j)
111    {
112       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
113    }
114      
115    for (ListDocEntry::iterator it = DocEntries.begin();  
116                               it != DocEntries.end();
117                              ++it)
118    {   
119       // we skip delimitors (start and end one) because 
120       // we force them as 'no length'
121       if ( (*it)->GetGroup() == 0xfffe )
122       {
123          continue;
124       }
125
126       // Fix in order to make some MR PHILIPS images e-film readable
127       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
128       // we just *always* ignore spurious fffe|0000 tag ! 
129       if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
130       {
131          break; // FIXME : continue; ?!?
132       }
133
134       (*it)->WriteContent(fp, filetype);
135    }
136       
137     //we force the writting of an 'Item Delimitation' item
138     // because we wrote the Item as a 'no Length' item
139    for(j=0;j<4;++j)
140    {
141       binary_write( *fp, itemt[j]);  // fffe e000 ffff ffff 
142    }
143  
144 }
145
146 //-----------------------------------------------------------------------------
147 // Public
148 /**
149  * \brief   adds any Entry (Dicom Element) to the Sequence Item
150  * @param entry Entry to add
151  */
152 bool SQItem::AddEntry(DocEntry *entry)
153 {
154    DocEntries.push_back(entry);
155    //TODO : check if it worked
156    return true;
157 }   
158
159 /**
160  * \brief   Sets Entry (Dicom Element) value of an element,
161  *          specified by it's tag (Group, Number) 
162  *          and the length, too ...  inside a SQ Item
163  *          If the Element is not found, it's just created !
164  * \warning we suppose, right now, the element belongs to a Public Group
165  *          (NOT a shadow one)       
166  * @param   val string value to set
167  * @param   group  Group number of the searched tag.
168  * @param   elem Element number of the searched tag.
169  * @return  true if element was found or created successfully
170  */
171
172 bool SQItem::SetEntryValue(std::string const &val, uint16_t group, 
173                       uint16_t elem)
174 {
175    for(ListDocEntry::iterator i = DocEntries.begin(); 
176                               i != DocEntries.end(); 
177                             ++i)
178    { 
179       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
180       {
181          continue;
182       }
183
184       if (  ( group  < (*i)->GetGroup() )
185           ||( group == (*i)->GetGroup() && elem < (*i)->GetElement()) )
186       {
187          // instead of ReplaceOrCreate 
188          // that is a method of Document :-( 
189          ValEntry* entry = 0;
190          TagKey key = DictEntry::TranslateToKey(group, elem);
191
192          // we assume a Public Dictionary *is* loaded
193          Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
194          // if the invoked (group,elem) doesn't exist inside the Dictionary
195          // we create a VirtualDictEntry
196          DictEntry *dictEntry = pubDict->GetEntry(group, elem);
197          if (dictEntry == NULL)
198          {
199             dictEntry = 
200                Global::GetDicts()->NewVirtualDictEntry(group, elem,
201                                                        "UN", GDCM_UNKNOWN, 
202                                                         GDCM_UNKNOWN);
203          } 
204          // we assume the constructor didn't fail
205          entry = new ValEntry(dictEntry);
206          if (entry)
207          {
208             entry->SetValue(val); 
209          }
210          DocEntries.insert(i,entry);
211
212          return true;
213       }   
214       if (group == (*i)->GetGroup() && elem == (*i)->GetElement() )
215       {
216          if ( ValEntry *entry = dynamic_cast<ValEntry*>(*i) )
217          {
218             entry->SetValue(val);
219          }
220          return true;    
221       }
222    }
223    return false;
224 }
225
226 /**
227  * \brief   Clear the std::list from given entry AND delete the entry.
228  * @param   entryToRemove Entry to remove AND delete.
229  * @return true if the entry was found and removed; false otherwise
230  */
231 bool SQItem::RemoveEntry( DocEntry* entryToRemove)
232 {
233    for(ListDocEntry::iterator it = DocEntries.begin();
234        it != DocEntries.end();
235        ++it)
236    {
237       if( *it == entryToRemove)
238       {
239          DocEntries.erase(it);
240          gdcmVerboseMacro( "One element erased: " << entryToRemove->GetKey() );
241          delete entryToRemove;
242          return true;
243       }
244    }
245    gdcmVerboseMacro( "Entry not found: " << entryToRemove->GetKey() );
246    return false ;
247 }
248
249 /**
250  * \brief   Clear the std::list from given entry BUT keep the entry.
251  * @param   entryToRemove Entry to remove.
252  * @return true if the entry was found and removed; false otherwise
253  */
254 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
255 {
256    for(ListDocEntry::iterator it = DocEntries.begin();
257        it != DocEntries.end();
258        ++it)
259    {
260       if( *it == entryToRemove)
261       {
262          DocEntries.erase(it);
263          gdcmVerboseMacro( "One element erased, no destroyed: "
264                             << entryToRemove->GetKey() );
265          return true;
266       }
267    }
268                                                                                 
269    gdcmVerboseMacro( "Entry not found:" << entryToRemove->GetKey() );
270    return false ;
271 }
272                                                                                 
273 /**
274  * \brief   Get the first Dicom entry while visiting the SQItem
275  * \return  The first DocEntry if found, otherwhise 0
276  */
277 DocEntry * SQItem::GetFirstEntry()
278 {
279    ItDocEntries = DocEntries.begin();
280    if (ItDocEntries != DocEntries.end())
281       return *ItDocEntries;
282    return 0;   
283 }
284                                                                                 
285 /**
286  * \brief   Get the next Dicom entry while visiting the chained list
287  * \return  The next DocEntry if found, otherwhise NULL
288  */
289 DocEntry *SQItem::GetNextEntry()
290 {
291   // gdcmAssertMacro (ItDocEntries != DocEntries.end());
292    {
293       ++ItDocEntries;
294       if (ItDocEntries != DocEntries.end())
295          return  *ItDocEntries;
296       return NULL;
297    }
298 }
299
300 //-----------------------------------------------------------------------------
301 // Protected
302 /**
303  * \brief   Gets a Dicom Element inside a SQ Item Entry
304  * @param   group Group number of the Entry
305  * @param   elem  Element number of the Entry
306  * @return Entry whose (group,elem) was passed. 0 if not found
307  */
308 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
309 {
310    for(ListDocEntry::iterator i = DocEntries.begin();
311                               i != DocEntries.end(); ++i)
312    {
313       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
314       {
315          return *i;
316       }
317    }
318    return 0;
319 }
320
321 /**
322  * \brief   Gets a Dicom Element inside a SQ Item Entry
323  * @param   group Group number of the Entry
324  * @param   elem  Element number of the Entry
325  * @return Entry whose (group,elem) was passed. 0 if not found
326  */
327 ValEntry* SQItem::GetValEntry(uint16_t group, uint16_t elem)
328 {
329    DocEntry *d = GetDocEntry(group, elem);
330    if ( ValEntry *e = dynamic_cast<ValEntry*>(d) )
331       return e;
332    return 0;
333 }
334
335 /**
336  * \brief   Gets a Dicom Element inside a SQ Item Entry
337  * @param   group   Group number of the Entry
338  * @param   elem  Element number of the Entry
339  * @return Entry whose (group,elem) was passed. 0 if not found
340  */
341 BinEntry* SQItem::GetBinEntry(uint16_t group, uint16_t elem)
342 {
343    DocEntry *d = GetDocEntry(group, elem);
344    if ( BinEntry *e = dynamic_cast<BinEntry*>(d) )
345       return e;
346    return 0;
347 }
348
349 /**
350  * \brief   Gets a Dicom Element inside a SQ Item Entry
351  * @param   group   Group number of the Entry
352  * @param   elem  Element number of the Entry
353  * @return Entry whose (group,elem) was passed. 0 if not found
354  */
355 SeqEntry* SQItem::GetSeqEntry(uint16_t group, uint16_t elem)
356 {
357    DocEntry *d = GetDocEntry(group, elem);
358    if ( SeqEntry *e = dynamic_cast<SeqEntry*>(d) )
359       return e;
360    return 0;
361 }
362
363
364 /**
365  * \brief   Get the value of a Dicom Element inside a SQ Item Entry
366  * \note : meaningfull only if the required entry is NEITHER a SeqEntry 
367  *                                                   NOR a BinEntry
368  * @param   group   Group number of the Entry
369  * @param   elem  Element number of the Entry 
370  * @return  'string value' of the entry whose (group,elem) was passed.
371  *           GDCM_UNFOUND if not found
372  */ 
373
374 std::string SQItem::GetEntryValue(uint16_t group, uint16_t elem)
375 {
376
377 /*
378    DocEntry *e = GetFirstEntry();
379    while (e)
380    {
381       if ( e->GetGroup() == group && e->GetElement() == elem)
382       {
383
384          if (ValEntry *ve = dynamic_cast<ValEntry*>(e))
385             return ve->GetValue();
386       }
387       e = GetNextEntry();
388    }   
389 */
390    for(ListDocEntry::iterator i = DocEntries.begin();
391                               i != DocEntries.end(); ++i)
392    {
393       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem)
394       {
395          if (ValEntry *ve = dynamic_cast<ValEntry*>(*i))
396            return ve->GetValue();
397       }
398    }
399    return GDCM_UNFOUND;
400 }
401 //-----------------------------------------------------------------------------
402 // Private
403
404
405 //-----------------------------------------------------------------------------
406
407 } // end namespace gdcm