]> Creatis software - gdcm.git/blob - src/gdcmDicomDirElement.cxx
d0bc124b95ebd851c98fc5ea881b1f2ffd72de0a
[gdcm.git] / src / gdcmDicomDirElement.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDicomDirElement.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/04 15:29:59 $
7   Version:   $Revision: 1.42 $
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 /// \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       std::string strType;
54       DicomElement elem;
55       DicomDirType type;
56
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;
84
85             from >> std::ws;
86             from.getline(buff, 1024, '"');
87             from >> std::ws;
88             from.getline(buff, 1024, '"');
89             elem.Value = buff;
90
91             AddEntry(type, elem);
92          }
93          from.getline(buff, 1024, '\n');
94       }
95       from.close();
96    }
97 }
98
99 /**
100  * \brief   canonical destructor 
101  */
102 DicomDirElement::~DicomDirElement()
103 {
104    DicomDirMetaList.clear();
105    DicomDirPatientList.clear();
106    DicomDirStudyList.clear();
107    DicomDirSerieList.clear();
108    DicomDirImageList.clear();
109 }
110
111 //-----------------------------------------------------------------------------
112 // Public
113 /**
114  * \brief Add an entry to one of the DicomDir Elements 
115  *        (Patient, Study, Serie, Image)
116  * @param type Element type (DD_PATIENT, DD_STUDY, DD_SERIE, DD_IMAGE) 
117  * @param elem elem
118  */
119 bool DicomDirElement::AddEntry(DicomDirType type, DicomElement const &elem)
120 {
121    switch( type )
122    {
123       case DD_IMAGE :
124          DicomDirImageList.push_back(elem);
125          break;
126       case DD_SERIE :
127          DicomDirSerieList.push_back(elem);
128          break;
129       case DD_STUDY :
130          DicomDirStudyList.push_back(elem);
131          break;
132       case DD_PATIENT :
133          DicomDirPatientList.push_back(elem);
134          break;
135       case DD_META :
136          DicomDirMetaList.push_back(elem);
137          break;
138       default :
139          return false;
140    }
141    return true;
142 }
143
144 /**
145  * \brief Add an entry to one of the DicomDir Elements 
146  *        (Patient, Study, Serie, Image)
147  * @param type Element type (DD_PATIENT, DD_STUDY, DD_SERIE, DD_IMAGE) 
148  * @param group  Group number of the entry to be added
149  * @param elem Element number of the entry to be added
150  */
151 void DicomDirElement::AddDicomDirElement(DicomDirType type,
152                                          uint16_t group, uint16_t elem)
153 {
154    DicomElement el;
155    el.Group = group;
156    el.Elem  = elem;
157    el.Value = "";
158    AddEntry(type, el);
159 }
160
161 //-----------------------------------------------------------------------------
162 // Protected
163
164 //-----------------------------------------------------------------------------
165 // Private
166
167 //-----------------------------------------------------------------------------
168 // Print
169 /**
170  * \brief   Print all
171  * @param   os The output stream to be written to.
172  */
173 void DicomDirElement::Print(std::ostream &os,std::string const &)
174 {
175    std::ostringstream s;
176    std::list<DicomElement>::iterator it;
177    //char greltag[10];  //group element tag
178    TagKey greltag;
179
180    s << "Meta Elements :"<<std::endl;
181    for (it = DicomDirMetaList.begin(); it != DicomDirMetaList.end(); ++it)
182    {
183       greltag = DictEntry::TranslateToKey(it->Group,it->Elem);
184       s << "   (" << greltag << ") = " << it->Value << std::endl;
185    }
186
187    s << "Patient Elements :"<<std::endl;
188    for (it = DicomDirPatientList.begin(); it != DicomDirPatientList.end(); ++it)
189    {
190       greltag = DictEntry::TranslateToKey(it->Group,it->Elem);
191       s << "   (" << greltag << ") = " << it->Value << std::endl;
192    }
193
194    s << "Study Elements :"<<std::endl;
195    for (it = DicomDirStudyList.begin(); it != DicomDirStudyList.end(); ++it)
196    {
197       greltag = DictEntry::TranslateToKey(it->Group, it->Elem);
198       s << "   (" << greltag << ") = " << it->Value << std::endl;
199    }
200
201    s << "Serie Elements :"<<std::endl;
202    for (it = DicomDirSerieList.begin(); it != DicomDirSerieList.end(); ++it)
203    {
204       greltag = DictEntry::TranslateToKey( it->Group, it->Elem);
205       s << "   (" << greltag << ") = " << it->Value << std::endl;
206    }
207
208    s << "Image Elements :"<<std::endl;
209    for (it = DicomDirImageList.begin(); it != DicomDirImageList.end(); ++it)
210    {
211       greltag = DictEntry::TranslateToKey(it->Group, it->Elem);
212       s << "   (" << greltag << ") = " << it->Value << std::endl;
213    }
214
215    os << s.str();
216 }
217
218 //-----------------------------------------------------------------------------
219 } // end namespace gdcm