+ 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;
+
+// gdcmDocument already executed
+// if user passed a root directory, sure we didn't get anything
+
+ 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
+ // we get current directory name
+ char* dummy= new char[1000];
+ getcwd(dummy, (size_t)1000);
+ SetFileName(dummy); // will be converted into a string
+ delete[] dummy; // no longer needed
+ }
+
+ if(parseDir)
+ {
+ dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : Parse directory"
+ " and create the DicomDir");
+ ParseDirectory();
+ }
+ }
+ else {
+ 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 ?
+ }
+ CreateDicomDir();
+ }
+}
+
+/**
+ * \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)
+{
+ 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 ...
+ SetElement(pathBidon, GDCM_DICOMDIR_META, NULL); // Set the META elements
+
+ AddDicomDirMeta();
+}
+
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief Canonical destructor
+ */
+gdcmDicomDir::~gdcmDicomDir()
+{
+ SetStartMethod(NULL);
+ SetProgressMethod(NULL);
+ SetEndMethod(NULL);
+
+ if(metaElems)
+ delete metaElems;
+
+ for(ListDicomDirPatient::iterator cc=patients.begin();cc!=patients.end();++cc)
+ {
+ delete *cc;
+ }
+}
+
+//-----------------------------------------------------------------------------
+// Print
+/**
+ * \ingroup gdcmDicomDir
+ * \brief Canonical Printer
+ */
+void gdcmDicomDir::Print(std::ostream &os)
+{
+ if(metaElems)
+ {
+ metaElems->SetPrintLevel(printLevel);
+ metaElems->Print(os);
+ }
+ for(ListDicomDirPatient::iterator cc=patients.begin();cc!=patients.end();++cc)
+ {
+ (*cc)->SetPrintLevel(printLevel);
+ (*cc)->Print(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
+ * a well formed and usable DicomDir.
+ * @return true when gdcmDocument is the one of a reasonable DicomDir,
+ * false otherwise.
+ */
+bool gdcmDicomDir::IsReadable(void)
+{
+ if(!gdcmDocument::IsReadable())
+ return(false);
+ if(!metaElems)
+ return(false);
+ if(patients.size()<=0)
+ return(false);
+
+ return(true);
+}
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief fills the whole structure, starting from a root Directory
+ */
+void gdcmDicomDir::ParseDirectory(void)
+{
+ CreateDicomDirChainedList(GetFileName());
+ CreateDicomDir();
+}
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief Set the start method to call when the parsing of the directory starts
+ * @param method Method to call
+ * @param arg Argument to pass to the method
+ * @param argDelete Argument
+ * \warning In python : the arg parameter isn't considered
+ */
+void gdcmDicomDir::SetStartMethod(gdcmMethod *method,void *arg,gdcmMethod *argDelete)
+{
+ if((startArg)&&(startMethodArgDelete))
+ startMethodArgDelete(startArg);
+
+ startMethod=method;
+ startArg=arg;
+ startMethodArgDelete=argDelete;
+}
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief Set the method to delete the argument
+ * The argument is destroyed when the method is changed or when the
+ * class is destroyed
+ * @param method Method to call to delete the argument
+ */
+void gdcmDicomDir::SetStartMethodArgDelete(gdcmMethod *method)
+{
+ startMethodArgDelete=method;
+}
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief Set the progress method to call when the parsing of the directory progress
+ * @param method Method to call
+ * @param arg Argument to pass to the method
+ * @param argDelete Argument
+ * \warning In python : the arg parameter isn't considered
+ */
+void gdcmDicomDir::SetProgressMethod(gdcmMethod *method,void *arg,gdcmMethod *argDelete)
+{
+ if((progressArg)&&(progressMethodArgDelete))
+ progressMethodArgDelete(progressArg);
+
+ progressMethod=method;
+ progressArg=arg;
+ progressMethodArgDelete=argDelete;
+}
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief Set the method to delete the argument
+ * The argument is destroyed when the method is changed or when the
+ * class is destroyed
+ * @param method Method to call to delete the argument
+ */
+void gdcmDicomDir::SetProgressMethodArgDelete(gdcmMethod *method)
+{
+ progressMethodArgDelete=method;
+}
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief Set the end method to call when the parsing of the directory ends
+ * @param method Method to call
+ * @param arg Argument to pass to the method
+ * @param argDelete Argument
+ * \warning In python : the arg parameter isn't considered
+ */
+void gdcmDicomDir::SetEndMethod(gdcmMethod *method, void *arg, gdcmMethod *argDelete)
+{
+ if((endArg)&&(endMethodArgDelete))
+ endMethodArgDelete(endArg);
+
+ endMethod=method;
+ endArg=arg;
+ endMethodArgDelete=argDelete;
+}
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief Set the method to delete the argument
+ * The argument is destroyed when the method is changed or when the class
+ * is destroyed
+ * @param method Method to call to delete the argument
+ */
+void gdcmDicomDir::SetEndMethodArgDelete(gdcmMethod *method)
+{
+ endMethodArgDelete=method;
+}
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief writes on disc a DICOMDIR
+ * \ warning does NOT add the missing elements in the header :
+ * it's up to the user doing it !
+ * \todo : to be re-written using the DICOMDIR tree-like structure
+ * *not* the chained list
+ * (does NOT exist if the DICOMDIR is user-forged !)
+ * @param fileName file to be written to
+ * @return false only when fail to open
+ */
+
+bool gdcmDicomDir::Write(std::string fileName)
+{
+ FILE * fp1;
+
+ fp1=fopen(fileName.c_str(),"wb");
+ if(fp1==NULL)
+ {
+ printf("Failed to open(write) File [%s] \n",fileName.c_str());
+ return(false);
+ }
+
+ char * filePreamble = new char[128];
+ fwrite(filePreamble,128,1,fp1);
+ fwrite("DICM",4,1,fp1);
+ delete[] filePreamble;
+ UpdateDirectoryRecordSequenceLength();
+ WriteEntries(fp1);
+
+ fclose(fp1);
+ return true;
+}
+
+/**
+ * \brief Writes in a file using the tree-like structure.
+ * @param _fp already open file pointer
+ */
+
+void gdcmDicomDir::WriteEntries(FILE *_fp)
+{
+ /// \todo (?) tester les echecs en ecriture
+ /// (apres chaque fwrite, dans le WriteEntry)
+
+
+/* TODO : to go on compiling
+
+ gdcmDicomDirMeta *ptrMeta;
+ ListDicomDirPatient::iterator itPatient;
+ ListDicomDirStudy::iterator itStudy;
+ ListDicomDirSerie::iterator itSerie;
+ ListDicomDirImage::iterator itImage;
+ ListTag::iterator i;
+
+ ptrMeta= GetDicomDirMeta();
+ for(i=ptrMeta->debut();i!=ptrMeta->fin();++i) {
+ WriteEntry(*i,_fp, gdcmExplicitVR);
+ }
+
+ itPatient = GetDicomDirPatients().begin();
+ while ( itPatient != GetDicomDirPatients().end() ) {
+ for(i=(*itPatient)->debut();i!=(*itPatient)->fin();++i) {
+ WriteEntry(*i,_fp, gdcmExplicitVR);
+ }
+ itStudy = ((*itPatient)->GetDicomDirStudies()).begin();
+ while (itStudy != (*itPatient)->GetDicomDirStudies().end() ) {
+ for(i=(*itStudy)->debut();i!=(*itStudy)->fin();++i) {
+ WriteEntry(*i,_fp, gdcmExplicitVR);
+ }
+ itSerie = ((*itStudy)->GetDicomDirSeries()).begin();
+ while (itSerie != (*itStudy)->GetDicomDirSeries().end() ) {
+ for(i=(*itSerie)->debut();i!=(*itSerie)->fin();++i) {
+ WriteEntry(*i,_fp, gdcmExplicitVR);
+ }
+ itImage = ((*itSerie)->GetDicomDirImages()).begin();
+ while (itImage != (*itSerie)->GetDicomDirImages().end() ) {
+ for(i=(*itImage)->debut();i!=(*itImage)->fin();++i) {
+ WriteEntry(*i,_fp, gdcmExplicitVR);
+ }
+ ++itImage;
+ }
+ ++itSerie;
+ }
+ ++itStudy;
+ }
+ ++itPatient;
+ }
+ */
+}
+
+//-----------------------------------------------------------------------------
+// Protected
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief create a gdcmDocument-like chained list from a root Directory
+ * @param path entry point of the tree-like structure
+ */
+void gdcmDicomDir::CreateDicomDirChainedList(std::string path)
+{
+ CallStartMethod();
+
+ gdcmDirList fileList(path,1); // gets recursively the file list
+ unsigned int count=0;
+ ListHeader list;
+ gdcmHeader *header;
+
+ tagHT.clear();
+ patients.clear();
+
+ for(gdcmDirList::iterator it=fileList.begin();
+ it!=fileList.end();
+ ++it)
+ {
+ progress=(float)(count+1)/(float)fileList.size();
+ CallProgressMethod();
+ if(abort)
+ break;
+
+ header=new gdcmHeader(it->c_str());
+ if(header->IsReadable())
+ list.push_back(header); // adds the file header to the chained list
+ else
+ delete header;
+
+ count++;
+ }
+ // sorts Patient/Study/Serie/
+ std::sort(list.begin(),list.end(),gdcmDicomDir::HeaderLessThan);
+
+ std::string tmp=fileList.GetDirName();
+
+ //for each Header of the chained list, add/update the Patient/Study/Serie/Image info
+ SetElements(tmp,list);
+
+ CallEndMethod();
+}
+
+/**
+ * \ingroup gdcmDicomDir
+ * \brief adds *the* Meta to a partially created DICOMDIR
+ */
+
+ // FIXME : Heuuuuu ! Il prend les Entries du Document deja parse,
+ // il ne fabrique rien !
+
+gdcmDicomDirMeta * gdcmDicomDir::NewMeta(void) {
+ gdcmDicomDirMeta *m = new gdcmDicomDirMeta(&tagHT);
+ for (TagDocEntryHT::iterator cc = tagHT.begin();cc != tagHT.end();++cc) {
+ m->AddDocEntry(cc->second);
+ }
+ return m;
+}