Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

gdcmSQItem.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002   
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmSQItem.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2005/02/07 08:48:18 $
00007   Version:   $Revision: 1.70 $
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 
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 // Constructor / Destructor
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 // Public
00054 /*
00055  * \brief   canonical Writer
00056  * @param fp     file pointer to an already open file. 
00057  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
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     //we force the writting of an 'Item' Start Element
00066     // because we want to write the Item as a 'no Length' item
00067    for(j=0;j<4;++j)
00068    {
00069       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
00070    }
00071      
00072    for (ListDocEntry::iterator it = DocEntries.begin();  
00073                                it != DocEntries.end();
00074                              ++it)
00075    {   
00076       // we skip delimitors (start and end one) because 
00077       // we force them as 'no length'
00078       if ( (*it)->GetGroup() == 0xfffe )
00079       {
00080          continue;
00081       }
00082 
00083       // Fix in order to make some MR PHILIPS images e-film readable
00084       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
00085       // we just *always* ignore spurious fffe|0000 tag ! 
00086       if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
00087       {
00088          break; // FIXME : continue; ?!?
00089       }
00090 
00091       (*it)->WriteContent(fp, filetype);
00092    }
00093       
00094     //we force the writting of an 'Item Delimitation' item
00095     // because we wrote the Item as a 'no Length' item
00096    for(j=0;j<4;++j)
00097    {
00098       binary_write( *fp, itemt[j]);  // fffe e000 ffff ffff 
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 // Protected
00244 
00245 //-----------------------------------------------------------------------------
00246 // Private
00247 
00248 //-----------------------------------------------------------------------------
00249 // Print
00250 /*
00251  * \brief   canonical Printer
00252  * @param os     Stream to print to. 
00253  * @param indent Indentation string to be prepended during printing.
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 } // end namespace gdcm

Generated on Thu Feb 10 22:18:00 2005 for gdcm by doxygen 1.3.6