]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
Add comment
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/09/06 17:15:25 $
7   Version:   $Revision: 1.75 $
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    SQItemNumber = 0;
42 }
43
44 /**
45  * \brief   Canonical destructor.
46  */
47 SQItem::~SQItem() 
48 {
49    ClearEntry();
50 }
51
52 //-----------------------------------------------------------------------------
53 // Public
54 /*
55  * \brief   canonical Writer
56  * @param fp     file pointer to an already open file. 
57  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
58  */
59 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
60 {
61    int j;
62    uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
63    uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
64
65     //we force the writting of an 'Item' Start Element
66     // because we want to write the Item as a 'no Length' item
67    for(j=0;j<4;++j)
68    {
69       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
70    }
71      
72    for (ListDocEntry::iterator it = DocEntries.begin();  
73                                it != DocEntries.end();
74                              ++it)
75    {   
76       // we skip delimitors (start and end one) because 
77       // we force them as 'no length'
78       if ( (*it)->GetGroup() == 0xfffe )
79       {
80          continue;
81       }
82
83       // Fix in order to make some MR PHILIPS images e-film readable
84       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
85       // we just *always* ignore spurious fffe|0000 tag ! 
86       if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
87       {
88           break; // FIXME : break or continue; ?!?  
89                  // --> makes no difference since the only bugged file we have
90                  // contains 'impossible tag' fffe|0000 in last position !                            
91       }
92
93       (*it)->WriteContent(fp, filetype);
94    }
95       
96     //we force the writting of an 'Item Delimitation' item
97     // because we wrote the Item as a 'no Length' item
98    for(j=0;j<4;++j)
99    {
100       binary_write( *fp, itemt[j]);  // fffe e000 ffff ffff 
101    } 
102 }
103
104 /**
105  * \brief   Inserts *in the right place* any Entry (Dicom Element)
106  *          into the Sequence Item
107  * @param entry Entry to add
108  * @return always true 
109  */
110 bool SQItem::AddEntry(DocEntry *entry)
111 {   
112    if (DocEntries.empty() )
113    {
114       DocEntries.push_back(entry);
115       return true;
116    }
117  
118    ListDocEntry::iterator insertSpot;
119    ListDocEntry::iterator it = DocEntries.end();
120    do
121    {
122       it--;
123
124       if ( (*it)->IsItemDelimitor() )
125       {
126          continue;
127       }
128       if ( (*it)->GetGroup() < entry->GetGroup() )
129          break;
130       else
131          if ( (*it)->GetGroup() == entry->GetGroup() &&
132               (*it)->GetElement() < entry->GetElement() )
133             break;
134    } while (it != DocEntries.begin() );
135   
136    insertSpot = it++;
137    insertSpot++; // ?!?
138    DocEntries.insert(insertSpot, entry); 
139    return true;
140 }   
141
142 /**
143  * \brief   Clear the std::list from given entry AND delete the entry.
144  * @param   entryToRemove Entry to remove AND delete.
145  * @return true if the entry was found and removed; false otherwise
146  */
147 bool SQItem::RemoveEntry( DocEntry *entryToRemove )
148 {
149    for(ListDocEntry::iterator it = DocEntries.begin();
150                               it != DocEntries.end();
151                             ++it)
152    {
153       if ( *it == entryToRemove )
154       {
155          DocEntries.erase(it);
156          gdcmWarningMacro( "One element erased: " << entryToRemove->GetKey() );
157          delete entryToRemove;
158          return true;
159       }
160    }
161    gdcmWarningMacro( "Entry not found: " << entryToRemove->GetKey() );
162    return false ;
163 }
164
165 /**
166  * \brief   Clear the std::list from given entry BUT keep the entry.
167  * @param   entryToRemove Entry to remove.
168  * @return true if the entry was found and removed; false otherwise
169  */
170 bool SQItem::RemoveEntryNoDestroy(DocEntry *entryToRemove)
171 {
172    for(ListDocEntry::iterator it =  DocEntries.begin();
173                               it != DocEntries.end();
174                             ++it)
175    {
176       if ( *it == entryToRemove )
177       {
178          DocEntries.erase(it);
179          gdcmWarningMacro( "One element removed, no destroyed: "
180                             << entryToRemove->GetKey() );
181          return true;
182       }
183    }
184                                                                                 
185    gdcmWarningMacro( "Entry not found:" << entryToRemove->GetKey() );
186    return false ;
187 }
188                                                                                 
189 /**
190  * \brief  Remove all entry in the Sequence Item 
191  */
192 void SQItem::ClearEntry()
193 {
194    for(ListDocEntry::iterator cc = DocEntries.begin();
195                               cc != DocEntries.end();
196                             ++cc)
197    {
198       delete *cc;
199    }
200    DocEntries.clear();
201 }
202
203 /**
204  * \brief  Clear the std::list from given Sequence Item  BUT keep the entries
205  */
206 void SQItem::ClearEntryNoDestroy()
207 {
208    DocEntries.clear();
209 }
210
211
212 /**
213  * \brief  Move all the entries from a given Sequence Item 
214  */
215 void SQItem::MoveObject(SQItem *source)
216 {
217    DocEntries = source->DocEntries;
218    source->ClearEntryNoDestroy();
219 }
220
221 /**
222  * \brief   Get the first Dicom entry while visiting the SQItem
223  * \return  The first DocEntry if found, otherwhise 0
224  */
225 DocEntry *SQItem::GetFirstEntry()
226 {
227    ItDocEntries = DocEntries.begin();
228    if ( ItDocEntries != DocEntries.end() )
229       return *ItDocEntries;
230    return 0;   
231 }
232                                                                                 
233 /**
234  * \brief   Get the next Dicom entry while visiting the SQItem
235  * \return  The next DocEntry if found, otherwhise NULL
236  */
237 DocEntry *SQItem::GetNextEntry()
238 {
239    ++ItDocEntries;
240    if ( ItDocEntries != DocEntries.end() )
241       return  *ItDocEntries;
242    return NULL;
243 }
244
245 /**
246  * \brief   Gets a Dicom Element inside a SQ Item Entry
247  * @param   group   Group number of the Entry
248  * @param   elem  Element number of the Entry
249  * @return Entry whose (group,elem) was passed. 0 if not found
250  */
251 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
252 {
253    for(ListDocEntry::iterator i =  DocEntries.begin();
254                               i != DocEntries.end(); 
255                             ++i)
256    {
257       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
258          return *i;
259    }
260    return NULL;
261 }
262
263 //-----------------------------------------------------------------------------
264 // Protected
265
266 //-----------------------------------------------------------------------------
267 // Private
268
269 //-----------------------------------------------------------------------------
270 // Print
271 /*
272  * \brief   canonical Printer
273  * @param os     Stream to print to. 
274  * @param indent Indentation string to be prepended during printing.
275  */
276 void SQItem::Print(std::ostream &os, std::string const &)
277 {
278    std::ostringstream s;
279
280    if (SQDepthLevel > 0)
281    {
282       for (int i = 0; i < SQDepthLevel; ++i)
283       {
284          s << "   | " ;
285       }
286    }
287    os << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
288    for (ListDocEntry::iterator i  = DocEntries.begin();
289                                i != DocEntries.end();
290                              ++i)
291    {
292       DocEntry *Entry = *i;
293       bool PrintEndLine = true;
294
295       os << s.str();
296       Entry->SetPrintLevel(PrintLevel);
297       Entry->Print(os); 
298       if ( dynamic_cast<SeqEntry*>(Entry) )
299       {
300          PrintEndLine = false;
301       }
302       if (PrintEndLine)
303       {
304          os << std::endl;
305       }
306    } 
307 }
308
309 //-----------------------------------------------------------------------------
310 } // end namespace gdcm