00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "gdcmSQItem.h"
00020 #include "gdcmSeqEntry.h"
00021 #include "gdcmGlobal.h"
00022 #include "gdcmDictSet.h"
00023 #include "gdcmUtil.h"
00024 #include "gdcmDebug.h"
00025
00026 #include <fstream>
00027
00028 namespace GDCM_NAME_SPACE
00029 {
00030
00031
00035 SQItem::SQItem(int depthLevel )
00036 : DocEntrySet( )
00037 {
00038 SQDepthLevel = depthLevel;
00039 SQItemNumber = 0;
00040 }
00041
00045 SQItem::~SQItem()
00046 {
00047 ClearEntry();
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057 void SQItem::WriteContent(std::ofstream *fp, FileType filetype,
00058 bool insideMetaElements)
00059 {
00060 int j;
00061 uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
00062 uint16_t itemt[4]= { 0xfffe, 0xe00d, 0x0000, 0x0000 };
00063
00064
00065
00066 for(j=0;j<4;++j)
00067 {
00068 binary_write( *fp, item[j]);
00069 }
00070
00071 for (ListDocEntry::iterator it = DocEntries.begin();
00072 it != DocEntries.end();
00073 ++it)
00074 {
00075
00076
00077 if ( (*it)->GetGroup() == 0xfffe )
00078 {
00079 continue;
00080 }
00081
00082
00083
00084
00085 if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
00086 {
00087 break;
00088
00089
00090 }
00091
00092 (*it)->WriteContent(fp, filetype, false);
00093 }
00094
00095
00096
00097 for(j=0;j<4;++j)
00098 {
00099 binary_write( *fp, itemt[j]);
00100 }
00101 }
00102
00107 uint32_t SQItem::ComputeFullLength()
00108 {
00109 uint32_t l = 8;
00110 for (ListDocEntry::iterator it = DocEntries.begin();
00111 it != DocEntries.end();
00112 ++it)
00113 {
00114
00115
00116 if ( (*it)->GetGroup() == 0xfffe )
00117 {
00118 continue;
00119 }
00120 l += (*it)->ComputeFullLength();
00121 }
00122 l += 8;
00123 return l;
00124 }
00125
00132 bool SQItem::AddEntry(DocEntry *entry)
00133 {
00134 if (DocEntries.empty() )
00135 {
00136 DocEntries.push_back(entry);
00137 entry->Register();
00138 return true;
00139 }
00140
00141 ListDocEntry::iterator insertSpot;
00142 ListDocEntry::iterator it = DocEntries.end();
00143 do
00144 {
00145 it--;
00146
00147 if ( (*it)->IsItemDelimitor() )
00148 {
00149 continue;
00150 }
00151 if ( (*it)->GetGroup() < entry->GetGroup() )
00152 break;
00153 else
00154 if ( (*it)->GetGroup() == entry->GetGroup() &&
00155 (*it)->GetElement() < entry->GetElement() )
00156 break;
00157 } while (it != DocEntries.begin() );
00158
00159 ++it;
00160 insertSpot = it;
00161 DocEntries.insert(insertSpot, entry);
00162 entry->Register();
00163 return true;
00164 }
00165
00171 bool SQItem::RemoveEntry( DocEntry *entryToRemove )
00172 {
00173 for(ListDocEntry::iterator it = DocEntries.begin();
00174 it != DocEntries.end();
00175 ++it)
00176 {
00177 if ( *it == entryToRemove )
00178 {
00179 DocEntries.erase(it);
00180 gdcmDebugMacro( "One element erased: " << entryToRemove->GetKey() );
00181 entryToRemove->Unregister();
00182 return true;
00183 }
00184 }
00185 gdcmWarningMacro( "Entry not found: " << entryToRemove->GetKey() );
00186 return false ;
00187 }
00188
00192 void SQItem::ClearEntry()
00193 {
00194 for(ListDocEntry::iterator cc = DocEntries.begin();
00195 cc != DocEntries.end();
00196 ++cc)
00197 {
00198 (*cc)->Unregister();
00199 }
00200 DocEntries.clear();
00201 }
00202
00207 DocEntry *SQItem::GetFirstEntry()
00208 {
00209 ItDocEntries = DocEntries.begin();
00210 if ( ItDocEntries != DocEntries.end() )
00211 return *ItDocEntries;
00212 return 0;
00213 }
00214
00219 DocEntry *SQItem::GetNextEntry()
00220 {
00221 ++ItDocEntries;
00222 if ( ItDocEntries != DocEntries.end() )
00223 return *ItDocEntries;
00224 return NULL;
00225 }
00226
00233 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
00234 {
00235 for(ListDocEntry::iterator i = DocEntries.begin();
00236 i != DocEntries.end();
00237 ++i)
00238 {
00239 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
00240 return *i;
00241 }
00242 return NULL;
00243 }
00244
00250 void SQItem::Copy(DocEntrySet *set)
00251 {
00252
00253 ClearEntry();
00254
00255 DocEntrySet::Copy(set);
00256
00257 SQItem *sq = dynamic_cast<SQItem *>(set);
00258 if( sq )
00259 {
00260 SQDepthLevel = sq->SQDepthLevel;
00261 SQItemNumber = sq->SQItemNumber;
00262
00263 DocEntries = sq->DocEntries;
00264 for(ItDocEntries = DocEntries.begin();ItDocEntries != DocEntries.end();++ItDocEntries)
00265 (*ItDocEntries)->Register();
00266 }
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 void SQItem::Print(std::ostream &os, std::string const &)
00283 {
00284 std::ostringstream s;
00285
00286 if (SQDepthLevel > 0)
00287 {
00288 for (int i = 0; i < SQDepthLevel; ++i)
00289 {
00290 s << " | " ;
00291 }
00292 }
00293 os << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
00294 for (ListDocEntry::iterator i = DocEntries.begin();
00295 i != DocEntries.end();
00296 ++i)
00297 {
00298 DocEntry *Entry = *i;
00299 bool PrintEndLine = true;
00300
00301 os << s.str();
00302 Entry->SetPrintLevel(PrintLevel);
00303 Entry->Print(os);
00304 if ( dynamic_cast<SeqEntry*>(Entry) )
00305 {
00306 PrintEndLine = false;
00307 }
00308 if (PrintEndLine)
00309 {
00310 os << std::endl;
00311 }
00312 }
00313 }
00314
00315
00316 }