]> Creatis software - gdcm.git/blobdiff - src/gdcmDicomDir.cxx
* Test/PrintAllDocument.cxx: looping on files is now effective. It used to
[gdcm.git] / src / gdcmDicomDir.cxx
index da2bf72bf6210aff623d8891bce5d6523e6ab569..7fbc137c187952a3e690a05ee07aa930f246ef4d 100644 (file)
@@ -1,5 +1,21 @@
-// gdcmDicomDir.cxx
-//-----------------------------------------------------------------------------
+/*=========================================================================
+  
+  Program:   gdcm
+  Module:    $RCSfile: gdcmDicomDir.cxx,v $
+  Language:  C++
+  Date:      $Date: 2004/06/23 13:02:36 $
+  Version:   $Revision: 1.52 $
+  
+  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
+  l'Image). All rights reserved. See Doc/License.txt or
+  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.htm for details.
+  
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+  
+=========================================================================*/
+
 #include <string>
 #include <algorithm>
 #include <sys/types.h>
@@ -19,7 +35,7 @@
 #include "gdcmUtil.h"
 #include "gdcmDebug.h"
 #include "gdcmGlobal.h"
-
+#include "gdcmHeader.h"
 #include "gdcmSeqEntry.h"
 #include "gdcmSQItem.h"
 #include "gdcmValEntry.h"
 //  For full DICOMDIR description, see:
 //  PS 3.3-2003, pages 731-750
 //-----------------------------------------------------------------------------
+// Constructor / Destructor
 
+void gdcmDicomDir::Initialize(void)
+{
+   startMethod             = NULL;
+   progressMethod          = NULL;
+   endMethod               = NULL;
+   startMethodArgDelete    = NULL;
+   progressMethodArgDelete = NULL;
+   endMethodArgDelete      = NULL;
+   startArg                = NULL;
+   progressArg             = NULL;
+   endArg                  = NULL;
+
+   progress = 0.0;
+   abort = false;
+
+   metaElems = (gdcmDicomDirMeta *)0;   
+}
 
-// Constructor / Destructor
 
 /**
  * \brief Constructor Parses recursively the directory and creates the DicomDir
@@ -49,31 +82,16 @@ gdcmDicomDir::gdcmDicomDir(const char *FileName, bool parseDir,
                            bool exception_on_error):
    gdcmDocument(FileName,exception_on_error,true) // true : enable SeQuences
 {
- // que l'on ai passe un root directory ou un DICOMDIR
- // et quelle que soit la valeur de parseDir,
- // on a lance gdcmDocument 
-      
-   startMethod=            NULL;
-   progressMethod=         NULL;
-   endMethod=              NULL;
-   startMethodArgDelete=   NULL;
-   progressMethodArgDelete=NULL;
-   endMethodArgDelete=     NULL;
-   startArg=               NULL;
-   progressArg=            NULL;
-   endArg=                 NULL;
-
-   progress=0.0;
-   abort=false;
-
-   metaElems=NULL;   
+   // que l'on ai passe un root directory ou un DICOMDIR
+   // et quelle que soit la valeur de parseDir,
+   // on a lance gdcmDocument 
+   Initialize();
 
-// gdcmDocument already executed
-// if user passed a root directory, sure we didn't get anything
+   // gdcmDocument already executed
+   // if user passed a root directory, sure we didn't get anything
 
-   if( GetEntry().begin()==GetEntry().end() ) 
+   if( GetEntry().begin() == GetEntry().end() ) 
    {
-     // if parseDir == false, it should be tagged as an error
       dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : entry HT empty");
 
       if(strlen(FileName)==1 && FileName[0]=='.') { // user passed '.' as Name
@@ -89,14 +107,19 @@ gdcmDicomDir::gdcmDicomDir(const char *FileName, bool parseDir,
          dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : Parse directory"
                         " and create the DicomDir");
          ParseDirectory();
+      } else {
+         /// \todo if parseDir == false, it should be tagged as an error
       }
    }
-   else {
-      gdcmDocEntry *e = GetDocEntryByNumber(0x0004, 0x1220); // Directory record sequence
+   else
+   {
+      // Directory record sequence
+      gdcmDocEntry *e = GetDocEntryByNumber(0x0004, 0x1220);
       if (e==NULL) {
-         dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : NO Directory record sequence (0x0004,0x1220)"
-                     );
-         // FIXME : what to do when the parsed file IS NOT a DICOMDIR file ?         
+         dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : NO Directory record"
+                        " sequence (0x0004,0x1220)");
+         /// \todo FIXME : what to do when the parsed file IS NOT a
+         ///       DICOMDIR file ?         
       }      
       CreateDicomDir();
    } 
@@ -110,25 +133,15 @@ gdcmDicomDir::gdcmDicomDir(const char *FileName, bool parseDir,
 gdcmDicomDir::gdcmDicomDir(bool exception_on_error):                           
    gdcmDocument(exception_on_error)
 { 
-   startMethod=            NULL;
-   progressMethod=         NULL;
-   endMethod=              NULL;
-   startMethodArgDelete=   NULL;
-   progressMethodArgDelete=NULL;
-   endMethodArgDelete=     NULL;
-   startArg=               NULL;
-   progressArg=            NULL;
-   endArg=                 NULL;
-   progress=0.0;
-   abort=false;
-   std::string pathBidon = ""; // Sorry, NULL not allowed ...
+
+   Initialize();
+
+   std::string pathBidon = "Bidon"; // Sorry, NULL not allowed ...
    SetElement(pathBidon, GDCM_DICOMDIR_META, NULL); // Set the META elements
    AddDicomDirMeta();
 }
 
-
 /**
- * \ingroup gdcmDicomDir
  * \brief  Canonical destructor 
  */
 gdcmDicomDir::~gdcmDicomDir() 
