+/**
+ * \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 Document is the one of a reasonable DicomDir,
+ * false otherwise.
+ */
+bool DicomDir::IsReadable()
+{
+ if( !Document::IsReadable() )
+ {
+ return false;
+ }
+ if( !MetaElems )
+ {
+ return false;
+ }
+ if( Patients.size() <= 0 )
+ {
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * \brief Sets all fields to NULL
+ */
+
+void DicomDir::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 DicomDir
+ * \brief fills the whole structure, starting from a root Directory
+ */
+void DicomDir::ParseDirectory()
+{
+ CreateDicomDirChainedList( GetFileName() );
+ CreateDicomDir();
+}
+
+/**
+ * \ingroup DicomDir
+ * \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 DicomDir::SetStartMethod(Method* method, void* arg,
+ Method* argDelete )
+{
+ if( StartArg && StartMethodArgDelete )
+ {
+ StartMethodArgDelete( StartArg );
+ }
+
+ StartMethod = method;
+ StartArg = arg;
+ StartMethodArgDelete = argDelete;
+}
+
+/**
+ * \ingroup DicomDir
+ * \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 DicomDir::SetStartMethodArgDelete(Method* method)
+{
+ StartMethodArgDelete = method;
+}
+
+/**
+ * \ingroup DicomDir
+ * \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 DicomDir::SetProgressMethod(Method* method, void* arg,
+ Method* argDelete )
+{
+ if( ProgressArg && ProgressMethodArgDelete )
+ {
+ ProgressMethodArgDelete( ProgressArg );
+ }
+
+ ProgressMethod = method;
+ ProgressArg = arg;
+ ProgressMethodArgDelete = argDelete;
+}
+
+/**
+ * \ingroup DicomDir
+ * \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 DicomDir::SetProgressMethodArgDelete(Method* method)
+{
+ ProgressMethodArgDelete = method;
+}
+
+/**
+ * \ingroup DicomDir
+ * \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 DicomDir::SetEndMethod(Method* method, void* arg,
+ Method* argDelete )
+{
+ if( EndArg && EndMethodArgDelete )
+ {
+ EndMethodArgDelete( EndArg );
+ }
+
+ EndMethod = method;
+ EndArg = arg;
+ EndMethodArgDelete = argDelete;
+}
+
+/**
+ * \ingroup DicomDir
+ * \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 DicomDir::SetEndMethodArgDelete(Method* method)
+{
+ EndMethodArgDelete = method;
+}
+
+/**
+ * \ingroup DicomDir
+ * \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 DicomDir::WriteDicomDir(std::string const& fileName)
+{
+ int i;
+ uint16_t sq[4] = { 0x0004, 0x1220, 0xffff, 0xffff };
+ uint16_t sqt[4]= { 0xfffe, 0xe0dd, 0xffff, 0xffff };
+
+ std::ofstream* fp = new std::ofstream(fileName.c_str(),
+ std::ios::out | std::ios::binary);
+ if( !fp )
+ {
+ dbg.Verbose(2, "Failed to open(write) File: ", fileName.c_str());
+ return false;
+ }
+
+ char filePreamble[128];
+ memset(filePreamble, 0, 128);
+ fp->write(filePreamble, 128); //FIXME
+ binary_write( *fp, "DICM");
+
+ DicomDirMeta *ptrMeta = GetDicomDirMeta();
+ ptrMeta->Write(fp, ExplicitVR);
+
+ // force writing 0004|1220 [SQ ], that CANNOT exist within DicomDirMeta
+ for(i=0;i<4;++i)
+ {
+ binary_write(*fp, sq[i]);
+ }
+
+ for(ListDicomDirPatient::iterator cc = Patients.begin();
+ cc != Patients.end();
+ ++cc )
+ {
+ (*cc)->Write( fp, ExplicitVR );
+ }
+
+ // force writing Sequence Delimitation Item
+ for(i=0;i<4;++i)
+ {
+ binary_write(*fp, sqt[i]); // fffe e0dd ffff ffff
+ }
+
+ fp->close();
+ return true;
+}