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