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