]> Creatis software - gdcm.git/blob - src/gdcmSeqEntry.cxx
* src/*.cxx *.h Reference to License.htm fixed to License.html.
[gdcm.git] / src / gdcmSeqEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/09/27 08:39:07 $
7   Version:   $Revision: 1.30 $
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 //-----------------------------------------------------------------------------
28 // Constructor / Destructor
29 /**
30  * \ingroup gdcmSeqEntry
31  * \brief   Constructor from a given gdcmSeqEntry
32  */
33 gdcmSeqEntry::gdcmSeqEntry( gdcmDictEntry* e ) 
34              : gdcmDocEntry(e)
35 {
36    UsableLength = 0;
37    ReadLength = 0xffffffff;
38    SQDepthLevel = -1;
39
40    delimitor_mode = false;
41    seq_term  = NULL;
42 }
43
44 /**
45  * \brief   Constructor from a given gdcmSeqEntry
46  * @param   e Pointer to existing Doc entry
47  * @param   depth depth level of the current Seq entry
48   */
49 gdcmSeqEntry::gdcmSeqEntry( gdcmDocEntry* e, int depth )
50              : gdcmDocEntry( e->GetDictEntry() )
51 {
52    this->UsableLength = 0;
53    this->ReadLength   = 0xffffffff;
54    SQDepthLevel = depth;
55
56    this->ImplicitVR   = e->IsImplicitVR();
57    this->Offset       = e->GetOffset();
58 }
59
60 /**
61  * \brief   Canonical destructor.
62  */
63 gdcmSeqEntry::~gdcmSeqEntry() {
64    for(ListSQItem::iterator cc = items.begin();cc != items.end();++cc)
65    {
66       delete *cc;
67    }
68    if (!seq_term)
69       delete seq_term;
70 }
71
72 /**
73  * \brief   canonical Printer
74  */
75 void gdcmSeqEntry::Print( std::ostream &os )
76 {
77    // First, Print the Dicom Element itself.
78    SetPrintLevel(2);   
79    gdcmDocEntry::Print(os);
80    os << std::endl;
81
82    if (GetReadLength() == 0)
83       return;
84
85    // Then, Print each SQ Item   
86    for(ListSQItem::iterator cc = items.begin();cc != items.end();++cc)
87    {
88       (*cc)->Print(os);   
89    }
90
91    // at end, print the sequence terminator item, if any
92    if (delimitor_mode) {
93       for ( int i = 0; i < SQDepthLevel; i++ )
94       {
95          os << "   | " ;
96       }
97       if (seq_term != NULL)
98       {
99          seq_term->Print(os);
100          os << std::endl;
101       } 
102       else 
103       {
104          // fuse
105          os << "      -------------- should have a sequence terminator item";
106       }
107    }                    
108 }
109
110 /*
111  * \brief   canonical Writer
112  */
113 void gdcmSeqEntry::Write(FILE* fp, FileType filetype)
114 {
115    uint16_t seq_term_gr = 0xfffe;
116    uint16_t seq_term_el = 0xe0dd;
117    uint32_t seq_term_lg = 0xffffffff;
118
119    //uint16_t item_term_gr = 0xfffe;
120    //uint16_t item_term_el = 0xe00d;
121    
122    gdcmDocEntry::Write(fp, filetype);
123    for(ListSQItem::iterator cc  = GetSQItems().begin();
124                             cc != GetSQItems().end();
125                           ++cc)
126    {        
127       (*cc)->Write(fp, filetype);
128    }
129    
130    // we force the writting of a Sequence Delimitation item
131    // because we wrote the Sequence as a 'no Length' sequence
132    fwrite ( &seq_term_gr,(size_t)2 ,(size_t)1 ,fp);
133    fwrite ( &seq_term_el,(size_t)2 ,(size_t)1 ,fp);
134    fwrite ( &seq_term_lg,(size_t)4 ,(size_t)1 ,fp); 
135 }
136
137 //-----------------------------------------------------------------------------
138 // Public
139
140 /// \brief   adds the passed ITEM to the ITEM chained List for this SeQuence.
141 void gdcmSeqEntry::AddEntry(gdcmSQItem *sqItem, int itemNumber)
142 {
143    sqItem->SetSQItemNumber(itemNumber);
144    items.push_back(sqItem);
145 }
146
147 /**
148  * \brief return a pointer to the SQItem referenced by its ordinal number.
149  *        Returns the first item when argument is negative.
150  *        Returns the last item when argument is bigget than the total
151  *        item number.
152  */
153 gdcmSQItem* gdcmSeqEntry::GetSQItemByOrdinalNumber(int nb)
154 {
155    if (nb<0)
156       return (*(items.begin()));
157    int count = 0 ;
158    for(ListSQItem::iterator cc = items.begin();
159        cc != items.end();
160        count ++, ++cc){
161       if (count==nb)
162          return *cc;
163    }
164    return (*(items.end())); // Euhhhhh ?!? Is this the last one . FIXME
165 }
166 //-----------------------------------------------------------------------------
167 // Protected
168
169
170 //-----------------------------------------------------------------------------
171 // Private
172
173 //-----------------------------------------------------------------------------