]> Creatis software - gdcm.git/blob - src/gdcmSeqEntry.cxx
- now, DicomDir stuff stakes into account the 'new' structure
[gdcm.git] / src / gdcmSeqEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/08/26 15:29:53 $
7   Version:   $Revision: 1.23 $
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 = e->GetDepthLevel();
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 = 0x00000000;
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     //we force the writting of an Item Delimitation item
121     // because we wrote the Item as a 'no Length' item
122       fwrite ( &item_term_gr,(size_t)2 ,(size_t)1 ,fp);
123       fwrite ( &item_term_el,(size_t)2 ,(size_t)1 ,fp);   
124       fwrite ( &seq_term_lg, (size_t)4 ,(size_t)1 ,fp); // Heu .....
125    }
126    
127     //we force the writting of a Sequence Delimitation item
128     // because we wrote the Sequence as a 'no Length' sequence
129    fwrite ( &seq_term_gr,(size_t)2 ,(size_t)1 ,fp);
130    fwrite ( &seq_term_el,(size_t)2 ,(size_t)1 ,fp);
131    fwrite ( &seq_term_lg,(size_t)4 ,(size_t)1 ,fp); 
132 }
133
134 //-----------------------------------------------------------------------------
135 // Public
136
137  /// \brief   adds the passed ITEM to the ITEM chained List for this SeQuence.      
138 void gdcmSeqEntry::AddEntry(gdcmSQItem *sqItem, int itemNumber) {
139    sqItem->SetSQItemNumber(itemNumber);
140    items.push_back(sqItem);
141 }
142
143 /// \brief Sets the depth level of a Sequence Entry embedded in a SeQuence 
144 void gdcmSeqEntry::SetDepthLevel(int depth) {
145    SQDepthLevel = depth;
146 }
147
148 /// \brief return a pointer to the SQItem referenced by its ordinal number
149 /// (returns the first one if ordinal number is <0
150 ///  returns the last  one if ordinal number is > item number
151
152 gdcmSQItem *gdcmSeqEntry::GetSQItemByOrdinalNumber(int nb) {
153    if (nb<0)
154       return (*(items.begin()));
155    int count = 0 ;
156    for(ListSQItem::iterator cc = items.begin();
157        cc != items.end();
158        count ++, ++cc){
159       if (count==nb)
160          return *cc;
161    }
162    return (*(items.end())); // Euhhhhh ?!? Is this the last one . FIXME
163 }
164 //-----------------------------------------------------------------------------
165 // Protected
166
167
168 //-----------------------------------------------------------------------------
169 // Private
170
171 //-----------------------------------------------------------------------------