]> Creatis software - gdcm.git/blob - src/gdcmSeqEntry.cxx
* Minor coding-style clean up
[gdcm.git] / src / gdcmSeqEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/18 08:35:50 $
7   Version:   $Revision: 1.58 $
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 
31 {
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 /**
35  * \brief   Constructor from a given SeqEntry
36  */
37 SeqEntry::SeqEntry( DictEntry *e ) 
38              : DocEntry(e)
39 {
40    Length       = 0;
41    ReadLength   = 0xffffffff;
42    SQDepthLevel = -1;
43
44    DelimitorMode = false;
45    SeqTerm  = NULL;
46 }
47
48 /**
49  * \brief   Constructor from a given DocEntry
50  * @param   e Pointer to existing Doc entry
51  * @param   depth depth level of the current Seq entry
52  */
53 SeqEntry::SeqEntry( DocEntry *e, int depth )
54              : DocEntry( e->GetDictEntry() )
55 {
56    Length       = 0;
57    ReadLength   = 0xffffffff;
58    SQDepthLevel = depth;
59
60    ImplicitVR   = e->IsImplicitVR();
61    Offset       = e->GetOffset();
62    SeqTerm = NULL;
63 }
64
65 /**
66  * \brief   Canonical destructor.
67  */
68 SeqEntry::~SeqEntry()
69 {
70    ClearSQItem();
71 }
72
73 //-----------------------------------------------------------------------------
74 // Public
75 /*
76  * \brief   canonical Writer
77  * @param fp pointer to an already open file
78  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
79  */
80 void SeqEntry::WriteContent(std::ofstream *fp, FileType filetype)
81 {
82    uint16_t seq_term_gr = 0xfffe;
83    uint16_t seq_term_el = 0xe0dd;
84    uint32_t seq_term_lg = 0xffffffff;
85
86    //uint16_t item_term_gr = 0xfffe;
87    //uint16_t item_term_el = 0xe00d;
88    
89    DocEntry::WriteContent(fp, filetype);
90    for(ListSQItem::iterator cc  = Items.begin();
91                             cc != Items.end();
92                           ++cc)
93    {        
94       (*cc)->WriteContent(fp, filetype);
95    }
96    
97    // we force the writting of a Sequence Delimitation item
98    // because we wrote the Sequence as a 'no Length' sequence
99    binary_write(*fp, seq_term_gr);
100    binary_write(*fp, seq_term_el);
101    binary_write(*fp, seq_term_lg);
102 }
103
104 /**
105  * \brief   adds the passed ITEM to the ITEM chained List for this SeQuence.
106  * @param sqItem SQItem to be pushed back in the SeqEntry
107  * @param itemNumber ordinal number of the SQItem
108  *  \note NOT end-user intendend method !
109  */
110 void SeqEntry::AddSQItem(SQItem *sqItem, int itemNumber)
111 {
112 // FIXME : SQItemNumber is supposed to be the ordinal number of the SQItem
113 //         within the Sequence.
114 //         Either only 'push_back' is allowed, 
115 //                and we just have to do something like SeqEntry::lastNb++
116 //         Or we can add (or remove) anywhere, and SQItemNumber will be broken
117    sqItem->SetSQItemNumber(itemNumber);
118    Items.push_back(sqItem);
119 }
120
121 /**
122  * \brief Remove all SQItem.
123  */
124 void SeqEntry::ClearSQItem()
125 {
126    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
127    {
128       delete *cc;
129    }
130    if (SeqTerm)
131    {
132       delete SeqTerm;
133    }
134 }
135
136 /**
137  * \brief   Get the first entry while visiting the SeqEntry
138  * \return  The first SQItem if found, otherwhise NULL
139  */ 
140 SQItem *SeqEntry::GetFirstSQItem()
141 {
142    ItSQItem = Items.begin();
143    if (ItSQItem != Items.end())
144       return *ItSQItem;
145    return NULL;
146
147
148 /**
149  * \brief   Get the next SQItem while visiting the SeqEntry
150  * \note : meaningfull only if GetFirstEntry already called
151  * \return  The next SQItem if found, otherwhise NULL
152  */
153
154 SQItem *SeqEntry::GetNextSQItem()
155 {
156    gdcmAssertMacro (ItSQItem != Items.end())
157    {
158       ++ItSQItem;
159       if (ItSQItem != Items.end())
160          return *ItSQItem;
161    }
162    return NULL;
163 }
164  
165 /**
166  * \brief return a pointer to the SQItem referenced by its ordinal number.
167  *        Returns the first item when argument is negative.
168  *        Returns the  last item when argument is bigger than the total
169  *        item number.
170  */
171 SQItem *SeqEntry::GetSQItem(int nb)
172 {
173    if (nb<0)
174    {
175       return *(Items.begin());
176    }
177    int count = 0 ;
178    for(ListSQItem::iterator cc = Items.begin();
179                            cc != Items.end();
180                            count ++, ++cc)
181    {
182       if (count == nb)
183       {
184          return *cc;
185       }
186    }
187    return *(Items.end());
188 }
189
190 /**
191  * \brief returns the number of SQItems within the current Sequence
192  */
193 unsigned int SeqEntry::GetNumberOfSQItems()
194 {
195    return Items.size();
196 }
197
198 //-----------------------------------------------------------------------------
199 // Protected
200
201
202 //-----------------------------------------------------------------------------
203 // Private
204
205 //-----------------------------------------------------------------------------
206 // Print
207 /**
208  * \brief   canonical Printer
209  */
210 void SeqEntry::Print( std::ostream &os, std::string const & )
211 {
212    // First, Print the Dicom Element itself.
213    os << "S ";
214    DocEntry::Print(os);
215    os << std::endl;
216
217    if (GetReadLength() == 0)
218       return;
219
220    // Then, Print each SQ Item   
221    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
222    {
223       (*cc)->SetPrintLevel(PrintLevel);
224       (*cc)->Print(os);   
225    }
226
227    // at end, print the sequence terminator item, if any
228    if (DelimitorMode)
229    {
230       int i;
231       for ( i = 0; i < SQDepthLevel; i++ )
232          os << "   | " ;
233       os << " --- "  << std::endl;
234       for ( i = 0; i < SQDepthLevel; i++ )
235          os << "   | " ;
236       if (SeqTerm != NULL)
237       {
238          SeqTerm->SetPrintLevel(PrintLevel);
239          SeqTerm->Print(os);
240          os << std::endl;
241       } 
242       else 
243       {
244          // fuse
245          gdcmWarningMacro("  -------- should have a sequence terminator item");
246       }
247    }
248 }
249
250 //-----------------------------------------------------------------------------
251 } // end namespace gdcm