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