@@ -140,7 +153,9 @@ gdcmDicomDir::~gdcmDicomDir()
    if(metaElems)
       delete metaElems;
    
-   for(ListDicomDirPatient::iterator cc=patients.begin();cc!=patients.end();++cc)
+   for(ListDicomDirPatient::iterator cc = patients.begin();
+                                     cc!= patients.end();
+                                   ++cc)
    {
       delete *cc;
    }
@@ -149,7 +164,6 @@ gdcmDicomDir::~gdcmDicomDir()
 //-----------------------------------------------------------------------------
 // Print
 /**
- * \ingroup gdcmDicomDir
  * \brief  Canonical Printer 
  */
 void gdcmDicomDir::Print(std::ostream &os)
@@ -159,7 +173,9 @@ void gdcmDicomDir::Print(std::ostream &os)
       metaElems->SetPrintLevel(printLevel);
       metaElems->Print(os);   
    }   
-   for(ListDicomDirPatient::iterator cc=patients.begin();cc!=patients.end();++cc)
+   for(ListDicomDirPatient::iterator cc  = patients.begin();
+                                     cc != patients.end();
+                                   ++cc)
    {
      (*cc)->SetPrintLevel(printLevel);
      (*cc)->Print(os);     
@@ -169,7 +185,6 @@ void gdcmDicomDir::Print(std::ostream &os)
 //-----------------------------------------------------------------------------
 // Public
 /**
- * \ingroup gdcmDicomDir
  * \brief  This predicate, based on hopefully reasonable heuristics,
  *         decides whether or not the current header was properly parsed
  *         and contains the mandatory information for being considered as
@@ -328,7 +343,7 @@ bool gdcmDicomDir::Write(std::string fileName)
  * @param   _fp already open file pointer
  */
 
-void gdcmDicomDir::WriteEntries(FILE *_fp)
+void gdcmDicomDir::WriteEntries(FILE *) //_fp
 {   
    /// \todo (?) tester les echecs en ecriture 
    ///          (apres chaque fwrite, dans le WriteEntry)
@@ -353,8 +368,8 @@ void gdcmDicomDir::WriteEntries(FILE *_fp)
       for(i=(*itPatient)->debut();i!=(*itPatient)->fin();++i) {
          WriteEntry(*i,_fp, gdcmExplicitVR);
       }
-      itStudy = ((*itPatient)->GetDicomDirStudies()).begin();        
-      while (itStudy != (*itPatient)->GetDicomDirStudies().end() ) {   
+      itStudy = ((*itPatient)->GetDicomDirStudies()).begin();     
+      while (itStudy != (*itPatient)->GetDicomDirStudies().end() ) {
          for(i=(*itStudy)->debut();i!=(*itStudy)->fin();++i) {
             WriteEntry(*i,_fp, gdcmExplicitVR);
          } 
@@ -368,11 +383,11 @@ void gdcmDicomDir::WriteEntries(FILE *_fp)
                for(i=(*itImage)->debut();i!=(*itImage)->fin();++i) {
                   WriteEntry(*i,_fp, gdcmExplicitVR);
                }
-               ++itImage;                  
-           }
-           ++itSerie;                                
+               ++itImage;
+            }
+            ++itSerie;
          }
-        ++itStudy;            
+         ++itStudy;
       } 
       ++itPatient;     
    }
