]> Creatis software - gdcm.git/blob - src/gdcmSeqEntry.cxx
Fix mistypings
[gdcm.git] / src / gdcmSeqEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/10/08 15:20:17 $
7   Version:   $Revision: 1.72 $
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 "gdcmSeqEntry.h"
20 #include "gdcmSQItem.h"
21 #include "gdcmTS.h"
22 #include "gdcmGlobal.h"
23 #include "gdcmUtil.h"
24 #include "gdcmDebug.h"
25
26 #include <iostream>
27 #include <iomanip>
28 #include <fstream>
29
30 namespace GDCM_NAME_SPACE 
31 {
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34
35 // Constructor / Destructor
36 /**
37  * \brief   Constructor from a given SeqEntry
38  */
39 SeqEntry::SeqEntry( uint16_t group,uint16_t elem ) 
40              : DocEntry(group, elem, "SQ")
41 {
42    Length       = 0;
43    ReadLength   = 0xffffffff;
44    SQDepthLevel = -1;
45
46    DelimitorMode = false;
47    SeqTerm  = NULL;
48 }
49
50 /**
51  * \brief   Constructor from a given DocEntry
52  * @param   e Pointer to existing Doc entry
53  * @param   depth depth level of the current Seq entry
54  */
55 SeqEntry::SeqEntry( DocEntry *e, int depth )
56              //: DocEntry( e->GetDictEntry() )
57             : DocEntry( e->GetGroup(), e->GetElement(), "SQ" /*e->GetVR()*/ )
58 {
59    Length       = 0;
60    ReadLength   = 0xffffffff;
61    SQDepthLevel = depth;
62
63    ImplicitVR   = e->IsImplicitVR();
64    Offset       = e->GetOffset();
65    SeqTerm = NULL;
66 }
67
68 /**
69  * \brief   Canonical destructor.
70  */
71 SeqEntry::~SeqEntry()
72 {
73    ClearSQItem();
74 }
75
76 //-----------------------------------------------------------------------------
77 // Public
78 /*
79  * \brief   canonical Writer
80  * @param fp pointer to an already open file
81  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
82  */
83 void SeqEntry::WriteContent(std::ofstream *fp, FileType filetype, bool , bool )
84 {
85    uint16_t seq_term_gr = 0xfffe;
86    uint16_t seq_term_el = 0xe0dd;
87    uint32_t seq_term_lg = 0x00000000;
88  
89    // ignore 'Zero length' Sequences
90    if ( GetReadLength() == 0 )
91       return;
92    // false : we are not in MetaElements
93    // true : we are inside a Sequence
94    DocEntry::WriteContent(fp, filetype, false, true);
95    for(ListSQItem::iterator cc  = Items.begin();
96                             cc != Items.end();
97                           ++cc)
98    {   
99       (*cc)->WriteContent(fp, filetype, false, true);
100    }
101    
102    // we force the writting of a Sequence Delimitation item
103    // because we wrote the Sequence as a 'no Length' sequence
104    binary_write(*fp, seq_term_gr);
105    binary_write(*fp, seq_term_el);
106    binary_write(*fp, seq_term_lg);
107 }
108
109 /**
110  * \brief   Compute the full length of the SeqEntry (not only value
111  *          length) depending on the VR.
112  */
113 uint32_t SeqEntry::ComputeFullLength()
114 {
115    uint32_t l = 12; // Tag (4) + VR (explicit) 4 + 4 (length);   
116    for(ListSQItem::iterator cc  = Items.begin();
117                             cc != Items.end();
118                           ++cc)
119    {        
120       l += (*cc)->ComputeFullLength();
121    }   
122    l += 8; // seq_term Tag (4) +  seq_term_lg (4)
123    return l;
124 }
125
126 /**
127  * \brief   adds the passed ITEM to the ITEM chained List for this SeQuence.
128  * @param sqItem SQItem to be pushed back in the SeqEntry
129  * @param itemNumber ordinal number of the SQItem
130  *  \note NOT end-user intendend method !
131  */
132 void SeqEntry::AddSQItem(SQItem *sqItem, int itemNumber)
133 {
134 // FIXME : SQItemNumber is supposed to be the ordinal number of the SQItem
135 //         within the Sequence.
136 //         Either only 'push_back' is allowed, 
137 //                and we just have to do something like SeqEntry::lastNb++
138 //         Or we can add (or remove) anywhere, and SQItemNumber will be broken
139    sqItem->SetSQItemNumber(itemNumber);
140    Items.push_back(sqItem);
141    sqItem->Register();
142 }
143
144 /**
145  * \brief Remove all SQItem.
146  */
147 void SeqEntry::ClearSQItem()
148 {
149    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
150    {
151       (*cc)->Unregister();
152    }
153    if (SeqTerm)
154    {
155       SeqTerm->Unregister();
156    }
157 }
158
159 /**
160  * \brief   Get the first entry while visiting the SeqEntry
161  * \return  The first SQItem if found, otherwhise NULL
162  */ 
163 SQItem *SeqEntry::GetFirstSQItem()
164 {
165    ItSQItem = Items.begin();
166    if (ItSQItem != Items.end())
167       return *ItSQItem;
168    return NULL;
169
170
171 /**
172  * \brief   Get the next SQItem while visiting the SeqEntry
173  * \note : meaningfull only if GetFirstEntry already called
174  * \return  The next SQItem if found, otherwhise NULL
175  */
176
177 SQItem *SeqEntry::GetNextSQItem()
178 {
179    gdcmAssertMacro (ItSQItem != Items.end())
180    {
181       ++ItSQItem;
182       if (ItSQItem != Items.end())
183          return *ItSQItem;
184    }
185    return NULL;
186 }
187  
188 /**
189  * \brief return a pointer to the SQItem referenced by its ordinal number.
190  *        Returns the first item when argument is negative.
191  *        Returns the  last item when argument is bigger than the total
192  *        item number.
193  */
194 SQItem *SeqEntry::GetSQItem(int nb)
195 {
196    if (nb<0)
197    {
198       return *(Items.begin());
199    }
200    int count = 0 ;
201    for(ListSQItem::iterator cc = Items.begin();
202                            cc != Items.end();
203                            count ++, ++cc)
204    {
205       if (count == nb)
206       {
207          return *cc;
208       }
209    }
210    return *(Items.end());
211 }
212
213 /**
214  * \brief returns the number of SQItems within the current Sequence
215  */
216 unsigned int SeqEntry::GetNumberOfSQItems()
217 {
218    return Items.size();
219 }
220
221 /**
222  * \brief Sets the Sequence Delimitation Item
223  * \param e Delimitation item
224  */
225 void SeqEntry::SetDelimitationItem(DocEntry *e)
226 {
227    if( SeqTerm != e )
228    {
229       if( SeqTerm )
230          SeqTerm->Unregister();
231       SeqTerm = e;
232       if( SeqTerm )
233          SeqTerm->Register();
234    }
235 }
236
237 /**
238  * \brief Copies all the attributes from an other DocEntry 
239  * @param doc entry to copy from
240  * @remarks The contained SQItems a not copied, only referenced
241  */
242 void SeqEntry::Copy(DocEntry *doc)
243 {
244    // Delete previous SQ items
245    ClearSQItem();
246
247    DocEntry::Copy(doc);
248    SeqEntry *entry = dynamic_cast<SeqEntry *>(doc);
249    if ( entry )
250    {
251       DelimitorMode = entry->DelimitorMode;
252       SQDepthLevel = entry->SQDepthLevel;
253
254       SeqTerm = entry->SeqTerm;
255       if(SeqTerm)
256          SeqTerm->Register();
257       Items = entry->Items;
258       for(ItSQItem = Items.begin();ItSQItem != Items.end(); ++ItSQItem)
259       {
260          (*ItSQItem)->Register();
261       }
262    }
263 }
264
265 //-----------------------------------------------------------------------------
266 // Protected
267
268 //-----------------------------------------------------------------------------
269 // Private
270
271 //-----------------------------------------------------------------------------
272 // Print
273 /**
274  * \brief   canonical Printer
275  */
276 void SeqEntry::Print( std::ostream &os, std::string const & )
277 {
278    // os << "S ";
279    
280    // First, Print the common part (vr [length offset] name).
281    DocEntry::Print(os);
282    os << std::endl;
283
284    if (GetReadLength() == 0)
285       return;
286
287    // Then, Print each SQ Item   
288    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
289    {
290       (*cc)->SetPrintLevel(PrintLevel);
291       (*cc)->Print(os);   
292    }
293
294    // at end, print the sequence terminator item, if any
295    if (DelimitorMode)
296    {
297       int i;
298       for ( i = 0; i < SQDepthLevel; i++ )
299          os << "   | " ;
300       os << " --- "  << std::endl;
301       for ( i = 0; i < SQDepthLevel; i++ )
302          os << "   | " ;
303       if (SeqTerm != NULL)
304       {
305          SeqTerm->SetPrintLevel(PrintLevel);
306          SeqTerm->Print(os);
307          os << std::endl;
308       } 
309       else 
310       {
311          // fuse
312          gdcmWarningMacro("  -------- should have a sequence terminator item");
313       }
314    }
315 }
316
317 //-----------------------------------------------------------------------------
318 } // end namespace gdcm