]> Creatis software - gdcm.git/blob - src/gdcmSeqEntry.cxx
9f2c4d195f3f622047720f68595af5b92c86cbf4
[gdcm.git] / src / gdcmSeqEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/07 16:45:52 $
7   Version:   $Revision: 1.42 $
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    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  * \brief   canonical Printer
83  */
84 void SeqEntry::Print( std::ostream &os )
85 {
86    // First, Print the Dicom Element itself.
87    os << "S ";
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)->SetPrintLevel(PrintLevel);
98       (*cc)->Print(os);   
99    }
100
101    // at end, print the sequence terminator item, if any
102    if (DelimitorMode)
103    {
104       for ( int i = 0; i < SQDepthLevel; i++ )
105       {
106          os << "   | " ;
107       }
108       if (SeqTerm != NULL)
109       {
110          SeqTerm->SetPrintLevel(PrintLevel);
111          SeqTerm->Print(os);
112          os << std::endl;
113       } 
114       else 
115       {
116          // fuse
117          os << "      -------------- should have a sequence terminator item";
118       }
119    }                    
120 }
121
122 /*
123  * \brief   canonical Writer
124  */
125 void SeqEntry::WriteContent(std::ofstream *fp, FileType filetype)
126 {
127    uint16_t seq_term_gr = 0xfffe;
128    uint16_t seq_term_el = 0xe0dd;
129    uint32_t seq_term_lg = 0xffffffff;
130
131    //uint16_t item_term_gr = 0xfffe;
132    //uint16_t item_term_el = 0xe00d;
133    
134    DocEntry::WriteContent(fp, filetype);
135    for(ListSQItem::iterator cc  = Items.begin();
136                             cc != Items.end();
137                           ++cc)
138    {        
139       (*cc)->WriteContent(fp, filetype);
140    }
141    
142    // we force the writting of a Sequence Delimitation item
143    // because we wrote the Sequence as a 'no Length' sequence
144    binary_write(*fp, seq_term_gr);
145    binary_write(*fp, seq_term_el);
146    binary_write(*fp, seq_term_lg);
147 }
148
149 //-----------------------------------------------------------------------------
150 // Public
151
152 /// \brief   adds the passed ITEM to the ITEM chained List for this SeQuence.
153 void SeqEntry::AddEntry(SQItem *sqItem, int itemNumber)
154 {
155    sqItem->SetSQItemNumber(itemNumber);
156    Items.push_back(sqItem);
157 }
158
159 /**
160  * \brief return a pointer to the SQItem referenced by its ordinal number.
161  *        Returns the first item when argument is negative.
162  *        Returns the last item when argument is bigget than the total
163  *        item number.
164  */
165 SQItem *SeqEntry::GetSQItemByOrdinalNumber(int nb)
166 {
167    if (nb<0)
168    {
169       return *(Items.begin());
170    }
171    int count = 0 ;
172    for(ListSQItem::iterator cc = Items.begin();
173                            cc != Items.end();
174                            count ++, ++cc)
175    {
176       if (count == nb)
177       {
178          return *cc;
179       }
180    }
181    return *(Items.end()); // Euhhhhh ?!? Is this the last one . FIXME
182 }
183 //-----------------------------------------------------------------------------
184 // Protected
185
186
187 //-----------------------------------------------------------------------------
188 // Private
189
190 //-----------------------------------------------------------------------------
191 } // end namespace gdcm
192