00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "gdcmDocEntrySet.h"
00019
00020 #include "gdcmDebug.h"
00021 #include "gdcmCommon.h"
00022 #include "gdcmDictSet.h"
00023 #include "gdcmGlobal.h"
00024 #include "gdcmDocEntry.h"
00025 #include "gdcmSeqEntry.h"
00026 #include "gdcmUtil.h"
00027 #include "gdcmDataEntry.h"
00028 #include "gdcmVR.h"
00029
00030 namespace gdcm
00031 {
00032
00033
00034 DocEntrySet::DocEntrySet()
00035 {
00036 PreviousDocEntry = 0;
00037 }
00038
00039
00047 std::string DocEntrySet::GetEntryString(uint16_t group, uint16_t elem)
00048 {
00049 DataEntry *entry = dynamic_cast<DataEntry *>(GetDocEntry(group,elem));
00050 if ( entry )
00051 {
00052 if( entry->IsNotLoaded() )
00053 return GDCM_NOTLOADED;
00054 if( entry->IsUnfound() )
00055 return GDCM_UNFOUND;
00056 if( entry->IsUnread() )
00057 return GDCM_UNREAD;
00058 return entry->GetString();
00059 }
00060 return GDCM_UNFOUND;
00061 }
00062
00069 void *DocEntrySet::GetEntryBinArea(uint16_t group, uint16_t elem)
00070 {
00071 DataEntry *entry = GetDataEntry(group, elem);
00072 if ( entry )
00073 return entry->GetBinArea();
00074 return 0;
00075 }
00076
00084 int DocEntrySet::GetEntryLength(uint16_t group, uint16_t elem)
00085 {
00086 DocEntry *entry = GetDocEntry(group, elem);
00087 if ( entry )
00088 return entry->GetLength();
00089 return -1;
00090 }
00091
00099 DataEntry *DocEntrySet::GetDataEntry(uint16_t group, uint16_t elem)
00100 {
00101 DocEntry *currentEntry = GetDocEntry(group, elem);
00102 if ( !currentEntry )
00103 return NULL;
00104
00105 return dynamic_cast<DataEntry*>(currentEntry);
00106 }
00107
00115 SeqEntry *DocEntrySet::GetSeqEntry(uint16_t group, uint16_t elem)
00116 {
00117 DocEntry *currentEntry = GetDocEntry(group, elem);
00118 if ( !currentEntry )
00119 return NULL;
00120
00121 return dynamic_cast<SeqEntry*>(currentEntry);
00122 }
00123
00132 bool DocEntrySet::SetEntryString(std::string const &content,
00133 uint16_t group, uint16_t elem)
00134 {
00135 DataEntry *entry = GetDataEntry(group, elem);
00136 if (!entry )
00137 {
00138 gdcmWarningMacro( "No corresponding DataEntry " << std::hex << group <<
00139 "," << elem << " element (try promotion first).");
00140 return false;
00141 }
00142 return SetEntryString(content,entry);
00143 }
00144
00154 bool DocEntrySet::SetEntryBinArea(uint8_t *content, int lgth,
00155 uint16_t group, uint16_t elem)
00156 {
00157 DataEntry *entry = GetDataEntry(group, elem);
00158 if (!entry )
00159 {
00160 gdcmWarningMacro( "No corresponding DataEntry " << std::hex << group <<
00161 "," << elem << " element (try promotion first).");
00162 return false;
00163 }
00164
00165 return SetEntryBinArea(content,lgth,entry);
00166 }
00167
00174 bool DocEntrySet::SetEntryString(std::string const &content, DataEntry *entry)
00175 {
00176 if (entry)
00177 {
00178 entry->SetString(content);
00179 return true;
00180 }
00181 return false;
00182 }
00183
00191 bool DocEntrySet::SetEntryBinArea(uint8_t *content, int lgth, DataEntry *entry)
00192 {
00193 if (entry)
00194 {
00195 entry->SetLength(lgth);
00196 entry->SetBinArea(content);
00197 return true;
00198 }
00199 return false;
00200 }
00201
00212 DataEntry *DocEntrySet::InsertEntryString(std::string const &value,
00213 uint16_t group, uint16_t elem,
00214 VRKey const &vr )
00215 {
00216 DataEntry *dataEntry = 0;
00217 DocEntry *currentEntry = GetDocEntry( group, elem );
00218
00219 if (currentEntry)
00220 {
00221 dataEntry = dynamic_cast<DataEntry *>(currentEntry);
00222
00223
00224 if ( dataEntry )
00225 if ( dataEntry->GetVR()!=vr )
00226 dataEntry = NULL;
00227
00228
00229 if ( !dataEntry)
00230 {
00231 if ( !RemoveEntry(currentEntry) )
00232 {
00233 gdcmWarningMacro( "Removal of previous DocEntry failed.");
00234 return NULL;
00235 }
00236 }
00237 }
00238
00239
00240 if ( !dataEntry )
00241 {
00242 dataEntry = NewDataEntry( group, elem, vr );
00243
00244 if ( !AddEntry(dataEntry) )
00245 {
00246 gdcmWarningMacro("AddEntry failed although this is a creation.");
00247 dataEntry->Delete();
00248 return NULL;
00249 }
00250 dataEntry->Delete();
00251 }
00252
00253
00254 SetEntryString(value, dataEntry);
00255 return dataEntry;
00256 }
00257
00270 DataEntry *DocEntrySet::InsertEntryBinArea(uint8_t *binArea, int lgth,
00271 uint16_t group, uint16_t elem,
00272 VRKey const &vr )
00273 {
00274 DataEntry *dataEntry = 0;
00275 DocEntry *currentEntry = GetDocEntry( group, elem );
00276
00277
00278 if (currentEntry)
00279 {
00280 dataEntry = dynamic_cast<DataEntry *>(currentEntry);
00281
00282
00283 if ( dataEntry )
00284 if ( dataEntry->GetVR()!=vr )
00285 dataEntry = NULL;
00286
00287
00288 if ( !dataEntry)
00289 {
00290 if ( !RemoveEntry(currentEntry) )
00291 {
00292 gdcmWarningMacro( "Removal of previous DocEntry failed.");
00293 return NULL;
00294 }
00295 }
00296 }
00297
00298
00299 if ( !dataEntry)
00300 {
00301 dataEntry = NewDataEntry(group, elem, vr);
00302
00303 if ( !AddEntry(dataEntry) )
00304 {
00305 gdcmWarningMacro( "AddEntry failed although this is a creation.");
00306 dataEntry->Delete();
00307 return NULL;
00308 }
00309 dataEntry->Delete();
00310 }
00311
00312
00313 uint8_t *tmpArea;
00314 if ( lgth>0 && binArea )
00315 {
00316 tmpArea = new uint8_t[lgth];
00317 memcpy(tmpArea,binArea,lgth);
00318 }
00319 else
00320 {
00321 tmpArea = 0;
00322 }
00323 if ( !SetEntryBinArea(tmpArea,lgth,dataEntry) )
00324 {
00325 if ( tmpArea )
00326 {
00327 delete[] tmpArea;
00328 }
00329 }
00330 return dataEntry;
00331 }
00332
00341 SeqEntry *DocEntrySet::InsertSeqEntry(uint16_t group, uint16_t elem)
00342 {
00343 SeqEntry *seqEntry = 0;
00344 DocEntry *currentEntry = GetDocEntry( group, elem );
00345
00346
00347 if ( currentEntry )
00348 {
00349 seqEntry = dynamic_cast<SeqEntry *>(currentEntry);
00350
00351
00352 if ( seqEntry )
00353 seqEntry = NULL;
00354
00355
00356 if ( !seqEntry )
00357 {
00358 if (!RemoveEntry(currentEntry))
00359 {
00360 gdcmWarningMacro( "Removal of previous DocEntry failed for ("
00361 <<std::hex << group << "|" << elem <<")" );
00362 return NULL;
00363 }
00364 }
00365 }
00366
00367 if ( !seqEntry )
00368 {
00369 seqEntry = NewSeqEntry(group, elem);
00370
00371 if ( !AddEntry(seqEntry) )
00372 {
00373 gdcmWarningMacro( "AddEntry failed although this is a creation for ("
00374 <<std::hex << group << "|" << elem <<")" );
00375 seqEntry->Delete();
00376 return NULL;
00377 }
00378 seqEntry->Delete();
00379 }
00380
00381
00382
00383 return seqEntry;
00384 }
00385
00392 bool DocEntrySet::CheckIfEntryExist(uint16_t group, uint16_t elem )
00393 {
00394 return GetDocEntry(group,elem)!=NULL;
00395 }
00396
00406 DataEntry *DocEntrySet::NewDataEntry(uint16_t group,uint16_t elem,
00407 VRKey const &vr)
00408 {
00409 DictEntry *dictEntry = GetDictEntry(group, elem, vr);
00410
00411 DataEntry *newEntry = DataEntry::New(dictEntry);
00412 dictEntry->Unregister();
00413 if (!newEntry)
00414 {
00415 gdcmWarningMacro( "Failed to allocate DataEntry for ("
00416 <<std::hex << group << "|" << elem <<")" );
00417 return 0;
00418 }
00419 return newEntry;
00420 }
00421
00430 SeqEntry* DocEntrySet::NewSeqEntry(uint16_t group, uint16_t elem)
00431 {
00432 DictEntry *dictEntry = GetDictEntry(group, elem, "SQ");
00433
00434 SeqEntry *newEntry = SeqEntry::New( dictEntry );
00435 dictEntry->Unregister();
00436 if (!newEntry)
00437 {
00438 gdcmWarningMacro( "Failed to allocate SeqEntry for ("
00439 <<std::hex << group << "|" << elem <<")" );
00440 return 0;
00441 }
00442 return newEntry;
00443 }
00444
00445
00446
00457 DictEntry *DocEntrySet::GetDictEntry(uint16_t group,uint16_t elem)
00458 {
00459 DictEntry *found = 0;
00460 Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
00461 if (!pubDict)
00462 {
00463 gdcmWarningMacro( "We SHOULD have a default dictionary");
00464 }
00465 else
00466 {
00467 found = pubDict->GetEntry(group, elem);
00468 if( found )
00469 found->Register();
00470 }
00471 return found;
00472 }
00473
00484 DictEntry *DocEntrySet::GetDictEntry(uint16_t group, uint16_t elem,
00485 VRKey const &vr)
00486 {
00487 DictEntry *dictEntry = GetDictEntry(group,elem);
00488 DictEntry *goodEntry = dictEntry;
00489 VRKey goodVR = vr;
00490 TagName vm;
00491 if (elem == 0x0000)
00492 goodVR="UL";
00493
00494 if ( goodEntry )
00495 {
00496 if ( goodVR != goodEntry->GetVR()
00497 && goodVR != GDCM_VRUNKNOWN )
00498 {
00499 gdcmWarningMacro("For (" << std::hex << group << "|"
00500 << elem << "), found VR : [" << vr << "]"
00501 << " expected: [" << goodEntry->GetVR() << "]" ) ;
00502
00503
00504 vm = dictEntry->GetVM();
00505 goodEntry = NULL;
00506 }
00507 dictEntry->Unregister();
00508 }
00509 else
00510 {
00511 vm = "FIXME";
00512 }
00513
00514 if (!goodEntry)
00515 {
00516 if (dictEntry)
00517 {
00518
00519 goodEntry = DictEntry::New(group, elem, goodVR, vm,
00520 dictEntry->GetName() );
00521 }
00522 else
00523 {
00524 goodEntry = DictEntry::New(group, elem, goodVR);
00525 }
00526 }
00527 else
00528 {
00529 goodEntry->Register();
00530 }
00531 return goodEntry;
00532 }
00533
00534
00535
00536
00537
00538
00539
00540
00541 }