]> Creatis software - gdcm.git/blob - src/gdcmSeqEntry.cxx
a0fc0bcf4e599f6c1ed8cc9b6239786fbdd4581e
[gdcm.git] / src / gdcmSeqEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/17 11:13:21 $
7   Version:   $Revision: 1.44 $
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  * \ingroup SeqEntry
37  * \brief   Constructor from a given SeqEntry
38  */
39 SeqEntry::SeqEntry( DictEntry *e ) 
40              : DocEntry(e)
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 SeqEntry
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 {
58    Length       = 0;
59    ReadLength   = 0xffffffff;
60    SQDepthLevel = depth;
61
62    ImplicitVR   = e->IsImplicitVR();
63    Offset       = e->GetOffset();
64    SeqTerm = NULL;
65 }
66
67 /**
68  * \brief   Canonical destructor.
69  */
70 SeqEntry::~SeqEntry()
71 {
72    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
73    {
74       delete *cc;
75    }
76    if (SeqTerm)
77    {
78       delete SeqTerm;
79    }
80 }
81
82 /**
83  * \brief   canonical Printer
84  */
85 void SeqEntry::Print( std::ostream &os, std::string const & )
86 {
87    // First, Print the Dicom Element itself.
88    os << "S ";
89    DocEntry::Print(os);
90    os << std::endl;
91
92    if (GetReadLength() == 0)
93       return;
94
95    // Then, Print each SQ Item   
96    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
97    {
98       (*cc)->SetPrintLevel(PrintLevel);
99       (*cc)->Print(os);   
100    }
101
102    // at end, print the sequence terminator item, if any
103    if (DelimitorMode)
104    {
105       for ( int i = 0; i < SQDepthLevel; i++ )
106       {
107          os << "   | " ;
108       }
109       if (SeqTerm != NULL)
110       {
111          SeqTerm->SetPrintLevel(PrintLevel);
112          SeqTerm->Print(os);
113          os << std::endl;
114       } 
115       else 
116       {
117          // fuse
118          gdcmVerboseMacro("  -------- should have a sequence terminator item");
119       }
120    }                    
121 }
122
123 /*
124  * \brief   canonical Writer
125  */
126 void SeqEntry::WriteContent(std::ofstream *fp, FileType filetype)
127 {
128    uint16_t seq_term_gr = 0xfffe;
129    uint16_t seq_term_el = 0xe0dd;
130    uint32_t seq_term_lg = 0xffffffff;
131
132    //uint16_t item_term_gr = 0xfffe;
133    //uint16_t item_term_el = 0xe00d;
134    
135    DocEntry::WriteContent(fp, filetype);
136    for(ListSQItem::iterator cc  = Items.begin();
137                             cc != Items.end();
138                           ++cc)
139    {        
140       (*cc)->WriteContent(fp, filetype);
141    }
142    
143    // we force the writting of a Sequence Delimitation item
144    // because we wrote the Sequence as a 'no Length' sequence
145    binary_write(*fp, seq_term_gr);
146    binary_write(*fp, seq_term_el);
147    binary_write(*fp, seq_term_lg);
148 }
149
150 //-----------------------------------------------------------------------------
151 // Public
152
153 /// \brief   adds the passed ITEM to the ITEM chained List for this SeQuence.
154 void SeqEntry::AddEntry(SQItem *sqItem, int itemNumber)
155 {
156    sqItem->SetSQItemNumber(itemNumber);
157    Items.push_back(sqItem);
158 }
159
160 /**
161  * \brief return a pointer to the SQItem referenced by its ordinal number.
162  *        Returns the first item when argument is negative.
163  *        Returns the last item when argument is bigget than the total
164  *        item number.
165  */
166 SQItem *SeqEntry::GetSQItemByOrdinalNumber(int nb)
167 {
168    if (nb<0)
169    {
170       return *(Items.begin());
171    }
172    int count = 0 ;
173    for(ListSQItem::iterator cc = Items.begin();
174                            cc != Items.end();
175                            count ++, ++cc)
176    {
177       if (count == nb)
178       {
179          return *cc;
180       }
181    }
182    return *(Items.end()); // Euhhhhh ?!? Is this the last one . FIXME
183 }
184 //-----------------------------------------------------------------------------
185 // Protected
186
187
188 //-----------------------------------------------------------------------------
189 // Private
190
191 //-----------------------------------------------------------------------------
192 } // end namespace gdcm
193