00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "gdcmSeqEntry.h"
00020 #include "gdcmSQItem.h"
00021 #include "gdcmTS.h"
00022 #include "gdcmGlobal.h"
00023 #include "gdcmUtil.h"
00024 #include "gdcmDebug.h"
00025
00026 #include <iostream>
00027 #include <iomanip>
00028 #include <fstream>
00029
00030 namespace gdcm
00031 {
00032
00033
00037 SeqEntry::SeqEntry( DictEntry *e )
00038 : DocEntry(e)
00039 {
00040 Length = 0;
00041 ReadLength = 0xffffffff;
00042 SQDepthLevel = -1;
00043
00044 DelimitorMode = false;
00045 SeqTerm = NULL;
00046 }
00047
00053 SeqEntry::SeqEntry( DocEntry *e, int depth )
00054 : DocEntry( e->GetDictEntry() )
00055 {
00056 Length = 0;
00057 ReadLength = 0xffffffff;
00058 SQDepthLevel = depth;
00059
00060 ImplicitVR = e->IsImplicitVR();
00061 Offset = e->GetOffset();
00062 SeqTerm = NULL;
00063 }
00064
00068 SeqEntry::~SeqEntry()
00069 {
00070 ClearSQItem();
00071 }
00072
00073
00074
00075
00076
00077
00078
00079
00080 void SeqEntry::WriteContent(std::ofstream *fp, FileType filetype)
00081 {
00082 uint16_t seq_term_gr = 0xfffe;
00083 uint16_t seq_term_el = 0xe0dd;
00084 uint32_t seq_term_lg = 0x00000000;
00085
00086
00087 if ( GetReadLength() == 0 )
00088 return;
00089
00090 DocEntry::WriteContent(fp, filetype);
00091 for(ListSQItem::iterator cc = Items.begin();
00092 cc != Items.end();
00093 ++cc)
00094 {
00095 (*cc)->WriteContent(fp, filetype);
00096 }
00097
00098
00099
00100 binary_write(*fp, seq_term_gr);
00101 binary_write(*fp, seq_term_el);
00102 binary_write(*fp, seq_term_lg);
00103 }
00104
00109 uint32_t SeqEntry::ComputeFullLength()
00110 {
00111 uint32_t l = 12;
00112 for(ListSQItem::iterator cc = Items.begin();
00113 cc != Items.end();
00114 ++cc)
00115 {
00116 l += (*cc)->ComputeFullLength();
00117 }
00118 l += 8;
00119 return l;
00120 }
00121
00128 void SeqEntry::AddSQItem(SQItem *sqItem, int itemNumber)
00129 {
00130
00131
00132
00133
00134
00135 sqItem->SetSQItemNumber(itemNumber);
00136 Items.push_back(sqItem);
00137 sqItem->Register();
00138 }
00139
00143 void SeqEntry::ClearSQItem()
00144 {
00145 for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
00146 {
00147 (*cc)->Unregister();
00148 }
00149 if (SeqTerm)
00150 {
00151 SeqTerm->Unregister();
00152 }
00153 }
00154
00159 SQItem *SeqEntry::GetFirstSQItem()
00160 {
00161 ItSQItem = Items.begin();
00162 if (ItSQItem != Items.end())
00163 return *ItSQItem;
00164 return NULL;
00165 }
00166
00173 SQItem *SeqEntry::GetNextSQItem()
00174 {
00175 gdcmAssertMacro (ItSQItem != Items.end())
00176 {
00177 ++ItSQItem;
00178 if (ItSQItem != Items.end())
00179 return *ItSQItem;
00180 }
00181 return NULL;
00182 }
00183
00190 SQItem *SeqEntry::GetSQItem(int nb)
00191 {
00192 if (nb<0)
00193 {
00194 return *(Items.begin());
00195 }
00196 int count = 0 ;
00197 for(ListSQItem::iterator cc = Items.begin();
00198 cc != Items.end();
00199 count ++, ++cc)
00200 {
00201 if (count == nb)
00202 {
00203 return *cc;
00204 }
00205 }
00206 return *(Items.end());
00207 }
00208
00212 unsigned int SeqEntry::GetNumberOfSQItems()
00213 {
00214 return Items.size();
00215 }
00216
00221 void SeqEntry::SetDelimitationItem(DocEntry *e)
00222 {
00223 if( SeqTerm != e )
00224 {
00225 if( SeqTerm )
00226 SeqTerm->Unregister();
00227 SeqTerm = e;
00228 if( SeqTerm )
00229 SeqTerm->Register();
00230 }
00231 }
00232
00238 void SeqEntry::Copy(DocEntry *doc)
00239 {
00240
00241 ClearSQItem();
00242
00243 DocEntry::Copy(doc);
00244 SeqEntry *entry = dynamic_cast<SeqEntry *>(doc);
00245 if ( entry )
00246 {
00247 DelimitorMode = entry->DelimitorMode;
00248 SQDepthLevel = entry->SQDepthLevel;
00249
00250 SeqTerm = entry->SeqTerm;
00251 if(SeqTerm)
00252 SeqTerm->Register();
00253 Items = entry->Items;
00254 for(ItSQItem = Items.begin();ItSQItem != Items.end(); ++ItSQItem)
00255 {
00256 (*ItSQItem)->Register();
00257 }
00258 }
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00272 void SeqEntry::Print( std::ostream &os, std::string const & )
00273 {
00274
00275 os << "S ";
00276 DocEntry::Print(os);
00277 os << std::endl;
00278
00279 if (GetReadLength() == 0)
00280 return;
00281
00282
00283 for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
00284 {
00285 (*cc)->SetPrintLevel(PrintLevel);
00286 (*cc)->Print(os);
00287 }
00288
00289
00290 if (DelimitorMode)
00291 {
00292 int i;
00293 for ( i = 0; i < SQDepthLevel; i++ )
00294 os << " | " ;
00295 os << " --- " << std::endl;
00296 for ( i = 0; i < SQDepthLevel; i++ )
00297 os << " | " ;
00298 if (SeqTerm != NULL)
00299 {
00300 SeqTerm->SetPrintLevel(PrintLevel);
00301 SeqTerm->Print(os);
00302 os << std::endl;
00303 }
00304 else
00305 {
00306
00307 gdcmWarningMacro(" -------- should have a sequence terminator item");
00308 }
00309 }
00310 }
00311
00312
00313 }