gdcmDocEntrySet.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmDocEntrySet.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2007/07/27 21:21:48 $
00007   Version:   $Revision: 1.75 $
00008                                                                                 
00009   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
00010   l'Image). All rights reserved. See Doc/License.txt or
00011   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
00012                                                                                 
00013      This software is distributed WITHOUT ANY WARRANTY; without even
00014      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015      PURPOSE.  See the above copyright notices for more information.
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 #if defined(__BORLANDC__)
00031    #include <mem.h> // for memset
00032 #endif 
00033 
00034 namespace GDCM_NAME_SPACE 
00035 {
00036 //-----------------------------------------------------------------------------
00037 // Constructor / Destructor
00038 DocEntrySet::DocEntrySet() 
00039 { 
00040    PreviousDocEntry = 0;
00041 }
00042 //-----------------------------------------------------------------------------
00043 // Public
00051 std::string DocEntrySet::GetEntryString(uint16_t group, uint16_t elem)
00052 {
00053    DataEntry *entry = dynamic_cast<DataEntry *>(GetDocEntry(group,elem));
00054    if ( entry )
00055    {
00056       if( entry->IsNotLoaded() )
00057          return GDCM_NOTLOADED;
00058       if( entry->IsUnfound() )
00059          return GDCM_UNFOUND;
00060       if( entry->IsUnread() )
00061          return GDCM_UNREAD;
00062       return entry->GetString();
00063    }
00064    return GDCM_UNFOUND;
00065 }
00066 
00073 void *DocEntrySet::GetEntryBinArea(uint16_t group, uint16_t elem) 
00074 {
00075    DataEntry *entry = GetDataEntry(group, elem);
00076    if ( entry )
00077       return entry->GetBinArea();
00078    return 0;
00079 }
00080 
00088 int DocEntrySet::GetEntryLength(uint16_t group, uint16_t elem)
00089 {
00090    DocEntry *entry = GetDocEntry(group, elem);
00091    if ( entry )
00092       return entry->GetLength();
00093    return -1;
00094 }
00095 
00103 DataEntry *DocEntrySet::GetDataEntry(uint16_t group, uint16_t elem)
00104 {
00105    DocEntry *currentEntry = GetDocEntry(group, elem);
00106    if ( !currentEntry )
00107       return NULL;
00108 
00109    return dynamic_cast<DataEntry*>(currentEntry);
00110 }
00111 
00119 SeqEntry *DocEntrySet::GetSeqEntry(uint16_t group, uint16_t elem)
00120 {
00121    DocEntry *currentEntry = GetDocEntry(group, elem);
00122    if ( !currentEntry )
00123       return NULL;
00124 
00125    return dynamic_cast<SeqEntry*>(currentEntry);
00126 }
00127 
00136 bool DocEntrySet::SetEntryString(std::string const &content, 
00137                                  uint16_t group, uint16_t elem) 
00138 {
00139    DataEntry *entry = GetDataEntry(group, elem);
00140    if (!entry )
00141    {
00142       gdcmWarningMacro( "No corresponding DataEntry " << std::hex << group <<
00143                          "," << elem << " element (try promotion first).");
00144       return false;
00145    }
00146    return SetEntryString(content,entry);
00147 }
00148 
00158 bool DocEntrySet::SetEntryBinArea(uint8_t *content, int lgth, 
00159                                   uint16_t group, uint16_t elem) 
00160 {
00161    DataEntry *entry = GetDataEntry(group, elem);
00162    if (!entry )
00163    {
00164       gdcmWarningMacro( "No corresponding DataEntry " << std::hex << group <<
00165                         "," << elem << " element (try promotion first).");
00166       return false;
00167    }
00168 
00169    return SetEntryBinArea(content,lgth,entry);
00170 } 
00171 
00178 bool DocEntrySet::SetEntryString(std::string const &content, DataEntry *entry)
00179 {
00180    if (entry)
00181    {
00182       entry->SetString(content);
00183       return true;
00184    }
00185    return false;
00186 }
00187 
00195 bool DocEntrySet::SetEntryBinArea(uint8_t *content, int lgth, DataEntry *entry)
00196 {
00197    if (entry)
00198    {
00199       entry->SetLength(lgth);
00200       entry->SetBinArea(content);  
00201       return true;
00202    }
00203    return false;
00204 }
00205 
00216 DataEntry *DocEntrySet::InsertEntryString(std::string const &value, 
00217                                           uint16_t group, uint16_t elem,
00218                                           VRKey const &vr )
00219 {
00220    DataEntry *dataEntry = 0;
00221    DocEntry *currentEntry = GetDocEntry( group, elem );
00222    VRKey localVR = vr;
00223    if (currentEntry)
00224    {
00225       dataEntry = dynamic_cast<DataEntry *>(currentEntry);
00226 
00227       // Verify the VR
00228       if ( dataEntry )
00229          if ( dataEntry->GetVR()!=vr )
00230             dataEntry = NULL;
00231 
00232       // if currentEntry doesn't correspond to the requested dataEntry
00233       if ( !dataEntry)
00234       {
00235          if ( !RemoveEntry(currentEntry) )
00236          {
00237             gdcmWarningMacro( "Removal of previous DocEntry failed.");
00238             return NULL;
00239          }
00240       }
00241    }
00242   
00243    else // the 'currentEntry' was not found
00244    {
00245       if ( vr == GDCM_VRUNKNOWN ) // user didn't specify a VR.
00246                                   //  Probabely he trusts the Dicom Dict !
00247       {
00248           DictEntry *e = 
00249             Global::GetDicts()->GetDefaultPubDict()->GetEntry(group, elem);
00250           if ( e )
00251           {
00252              localVR = e->GetVR();  
00253              e->Register(); // ?? JPRx
00254          }
00255       }
00256    }
00257 
00258    // Create a new dataEntry if necessary
00259    if ( !dataEntry )
00260    {
00261       dataEntry = NewDataEntry( group, elem, localVR );
00262 
00263       if ( !AddEntry(dataEntry) )
00264       {
00265          gdcmWarningMacro("AddEntry " << dataEntry->GetKey() 
00266                  << " failed although this is a creation.");
00267          dataEntry->Delete();
00268          return NULL;
00269       }
00270       dataEntry->Delete(); // ?!? JPRx
00271    }
00272 
00273    // Set the dataEntry value
00274    SetEntryString(value, dataEntry); // The std::string value
00275    return dataEntry;
00276 }
00277 
00290 DataEntry *DocEntrySet::InsertEntryBinArea(uint8_t *binArea, int lgth, 
00291                                            uint16_t group, uint16_t elem,
00292                                            VRKey const &vr )
00293 {
00294    DataEntry *dataEntry = 0;
00295    DocEntry *currentEntry = GetDocEntry( group, elem );
00296 
00297    // Verify the currentEntry
00298    if (currentEntry)
00299    {
00300       dataEntry = dynamic_cast<DataEntry *>(currentEntry);
00301 
00302       // Verify the VR
00303       if ( dataEntry )
00304          if ( dataEntry->GetVR()!=vr )
00305             dataEntry = NULL;
00306 
00307       // if currentEntry doesn't correspond to the requested dataEntry
00308       if ( !dataEntry)
00309       {
00310          if ( !RemoveEntry(currentEntry) )
00311          {
00312             gdcmWarningMacro( "Removal of previous DocEntry failed.");
00313             return NULL;
00314          }
00315       }
00316    }
00317 
00318    // Create a new dataEntry if necessary
00319    if ( !dataEntry)
00320    {
00321       dataEntry = NewDataEntry(group, elem, vr);
00322 
00323       if ( !AddEntry(dataEntry) )
00324       {
00325          gdcmWarningMacro( "AddEntry failed although this is a creation.");
00326          dataEntry->Delete();
00327          return NULL;
00328       }
00329       dataEntry->Delete();
00330    }
00331 
00332    // Set the dataEntry value
00333    uint8_t *tmpArea;
00334    if ( lgth>0 && binArea )
00335    {
00336       tmpArea = new uint8_t[lgth];
00337       memcpy(tmpArea,binArea,lgth);
00338    }
00339    else
00340    {
00341       tmpArea = 0;
00342    }
00343    if ( !SetEntryBinArea(tmpArea,lgth,dataEntry) )
00344    {
00345       if ( tmpArea )
00346       {
00347          delete[] tmpArea;
00348       }
00349    }
00350    return dataEntry;
00351 }  
00352 
00361 SeqEntry *DocEntrySet::InsertSeqEntry(uint16_t group, uint16_t elem)
00362 {
00363    SeqEntry *seqEntry = 0;
00364    DocEntry *currentEntry = GetDocEntry( group, elem );
00365 
00366    // Verify the currentEntry
00367    if ( currentEntry )
00368    {
00369       seqEntry = dynamic_cast<SeqEntry *>(currentEntry);
00370 
00371       // Verify the VR
00372       if ( seqEntry )
00373          seqEntry = NULL;
00374 
00375       // if currentEntry doesn't correspond to the requested seqEntry
00376       if ( !seqEntry )
00377       {
00378          if (!RemoveEntry(currentEntry))
00379          {
00380             gdcmWarningMacro( "Removal of previous DocEntry failed for ("
00381                <<std::hex << group << "|" << elem <<")" );
00382             return NULL;
00383          }
00384       }
00385    }
00386    // Create a new seqEntry if necessary
00387    if ( !seqEntry )
00388    {
00389       seqEntry = NewSeqEntry(group, elem);
00390 
00391       if ( !AddEntry(seqEntry) )
00392       {
00393          gdcmWarningMacro( "AddEntry failed although this is a creation for ("
00394             <<std::hex << group << "|" << elem <<")" );
00395          seqEntry->Delete();
00396          return NULL;
00397       }
00398       seqEntry->Delete();
00399    }
00400    // Remark :
00401    // SequenceDelimitationItem will be added at the end of the SeqEntry,
00402    // at write time
00403    return seqEntry;
00404 } 
00405  
00412 bool DocEntrySet::CheckIfEntryExist(uint16_t group, uint16_t elem )
00413 {
00414    return GetDocEntry(group,elem)!=NULL;
00415 }
00416 
00417 
00425 DataEntry *DocEntrySet::NewDataEntry(uint16_t group, uint16_t elem,
00426                                      VRKey const &vr) 
00427 {
00428 
00429    DataEntry *newEntry = DataEntry::New(group, elem, vr);
00430    if (!newEntry) 
00431    {
00432       gdcmWarningMacro( "Failed to allocate DataEntry for ("
00433           <<std::hex << group << "|" << elem <<")" );
00434       return 0;
00435    }
00436    return newEntry;
00437 }
00438 
00445 SeqEntry* DocEntrySet::NewSeqEntry(uint16_t group, uint16_t elem) 
00446 {
00447    //DictEntry *dictEntry = GetDictEntry(group, elem, "SQ");
00448 
00449    //SeqEntry *newEntry = SeqEntry::New( dictEntry );
00450    SeqEntry *newEntry = SeqEntry::New( group, elem );
00451    //dictEntry->Unregister(); // GetDictEntry register it
00452    if (!newEntry)
00453    {
00454       gdcmWarningMacro( "Failed to allocate SeqEntry for ("
00455          <<std::hex << group << "|" << elem <<")" );
00456       return 0;
00457    }     
00458    return newEntry;
00459 }
00460 
00461 //-----------------------------------------------------------------------------
00462 // Protected
00473 DictEntry *DocEntrySet::GetDictEntry(uint16_t group,uint16_t elem) 
00474 {
00475    DictEntry *found = 0;
00478    Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
00479    if (!pubDict) 
00480    {
00481       gdcmWarningMacro( "We SHOULD have a default dictionary");
00482    }
00483    else
00484    {
00485       found = pubDict->GetEntry(group, elem);
00486       if( found )
00487          found->Register();
00488    }
00489    return found;
00490 }
00491 
00492 //-----------------------------------------------------------------------------
00493 // Private
00494 
00495 //-----------------------------------------------------------------------------
00496 // Print
00497 
00498 //-----------------------------------------------------------------------------
00499 } // end namespace gdcm

Generated on Fri Aug 24 12:59:30 2007 for gdcm by  doxygen 1.4.6