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