+/**
+ * \brief adds to the HTable
+ * the Entries (Dicom Elements) corresponding to the given type
+ * @param path full path file name (only used when type = GDCM_DICOMDIR_IMAGE
+ * @param type DicomDirObject type to create (GDCM_DICOMDIR_PATIENT,
+ * GDCM_DICOMDIR_STUDY, GDCM_DICOMDIR_SERIE ...)
+ * @param header Header of the current file
+ */
+void DicomDir::SetElement(std::string const &path, DicomDirType type,
+ Document *header)
+{
+ ListDicomDirElem elemList;
+ ListDicomDirElem::const_iterator it;
+ uint16_t tmpGr, tmpEl;
+ //DictEntry *dictEntry;
+ DataEntry *entry;
+ std::string val;
+ SQItem *si;
+ switch( type )
+ {
+ case GDCM_DICOMDIR_IMAGE:
+ elemList = Global::GetDicomDirElements()->GetDicomDirImageElements();
+ si = DicomDirImage::New(true);
+ if ( !AddImageToEnd(static_cast<DicomDirImage *>(si)) )
+ {
+ si->Delete();
+ gdcmErrorMacro( "Add ImageToEnd failed");
+ }
+ break;
+ case GDCM_DICOMDIR_SERIE:
+ elemList = Global::GetDicomDirElements()->GetDicomDirSerieElements();
+ si = DicomDirSerie::New(true);
+ if ( !AddSerieToEnd(static_cast<DicomDirSerie *>(si)) )
+ {
+ si->Delete();
+ gdcmErrorMacro( "Add SerieToEnd failed");
+ }
+ break;
+ case GDCM_DICOMDIR_STUDY:
+ elemList = Global::GetDicomDirElements()->GetDicomDirStudyElements();
+ si = DicomDirStudy::New(true);
+ if ( !AddStudyToEnd(static_cast<DicomDirStudy *>(si)) )
+ {
+ si->Delete();
+ gdcmErrorMacro( "Add StudyToEnd failed");
+ }
+ break;
+ case GDCM_DICOMDIR_PATIENT:
+ elemList = Global::GetDicomDirElements()->GetDicomDirPatientElements();
+ si = DicomDirPatient::New(true);
+ if ( !AddPatientToEnd(static_cast<DicomDirPatient *>(si)) )
+ {
+ si->Delete();
+ gdcmErrorMacro( "Add PatientToEnd failed");
+ }
+ break;
+ case GDCM_DICOMDIR_META: // never used ?!? --> Done within DoTheLoadingJob
+ if ( MetaElems )
+ {
+ MetaElems->Delete();
+ gdcmErrorMacro( "MetaElements already exist, they will be destroyed");
+ }
+ elemList = Global::GetDicomDirElements()->GetDicomDirMetaElements();
+ MetaElems = DicomDirMeta::New(true);
+ si = MetaElems;
+ break;
+ default:
+ return;
+ }
+
+ // FIXME : troubles found when it's a SeqEntry
+
+ // removed all the seems-to-be-useless stuff about Referenced Image Sequence
+ // to avoid further troubles
+ // imageElem 0008 1140 "" // Referenced Image Sequence
+ // imageElem fffe e000 "" // 'no length' item : length to be set to 0xffffffff later
+ // imageElem 0008 1150 "" // Referenced SOP Class UID : to be set/forged later
+ // imageElem 0008 1155 "" // Referenced SOP Instance UID : to be set/forged later
+ // imageElem fffe e00d "" // Item delimitation : length to be set to ZERO later
+
+ std::string referencedVal;
+ // for all the relevant elements found in their own spot of the DicomDir.dic
+ for( it = elemList.begin(); it != elemList.end(); ++it)
+ {
+ tmpGr = it->Group;
+ tmpEl = it->Elem;
+
+ entry = DataEntry::New(tmpGr, tmpEl, it->VR); // dicomelements file was modified, to store VR
+ entry->SetOffset(0); // just to avoid further missprinting
+
+ if ( header )
+ {
+ // NULL when we Build Up (ex nihilo) a DICOMDIR
+ // or when we add the META elems
+ val = header->GetEntryString(tmpGr, tmpEl);
+ }
+ else
+ {
+ val = GDCM_UNFOUND;
+ }
+
+ if ( val == GDCM_UNFOUND)
+ {
+ if ( tmpGr == 0x0004 ) // never present in File !
+ {
+ switch (tmpEl)
+ {
+ case 0x1130: // File-set ID
+ // force to the *end* File Name
+ val = Util::GetName( path );
+ break;
+
+ case 0x1500: // Only used for image
+ if ( header->GetFileName().substr(0, path.length()) != path )
+ {
+ gdcmWarningMacro( "The base path of file name is incorrect");
+ val = header->GetFileName();
+ }
+ else
+ {
+ // avoid the first '/' in File name !
+ if ( header->GetFileName().c_str()[path.length()]
+ == GDCM_FILESEPARATOR )
+ val = &(header->GetFileName().c_str()[path.length()+1]);
+ else
+ val = &(header->GetFileName().c_str()[path.length()]);
+ }
+ break;
+
+ case 0x1510: // Referenced SOP Class UID in File
+ referencedVal = header->GetEntryString(0x0008, 0x0016);
+ // FIXME : probabely something to check
+ val = referencedVal;
+ break;
+
+ case 0x1511: // Referenced SOP Instance UID in File
+ referencedVal = header->GetEntryString(0x0008, 0x0018);
+ // FIXME : probabely something to check
+ val = referencedVal;
+ break;
+
+ case 0x1512: // Referenced Transfer Syntax UID in File
+ referencedVal = header->GetEntryString(0x0002, 0x0010);
+ // FIXME : probabely something to check
+ val = referencedVal;
+ break;
+
+ default :
+ val = it->Value;
+ }
+ }
+ else
+ {
+ // If the entry is not found in the Header, don't write its 'value' in the DICOMDIR !
+ entry->Delete();
+ continue;
+ }
+ }
+ else
+ {
+ if ( header->GetEntryLength(tmpGr,tmpEl) == 0 )
+ {
+ val = it->Value;
+ // Don't polute the DICOMDIR with empty fields
+ if (val == "")
+ {
+ entry->Delete();
+ continue;
+ }
+ }
+ }
+
+/* FIX later the pb of creating the 'Implementation Version Name'!
+
+ if (val == GDCM_UNFOUND)
+ val = "";
+
+ if ( tmpGr == 0x0002 && tmpEl == 0x0013)
+ {
+ // 'Implementation Version Name'
+ std::string val = "GDCM ";
+ val += Util::GetVersion();
+ }
+*/
+
+ entry->SetString( val ); // troubles expected when vr=SQ ...
+
+ if ( type == GDCM_DICOMDIR_META ) // fusible : should never print !
+ {
+ gdcmDebugMacro("GDCM_DICOMDIR_META ?!? should never print that");
+ }
+
+ si->AddEntry(entry);
+ entry->Delete();
+ }
+}
+