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