]> Creatis software - gdcm.git/blob - src/gdcmDicomDirElement.cxx
Fix mistypings
[gdcm.git] / src / gdcmDicomDirElement.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDicomDirElement.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:08 $
7   Version:   $Revision: 1.45 $
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_NAME_SPACE 
28 {
29 //-----------------------------------------------------------------------------
30 /// \brief auto generate function, to fill up the default elements for 
31 ///        a DICOMDIR, if relevant file is not found on user's disk
32 void FillDefaultDIRDict(DicomDirElement *dde);
33
34 //-----------------------------------------------------------------------------
35 // Constructor / Destructor
36 /**
37  * \brief   constructor : populates the chained lists 
38  *          from the file 'Dicts/DicomDir.dic'
39  */
40 DicomDirElement::DicomDirElement()
41 {
42    std::string filename = DictSet::BuildDictPath() + DICT_ELEM;
43    std::ifstream from(filename.c_str());
44    if ( !from )
45    {
46       gdcmWarningMacro( "Can't open DicomDirElement dictionary" 
47                         << filename.c_str());
48       FillDefaultDIRDict( this );
49    }
50    else
51    {
52       char buff[1024];
53       char buff2[1024];
54       std::string strType;
55       DicomElement elem;
56       DicomDirType type;
57       while (!from.eof())
58       {
59          from >> std::ws;
60          from.getline(buff, 1024, ' ');
61          strType = buff;
62
63          if ( strType == "imageElem" )
64             type = DD_IMAGE;
65          else if ( strType == "serieElem" )
66             type = DD_SERIE;
67          else if ( strType == "studyElem" )
68             type = DD_STUDY;
69          else if ( strType == "patientElem" )
70             type = DD_PATIENT;
71          else if ( strType == "metaElem" )
72             type = DD_META;
73          else
74          {
75             gdcmWarningMacro("Unknown type (" << strType 
76                              << ") found in the file : "
77                              << filename.c_str());
78             type = DD_UNKNOWN;
79          }
80
81          if ( type!=DD_UNKNOWN )
82          {
83             from >> std::hex >> elem.Group >> elem.Elem;//  >> elem.VR;
84
85             from.getline(buff2, 1024, '"');
86             from >> std::ws;
87             from.getline(buff2, 1024, '"');
88             elem.VR[0] = buff2[0];
89             elem.VR[1] = buff2[1];
90  // std::cout << "VR : [" <<  elem.VR[0] << elem.VR[1] << "]" << std::endl;  // JPR
91             from >> std::ws;
92             from.getline(buff, 1024, '"');
93             from >> std::ws;
94             from.getline(buff, 1024, '"');
95             elem.Value = buff;
96     
97             AddEntry(type, elem);
98          }
99          from.getline(buff, 1024, '\n');
100       }
101       from.close();
102    }
103 }
104
105 /**
106  * \brief   canonical destructor 
107  */
108 DicomDirElement::~DicomDirElement()
109 {
110    DicomDirMetaList.clear();
111    DicomDirPatientList.clear();
112    DicomDirStudyList.clear();
113    DicomDirSerieList.clear();
114    DicomDirImageList.clear();
115 }
116
117 //-----------------------------------------------------------------------------
118 // Public
119 /**
120  * \brief Add an entry to one of the DicomDir Elements 
121  *        (Patient, Study, Serie, Image)
122  * @param type Element type (DD_PATIENT, DD_STUDY, DD_SERIE, DD_IMAGE) 
123  * @param elem elem
124  */
125 bool DicomDirElement::AddEntry(DicomDirType type, DicomElement const &elem)
126 {
127    switch( type )
128    {
129       case DD_IMAGE :
130          DicomDirImageList.push_back(elem);
131          break;
132       case DD_SERIE :
133          DicomDirSerieList.push_back(elem);
134          break;
135       case DD_STUDY :
136          DicomDirStudyList.push_back(elem);
137          break;
138       case DD_PATIENT :
139          DicomDirPatientList.push_back(elem);
140          break;
141       case DD_META :
142          DicomDirMetaList.push_back(elem);
143          break;
144       default :
145          return false;
146    }
147    return true;
148 }
149
150 /**
151  * \brief Add an entry to one of the DicomDir Elements 
152  *        (Patient, Study, Serie, Image)
153  * @param type Element type (DD_PATIENT, DD_STUDY, DD_SERIE, DD_IMAGE) 
154  * @param group  Group number of the entry to be added
155  * @param elem Element number of the entry to be added
156  * @param vr Value Representation of the entry to be added
157  */
158 void DicomDirElement::AddDicomDirElement(DicomDirType type,
159                                          uint16_t group, uint16_t elem, VRKey vr)
160 {
161    DicomElement el;
162    el.Group = group;
163    el.Elem  = elem;
164    el.VR    = vr;
165    el.Value = "";
166    AddEntry(type, el);
167 }
168
169 //-----------------------------------------------------------------------------
170 // Protected
171
172 //-----------------------------------------------------------------------------
173 // Private
174
175 //-----------------------------------------------------------------------------
176 // Print
177 /**
178  * \brief   Print all
179  * @param   os The output stream to be written to.
180  */
181 void DicomDirElement::Print(std::ostream &os,std::string const &)
182 {
183    std::ostringstream s;
184    std::list<DicomElement>::iterator it;
185    TagKey greltag;
186
187    s << "Meta Elements :"<<std::endl;
188    for (it = DicomDirMetaList.begin(); it != DicomDirMetaList.end(); ++it)
189    {
190       greltag = DictEntry::TranslateToKey(it->Group,it->Elem);
191       s << "   (" << greltag << ") = " << it->Value << std::endl;
192    }
193
194    s << "Patient Elements :"<<std::endl;
195    for (it = DicomDirPatientList.begin(); it != DicomDirPatientList.end(); ++it)
196    {
197       greltag = DictEntry::TranslateToKey(it->Group,it->Elem);
198       s << "   (" << greltag << ") = " << it->Value << std::endl;
199    }
200
201    s << "Study Elements :"<<std::endl;
202    for (it = DicomDirStudyList.begin(); it != DicomDirStudyList.end(); ++it)
203    {
204       greltag = DictEntry::TranslateToKey(it->Group, it->Elem);
205       s << "   (" << greltag << ") = " << it->Value << std::endl;
206    }
207
208    s << "Serie Elements :"<<std::endl;
209    for (it = DicomDirSerieList.begin(); it != DicomDirSerieList.end(); ++it)
210    {
211       greltag = DictEntry::TranslateToKey( it->Group, it->Elem);
212       s << "   (" << greltag << ") = " << it->Value << std::endl;
213    }
214
215    s << "Image Elements :"<<std::endl;
216    for (it = DicomDirImageList.begin(); it != DicomDirImageList.end(); ++it)
217    {
218       greltag = DictEntry::TranslateToKey(it->Group, it->Elem);
219       s << "   (" << greltag << ") = " << it->Value << std::endl;
220    }
221
222    os << s.str();
223 }
224
225 //-----------------------------------------------------------------------------
226 } // end namespace gdcm