]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
2005-01-18 Jean-Pierre Roux <jpr@creatis.univ-lyon1.fr>
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/18 08:01:42 $
7   Version:   $Revision: 1.53 $
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  */
62 void SQItem::Print(std::ostream &os, std::string const &)
63 {
64    std::ostringstream s;
65
66    if (SQDepthLevel > 0)
67    {
68       for (int i = 0; i < SQDepthLevel; ++i)
69       {
70          s << "   | " ;
71       }
72    }
73    os << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
74    for (ListDocEntry::iterator i  = DocEntries.begin();
75                                i != DocEntries.end();
76                              ++i)
77    {
78       DocEntry *Entry = *i;
79       bool PrintEndLine = true;
80
81       os << s.str();
82       Entry->SetPrintLevel(PrintLevel);
83       Entry->Print(os); 
84       if ( /* SeqEntry *seqEntry =*/ dynamic_cast<SeqEntry*>(Entry) )
85       {
86          //(void)seqEntry;  //not used
87          PrintEndLine = false;
88       }
89       if (PrintEndLine)
90       {
91          os << std::endl;
92       }
93    } 
94 }
95
96 /*
97  * \brief   canonical Writer
98  */
99 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
100 {
101    int j;
102    uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
103    uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
104
105     //we force the writting of an 'Item' Start Element
106     // because we want to write the Item as a 'no Length' item
107    for(j=0;j<4;++j)
108    {
109       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
110    }
111      
112    for (ListDocEntry::iterator it = DocEntries.begin();  
113                               it != DocEntries.end();
114                              ++it)
115    {   
116       // we skip delimitors (start and end one) because 
117       // we force them as 'no length'
118       if ( (*it)->GetGroup() == 0xfffe )
119       {
120          continue;
121       }
122
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 )
127       {
128          break; // FIXME : continue; ?!?
129       }
130
131       (*it)->WriteContent(fp, filetype);
132    }
133       
134     //we force the writting of an 'Item Delimitation' item
135     // because we wrote the Item as a 'no Length' item
136    for(j=0;j<4;++j)
137    {
138       binary_write( *fp, itemt[j]);  // fffe e000 ffff ffff 
139    }
140  
141 }
142
143 //-----------------------------------------------------------------------------
144 // Public
145 /**
146  * \brief   adds any Entry (Dicom Element) to the Sequence Item
147  */
148 bool SQItem::AddEntry(DocEntry *entry)
149 {
150    DocEntries.push_back(entry);
151    //TODO : check if it worked
152    return true;
153 }   
154
155 /**
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
161  *          (NOT a shadow one)       
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
166  */
167
168 bool SQItem::SetEntry(std::string const &val, uint16_t group, 
169                       uint16_t elem)
170 {
171    for(ListDocEntry::iterator i = DocEntries.begin(); 
172                               i != DocEntries.end(); 
173                             ++i)
174    { 
175       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
176       {
177          continue;
178       }
179
180       if (  ( group  < (*i)->GetGroup() )
181           ||( group == (*i)->GetGroup() && elem < (*i)->GetElement()) )
182       {
183          // instead of ReplaceOrCreate 
184          // that is a method of Document :-( 
185          ValEntry* entry = 0;
186          TagKey key = DictEntry::TranslateToKey(group, elem);
187
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)
194          {
195             dictEntry = 
196                Global::GetDicts()->NewVirtualDictEntry(group, elem,
197                                                        "UN", GDCM_UNKNOWN, 
198                                                         GDCM_UNKNOWN);
199          } 
200          // we assume the constructor didn't fail
201          entry = new ValEntry(dictEntry);
202          if (entry)
203          {
204             entry->SetValue(val); 
205          }
206          DocEntries.insert(i,entry);
207
208          return true;
209       }   
210       if (group == (*i)->GetGroup() && elem == (*i)->GetElement() )
211       {
212          if ( ValEntry* entry = dynamic_cast<ValEntry*>(*i) )
213          {
214             entry->SetValue(val);
215          }
216          return true;    
217       }
218    }
219    return false;
220 }
221
222 /**
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
227  */
228 bool SQItem::RemoveEntry( DocEntry* entryToRemove)
229 {
230    for(ListDocEntry::iterator it = DocEntries.begin();
231        it != DocEntries.end();
232        ++it)
233    {
234       if( *it == entryToRemove)
235       {
236          DocEntries.erase(it);
237          gdcmVerboseMacro( "One element erased.");
238          delete entryToRemove;
239          return true;
240       }
241    }
242    gdcmVerboseMacro( "Value not present.");
243    return false ;
244 }
245
246 /**
247  * \brief   Clear the hash table from given entry BUT keep the entry.
248  * @param   entryToRemove Entry to remove.
249  */
250 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
251 {
252    for(ListDocEntry::iterator it = DocEntries.begin();
253        it != DocEntries.end();
254        ++it)
255    {
256       if( *it == entryToRemove)
257       {
258          DocEntries.erase(it);
259          gdcmVerboseMacro( "One element erased.");
260          return true;
261       }
262    }
263                                                                                 
264    gdcmVerboseMacro( "Value not present.");
265    return false ;
266 }
267                                                                                 
268 /**
269  * \brief   Initialise the visit of the chained list
270  */
271 void SQItem::Initialize()
272 {
273    ItDocEntries = DocEntries.begin();
274 }
275                                                                                 
276 /**
277  * \brief   Get the next entry while visiting the chained list
278  * \return  The next DocEntry if found, otherwhise NULL
279  */
280 DocEntry *SQItem::GetNextEntry()
281 {
282    if (ItDocEntries != DocEntries.end())
283    {
284       DocEntry *tmp = *ItDocEntries;
285       ++ItDocEntries;
286                                                                                 
287       return tmp;
288    }
289    else
290    {
291       return NULL;
292    }
293 }
294
295 //-----------------------------------------------------------------------------
296 // Protected
297 /**
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
301  * @return
302  */
303 DocEntry* SQItem::GetDocEntry(uint16_t group, uint16_t elem)
304 {
305    for(ListDocEntry::iterator i = DocEntries.begin();
306                               i != DocEntries.end(); ++i)
307    {
308       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
309       {
310          return *i;
311       }
312    }
313    return 0;
314 }
315
316 /**
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 
320  * @return
321  */ 
322
323 std::string SQItem::GetEntry(uint16_t group, uint16_t elem)
324 {
325    for(ListDocEntry::iterator i = DocEntries.begin();
326                               i != DocEntries.end(); ++i)
327    {
328       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem)
329       {
330          return ((ValEntry *)(*i))->GetValue();   //FIXME
331       }
332    }
333    return GDCM_UNFOUND;
334 }
335 //-----------------------------------------------------------------------------
336 // Private
337
338
339 //-----------------------------------------------------------------------------
340
341 } // end namespace gdcm