]> Creatis software - gdcm.git/blob - src/gdcmDicomDirElement.cxx
Add some commented out lines to prepare future version of 'Full DICOMDIR' stuff
[gdcm.git] / src / gdcmDicomDirElement.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDicomDirElement.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/07 14:48:34 $
7   Version:   $Revision: 1.35 $
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       gdcmWarningMacro( "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             gdcmWarningMacro("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 to one of the DicomDir Elements 
112  *        (Patient, Study, Serie, Image)
113  * @param type Element type (DD_PATIENT, DD_STUDY, DD_SERIE, DD_IMAGE) 
114  * @param elem elem
115  */
116 bool DicomDirElement::AddEntry(DicomDirType type, Element const &elem)
117 {
118    switch( type )
119    {
120       case DD_META :
121          DicomDirMetaList.push_back(elem);
122          break;
123       case DD_PATIENT :
124          DicomDirPatientList.push_back(elem);
125          break;
126       case DD_STUDY :
127          DicomDirStudyList.push_back(elem);
128          break;
129       case DD_SERIE :
130          DicomDirSerieList.push_back(elem);
131          break;
132       case DD_IMAGE :
133          DicomDirImageList.push_back(elem);
134          break;
135       default :
136          return false;
137    }
138    return true;
139 }
140
141 /**
142  * \brief Add an entry to one of the DicomDir Elements 
143  *        (Patient, Study, Serie, Image)
144  * @param type Element type (DD_PATIENT, DD_STUDY, DD_SERIE, DD_IMAGE) 
145  * @param group  Group number of the entry to be added
146  * @param elem Element number of the entry to be added
147  */
148 void DicomDirElement::AddDicomDirElement(DicomDirType type,
149                                          uint16_t group, uint16_t elem)
150 {
151    Element el;
152    el.Group = group;
153    el.Elem  = elem;
154    el.Value = "";
155    AddEntry(type, el);
156 }
157 //-----------------------------------------------------------------------------
158 // Protected
159
160 //-----------------------------------------------------------------------------
161 // Private
162
163 //-----------------------------------------------------------------------------
164 // Print
165 /**
166  * \brief   Print all
167  * @param   os The output stream to be written to.
168  */
169 void DicomDirElement::Print(std::ostream &os)
170 {
171    std::ostringstream s;
172    std::list<Element>::iterator it;
173    //char greltag[10];  //group element tag
174    std::string greltag;
175
176    s << "Meta Elements :"<<std::endl;
177    for (it = DicomDirMetaList.begin(); it != DicomDirMetaList.end(); ++it)
178    {
179       greltag = Util::Format("%04x|%04x ",it->Group,it->Elem);
180       s << "   (" << greltag << ") = " << it->Value << std::endl;
181    }
182
183    s << "Patient Elements :"<<std::endl;
184    for (it = DicomDirPatientList.begin(); it != DicomDirPatientList.end(); ++it)
185    {
186       greltag = Util::Format("%04x|%04x ",it->Group,it->Elem);
187       s << "   (" << greltag << ") = " << it->Value << std::endl;
188    }
189
190    s << "Study Elements :"<<std::endl;
191    for (it = DicomDirStudyList.begin(); it != DicomDirStudyList.end(); ++it)
192    {
193       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
194       s << "   (" << greltag << ") = " << it->Value << std::endl;
195    }
196
197    s << "Serie Elements :"<<std::endl;
198    for (it = DicomDirSerieList.begin(); it != DicomDirSerieList.end(); ++it)
199    {
200       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
201       s << "   (" << greltag << ") = " << it->Value << std::endl;
202    }
203
204    s << "Image Elements :"<<std::endl;
205    for (it = DicomDirImageList.begin(); it != DicomDirImageList.end(); ++it)
206    {
207       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
208       s << "   (" << greltag << ") = " << it->Value << std::endl;
209    }
210
211    os << s.str();
212 }
213
214 //-----------------------------------------------------------------------------
215 } // end namespace gdcm