]> Creatis software - gdcm.git/blob - src/gdcmDicomDirElement.cxx
Replace confusing names gdcmImage, etc by gdcmDicomDirImage etc, to avoid
[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              DicomDirMetaList.push_back(elem);
57          else if(type=="patientElem")
58             DicomDirPatientList.push_back(elem);
59          else if(type=="studyElem")
60             DicomDirStudyList.push_back(elem);
61          else if(type=="serieElem")
62             DicomDirSerieList.push_back(elem);
63          else if(type=="imageElem")
64             DicomDirImageList.push_back(elem);
65       }
66       from.getline(buff, 1024, '\n');
67    }
68    from.close();
69 }
70
71 /**
72  * \ingroup gdcmDicomDirElement
73  * \brief   destructor 
74  */
75  gdcmDicomDirElement::~gdcmDicomDirElement() {
76    DicomDirMetaList.clear();
77    DicomDirPatientList.clear();
78    DicomDirStudyList.clear();
79    DicomDirSerieList.clear();
80    DicomDirImageList.clear();
81 }
82
83 //-----------------------------------------------------------------------------
84 // Print
85 /**
86  * \ingroup gdcmDicomDirElement
87  * \brief   Print all
88  * \todo add a 'Print Level' check 
89  * @param   os The output stream to be written to.
90  */
91 void gdcmDicomDirElement::Print(std::ostream &os) {
92    std::ostringstream s;
93    std::list<gdcmElement>::iterator it;
94    char greltag[10];  //group element tag
95
96    s << "Meta Elements :"<<std::endl;
97    for (it = DicomDirMetaList.begin();it!=DicomDirMetaList.end();++it)
98    {
99       sprintf(greltag,"%04x|%04x ",it->group,it->elem);
100       s << "   ("<<greltag<<") = "<< it->value<<std::endl;
101    }
102
103    s << "Patient Elements :"<<std::endl;
104    for (it = DicomDirPatientList.begin();it!=DicomDirPatientList.end();++it)
105    {
106       sprintf(greltag,"%04x|%04x ",it->group,it->elem);
107       s << "   ("<<greltag<<") = "<< it->value<<std::endl;
108    }
109
110    s << "Study Elements :"<<std::endl;
111    for (it = DicomDirStudyList.begin();it!=DicomDirStudyList.end();++it)
112    {
113       sprintf(greltag,"%04x|%04x ",it->group,it->elem);
114       s << "   ("<<greltag<<") = "<< it->value<<std::endl;
115    }
116
117    s << "Serie Elements :"<<std::endl;
118    for (it = DicomDirSerieList.begin();it!=DicomDirSerieList.end();++it)
119    {
120       sprintf(greltag,"%04x|%04x ",it->group,it->elem);
121       s << "   ("<<greltag<<") = "<< it->value<<std::endl;
122    }
123
124    s << "Image Elements :"<<std::endl;
125    for (it = DicomDirImageList.begin();it!=DicomDirImageList.end();++it)
126    {
127       sprintf(greltag,"%04x|%04x ",it->group,it->elem);
128       s << "   ("<<greltag<<") = "<< it->value<<std::endl;
129    }
130
131    os << s.str();
132 }
133
134 //-----------------------------------------------------------------------------
135 // Public
136
137 //-----------------------------------------------------------------------------
138 // Protected
139
140 //-----------------------------------------------------------------------------
141 // Private
142
143 //-----------------------------------------------------------------------------