@@ -393,7 +408,7 @@ void gdcmDicomDir::CreateDicomDirChainedList(std::string path)
 
    gdcmDirList fileList(path,1); // gets recursively the file list
    unsigned int count=0;
-   ListHeader list;
+   VectDocument list;
    gdcmHeader *header;
 
    tagHT.clear();
@@ -492,7 +507,7 @@ gdcmDicomDirPatient * gdcmDicomDir::NewPatient(void) {
          }
       else
          {
-            entry->SetLength(entry->GetValue().length());       
+            entry->SetLength(entry->GetValue().length());
          } 
       s->AddDocEntry(entry);
    }
@@ -502,26 +517,25 @@ gdcmDicomDirPatient * gdcmDicomDir::NewPatient(void) {
    return p;   
 }
 
-
 /**
- * \ingroup gdcmDicomDir
  * \brief   adds to the HTable 
  *          the gdcmEntries (Dicom Elements) corresponding to the given type
  * @param   path full path file name (only used when type = GDCM_DICOMDIR_IMAGE
- * @param   type gdcmObject type to create (GDCM_DICOMDIR_PATIENT, GDCM_DICOMDIR_STUDY, GDCM_DICOMDIR_SERIE ...)
+ * @param   type gdcmObject type to create (GDCM_DICOMDIR_PATIENT,
+ *          GDCM_DICOMDIR_STUDY, GDCM_DICOMDIR_SERIE ...)
  * @param   header gdcmHeader of the current file
  */
