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