]> Creatis software - gdcm.git/blob - src/gdcmSeqEntry.cxx
* Preparation of writing a gdcmHeader iterator: generalisation of gdcmTagKey
[gdcm.git] / src / gdcmSeqEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/09/13 12:10:53 $
7   Version:   $Revision: 1.27 $
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    UsableLength = 0;
37    ReadLength = 0xffffffff;
38    SQDepthLevel = depth;
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+1;i++)
94          os << "   | " ;
95       if (seq_term != NULL) {
96          seq_term->Print(os);
97          os << std::endl;
98       } 
99       else 
100          // fusible
101          os << "      -------------- should have a sequence terminator item";
102    }                    
103 }
104
105 /*
106  * \brief   canonical Writer
107  */
108 void gdcmSeqEntry::Write(FILE *fp, FileType filetype)
109 {
110    uint16_t seq_term_gr = 0xfffe;
111    uint16_t seq_term_el = 0xe0dd;
112    uint32_t seq_term_lg = 0xffffffff;
113
114    //uint16_t item_term_gr = 0xfffe;
115    //uint16_t item_term_el = 0xe00d;
116    
117    gdcmDocEntry::Write(fp, filetype);
118    for(ListSQItem::iterator cc  = GetSQItems().begin();
119                             cc != GetSQItems().end();
120                           ++cc)
121    {        
122       (*cc)->Write(fp, filetype);
123    }
124    
125    // we force the writting of a Sequence Delimitation item
126    // because we wrote the Sequence as a 'no Length' sequence
127    fwrite ( &seq_term_gr,(size_t)2 ,(size_t)1 ,fp);
128    fwrite ( &seq_term_el,(size_t)2 ,(size_t)1 ,fp);
129    fwrite ( &seq_term_lg,(size_t)4 ,(size_t)1 ,fp); 
130 }
131
132 //-----------------------------------------------------------------------------
133 // Public
134
135 /// \brief   adds the passed ITEM to the ITEM chained List for this SeQuence.
136 void gdcmSeqEntry::AddEntry(gdcmSQItem *sqItem, int itemNumber)
137 {
138    sqItem->SetSQItemNumber(itemNumber);
139    items.push_back(sqItem);
140 }
141
142 /**
143  * \brief return a pointer to the SQItem referenced by its ordinal number.
144  *        Returns the first item when argument is negative.
145  *        Returns the last item when argument is bigget than the total
146  *        item number.
147  */
148 gdcmSQItem *gdcmSeqEntry::GetSQItemByOrdinalNumber(int nb)
149 {
150    if (nb<0)
151       return (*(items.begin()));
152    int count = 0 ;
153    for(ListSQItem::iterator cc = items.begin();
154        cc != items.end();
155        count ++, ++cc){
156       if (count==nb)
157          return *cc;
158    }
159    return (*(items.end())); // Euhhhhh ?!? Is this the last one . FIXME
160 }
161 //-----------------------------------------------------------------------------
162 // Protected
163
164
165 //-----------------------------------------------------------------------------
166 // Private
167
168 //-----------------------------------------------------------------------------