]> Creatis software - gdcm.git/blob - src/gdcmDicomDir.cxx
upgrade GdcmHeaderEntry Print Method for DICOMDIR
[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
29    CreateDicomDir();
30 }
31
32
33 /*
34  * \ingroup gdcmDicomDir
35  * \brief   
36  * @param   exception_on_error
37  */
38 gdcmDicomDir::gdcmDicomDir(bool exception_on_error):                           
39    gdcmParser(exception_on_error )  
40 {    
41
42 }
43
44
45
46 gdcmDicomDir::~gdcmDicomDir() 
47 {
48    for(ListPatient::iterator cc = patients.begin();cc!=patients.end();++cc)
49    {
50       delete *cc;
51    }
52 }
53
54 //-----------------------------------------------------------------------------
55 // Print
56 void gdcmDicomDir::Print(std::ostream &os)
57 {
58    for(ListPatient::iterator cc = patients.begin();cc!=patients.end();++cc)
59    {
60       (*cc)->SetPrintLevel(printLevel);
61       (*cc)->Print(os);
62    }
63 }
64
65 //-----------------------------------------------------------------------------
66 // Public
67
68 //-----------------------------------------------------------------------------
69 // Protected
70
71 //-----------------------------------------------------------------------------
72 // Private
73 void gdcmDicomDir::CreateDicomDir(void)
74 {
75    // The list is parsed. When a tag is found :
76    //  1 - we save the beginning iterator
77    //  2 - we continue to parse
78    //  3 - we find an other tag
79    //       + we create the object for the precedent tag
80    //       + loop to 1 -
81
82    gdcmDicomDirType type=gdcmDicomDir::GDCM_NONE;
83    ListTag::iterator begin;
84    ListTag::iterator end;
85
86    begin=GetListEntry().begin();
87    end=begin;
88    for(ListTag::iterator i=GetListEntry().begin();i != GetListEntry().end();++i) 
89    {
90       // std::cout << std::hex << (*i)->GetGroup() << 
91       //                  " " << (*i)->GetElement() << endl;
92
93       std::string v = (*i)->GetValue();
94       if (v == "PATIENT ") 
95       {
96 //         std::cout<<"PATIENT"<<std::endl;
97          end=i;
98          AddObjectToEnd(type,begin,end);
99
100          type=gdcmDicomDir::GDCM_PATIENT;
101          begin=end;
102       } 
103
104       if (v == "STUDY ")
105       {
106 //         std::cout<<"STUDY"<<std::endl;
107          end=i;
108          AddObjectToEnd(type,begin,end);
109
110          type=gdcmDicomDir::GDCM_STUDY;
111          begin=end;
112       }
113
114       if (v == "SERIES") 
115       {
116 //         std::cout<<"SERIES"<<std::endl;
117          end=i;
118          AddObjectToEnd(type,begin,end);
119
120          type=gdcmDicomDir::GDCM_SERIE;
121          begin=end;
122       }
123
124       if (v == "IMAGE ") 
125       {
126 //         std::cout<<"IMAGE"<<std::endl;
127          end=i;
128          AddObjectToEnd(type,begin,end);
129
130          type=gdcmDicomDir::GDCM_IMAGE;
131          begin=end;
132       }
133    }
134
135    end=GetListEntry().end();
136    AddObjectToEnd(type,begin,end);
137 }
138
139 void gdcmDicomDir::AddObjectToEnd(gdcmDicomDirType type,ListTag::iterator begin,ListTag::iterator end)
140 {
141    if(begin==end)
142       return;
143
144    switch(type)
145    {
146       case gdcmDicomDir::GDCM_PATIENT:
147          AddPatientToEnd(begin,end);
148          break;
149       case gdcmDicomDir::GDCM_STUDY:
150          AddStudyToEnd(begin,end);
151          break;
152       case gdcmDicomDir::GDCM_SERIE:
153          AddSerieToEnd(begin,end);
154          break;
155       case gdcmDicomDir::GDCM_IMAGE:
156          AddImageToEnd(begin,end);
157          break;
158    }
159 }
160
161 void gdcmDicomDir::AddPatientToEnd(ListTag::iterator begin,ListTag::iterator end)
162 {
163    patients.push_back(new gdcmPatient(begin,end));
164 }
165
166 void gdcmDicomDir::AddStudyToEnd(ListTag::iterator begin,ListTag::iterator end)
167 {
168    if(patients.size()>0)
169    {
170       ListPatient::iterator itp=patients.end();
171       itp--;
172       (*itp)->AddStudy(new gdcmStudy(begin,end));
173    }
174 }
175
176 void gdcmDicomDir::AddSerieToEnd(ListTag::iterator begin,ListTag::iterator end)
177 {
178    if(patients.size()>0)
179    {
180       ListPatient::iterator itp=patients.end();
181       itp--;
182
183       if((*itp)->GetStudies().size()>0)
184       {
185          ListStudy::iterator itst=(*itp)->GetStudies().end();
186          itst--;
187          (*itst)->AddSerie(new gdcmSerie(begin,end));
188       }
189    }
190 }
191
192 void gdcmDicomDir::AddImageToEnd(ListTag::iterator begin,ListTag::iterator end)
193 {
194    if(patients.size()>0)
195    {
196       ListPatient::iterator itp=patients.end();
197       itp--;
198
199       if((*itp)->GetStudies().size()>0)
200       {
201          ListStudy::iterator itst=(*itp)->GetStudies().end();
202          itst--;
203
204          if((*itst)->GetSeries().size()>0)
205          {
206             ListSerie::iterator its=(*itst)->GetSeries().end();
207             its--;
208             (*its)->AddImage(new gdcmImage(begin,end));
209          }
210       }
211    }
212 }
213
214 //-----------------------------------------------------------------------------