]> Creatis software - gdcm.git/blobdiff - src/gdcmDicomDir.cxx
* gdcmPython/testSuite.py checks on CR-MONO1-10-chest.dcm moved to
[gdcm.git] / src / gdcmDicomDir.cxx
index b3e0d686bf93b3ec4f00f13ca7ba21c19aff2cee..3959a469bd712cfb07f1f7faa73e5f814cd56ec3 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmDicomDir.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/07/19 03:34:11 $
-  Version:   $Revision: 1.57 $
+  Date:      $Date: 2004/08/02 16:42:14 $
+  Version:   $Revision: 1.63 $
   
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -19,7 +19,6 @@
 #include <string>
 #include <algorithm>
 #include <sys/types.h>
-#include <errno.h>
 
 #ifdef _MSC_VER 
    #include <direct.h>
 //-----------------------------------------------------------------------------
 // Constructor / Destructor
 
-void gdcmDicomDir::Initialize()
-{
-   startMethod             = NULL;
-   progressMethod          = NULL;
-   endMethod               = NULL;
-   startMethodArgDelete    = NULL;
-   progressMethodArgDelete = NULL;
-   endMethodArgDelete      = NULL;
-   startArg                = NULL;
-   progressArg             = NULL;
-   endArg                  = NULL;
-
-   progress = 0.0;
-   abort = false;
+/**
+ * \ingroup gdcmDicomDir
+ * \brief   Constructor : creates an empty gdcmDicomDir
+ */
+gdcmDicomDir::gdcmDicomDir()
+   :gdcmDocument( )
+{ 
+   Initialize();
 
-   metaElems = (gdcmDicomDirMeta *)0;   
+   std::string pathBidon = "Bidon"; // Sorry, NULL not allowed ...
+   SetElement(pathBidon, GDCM_DICOMDIR_META, NULL); // Set the META elements
+   AddDicomDirMeta();
 }
 
-
 /**
  * \brief Constructor Parses recursively the directory and creates the DicomDir
  *        or uses an already built DICOMDIR, depending on 'parseDir' value.
@@ -76,11 +70,9 @@ void gdcmDicomDir::Initialize()
  *                        and wants to explore recursively the directories
  *                      - false if user passed an already built DICOMDIR file
  *                        and wants to use it 
- * @param exception_on_error whether we want to throw an exception or not
  */
-gdcmDicomDir::gdcmDicomDir(std::string const & fileName, bool parseDir,
-                           bool exception_on_error):
-   gdcmDocument( fileName, exception_on_error, true) // true : enable SeQuences
+gdcmDicomDir::gdcmDicomDir(std::string const & fileName, bool parseDir ):
+   gdcmDocument( fileName )
 {
    // que l'on ai passe un root directory ou un DICOMDIR
    // et quelle que soit la valeur de parseDir,
@@ -90,21 +82,22 @@ gdcmDicomDir::gdcmDicomDir(std::string const & fileName, bool parseDir,
    // gdcmDocument already executed
    // if user passed a root directory, sure we didn't get anything
 
-   if( TagHT.begin() == TagHT.end() ) 
+   if ( TagHT.begin() == TagHT.end() ) 
    {
       dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : entry HT empty");
 
-      if( fileName.size() == 1 && fileName[0] == '.' )
+      if ( fileName.size() == 1 && fileName[0] == '.' )
       {
          // user passed '.' as Name
          // we get current directory name
          char* dummy = new char[1000];
          getcwd(dummy, (size_t)1000);
+         std::cout << "Directory to parse : [" << dummy << "]" << std::endl;
          SetFileName( dummy ); // will be converted into a string
-         delete[] dummy;     // no longer needed   
+         delete[] dummy;       // no longer needed   
       }
 
-      if( parseDir )
+      if ( parseDir )
       {
          dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : Parse directory"
                         " and create the DicomDir");
@@ -113,6 +106,8 @@ gdcmDicomDir::gdcmDicomDir(std::string const & fileName, bool parseDir,
       else
       {
          /// \todo if parseDir == false, it should be tagged as an error
+         // NON ! il suffit d'appeler ParseDirectory() 
+         // apres le constructeur
       }
    }
    else
@@ -130,21 +125,6 @@ gdcmDicomDir::gdcmDicomDir(std::string const & fileName, bool parseDir,
    }
 }
 
-/**
- * \ingroup gdcmDicomDir
- * \brief   Constructor : creates an empty gdcmDicomDir
- * @param   exception_on_error whether we want to throw an exception or not
- */
-gdcmDicomDir::gdcmDicomDir(bool exception_on_error):                           
-   gdcmDocument( exception_on_error )
-{ 
-   Initialize();
-
-   std::string pathBidon = "Bidon"; // Sorry, NULL not allowed ...
-   SetElement(pathBidon, GDCM_DICOMDIR_META, NULL); // Set the META elements
-   AddDicomDirMeta();
-}
-
 /**
  * \brief  Canonical destructor 
  */
@@ -154,7 +134,7 @@ gdcmDicomDir::~gdcmDicomDir()
    SetProgressMethod(NULL);
    SetEndMethod(NULL);
 
-   if( metaElems )
+   if ( metaElems )
    {
       delete metaElems;
    }
@@ -216,6 +196,29 @@ bool gdcmDicomDir::IsReadable()
    return true;
 }
 
+/**
+ * \brief Sets all fields to NULL
+ */
+
+void gdcmDicomDir::Initialize()
+{
+   startMethod             = NULL;
+   progressMethod          = NULL;
+   endMethod               = NULL;
+   startMethodArgDelete    = NULL;
+   progressMethodArgDelete = NULL;
+   endMethodArgDelete      = NULL;
+   startArg                = NULL;
+   progressArg             = NULL;
+   endArg                  = NULL;
+
+   progress = 0.0;
+   abort = false;
+
+   metaElems = 0;   
+}
+
+
 /**
  * \ingroup gdcmDicomDir
  * \brief  fills the whole structure, starting from a root Directory
@@ -339,9 +342,7 @@ void gdcmDicomDir::SetEndMethodArgDelete(gdcmMethod *method)
  
 bool gdcmDicomDir::Write(std::string const & fileName) 
 {
-   FILE * fp1;
-
-   fp1 = fopen(fileName.c_str(), "wb");
+   FILE * fp1 = fopen(fileName.c_str(), "wb");
    if( !fp1 ) 
    {
       printf("Failed to open(write) File [%s] \n", fileName.c_str());
@@ -445,21 +446,29 @@ void gdcmDicomDir::CreateDicomDirChainedList(std::string const & path)
          break;
       }
 
-      header = new gdcmHeader(it->c_str(),false,true);
-      if(!header) {
-         std::cout << "echec new Header " << it->c_str() << std::endl; // JPR
-      }
-      if(header->IsReadable()) {
-         list.push_back(header);  // adds the file header to the chained list
-         std::cout << "readable : " <<it->c_str() << std::endl; // JPR
-       }
-      else
+      header = new gdcmHeader( it->c_str() );
+      if( !header )
       {
-         delete header;
+         dbg.Verbose( 1,
+                      "gdcmDicomDir::CreateDicomDirChainedList: "
+                      "failure in new Header ",
+                      it->c_str() );
       }
-      count++;
-
+      if( header->IsReadable() )
+      {
+         // Add the file header to the chained list:
+         list.push_back(header);
+         dbg.Verbose( 1,
+                      "gdcmDicomDir::CreateDicomDirChainedList: readable ",
+                      it->c_str() );
+       }
+       else
+       {
+          delete header;
+       }
+       count++;
    }
+
    // sorts Patient/Study/Serie/
    std::sort(list.begin(), list.end(), gdcmDicomDir::HeaderLessThan );
 
@@ -500,16 +509,19 @@ gdcmDicomDirPatient * gdcmDicomDir::NewPatient()
    uint16_t tmpGr,tmpEl;
    gdcmDictEntry *dictEntry;
    gdcmValEntry *entry;
-   
+
+   std::list<gdcmElement> elemList;   
+   elemList=gdcmGlobal::GetDicomDirElements()->GetDicomDirPatientElements(); 
+// Looks nice, but gdcmDicomDir IS NOT a gdcmObject ... 
+//   gdcmDicomDirPatient *p = new gdcmDicomDirPatient(ptagHT);
+//   FillObject(elemList);
+//   patients.push_front( p );
+//   return p;    
+/// \todo TODO : find a trick to use FillObject !!!
+
    gdcmSQItem *s = new gdcmSQItem(0);
-   
-   std::list<gdcmElement> elemList = 
-         gdcmGlobal::GetDicomDirElements()->GetDicomDirPatientElements();  
-   
-   /// \todo TODO : use FillObject !!!
 
-   // for all the DicomDirPatient Elements 
-     
+   // for all the DicomDirPatient Elements      
    for( it = elemList.begin(); it != elemList.end(); ++it ) 
    {
       tmpGr     = it->group;
@@ -567,24 +579,29 @@ void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,
    gdcmDictEntry *dictEntry;
    gdcmValEntry *entry;
    std::string val;
-
+   //gdcmObject *o;
    switch( type )
    {
-      case GDCM_DICOMDIR_PATIENT:
-         elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirPatientElements();
-         break;
-      case GDCM_DICOMDIR_STUDY:
-         elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirStudyElements();
+      case GDCM_DICOMDIR_IMAGE:
+         elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirImageElements();
          break;
+
       case GDCM_DICOMDIR_SERIE:
          elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirSerieElements();
          break;
-      case GDCM_DICOMDIR_IMAGE:
-         elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirImageElements();
+
+      case GDCM_DICOMDIR_STUDY:
+         elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirStudyElements();
          break;
+
+      case GDCM_DICOMDIR_PATIENT:
+         elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirPatientElements();
+         break;
+  
       case GDCM_DICOMDIR_META:
          elemList = gdcmGlobal::GetDicomDirElements()->GetDicomDirMetaElements();
          break;
+
       default:
          return;
    }
@@ -598,7 +615,8 @@ void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,
 
       entry->SetOffset(0); // just to avoid further missprinting
 
-      if( header )
+      if( header ) // NULL when we Build Up (ex nihilo) a DICOMDIR
+                   //   or when we add the META elems
       {
          val = header->GetEntryByNumber(tmpGr, tmpEl);
       }
@@ -646,7 +664,7 @@ void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,
       {
          if( dictEntry->GetGroup() == 0xfffe )
          {
-            entry->SetLength( entry->GetValue().length() );
+            entry->SetLength( entry->GetValue().length() ); // FIXME 
          }
          else if( dictEntry->GetVR() == "UL" || dictEntry->GetVR() == "SL" )
          {
@@ -665,8 +683,12 @@ void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,
             entry->SetLength( entry->GetValue().length() );
          }
       }
-      //AddDocEntry(entry); // both in H Table and in chained list
-      TagHT[entry->GetKey()] = entry;          // FIXME : use a SEQUENCE !
+      std::cout << " was TagHT[entry->GetKey()] = entry " << std::endl;
+      if ( type == GDCM_DICOMDIR_META ) {
+         std::cout << " special Treatment for GDCM_DICOMDIR_META" << std::endl;
+         
+      }
+      //TagHT[entry->GetKey()] = entry;          // FIXME : use a SEQUENCE !
    }
 }
 
@@ -724,8 +746,6 @@ void gdcmDicomDir::CreateDicomDir()
    //       + we create the object for the precedent tag
    //       + loop to 1 -
 
-   gdcmDicomDirType type = gdcmDicomDir::GDCM_DICOMDIR_META;
-   
    // Directory record sequence
    gdcmDocEntry *e = GetDocEntryByNumber(0x0004, 0x1220);
    if ( !e )
@@ -740,12 +760,14 @@ void gdcmDicomDir::CreateDicomDir()
    if ( !s )
    {
       dbg.Verbose(0, "gdcmDicomDir::CreateDicomDir: no SeqEntry present");
+      // useless : (0x0004,0x1220) IS a Sequence !
       return;
    }
 
+   gdcmDicomDirType type = gdcmDicomDir::GDCM_DICOMDIR_META;
+   metaElems = NewMeta();
+
    ListSQItem listItems = s->GetSQItems();
-   gdcmDicomDirMeta *m  = new gdcmDicomDirMeta(&TagHT);
-   (void)m; //??
    
    gdcmDocEntry * d;
    std::string v;