]> Creatis software - gdcm.git/blob - src/gdcmDicomDir.cxx
078c357514d0e5157380b64ca239907483b94db8
[gdcm.git] / src / gdcmDicomDir.cxx
1 // gdcmDicomDir.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmDicomDir.h"
4 #include "gdcmStudy.h"
5 #include "gdcmSerie.h"
6 #include "gdcmImage.h"
7 #include "gdcmUtil.h"
8
9 #include <string>
10
11 //-----------------------------------------------------------------------------
12 // Constructor / Destructor
13
14 /*
15  * \ingroup gdcmDicomDir
16  * \brief   
17  * @param   Filename
18  * @param   exception_on_error
19  */
20 gdcmDicomDir::gdcmDicomDir(std::string & FileName,
21                            bool exception_on_error):
22    gdcmParser(FileName.c_str(),exception_on_error, true )  
23 {
24    if ( GetListEntry().begin() == GetListEntry().end() ) 
25    {
26       dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir entry list empty");
27    }     
28    CreateDicomDir();
29 }
30
31
32 /*
33  * \ingroup gdcmDicomDir
34  * \brief   
35  * @param   exception_on_error
36  */
37 gdcmDicomDir::gdcmDicomDir(ListTag *l,
38                            bool exception_on_error):                           
39    gdcmParser(exception_on_error )  
40 {    
41    listEntries=*l;
42    CreateDicomDir();
43 }
44
45 /*
46  * \ingroup gdcmDicomDir
47  * \brief  Canonical destructor 
48  */
49 gdcmDicomDir::~gdcmDicomDir() 
50 {
51    for(ListPatient::iterator cc = patients.begin();cc!=patients.end();++cc)
52    {
53       delete *cc;
54    }
55 }
56
57
58
59 //-----------------------------------------------------------------------------
60 // Print
61 void gdcmDicomDir::Print(std::ostream &os)
62 {
63    for(ListPatient::iterator cc = patients.begin();cc!=patients.end();++cc)
64    {
65       (*cc)->SetPrintLevel(printLevel);
66       (*cc)->Print(os);
67    }
68 }
69
70 //-----------------------------------------------------------------------------
71 // Public
72
73
74 /**
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 
80  * @return
81  */
82 bool gdcmDicomDir::Write(std::string fileName) {
83
84    FILE * fp1;
85    fp1 = fopen(fileName.c_str(),"wb");
86    if (fp1 == NULL) {
87       printf("Failed to open (write) File [%s] \n",fileName.c_str());
88       return (false);
89    }
90    char * filePreamble;  
91    filePreamble=(char*)calloc(128,1);
92    fwrite(filePreamble,128,1,fp1);
93    fwrite("DICM",4,1,fp1);
94    free (filePreamble);
95    WriteEntries(DICOMDIR,fp1);
96     
97    return true;
98
99 }
100
101 //-----------------------------------------------------------------------------
102 // Protected
103
104 //-----------------------------------------------------------------------------
105 // Private
106 /*
107  * \ingroup gdcmDicomDir
108  * \brief   
109  * @param   
110  */
111 void gdcmDicomDir::CreateDicomDir()
112 {
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
118    //       + loop to 1 -
119
120    gdcmDicomDirType type=gdcmDicomDir::GDCM_NONE;
121    ListTag::iterator begin;
122    ListTag::iterator end;
123
124    begin=listEntries.begin();
125    end=begin;
126    for(ListTag::iterator i=listEntries.begin();i != listEntries.end();++i) 
127    {
128       // std::cout << std::hex << (*i)->GetGroup() << 
129       //                  " " << (*i)->GetElement() << endl;
130
131       std::string v = (*i)->GetValue();
132       if (v == "PATIENT ") 
133       {
134        //  std::cout<<"PATIENT"<<std::endl;
135          end=i;
136          AddObjectToEnd(type,begin,end);
137
138          type=gdcmDicomDir::GDCM_PATIENT;
139          begin=end;
140       } 
141
142       if (v == "STUDY ")
143       {
144        //  std::cout<<"STUDY"<<std::endl;
145          end=i;
146          AddObjectToEnd(type,begin,end);
147
148          type=gdcmDicomDir::GDCM_STUDY;
149          begin=end;
150       }
151
152       if (v == "SERIES") 
153       {
154        //  std::cout<<"SERIES"<<std::endl;
155          end=i;
156          AddObjectToEnd(type,begin,end);
157
158          type=gdcmDicomDir::GDCM_SERIE;
159          begin=end;
160       }
161
162       if (v == "IMAGE ") 
163       {
164        //  std::cout<<"IMAGE"<<std::endl;
165          end=i;
166          AddObjectToEnd(type,begin,end);
167
168          type=gdcmDicomDir::GDCM_IMAGE;
169          begin=end;
170       }
171    }
172
173    end=GetListEntry().end();
174    AddObjectToEnd(type,begin,end);
175 }
176 /*
177  * \ingroup gdcmDicomDir
178  * \brief   
179  * @param   
180  */
181 void gdcmDicomDir::AddObjectToEnd(gdcmDicomDirType type,ListTag::iterator begin,ListTag::iterator end)
182 {
183    if(begin==end)
184       return;
185
186    switch(type)
187    {
188       case gdcmDicomDir::GDCM_PATIENT:
189          AddPatientToEnd(begin,end);
190          break;
191       case gdcmDicomDir::GDCM_STUDY:
192          AddStudyToEnd(begin,end);
193          break;
194       case gdcmDicomDir::GDCM_SERIE:
195          AddSerieToEnd(begin,end);
196          break;
197       case gdcmDicomDir::GDCM_IMAGE:
198          AddImageToEnd(begin,end);
199          break;
200    }
201 }
202
203 /*
204  * \ingroup gdcmDicomDir
205  * \brief   
206  * @param   
207  */
208 void gdcmDicomDir::AddPatientToEnd(ListTag::iterator begin,ListTag::iterator end)
209 {
210    patients.push_back(new gdcmPatient(begin,end));
211 }
212
213 /*
214  * \ingroup gdcmDicomDir
215  * \brief   
216  * @param   
217  */
218  void gdcmDicomDir::AddStudyToEnd(ListTag::iterator begin,ListTag::iterator end)
219 {
220    if(patients.size()>0)
221    {
222       ListPatient::iterator itp=patients.end();
223       itp--;
224       (*itp)->AddStudy(new gdcmStudy(begin,end));
225    }
226 }
227 /*
228  * \ingroup gdcmDicomDir
229  * \brief   
230  * @param   
231  */
232 void gdcmDicomDir::AddSerieToEnd(ListTag::iterator begin,ListTag::iterator end)
233 {
234    if(patients.size()>0)
235    {
236       ListPatient::iterator itp=patients.end();
237       itp--;
238
239       if((*itp)->GetStudies().size()>0)
240       {
241          ListStudy::iterator itst=(*itp)->GetStudies().end();
242          itst--;
243          (*itst)->AddSerie(new gdcmSerie(begin,end));
244       }
245    }
246 }
247
248 /*
249  * \ingroup gdcmDicomDir
250  * \brief   
251  * @param   
252  */
253  void gdcmDicomDir::AddImageToEnd(ListTag::iterator begin,ListTag::iterator end)
254 {
255    if(patients.size()>0)
256    {
257       ListPatient::iterator itp=patients.end();
258       itp--;
259
260       if((*itp)->GetStudies().size()>0)
261       {
262          ListStudy::iterator itst=(*itp)->GetStudies().end();
263          itst--;
264
265          if((*itst)->GetSeries().size()>0)
266          {
267             ListSerie::iterator its=(*itst)->GetSeries().end();
268             its--;
269             (*its)->AddImage(new gdcmImage(begin,end));
270          }
271       }
272    }
273 }
274
275 //-----------------------------------------------------------------------------