2 //-----------------------------------------------------------------------------
3 #include "gdcmDicomDir.h"
11 //-----------------------------------------------------------------------------
12 // Constructor / Destructor
15 * \ingroup gdcmDicomDir
18 * @param exception_on_error
20 gdcmDicomDir::gdcmDicomDir(std::string & FileName,
21 bool exception_on_error):
22 gdcmParser(FileName.c_str(),exception_on_error, true )
24 if ( GetListEntry().begin() == GetListEntry().end() )
26 dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir entry list empty");
33 * \ingroup gdcmDicomDir
35 * @param exception_on_error
37 gdcmDicomDir::gdcmDicomDir(ListTag *l,
38 bool exception_on_error):
39 gdcmParser(exception_on_error )
46 * \ingroup gdcmDicomDir
47 * \brief Canonical destructor
49 gdcmDicomDir::~gdcmDicomDir()
51 for(ListPatient::iterator cc = patients.begin();cc!=patients.end();++cc)
59 //-----------------------------------------------------------------------------
61 void gdcmDicomDir::Print(std::ostream &os)
63 for(ListPatient::iterator cc = patients.begin();cc!=patients.end();++cc)
65 (*cc)->SetPrintLevel(printLevel);
70 //-----------------------------------------------------------------------------
75 * \ingroup gdcmDicomDir
76 * \brief writes on disc a DICOMDIR
77 * \ warning does NOT add the missing elements in the header :
78 * \ it's up to the user doing it !
79 * @param fileName file to be written to
82 bool gdcmDicomDir::Write(std::string fileName) {
85 fp1 = fopen(fileName.c_str(),"wb");
87 printf("Failed to open (write) File [%s] \n",fileName.c_str());
91 filePreamble=(char*)calloc(128,1);
92 fwrite(filePreamble,128,1,fp1);
93 fwrite("DICM",4,1,fp1);
95 WriteEntries(DICOMDIR,fp1);
101 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
107 * \ingroup gdcmDicomDir
111 void gdcmDicomDir::CreateDicomDir()
113 // The list is parsed. When a tag is found :
114 // 1 - we save the beginning iterator
115 // 2 - we continue to parse
116 // 3 - we find an other tag
117 // + we create the object for the precedent tag
120 gdcmDicomDirType type=gdcmDicomDir::GDCM_NONE;
121 ListTag::iterator begin;
122 ListTag::iterator end;
124 begin=listEntries.begin();
126 for(ListTag::iterator i=listEntries.begin();i != listEntries.end();++i)
128 // std::cout << std::hex << (*i)->GetGroup() <<
129 // " " << (*i)->GetElement() << endl;
131 std::string v = (*i)->GetValue();
134 // std::cout<<"PATIENT"<<std::endl;
136 AddObjectToEnd(type,begin,end);
138 type=gdcmDicomDir::GDCM_PATIENT;
144 // std::cout<<"STUDY"<<std::endl;
146 AddObjectToEnd(type,begin,end);
148 type=gdcmDicomDir::GDCM_STUDY;
154 // std::cout<<"SERIES"<<std::endl;
156 AddObjectToEnd(type,begin,end);
158 type=gdcmDicomDir::GDCM_SERIE;
164 // std::cout<<"IMAGE"<<std::endl;
166 AddObjectToEnd(type,begin,end);
168 type=gdcmDicomDir::GDCM_IMAGE;
173 end=GetListEntry().end();
174 AddObjectToEnd(type,begin,end);
177 * \ingroup gdcmDicomDir
181 void gdcmDicomDir::AddObjectToEnd(gdcmDicomDirType type,ListTag::iterator begin,ListTag::iterator end)
188 case gdcmDicomDir::GDCM_PATIENT:
189 AddPatientToEnd(begin,end);
191 case gdcmDicomDir::GDCM_STUDY:
192 AddStudyToEnd(begin,end);
194 case gdcmDicomDir::GDCM_SERIE:
195 AddSerieToEnd(begin,end);
197 case gdcmDicomDir::GDCM_IMAGE:
198 AddImageToEnd(begin,end);
204 * \ingroup gdcmDicomDir
208 void gdcmDicomDir::AddPatientToEnd(ListTag::iterator begin,ListTag::iterator end)
210 patients.push_back(new gdcmPatient(begin,end));
214 * \ingroup gdcmDicomDir
218 void gdcmDicomDir::AddStudyToEnd(ListTag::iterator begin,ListTag::iterator end)
220 if(patients.size()>0)
222 ListPatient::iterator itp=patients.end();
224 (*itp)->AddStudy(new gdcmStudy(begin,end));
228 * \ingroup gdcmDicomDir
232 void gdcmDicomDir::AddSerieToEnd(ListTag::iterator begin,ListTag::iterator end)
234 if(patients.size()>0)
236 ListPatient::iterator itp=patients.end();
239 if((*itp)->GetStudies().size()>0)
241 ListStudy::iterator itst=(*itp)->GetStudies().end();
243 (*itst)->AddSerie(new gdcmSerie(begin,end));
249 * \ingroup gdcmDicomDir
253 void gdcmDicomDir::AddImageToEnd(ListTag::iterator begin,ListTag::iterator end)
255 if(patients.size()>0)
257 ListPatient::iterator itp=patients.end();
260 if((*itp)->GetStudies().size()>0)
262 ListStudy::iterator itst=(*itp)->GetStudies().end();
265 if((*itst)->GetSeries().size()>0)
267 ListSerie::iterator its=(*itst)->GetSeries().end();
269 (*its)->AddImage(new gdcmImage(begin,end));
275 //-----------------------------------------------------------------------------