]> Creatis software - gdcm.git/blob - src/gdcmDicomDirElement.cxx
* src/*.cxx : first parss to normalize file organisation
[gdcm.git] / src / gdcmDicomDirElement.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDicomDirElement.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/01 10:29:54 $
7   Version:   $Revision: 1.33 $
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
27 namespace gdcm 
28 {
29 //-----------------------------------------------------------------------------
30 void FillDefaultDIRDict(DicomDirElement *dde);
31
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 /**
35  * \brief   constructor : populates the chained lists 
36  *          from the file 'Dicts/DicomDir.dic'
37  */
38 DicomDirElement::DicomDirElement()
39 {
40    std::string filename = DictSet::BuildDictPath() + DICT_ELEM;
41    std::ifstream from(filename.c_str());
42    if(!from)
43    {
44       gdcmVerboseMacro( "Can't open DicomDirElement dictionary" 
45                         << filename.c_str());
46       FillDefaultDIRDict( this );
47    }
48    else
49    {
50       char buff[1024];
51       std::string strType;
52       Element elem;
53       DicomDirType type;
54
55       while (!from.eof())
56       {
57          from >> std::ws;
58          from.getline(buff, 1024, ' ');
59          strType = buff;
60
61          if( strType == "metaElem" )
62             type = DD_META;
63          else if( strType == "patientElem" )
64             type = DD_PATIENT;
65          else if( strType == "studyElem" )
66             type = DD_STUDY;
67          else if( strType == "serieElem" )
68             type = DD_SERIE;
69          else if( strType == "imageElem" )
70             type = DD_IMAGE;
71          else
72          {
73             gdcmVerboseMacro("Unknown type found in the file : "
74                              <<filename.c_str());
75             type = DD_UNKNOWN;
76          }
77
78          if( type!=DD_UNKNOWN )
79          {
80             from >> std::hex >> elem.Group >> elem.Elem;
81
82             from >> std::ws;
83             from.getline(buff, 1024, '"');
84             from >> std::ws;
85             from.getline(buff, 1024, '"');
86             elem.Value = buff;
87
88             AddEntry(type, elem);
89          }
90          from.getline(buff, 1024, '\n');
91       }
92       from.close();
93    }
94 }
95
96 /**
97  * \brief   canonical destructor 
98  */
99 DicomDirElement::~DicomDirElement()
100 {
101    DicomDirMetaList.clear();
102    DicomDirPatientList.clear();
103    DicomDirStudyList.clear();
104    DicomDirSerieList.clear();
105    DicomDirImageList.clear();
106 }
107
108 //-----------------------------------------------------------------------------
109 // Public
110 /**
111  * \brief Add an entry 
112  * @param type type
113  * @param elem elem
114  */
115 bool DicomDirElement::AddEntry(DicomDirType type, Element const &elem)
116 {
117    switch( type )
118    {
119       case DD_META :
120          DicomDirMetaList.push_back(elem);
121          break;
122       case DD_PATIENT :
123          DicomDirPatientList.push_back(elem);
124          break;
125       case DD_STUDY :
126          DicomDirStudyList.push_back(elem);
127          break;
128       case DD_SERIE :
129          DicomDirSerieList.push_back(elem);
130          break;
131       case DD_IMAGE :
132          DicomDirImageList.push_back(elem);
133          break;
134       default :
135          return false;
136    }
137    return true;
138 }
139
140 //-----------------------------------------------------------------------------
141 // Protected
142
143 //-----------------------------------------------------------------------------
144 // Private
145
146 //-----------------------------------------------------------------------------
147 // Print
148 /**
149  * \brief   Print all
150  * @param   os The output stream to be written to.
151  */
152 void DicomDirElement::Print(std::ostream &os)
153 {
154    std::ostringstream s;
155    std::list<Element>::iterator it;
156    //char greltag[10];  //group element tag
157    std::string greltag;
158
159    s << "Meta Elements :"<<std::endl;
160    for (it = DicomDirMetaList.begin(); it != DicomDirMetaList.end(); ++it)
161    {
162       greltag = Util::Format("%04x|%04x ",it->Group,it->Elem);
163       s << "   (" << greltag << ") = " << it->Value << std::endl;
164    }
165
166    s << "Patient Elements :"<<std::endl;
167    for (it = DicomDirPatientList.begin(); it != DicomDirPatientList.end(); ++it)
168    {
169       greltag = Util::Format("%04x|%04x ",it->Group,it->Elem);
170       s << "   (" << greltag << ") = " << it->Value << std::endl;
171    }
172
173    s << "Study Elements :"<<std::endl;
174    for (it = DicomDirStudyList.begin(); it != DicomDirStudyList.end(); ++it)
175    {
176       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
177       s << "   (" << greltag << ") = " << it->Value << std::endl;
178    }
179
180    s << "Serie Elements :"<<std::endl;
181    for (it = DicomDirSerieList.begin(); it != DicomDirSerieList.end(); ++it)
182    {
183       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
184       s << "   (" << greltag << ") = " << it->Value << std::endl;
185    }
186
187    s << "Image Elements :"<<std::endl;
188    for (it = DicomDirImageList.begin(); it != DicomDirImageList.end(); ++it)
189    {
190       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
191       s << "   (" << greltag << ") = " << it->Value << std::endl;
192    }
193
194    os << s.str();
195 }
196
197 //-----------------------------------------------------------------------------
198 } // end namespace gdcm