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