+/**
+ * \brief adds *the* Meta to a partially created DICOMDIR
+ */
+DicomDirMeta *DicomDir::NewMeta()
+{
+ if ( MetaElems )
+ {
+ MetaElems->Delete();
+ }
+ DocEntry *entry = GetFirstEntry();
+ if ( entry )
+ {
+ MetaElems = DicomDirMeta::New(true); // true = empty
+
+ entry = GetFirstEntry();
+ while( entry )
+ {
+ if ( dynamic_cast<SeqEntry *>(entry) )
+ break;
+
+ MetaElems->AddEntry(entry);
+ RemoveEntry(entry);
+
+ entry = GetFirstEntry();
+ }
+ }
+ else // after root directory parsing
+ {
+ MetaElems = DicomDirMeta::New(false); // false = not empty
+ }
+ MetaElems->SetSQItemNumber(0); // To avoid further missprinting
+ return MetaElems;
+}
+
+/**
+ * \brief adds a new Patient (with the basic elements) to a partially created
+ * DICOMDIR
+ */
+DicomDirPatient *DicomDir::NewPatient()
+{
+ DicomDirPatient *dd = DicomDirPatient::New();
+ AddPatientToEnd( dd );
+ return dd;
+}
+
+/**
+ * \brief Remove all Patients
+ */
+void DicomDir::ClearPatient()
+{
+ for(ListDicomDirPatient::iterator cc = Patients.begin();
+ cc!= Patients.end();
+ ++cc)
+ {
+ (*cc)->Unregister();
+ }
+ Patients.clear();
+}
+
+/**
+ * \brief Get the first entry while visiting the DicomDirPatients
+ * \return The first DicomDirPatient if found, otherwhise NULL
+ */
+DicomDirPatient *DicomDir::GetFirstPatient()
+{
+ ItPatient = Patients.begin();
+ if ( ItPatient != Patients.end() )
+ return *ItPatient;
+ return NULL;
+}
+
+/**
+ * \brief Get the next entry while visiting the DicomDirPatients
+ * \note : meaningfull only if GetFirstEntry already called
+ * \return The next DicomDirPatient if found, otherwhise NULL
+ */
+DicomDirPatient *DicomDir::GetNextPatient()
+{
+ gdcmAssertMacro (ItPatient != Patients.end());
+
+ ++ItPatient;
+ if ( ItPatient != Patients.end() )
+ return *ItPatient;
+ return NULL;
+}
+
+/**
+ * \brief fills the whole structure, starting from a root Directory
+ */
+void DicomDir::ParseDirectory()
+{
+ CreateDicomDirChainedList( GetFileName() );
+ CreateDicomDir();
+}
+
+/**
+ * \brief writes on disc a DICOMDIR
+ * \ warning does NOT add the missing elements in the header :
+ * it's up to the user doing it !
+ * @param fileName file to be written to
+ * @return false only when fail to open
+ */
+
+bool DicomDir::Write(std::string const &fileName)
+{
+ int i;
+ uint16_t sq[6] = { 0x0004, 0x1220, 0x5153, 0x0000, 0xffff, 0xffff };
+ uint16_t sqt[4]= { 0xfffe, 0xe0dd, 0x0000, 0x0000 };
+
+ std::ofstream *fp = new std::ofstream(fileName.c_str(),
+ std::ios::out | std::ios::binary);
+ if ( !fp )
+ {
+ gdcmWarningMacro("Failed to open(write) File: " << fileName.c_str());
+ return false;
+ }
+
+ char filePreamble[128];
+ memset(filePreamble, 0, 128);
+ fp->write(filePreamble, 128);
+ binary_write( *fp, "DICM");
+
+ DicomDirMeta *ptrMeta = GetMeta();
+ ptrMeta->WriteContent(fp, ExplicitVR);
+
+ // force writing 0004|1220 [SQ ], that CANNOT exist within DicomDirMeta
+ for(i=0;i<6;++i)
+ {
+ binary_write(*fp, sq[i]);
+ }
+
+ for(ListDicomDirPatient::iterator cc = Patients.begin();
+ cc != Patients.end();
+ ++cc )
+ {
+ (*cc)->WriteContent( fp, ExplicitVR );
+ }
+
+ // force writing Sequence Delimitation Item
+ for(i=0;i<4;++i)
+ {
+ binary_write(*fp, sqt[i]); // fffe e0dd 0000 0000
+ }
+
+ fp->close();
+ delete fp;
+
+ return true;
+}
+
+/**
+ * \brief Anonymize a DICOMDIR
+ * @return true
+ */
+
+bool DicomDir::Anonymize()
+{
+ DataEntry *v;
+ // Something clever to be found to forge the Patient names
+ std::ostringstream s;
+ int i = 1;
+ for(ListDicomDirPatient::iterator cc = Patients.begin();
+ cc!= Patients.end();
+ ++cc)
+ {
+ s << i;
+ v = (*cc)->GetDataEntry(0x0010, 0x0010) ; // Patient's Name
+ if (v)
+ {
+ v->SetString(s.str());
+ }
+
+ v = (*cc)->GetDataEntry(0x0010, 0x0020) ; // Patient ID
+ if (v)
+ {
+ v->SetString(" ");
+ }
+
+ v = (*cc)->GetDataEntry(0x0010, 0x0030) ; // Patient's BirthDate
+ if (v)
+ {
+ v->SetString(" ");
+ }
+ s << "";
+ i++;
+ }
+ return true;
+}
+
+/**
+ * \brief Copies all the attributes from an other DocEntrySet
+ * @param set entry to copy from
+ * @remarks The contained DocEntries are not copied, only referenced
+ */
+void DicomDir::Copy(DocEntrySet *set)
+{
+ // Remove all previous childs
+ ClearPatient();
+
+ Document::Copy(set);
+
+ DicomDir *dd = dynamic_cast<DicomDir *>(set);
+ if( dd )
+ {
+ if(MetaElems)
+ MetaElems->Unregister();
+ MetaElems = dd->MetaElems;
+ if(MetaElems)
+ MetaElems->Register();
+
+ Patients = dd->Patients;
+ for(ItPatient = Patients.begin();ItPatient != Patients.end();++ItPatient)
+ (*ItPatient)->Register();
+ }
+}