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 "gdcmValEntry.h"
00022 #include "gdcmBinEntry.h"
00023 #include "gdcmGlobal.h"
00024 #include "gdcmDictSet.h"
00025 #include "gdcmUtil.h"
00026 #include "gdcmDebug.h"
00027
00028 #include <fstream>
00029
00030 namespace gdcm
00031 {
00032
00033
00037 SQItem::SQItem(int depthLevel )
00038 : DocEntrySet( )
00039 {
00040 SQDepthLevel = depthLevel;
00041 SQItemNumber = 0;
00042 }
00043
00047 SQItem::~SQItem()
00048 {
00049 ClearEntry();
00050 }
00051
00052
00053
00054
00055
00056
00057
00058
00059 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
00060 {
00061 int j;
00062 uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
00063 uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
00064
00065
00066
00067 for(j=0;j<4;++j)
00068 {
00069 binary_write( *fp, item[j]);
00070 }
00071
00072 for (ListDocEntry::iterator it = DocEntries.begin();
00073 it != DocEntries.end();
00074 ++it)
00075 {
00076
00077
00078 if ( (*it)->GetGroup() == 0xfffe )
00079 {
00080 continue;
00081 }
00082
00083
00084
00085
00086 if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
00087 {
00088 break;
00089 }
00090
00091 (*it)->WriteContent(fp, filetype);
00092 }
00093
00094
00095
00096 for(j=0;j<4;++j)
00097 {
00098 binary_write( *fp, itemt[j]);
00099 }
00100 }
00101
00107 bool SQItem::AddEntry(DocEntry *entry)
00108 {
00109 if (DocEntries.empty() )
00110 {
00111 DocEntries.push_back(entry);
00112 return true;
00113 }
00114
00115 ListDocEntry::iterator insertSpot;
00116 ListDocEntry::iterator it = DocEntries.end();
00117 do
00118 {
00119 it--;
00120
00121 if ( (*it)->IsItemDelimitor() )
00122 {
00123 continue;
00124 }
00125 if ( (*it)->GetGroup() < entry->GetGroup() )
00126 break;
00127 else
00128 if ( (*it)->GetGroup() == entry->GetGroup() &&
00129 (*it)->GetElement() < entry->GetElement() )
00130 break;
00131 } while (it != DocEntries.begin() );
00132
00133 insertSpot = it++;
00134 insertSpot++;
00135 DocEntries.insert(insertSpot, entry);
00136 return true;
00137 }
00138
00144 bool SQItem::RemoveEntry( DocEntry *entryToRemove )
00145 {
00146 for(ListDocEntry::iterator it = DocEntries.begin();
00147 it != DocEntries.end();
00148 ++it)
00149 {
00150 if( *it == entryToRemove )
00151 {
00152 DocEntries.erase(it);
00153 gdcmWarningMacro( "One element erased: " << entryToRemove->GetKey() );
00154 delete entryToRemove;
00155 return true;
00156 }
00157 }
00158 gdcmWarningMacro( "Entry not found: " << entryToRemove->GetKey() );
00159 return false ;
00160 }
00161
00167 bool SQItem::RemoveEntryNoDestroy(DocEntry *entryToRemove)
00168 {
00169 for(ListDocEntry::iterator it = DocEntries.begin();
00170 it != DocEntries.end();
00171 ++it)
00172 {
00173 if( *it == entryToRemove )
00174 {
00175 DocEntries.erase(it);
00176 gdcmWarningMacro( "One element erased, no destroyed: "
00177 << entryToRemove->GetKey() );
00178 return true;
00179 }
00180 }
00181
00182 gdcmWarningMacro( "Entry not found:" << entryToRemove->GetKey() );
00183 return false ;
00184 }
00185
00189 void SQItem::ClearEntry()
00190 {
00191 for(ListDocEntry::iterator cc = DocEntries.begin();
00192 cc != DocEntries.end();
00193 ++cc)
00194 {
00195 delete *cc;
00196 }
00197 DocEntries.clear();
00198 }
00199
00204 DocEntry *SQItem::GetFirstEntry()
00205 {
00206 ItDocEntries = DocEntries.begin();
00207 if( ItDocEntries != DocEntries.end() )
00208 return *ItDocEntries;
00209 return 0;
00210 }
00211
00216 DocEntry *SQItem::GetNextEntry()
00217 {
00218 ++ItDocEntries;
00219 if( ItDocEntries != DocEntries.end() )
00220 return *ItDocEntries;
00221 return NULL;
00222 }
00223
00230 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
00231 {
00232 for(ListDocEntry::iterator i = DocEntries.begin();
00233 i != DocEntries.end();
00234 ++i)
00235 {
00236 if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
00237 return *i;
00238 }
00239 return NULL;
00240 }
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 void SQItem::Print(std::ostream &os, std::string const &)
00256 {
00257 std::ostringstream s;
00258
00259 if (SQDepthLevel > 0)
00260 {
00261 for (int i = 0; i < SQDepthLevel; ++i)
00262 {
00263 s << " | " ;
00264 }
00265 }
00266 os << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
00267 for (ListDocEntry::iterator i = DocEntries.begin();
00268 i != DocEntries.end();
00269 ++i)
00270 {
00271 DocEntry *Entry = *i;
00272 bool PrintEndLine = true;
00273
00274 os << s.str();
00275 Entry->SetPrintLevel(PrintLevel);
00276 Entry->Print(os);
00277 if ( dynamic_cast<SeqEntry*>(Entry) )
00278 {
00279 PrintEndLine = false;
00280 }
00281 if (PrintEndLine)
00282 {
00283 os << std::endl;
00284 }
00285 }
00286 }
00287
00288
00289 }