]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
SQItem number within a given SeqEntry is now displayed at Print time
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/17 11:07:40 $
7   Version:   $Revision: 1.52 $
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  * \ingroup SQItem
36  * \brief   Constructor from a given SQItem
37  */
38 SQItem::SQItem(int depthLevel ) 
39           : DocEntrySet( )
40 {
41    SQDepthLevel = depthLevel;
42 }
43
44 /**
45  * \brief   Canonical destructor.
46  */
47 SQItem::~SQItem() 
48 {
49    for(ListDocEntry::iterator cc = DocEntries.begin();
50                              cc != DocEntries.end();
51                              ++cc)
52    {
53       delete *cc;
54    }
55    DocEntries.clear();
56 }
57
58 //-----------------------------------------------------------------------------
59 // Print
60 /*
61  * \brief   canonical Printer
62  */
63 void SQItem::Print(std::ostream &os, std::string const &)
64 {
65    std::ostringstream s;
66
67    if (SQDepthLevel > 0)
68    {
69       for (int i = 0; i < SQDepthLevel; ++i)
70       {
71          s << "   | " ;
72       }
73    }
74    os << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
75    for (ListDocEntry::iterator i  = DocEntries.begin();
76                                i != DocEntries.end();
77                              ++i)
78    {
79       DocEntry *Entry = *i;
80       bool PrintEndLine = true;
81
82       os << s.str();
83       Entry->SetPrintLevel(PrintLevel);
84       Entry->Print(os); 
85       if ( /* SeqEntry *seqEntry =*/ dynamic_cast<SeqEntry*>(Entry) )
86       {
87          //(void)seqEntry;  //not used
88          PrintEndLine = false;
89       }
90       if (PrintEndLine)
91       {
92          os << std::endl;
93       }
94    } 
95 }
96
97 /*
98  * \ingroup SQItem
99  * \brief   canonical Writer
100  */
101 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
102 {
103    int j;
104    uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
105    uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
106
107     //we force the writting of an 'Item' Start Element
108     // because we want to write the Item as a 'no Length' item
109    for(j=0;j<4;++j)
110    {
111       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
112    }
113      
114    for (ListDocEntry::iterator it = DocEntries.begin();  
115                               it != DocEntries.end();
116                              ++it)
117    {   
118       // we skip delimitors (start and end one) because 
119       // we force them as 'no length'
120       if ( (*it)->GetGroup() == 0xfffe )
121       {
122          continue;
123       }
124
125       // Fix in order to make some MR PHILIPS images e-film readable
126       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
127       // we just *always* ignore spurious fffe|0000 tag ! 
128       if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
129       {
130          break; // FIXME : continue; ?!?
131       }
132
133       (*it)->WriteContent(fp, filetype);
134    }
135       
136     //we force the writting of an 'Item Delimitation' item
137     // because we wrote the Item as a 'no Length' item
138    for(j=0;j<4;++j)
139    {
140       binary_write( *fp, itemt[j]);  // fffe e000 ffff ffff 
141    }
142  
143 }
144
145 //-----------------------------------------------------------------------------
146 // Public
147 /**
148  * \brief   adds any Entry (Dicom Element) to the Sequence Item
149  */
150 bool SQItem::AddEntry(DocEntry *entry)
151 {
152    DocEntries.push_back(entry);
153    //TODO : check if it worked
154    return true;
155 }   
156
157 /**
158  * \brief   Sets Entry (Dicom Element) value of an element,
159  *          specified by it's tag (Group, Number) 
160  *          and the length, too ...  inside a SQ Item
161  *          If the Element is not found, it's just created !
162  * \warning we suppose, right now, the element belongs to a Public Group
163  *          (NOT a shadow one)       
164  * @param   val string value to set
165  * @param   group Group number of the searched tag.
166  * @param   elem Element number of the searched tag.
167  * @return  true if element was found or created successfully
168  */
169
170 bool SQItem::SetEntry(std::string const &val, uint16_t group, 
171                       uint16_t elem)
172 {
173    for(ListDocEntry::iterator i = DocEntries.begin(); 
174                               i != DocEntries.end(); 
175                             ++i)
176    { 
177       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
178       {
179          continue;
180       }
181
182       if (  ( group  < (*i)->GetGroup() )
183           ||( group == (*i)->GetGroup() && elem < (*i)->GetElement()) )
184       {
185          // instead of ReplaceOrCreate 
186          // that is a method of Document :-( 
187          ValEntry* entry = 0;
188          TagKey key = DictEntry::TranslateToKey(group, elem);
189
190          // we assume a Public Dictionary *is* loaded
191          Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
192          // if the invoked (group,elem) doesn't exist inside the Dictionary
193          // we create a VirtualDictEntry
194          DictEntry *dictEntry = pubDict->GetDictEntry(group, elem);
195          if (dictEntry == NULL)
196          {
197             dictEntry = 
198                Global::GetDicts()->NewVirtualDictEntry(group, elem,
199                                                        "UN", GDCM_UNKNOWN, 
200                                                         GDCM_UNKNOWN);
201          } 
202          // we assume the constructor didn't fail
203          entry = new ValEntry(dictEntry);
204          if (entry)
205          {
206             entry->SetValue(val); 
207          }
208          DocEntries.insert(i,entry);
209
210          return true;
211       }   
212       if (group == (*i)->GetGroup() && elem == (*i)->GetElement() )
213       {
214          if ( ValEntry* entry = dynamic_cast<ValEntry*>(*i) )
215          {
216             entry->SetValue(val);
217          }
218          return true;    
219       }
220    }
221    return false;
222 }
223
224 /**
225  * \brief   Clear the hash table from given entry AND delete the entry.
226  * @param   entryToRemove Entry to remove AND delete.
227  * \warning Some problems when using under Windows... prefer the use of
228  *          Initialize / GetNext methods
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  */
252 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
253 {
254    for(ListDocEntry::iterator it = DocEntries.begin();
255        it != DocEntries.end();
256        ++it)
257    {
258       if( *it == entryToRemove)
259       {
260          DocEntries.erase(it);
261          gdcmVerboseMacro( "One element erased.");
262          return true;
263       }
264    }
265                                                                                 
266    gdcmVerboseMacro( "Value not present.");
267    return false ;
268 }
269                                                                                 
270 /**
271  * \brief   Initialise the visit of the chained list
272  */
273 void SQItem::Initialize()
274 {
275    ItDocEntries = DocEntries.begin();
276 }
277                                                                                 
278 /**
279  * \brief   Get the next entry whil visiting the chained list
280  * \return  The next DocEntry if found, otherwhise NULL
281  */
282 DocEntry *SQItem::GetNextEntry()
283 {
284    if (ItDocEntries != DocEntries.end())
285    {
286       DocEntry *tmp = *ItDocEntries;
287       ++ItDocEntries;
288                                                                                 
289       return tmp;
290    }
291    else
292    {
293       return NULL;
294    }
295 }
296
297 //-----------------------------------------------------------------------------
298 // Protected
299 /**
300  * \brief   Gets a Dicom Element inside a SQ Item Entry
301  * @param   group   Group number of the Entry
302  * @param   elem  Element number of the Entry
303  * @return
304  */
305 DocEntry* SQItem::GetDocEntry(uint16_t group, uint16_t elem)
306 {
307    for(ListDocEntry::iterator i = DocEntries.begin();
308                               i != DocEntries.end(); ++i)
309    {
310       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
311       {
312          return *i;
313       }
314    }
315    return 0;
316 }
317
318 /**
319  * \brief   Get the value of a Dicom Element inside a SQ Item Entry
320  * @param   group   Group number of the Entry
321  * @param   elem  Element number of the Entry 
322  * @return
323  */ 
324
325 std::string SQItem::GetEntry(uint16_t group, uint16_t elem)
326 {
327    for(ListDocEntry::iterator i = DocEntries.begin();
328                               i != DocEntries.end(); ++i)
329    {
330       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem)
331       {
332          return ((ValEntry *)(*i))->GetValue();   //FIXME
333       }
334    }
335    return GDCM_UNFOUND;
336 }
337 //-----------------------------------------------------------------------------
338 // Private
339
340
341 //-----------------------------------------------------------------------------
342
343 } // end namespace gdcm