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