-void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,gdcmHeader *header)
+void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,
+                              gdcmDocument *header)
 {
    std::list<gdcmElement> elemList;
    std::list<gdcmElement>::iterator it;
    guint16 tmpGr, tmpEl;
    gdcmDictEntry *dictEntry;
-   gdcmDocEntry *entry;
+   gdcmValEntry *entry;
    std::string val;
 
-   switch(type)
-   {
+   switch(type){
       case GDCM_DICOMDIR_PATIENT:
          elemList=gdcmGlobal::GetDicomDirElements()->GetDicomDirPatientElements();
          break;
@@ -541,12 +555,12 @@ void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,gdcmHeader
          return;
    }
 
-   for(it=elemList.begin();it!=elemList.end();++it)
-   {
+   for(it=elemList.begin();it!=elemList.end();++it) {
       tmpGr=it->group;
       tmpEl=it->elem;
       dictEntry=GetPubDict()->GetDictEntryByNumber(tmpGr,tmpEl);
-      entry=new gdcmDocEntry(dictEntry);
+      entry=new gdcmValEntry(dictEntry); // Be sure it's never a BinEntry !
+
       entry->SetOffset(0); // just to avoid further missprinting
 
       if(header)
@@ -557,20 +571,22 @@ void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,gdcmHeader
       if(val==GDCM_UNFOUND) 
       {
          if((tmpGr==0x0004) &&(tmpEl==0x1130) ) // File-set ID
-         {      
-          // force to the *end* File Name
-           val=GetName(path);              
+         {
+           // force to the *end* File Name
+           val=GetName(path);
          }
          else if( (tmpGr==0x0004) && (tmpEl==0x1500) ) // Only used for image
          {
             if(header->GetFileName().substr(0,path.length())!=path)
             {
-               dbg.Verbose(0, "gdcmDicomDir::SetElement : the base path of file name is incorrect");
+               dbg.Verbose(0, "gdcmDicomDir::SetElement : the base path"
+                              " of file name is incorrect");
                val=header->GetFileName();
             }
-            else {
+            else
+            {
                val=&(header->GetFileName().c_str()[path.length()]);
-           }   
+            }   
          }
          else
          {
@@ -580,40 +596,39 @@ void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,gdcmHeader
       else
       {
          if (header->GetEntryLengthByNumber(tmpGr,tmpEl)== 0)
-           val=it->value;
+            val=it->value;
       }
             
-      ((gdcmValEntry *)entry)->SetValue(val);
+      entry->SetValue(val);
 
       if(dictEntry)
       {
-         if(dictEntry->GetGroup()==0xfffe) 
-        {
-            entry->SetLength(((gdcmValEntry *)entry)->GetValue().length());     
-        }
-        else if( (dictEntry->GetVR()=="UL") || (dictEntry->GetVR()=="SL") ) 
+         if(dictEntry->GetGroup()==0xfffe)  {
+            entry->SetLength(entry->GetValue().length());
+      }
+     else if( (dictEntry->GetVR()=="UL") || (dictEntry->GetVR()=="SL") ) 
          {
             entry->SetLength(4);
          } 
-         else if( (dictEntry->GetVR()=="US") || (dictEntry->GetVR()=="SS") ) 
+      else if( (dictEntry->GetVR()=="US") || (dictEntry->GetVR()=="SS") ) 
          {
             entry->SetLength(2); 
          } 
-         else if(dictEntry->GetVR()=="SQ") 
+      else if(dictEntry->GetVR()=="SQ") 
          {
             entry->SetLength(0xffffffff);
          }
-         else
+      else
          {
-            entry->SetLength(((gdcmValEntry *)entry)->GetValue().length());     
+            entry->SetLength(entry->GetValue().length());
          }
       }
       //AddDocEntry(entry); // both in H Table and in chained list
       tagHT[entry->GetKey()] = entry;          // FIXME : use a SEQUENCE !
    }     
 }
+
 /**
- * \ingroup gdcmDicomDir
  * \brief   CallStartMethod
  */
 void gdcmDicomDir::CallStartMethod(void)
@@ -661,31 +676,48 @@ void gdcmDicomDir::CreateDicomDir()
 
    gdcmDicomDirType type=gdcmDicomDir::GDCM_DICOMDIR_META;
    
-   gdcmDocEntry *e = GetDocEntryByNumber(0x0004, 0x1220); // Directory record sequence
-   if (e==NULL) {
-      dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : NO Directory record sequence (0x0004,0x1220)"
-                  );
-         // FIXME : what to do when the parsed file IS NOT a DICOMDIR file ? 
+   // Directory record sequence
+   gdcmDocEntry *e = GetDocEntryByNumber(0x0004, 0x1220);
+   if (e==NULL)
+   {
+      dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : NO Directory record"
+                     " sequence (0x0004,0x1220)");
+      /// \todo FIXME: what to do when the parsed file IS NOT a DICOMDIR file ? 
       return;         
    }
    
-   gdcmDicomDirMeta *m = new gdcmDicomDirMeta(&tagHT);
-   
-   gdcmSeqEntry *s = (gdcmSeqEntry *)e;  // FIXME : It is allowed ???  
+   gdcmSeqEntry* s = dynamic_cast<gdcmSeqEntry*>(e);
+   if (!s)
+   {
+      dbg.Verbose(0, "gdcmDicomDir::CreateDicomDir: no SeqEntry present");
+      return;
+   }
+
    ListSQItem listItems = s->GetSQItems();
+   gdcmDicomDirMeta *m = new gdcmDicomDirMeta(&tagHT);
+   (void)m; //??
    
    gdcmDocEntry * d;   
+   std::string v;
    for(ListSQItem::iterator i=listItems.begin(); i !=listItems.end();++i) 
    {  
       d=(*i)->GetDocEntryByNumber(0x0004, 0x1430); // Directory Record Type   
-      std::string v=((gdcmValEntry *)d)->GetValue();
-      
+      if (gdcmValEntry* ValEntry = dynamic_cast< gdcmValEntry* >(d) )
+      {
+         v = ValEntry->GetValue();
+      }
+      else 
+      {
+         dbg.Verbose(0, "gdcmDicomDir::CreateDicomDir: not a ValEntry.");
+         continue;
+      } 
+
       if(v=="PATIENT ") 
       {  
          AddDicomDirPatientToEnd(*i);
          //AddObjectToEnd(type,*i);
          type=gdcmDicomDir::GDCM_DICOMDIR_PATIENT;
-      }        
+      }
 
       else if(v=="STUDY ")
       {
@@ -709,7 +741,9 @@ void gdcmDicomDir::CreateDicomDir()
       }
       
       else
-         continue ;  // It was 'non PATIENT', 'non STUDY', 'non SERIE', 'non IMAGE' SQItem      
+         // It was not a 'PATIENT', nor a 'STUDY', nor a 'SERIE',
+         // neither an 'IMAGE' SQItem. Skip to next item.
+         continue;
    }
 }
 /**
@@ -745,7 +779,7 @@ void gdcmDicomDir::CreateDicomDir()
          AddDicomDirImageToEnd(s);
          break;
       case gdcmDicomDir::GDCM_DICOMDIR_NONE:
-         AddDicomDirImageToEnd(s);     //FIXME
+         AddDicomDirImageToEnd(s);        //FIXME
          break;
    }
 }
@@ -841,7 +875,7 @@ void gdcmDicomDir::AddDicomDirSerieToEnd(gdcmSQItem *s)
  * @param   path path of the root directory
  * @param   list chained list of Headers
  */
-void gdcmDicomDir::SetElements(std::string &path, ListHeader &list)
+void gdcmDicomDir::SetElements(std::string &path, VectDocument &list)
 {
    std::string patPrevName="",         patPrevID="";
    std::string studPrevInstanceUID="", studPrevID="";
@@ -853,7 +887,7 @@ void gdcmDicomDir::SetElements(std::string &path, ListHeader &list)
 
    SetElement(path,GDCM_DICOMDIR_META,NULL);
 
-   for(ListHeader::iterator it=list.begin();it!=list.end();++it) 
+   for(VectDocument::iterator it=list.begin();it!=list.end();++it) 
    {
       // get the current file characteristics
       patCurName=        (*it)->GetEntryByNumber(0x0010,0x0010); 
@@ -890,7 +924,7 @@ void gdcmDicomDir::SetElements(std::string &path, ListHeader &list)
  * \ingroup gdcmDicomDir
  * \brief   compares two dgcmHeaders
  */
-bool gdcmDicomDir::HeaderLessThan(gdcmHeader *header1,gdcmHeader *header2)
+bool gdcmDicomDir::HeaderLessThan(gdcmDocument *header1,gdcmDocument *header2)
 {
    return(*header1<*header2);
 }
@@ -924,7 +958,7 @@ void gdcmDicomDir::UpdateDirectoryRecordSequenceLength() {
       }            
    }   
    //bool res=SetEntryLengthByNumber(offset, 0x0004, 0x1220); // Hope there is no dupps.
-        SetEntryLengthByNumber(offset, 0x0004, 0x1220); // Hope there is no dupps.
+    SetEntryLengthByNumber(offset, 0x0004, 0x1220); // Hope there is no dupps.
    return;
    */
 }