]> Creatis software - gdcm.git/blobdiff - src/gdcmDicomDir.cxx
Forget this one
[gdcm.git] / src / gdcmDicomDir.cxx
index c12a9a6f13c99970bba07ff357267faa5b789b41..1d5bed3a7f603d460b6a32cd922da8bf39421fe4 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmDicomDir.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/01/07 19:20:38 $
-  Version:   $Revision: 1.94 $
+  Date:      $Date: 2005/01/17 10:59:52 $
+  Version:   $Revision: 1.103 $
   
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 #include <sys/types.h>
 
 #ifdef _MSC_VER
-#define getcwd _getcwd
+#   define getcwd _getcwd
 #endif
 
-#if defined( _MSC_VER) || defined(__BORLANDC__)
-   #include <direct.h>
+#if defined(_MSC_VER) || defined(__BORLANDC__)
+#   include <direct.h>
 #else
-   #include <unistd.h>
+#   include <unistd.h>
 #endif
 
 namespace gdcm 
@@ -82,16 +82,21 @@ DicomDir::DicomDir()
 DicomDir::DicomDir(std::string const &fileName, bool parseDir ):
    Document( fileName )
 {
-   // Whatever user passed (a root directory or a DICOMDIR)
-   // and whatever the value of parseDir was,
-   // Document is already executed
+   // At this step, Document constructor is already executed,
+   // whatever user passed (a root directory or a DICOMDIR)
+   // and whatever the value of parseDir was.
+   // (nothing is cheked in Document constructor, to avoid overhead)
+
    Initialize();  // sets all private fields to NULL
 
    // if user passed a root directory, sure we didn't get anything
    if ( TagHT.begin() == TagHT.end() ) // when user passed a Directory to parse
    {
-      gdcmVerboseMacro("DicomDir::DicomDir : entry HT empty");
+      if (!parseDir)
+         gdcmVerboseMacro( "Entry HT empty for file: "<<fileName);
 
+   // Only if user passed a root directory
+   // ------------------------------------
       if ( fileName == "." )
       {
          // user passed '.' as Name
@@ -105,25 +110,25 @@ DicomDir::DicomDir(std::string const &fileName, bool parseDir ):
       {
          MetaElems = NewMeta();
 
-         gdcmVerboseMacro("DicomDir::DicomDir : Parse directory" 
-                     " and create the DicomDir");
+         gdcmVerboseMacro( "Parse directory and create the DicomDir");
          ParseDirectory();
       }
       else
       {
          /// \todo if parseDir == false, it should be tagged as an error
-         // NON ! il suffit d'appeler ParseDirectory() 
-         // apres le constructeur
+         // NO ! user may just call ParseDirectory() *after* constructor
       }
    }
-   else // Only if user passed a DICOMDIR
+   // Only if user passed a DICOMDIR
+   // ------------------------------
+   else 
    {
       // Directory record sequence
-      DocEntry *e = GetDocEntryByNumber(0x0004, 0x1220);
+      DocEntry *e = GetDocEntry(0x0004, 0x1220);
       if ( !e )
       {
-         gdcmVerboseMacro("DicomDir::DicomDir : NO Directory record"
-                        " sequence (0x0004,0x1220)");
+         gdcmVerboseMacro( "NO 'Directory record sequence' (0x0004,0x1220)"
+                          << " in file " << fileName);
          /// \todo FIXME : what do we do when the parsed file IS NOT a
          ///       DICOMDIR file ?         
       }
@@ -158,7 +163,7 @@ DicomDir::~DicomDir()
 /**
  * \brief  Canonical Printer 
  */
-void DicomDir::Print(std::ostream &os)
+void DicomDir::Print(std::ostream &os, std::string const & )
 {
    if( MetaElems )
    {
@@ -186,16 +191,19 @@ void DicomDir::Print(std::ostream &os)
  */
 bool DicomDir::IsReadable()
 {
-   if( !Document::IsReadable() )
+   if( Filetype == Unknown)
    {
+      gdcmVerboseMacro( "Wrong filetype");
       return false;
    }
    if( !MetaElems )
    {
+      gdcmVerboseMacro( "Meta Elements missing in DicomDir");
       return false;
    }
    if( Patients.size() <= 0 )
    {
+      gdcmVerboseMacro( "NO Patient in DicomDir");
       return false;
    }
 
@@ -406,12 +414,14 @@ bool DicomDir::WriteDicomDir(std::string const &fileName)
 void DicomDir::CreateDicomDirChainedList(std::string const & path)
 {
    CallStartMethod();
-   DirList fileList(path,1); // gets recursively the file list
+   DirList dirList(path,1); // gets recursively the file list
    unsigned int count = 0;
    VectDocument list;
    Header *header;
 
-   for( DirList::iterator it  = fileList.begin();
+   DirListType fileList = dirList.GetFilenames();
+
+   for( DirListType::iterator it  = fileList.begin();
                               it != fileList.end();
                               ++it )
    {
@@ -425,9 +435,7 @@ void DicomDir::CreateDicomDirChainedList(std::string const & path)
       header = new Header( it->c_str() );
       if( !header )
       {
-         gdcmVerboseMacro( "DicomDir::CreateDicomDirChainedList: " <<
-                      "failure in new Header " <<
-                      it->c_str() );
+         gdcmVerboseMacro( "Failure in new Header " << it->c_str() );
          continue;
       }
       
@@ -435,8 +443,7 @@ void DicomDir::CreateDicomDirChainedList(std::string const & path)
       {
          // Add the file header to the chained list:
          list.push_back(header);
-         gdcmVerboseMacro( "DicomDir::CreateDicomDirChainedList: readable " <<
-                      it->c_str() );
+         gdcmVerboseMacro( "Readable " << it->c_str() );
        }
        else
        {
@@ -447,7 +454,7 @@ void DicomDir::CreateDicomDirChainedList(std::string const & path)
    // sorts Patient/Study/Serie/
    std::sort(list.begin(), list.end(), DicomDir::HeaderLessThan );
    
-   std::string tmp = fileList.GetDirName();      
+   std::string tmp = dirList.GetDirName();      
    //for each Header of the chained list, add/update the Patient/Study/Serie/Image info
    SetElements(tmp, list);
    CallEndMethod();
@@ -511,7 +518,7 @@ DicomDirPatient *DicomDir::NewPatient()
    {
       tmpGr     = it->Group;
       tmpEl     = it->Elem;
-      dictEntry = GetPubDict()->GetDictEntryByNumber(tmpGr, tmpEl);
+      dictEntry = GetPubDict()->GetDictEntry(tmpGr, tmpEl);
       entry     = new ValEntry( dictEntry );
       entry->SetOffset(0); // just to avoid further missprinting
       entry->SetValue( it->Value );
@@ -550,8 +557,7 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
          si = new DicomDirImage();
          if( !AddDicomDirImageToEnd(static_cast<DicomDirImage *>(si)) )
          {
-            gdcmVerboseMacro("DicomDir::SetElement:"
-                        "Add DicomDirImageToEnd failed");
+            gdcmVerboseMacro( "Add DicomDirImageToEnd failed");
          }
          break;
       case GDCM_DICOMDIR_SERIE:
@@ -559,8 +565,7 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
          si = new DicomDirSerie();
          if( !AddDicomDirSerieToEnd(static_cast<DicomDirSerie *>(si)) )
          {
-            gdcmVerboseMacro("DicomDir::SetElement:"
-                        "Add DicomDirSerieToEnd failed");
+            gdcmVerboseMacro( "Add DicomDirSerieToEnd failed");
          }
          break;
       case GDCM_DICOMDIR_STUDY:
@@ -568,8 +573,7 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
          si = new DicomDirStudy();
          if( !AddDicomDirStudyToEnd(static_cast<DicomDirStudy *>(si)) )
          {
-            gdcmVerboseMacro("DicomDir::SetElement:"
-                        "Add DicomDirStudyToEnd failed");
+            gdcmVerboseMacro( "Add DicomDirStudyToEnd failed");
          }
          break;
       case GDCM_DICOMDIR_PATIENT:
@@ -577,8 +581,7 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
          si = new DicomDirPatient();
          if( !AddDicomDirPatientToEnd(static_cast<DicomDirPatient *>(si)) )
          {
-            gdcmVerboseMacro("DicomDir::SetElement:"
-                        "Add DicomDirPatientToEnd failed");
+            gdcmVerboseMacro( "Add DicomDirPatientToEnd failed");
          }
          break;
       case GDCM_DICOMDIR_META:
@@ -586,8 +589,7 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
          si = new DicomDirMeta();
          if( MetaElems )
          {
-            gdcmVerboseMacro("DicomDir::SetElement:"
-                        "MetaElements already exist, they will be destroyed");
+            gdcmVerboseMacro( "MetaElements already exist, they will be destroyed");
             delete MetaElems;
          }
          MetaElems = static_cast<DicomDirMeta *>(si);
@@ -609,7 +611,7 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
    {
       tmpGr     = it->Group;
       tmpEl     = it->Elem;
-      dictEntry = GetPubDict()->GetDictEntryByNumber(tmpGr, tmpEl);
+      dictEntry = GetPubDict()->GetDictEntry(tmpGr, tmpEl);
 
       entry     = new ValEntry( dictEntry ); // Be sure it's never a BinEntry !
 
@@ -619,7 +621,7 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
       {
          // NULL when we Build Up (ex nihilo) a DICOMDIR
          //   or when we add the META elems
-         val = header->GetEntryByNumber(tmpGr, tmpEl);
+         val = header->GetEntry(tmpGr, tmpEl);
       }
       else
       {
@@ -637,8 +639,7 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
          {
             if( header->GetFileName().substr(0, path.length()) != path )
             {
-               gdcmVerboseMacro("DicomDir::SetElement : the base path"
-                           " of file name is incorrect");
+               gdcmVerboseMacro( "The base path of file name is incorrect");
                val = header->GetFileName();
             }
             else
@@ -653,7 +654,7 @@ void DicomDir::SetElement(std::string const &path, DicomDirType type,
       }
       else
       {
-         if ( header->GetEntryLengthByNumber(tmpGr,tmpEl) == 0 )
+         if ( header->GetEntryLength(tmpGr,tmpEl) == 0 )
             val = it->Value;
       }
 
@@ -726,11 +727,10 @@ void DicomDir::CreateDicomDir()
    //       + loop to 1 -
 
    // Directory record sequence
-   DocEntry *e = GetDocEntryByNumber(0x0004, 0x1220);
+   DocEntry *e = GetDocEntry(0x0004, 0x1220);
    if ( !e )
    {
-      gdcmVerboseMacro("DicomDir::DicomDir : NO Directory record"
-                  " sequence (0x0004,0x1220)");
+      gdcmVerboseMacro( "NO Directory record sequence (0x0004,0x1220)");
       /// \todo FIXME: what to do when the parsed file IS NOT a DICOMDIR file ? 
       return;         
    }
@@ -738,12 +738,11 @@ void DicomDir::CreateDicomDir()
    SeqEntry *s = dynamic_cast<SeqEntry *>(e);
    if ( !s )
    {
-      gdcmVerboseMacro("DicomDir::CreateDicomDir: no SeqEntry present");
+      gdcmVerboseMacro( "No SeqEntry present");
       // useless : (0x0004,0x1220) IS a Sequence !
       return;
    }
 
-   DicomDirType type; // = DicomDir::GDCM_DICOMDIR_META;
    MetaElems = NewMeta();
 
    ListSQItem listItems = s->GetSQItems();
@@ -754,14 +753,14 @@ void DicomDir::CreateDicomDir()
    for( ListSQItem::iterator i = listItems.begin(); 
                              i !=listItems.end(); ++i ) 
    {
-      d = (*i)->GetDocEntryByNumber(0x0004, 0x1430); // Directory Record Type
+      d = (*i)->GetDocEntry(0x0004, 0x1430); // Directory Record Type
       if ( ValEntry* valEntry = dynamic_cast<ValEntry *>(d) )
       {
          v = valEntry->GetValue();
       }
       else
       {
-         gdcmVerboseMacro("DicomDir::CreateDicomDir: not a ValEntry.");
+         gdcmVerboseMacro( "Not a ValEntry.");
          continue;
       }
 
@@ -769,25 +768,21 @@ void DicomDir::CreateDicomDir()
       {
          si = new DicomDirPatient();
          AddDicomDirPatientToEnd( static_cast<DicomDirPatient *>(si) );
-         type = DicomDir::GDCM_DICOMDIR_PATIENT;
       }
       else if( v == "STUDY " )
       {
          si = new DicomDirStudy();
          AddDicomDirStudyToEnd( static_cast<DicomDirStudy *>(si) );
-         type = DicomDir::GDCM_DICOMDIR_STUDY;
       }
       else if( v == "SERIES" )
       {
          si = new DicomDirSerie();
          AddDicomDirSerieToEnd( static_cast<DicomDirSerie *>(si) );
-         type = DicomDir::GDCM_DICOMDIR_SERIE;
       }
       else if( v == "IMAGE " ) 
       {
          si = new DicomDirImage();
          AddDicomDirImageToEnd( static_cast<DicomDirImage *>(si) );
-         type = DicomDir::GDCM_DICOMDIR_IMAGE;
       }
       else
       {
@@ -920,12 +915,12 @@ void DicomDir::SetElements(std::string const & path, VectDocument const &list)
                                      it != list.end(); ++it )
    {
       // get the current file characteristics
-      patCurName         = (*it)->GetEntryByNumber(0x0010,0x0010);
-      patCurID           = (*it)->GetEntryByNumber(0x0010,0x0011);
-      studCurInstanceUID = (*it)->GetEntryByNumber(0x0020,0x000d);
-      studCurID          = (*it)->GetEntryByNumber(0x0020,0x0010);
-      serCurInstanceUID  = (*it)->GetEntryByNumber(0x0020,0x000e);
-      serCurID           = (*it)->GetEntryByNumber(0x0020,0x0011);
+      patCurName         = (*it)->GetEntry(0x0010,0x0010);
+      patCurID           = (*it)->GetEntry(0x0010,0x0011);
+      studCurInstanceUID = (*it)->GetEntry(0x0020,0x000d);
+      studCurID          = (*it)->GetEntry(0x0020,0x0010);
+      serCurInstanceUID  = (*it)->GetEntry(0x0020,0x000e);
+      serCurID           = (*it)->GetEntry(0x0020,0x0011);
 
       if( patCurName != patPrevName || patCurID != patPrevID || first )
       {
@@ -992,6 +987,34 @@ bool DicomDir::HeaderLessThan(Document *header1, Document *header2)
 {
    return *header1 < *header2;
 }
+
+/**
+ * \brief   Initialise the visit of the DicomDirPatients of the DicomDir
+ */
+void DicomDir::InitTraversal()
+{
+   ItDicomDirPatient = Patients.begin();
+}
+
+/**
+ * \brief   Get the next entry while visiting the DicomDirPatients
+ * \return  The next DicomDirPatient if found, otherwhise NULL
+ */
+DicomDirPatient *DicomDir::GetNextEntry()
+{
+   if (ItDicomDirPatient != Patients.end())
+   {
+      DicomDirPatient *tmp = *ItDicomDirPatient;
+      ++ItDicomDirPatient;
+      return tmp;
+   }
+   else
+   {
+      return NULL;
+   }
+}
+
+
 } // end namespace gdcm
 
 //-----------------------------------------------------------------------------