]> Creatis software - gdcm.git/blob - src/gdcmSeqEntry.cxx
2a7d748d3de136437ca9bbad6a4a9ffe7de5edfa
[gdcm.git] / src / gdcmSeqEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/19 12:44:00 $
7   Version:   $Revision: 1.36 $
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
25 #include <iostream>
26 #include <iomanip>
27 #include <fstream>
28
29 namespace gdcm 
30 {
31
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 /**
35  * \ingroup SeqEntry
36  * \brief   Constructor from a given SeqEntry
37  */
38 SeqEntry::SeqEntry( DictEntry* e ) 
39              : DocEntry(e)
40 {
41    UsableLength = 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    UsableLength = 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  * \brief   canonical Printer
83  */
84 void SeqEntry::Print( std::ostream &os )
85 {
86    // First, Print the Dicom Element itself.
87    SetPrintLevel(2);   
88    DocEntry::Print(os);
89    os << std::endl;
90
91    if (GetReadLength() == 0)
92       return;
93
94    // Then, Print each SQ Item   
95    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
96    {
97       (*cc)->Print(os);   
98    }
99
100    // at end, print the sequence terminator item, if any
101    if (DelimitorMode)
102    {
103       for ( int i = 0; i < SQDepthLevel; i++ )
104       {
105          os << "   | " ;
106       }
107       if (SeqTerm != NULL)
108       {
109          SeqTerm->Print(os);
110          os << std::endl;
111       } 
112       else 
113       {
114          // fuse
115          os << "      -------------- should have a sequence terminator item";
116       }
117    }                    
118 }
119
120 /*
121  * \brief   canonical Writer
122  */
123 void SeqEntry::Write(std::ofstream* fp, FileType filetype)
124 {
125    uint16_t seq_term_gr = 0xfffe;
126    uint16_t seq_term_el = 0xe0dd;
127    uint32_t seq_term_lg = 0xffffffff;
128
129    //uint16_t item_term_gr = 0xfffe;
130    //uint16_t item_term_el = 0xe00d;
131    
132    DocEntry::Write(fp, filetype);
133    for(ListSQItem::iterator cc  = Items.begin();
134                             cc != Items.end();
135                           ++cc)
136    {        
137       (*cc)->Write(fp, filetype);
138    }
139    
140    // we force the writting of a Sequence Delimitation item
141    // because we wrote the Sequence as a 'no Length' sequence
142    binary_write(*fp, seq_term_gr);
143    binary_write(*fp, seq_term_el);
144    binary_write(*fp, seq_term_lg);
145 }
146
147 //-----------------------------------------------------------------------------
148 // Public
149
150 /// \brief   adds the passed ITEM to the ITEM chained List for this SeQuence.
151 void SeqEntry::AddEntry(SQItem *sqItem, int itemNumber)
152 {
153    sqItem->SetSQItemNumber(itemNumber);
154    Items.push_back(sqItem);
155 }
156
157 /**
158  * \brief return a pointer to the SQItem referenced by its ordinal number.
159  *        Returns the first item when argument is negative.
160  *        Returns the last item when argument is bigget than the total
161  *        item number.
162  */
163 SQItem* SeqEntry::GetSQItemByOrdinalNumber(int nb)
164 {
165    if (nb<0)
166    {
167       return *(Items.begin());
168    }
169    int count = 0 ;
170    for(ListSQItem::iterator cc = Items.begin();
171                            cc != Items.end();
172                            count ++, ++cc)
173    {
174       if (count == nb)
175       {
176          return *cc;
177       }
178    }
179    return *(Items.end()); // Euhhhhh ?!? Is this the last one . FIXME
180 }
181 //-----------------------------------------------------------------------------
182 // Protected
183
184
185 //-----------------------------------------------------------------------------
186 // Private
187
188 //-----------------------------------------------------------------------------
189 } // end namespace gdcm
190