]> Creatis software - gdcm.git/blob - src/gdcmDicomDirElement.cxx
* ENH : now gdcmDicomDir::CreateDicomDir() returns also the meta elements
[gdcm.git] / src / gdcmDicomDirElement.cxx
1 // gdcmDicomDirElement.cxx
2 //-----------------------------------------------------------------------------
3 #include <fstream>
4 #include <stdio.h>    // For sprintf
5
6 #include "gdcmDicomDirElement.h"
7 #include "gdcmUtil.h"
8
9 #ifndef PUB_DICT_PATH
10 #  define PUB_DICT_PATH     "../Dicts/"
11 #endif
12 #define DICT_ELEM "DicomDir.dic"
13
14 #include <iostream>
15 #ifdef GDCM_NO_ANSI_STRING_STREAM
16 #  include <strstream>
17 #  define  ostringstream ostrstream
18 # else
19 #  include <sstream>
20 #endif
21
22 //-----------------------------------------------------------------------------
23 // Constructor / Destructor
24
25 /**
26  * \ingroup gdcmDicomDirElement
27  * \brief   constructor 
28  */
29  gdcmDicomDirElement::gdcmDicomDirElement(void) {
30    std::string filename=gdcmDictSet::BuildDictPath() + std::string(DICT_ELEM);
31    std::ifstream from(filename.c_str());
32    dbg.Error(!from, "gdcmDicomDirElement::gdcmDicomDirElement: can't open dictionary",filename.c_str());
33
34    char buff[1024];
35    std::string type;
36    gdcmElement elem;
37
38    while (!from.eof()) {
39       eatwhite(from);
40       from.getline(buff, 1024, ' ');
41       type = buff;
42
43       if( (type=="metaElem")  || (type=="patientElem") || 
44           (type=="studyElem") || (type=="serieElem")   || 
45           (type=="imageElem") )
46       {
47          from >> std::hex >> elem.group >> elem.elem;
48
49          eatwhite(from);
50          from.getline(buff, 1024, '"');
51          eatwhite(from);
52          from.getline(buff, 1024, '"');
53          elem.value = buff;
54
55          if(type=="metaElem")
56             MetaList.push_back(elem);
57          else if(type=="patientElem")
58             PatientList.push_back(elem);
59          else if(type=="studyElem")
60             StudyList.push_back(elem);
61          else if(type=="serieElem")
62             SerieList.push_back(elem);
63          else if(type=="imageElem")
64             ImageList.push_back(elem);
65       }
66
67       from.getline(buff, 1024, '\n');
68    }
69    from.close();
70 }
71
72 /**
73  * \ingroup gdcmDicomDirElement
74  * \brief   destructor 
75  */
76  gdcmDicomDirElement::~gdcmDicomDirElement() {
77    MetaList.clear();
78    PatientList.clear();
79    StudyList.clear();
80    SerieList.clear();
81    ImageList.clear();
82 }
83
84 //-----------------------------------------------------------------------------
85 // Print
86 /**
87  * \ingroup gdcmDicomDirElement
88  * \brief   Print all
89  * \todo add a 'Print Level' check 
90  * @param   os The output stream to be written to.
91  */
92 void gdcmDicomDirElement::Print(std::ostream &os) {
93    std::ostringstream s;
94    std::list<gdcmElement>::iterator it;
95    char greltag[10];  //group element tag
96
97    s << "Meta Elements :"<<std::endl;
98    for (it = MetaList.begin();it!=MetaList.end();++it)
99    {
100       sprintf(greltag,"%04x|%04x ",it->group,it->elem);
101       s << "   ("<<greltag<<") = "<< it->value<<std::endl;
102    }
103
104    s << "Patient Elements :"<<std::endl;
105    for (it = PatientList.begin();it!=PatientList.end();++it)
106    {
107       sprintf(greltag,"%04x|%04x ",it->group,it->elem);
108       s << "   ("<<greltag<<") = "<< it->value<<std::endl;
109    }
110
111    s << "Study Elements :"<<std::endl;
112    for (it = StudyList.begin();it!=StudyList.end();++it)
113    {
114       sprintf(greltag,"%04x|%04x ",it->group,it->elem);
115       s << "   ("<<greltag<<") = "<< it->value<<std::endl;
116    }
117
118    s << "Serie Elements :"<<std::endl;
119    for (it = SerieList.begin();it!=SerieList.end();++it)
120    {
121       sprintf(greltag,"%04x|%04x ",it->group,it->elem);
122       s << "   ("<<greltag<<") = "<< it->value<<std::endl;
123    }
124
125    s << "Image Elements :"<<std::endl;
126    for (it = ImageList.begin();it!=ImageList.end();++it)
127    {
128       sprintf(greltag,"%04x|%04x ",it->group,it->elem);
129       s << "   ("<<greltag<<") = "<< it->value<<std::endl;
130    }
131
132    os << s.str();
133 }
134
135 //-----------------------------------------------------------------------------
136 // Public
137
138 //-----------------------------------------------------------------------------
139 // Protected
140
141 //-----------------------------------------------------------------------------
142 // Private
143
144 //-----------------------------------------------------------------------------