]> Creatis software - gdcm.git/blob - src/gdcmDicomDirElement.cxx
aefcd12d74d9f4d9767e933db051c5e29ac8ca72
[gdcm.git] / src / gdcmDicomDirElement.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDicomDirElement.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/10/12 04:35:44 $
7   Version:   $Revision: 1.20 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmDicomDirElement.h"
20 #include "gdcmUtil.h"
21 #include "gdcmDebug.h"
22 #include "gdcmDictSet.h"
23
24 #include <fstream>
25 #include <iostream>
26 namespace gdcm 
27 {
28
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
31
32 /**
33  * \brief   constructor : populates the chained lists 
34  *          from the file 'Dicts/DicomDir.dic'
35  */
36 DicomDirElement::DicomDirElement()
37 {
38    std::string filename = DictSet::BuildDictPath() + std::string(DICT_ELEM);
39    std::ifstream from(filename.c_str());
40    dbg.Error(!from, "DicomDirElement::DicomDirElement: can't open dictionary",
41               filename.c_str());
42
43    char buff[1024];
44    std::string type;
45    Element elem;
46
47    while (!from.eof())
48    {
49       from >> std::ws;
50       from.getline(buff, 1024, ' ');
51       type = buff;
52
53       if( (type=="metaElem")  || (type=="patientElem") || 
54           (type=="studyElem") || (type=="serieElem")   || 
55           (type=="imageElem") )
56       {
57          from >> std::hex >> elem.Group >> elem.Elem;
58
59          from >> std::ws;
60          from.getline(buff, 1024, '"');
61          from >> std::ws;
62          from.getline(buff, 1024, '"');
63          elem.Value = buff;
64
65          if( type == "metaElem" )
66          {
67             DicomDirMetaList.push_back(elem);
68          }
69          else if( type == "patientElem" )
70          {
71             DicomDirPatientList.push_back(elem);
72          }
73          else if( type == "studyElem" )
74          {
75             DicomDirStudyList.push_back(elem);
76          }
77          else if( type == "serieElem" )
78          {
79             DicomDirSerieList.push_back(elem);
80          }
81          else if( type == "imageElem" )
82          {
83             DicomDirImageList.push_back(elem);
84          }
85       }
86       from.getline(buff, 1024, '\n');
87    }
88    from.close();
89 }
90
91 /**
92  * \ingroup DicomDirElement
93  * \brief   canonical destructor 
94  */
95 DicomDirElement::~DicomDirElement()
96 {
97    DicomDirMetaList.clear();
98    DicomDirPatientList.clear();
99    DicomDirStudyList.clear();
100    DicomDirSerieList.clear();
101    DicomDirImageList.clear();
102 }
103
104 //-----------------------------------------------------------------------------
105 // Print
106 /**
107  * \ingroup DicomDirElement
108  * \brief   Print all
109  * \todo add a 'Print Level' check 
110  * @param   os The output stream to be written to.
111  */
112 void DicomDirElement::Print(std::ostream &os)
113 {
114    std::ostringstream s;
115    std::list<Element>::iterator it;
116    //char greltag[10];  //group element tag
117    std::string greltag;
118
119    s << "Meta Elements :"<<std::endl;
120    for (it = DicomDirMetaList.begin(); it != DicomDirMetaList.end(); ++it)
121    {
122       greltag = Util::Format("%04x|%04x ",it->Group,it->Elem);
123       s << "   (" << greltag << ") = " << it->Value << std::endl;
124    }
125
126    s << "Patient Elements :"<<std::endl;
127    for (it = DicomDirPatientList.begin(); it != DicomDirPatientList.end(); ++it)
128    {
129       greltag = Util::Format("%04x|%04x ",it->Group,it->Elem);
130       s << "   (" << greltag << ") = " << it->Value << std::endl;
131    }
132
133    s << "Study Elements :"<<std::endl;
134    for (it = DicomDirStudyList.begin(); it != DicomDirStudyList.end(); ++it)
135    {
136       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
137       s << "   (" << greltag << ") = " << it->Value << std::endl;
138    }
139
140    s << "Serie Elements :"<<std::endl;
141    for (it = DicomDirSerieList.begin(); it != DicomDirSerieList.end(); ++it)
142    {
143       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
144       s << "   (" << greltag << ") = " << it->Value << std::endl;
145    }
146
147    s << "Image Elements :"<<std::endl;
148    for (it = DicomDirImageList.begin(); it != DicomDirImageList.end(); ++it)
149    {
150       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
151       s << "   (" << greltag << ") = " << it->Value << std::endl;
152    }
153
154    os << s.str();
155 }
156
157 //-----------------------------------------------------------------------------
158 // Public
159
160 //-----------------------------------------------------------------------------
161 // Protected
162
163 //-----------------------------------------------------------------------------
164 // Private
165
166 //-----------------------------------------------------------------------------
167
168 } // end namespace gdcm