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