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