]> Creatis software - gdcm.git/blob - src/gdcmDicomDirElement.cxx
ENH: update gdcmDebug after Benoit comments
[gdcm.git] / src / gdcmDicomDirElement.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDicomDirElement.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/07 19:20:38 $
7   Version:   $Revision: 1.25 $
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( "DicomDirElement::DicomDirElement: can't open dictionary"
44                   << filename.c_str());
45       FillDefaultDIRDict( this );
46    }
47    else
48    {
49       char buff[1024];
50       std::string type;
51       Element elem;
52
53       while (!from.eof())
54       {
55          from >> std::ws;
56          from.getline(buff, 1024, ' ');
57          type = buff;
58
59          if( type == "metaElem"  || type == "patientElem" || 
60              type == "studyElem" || type == "serieElem"   || 
61              type == "imageElem" )
62          {
63             from >> std::hex >> elem.Group >> elem.Elem;
64
65             from >> std::ws;
66             from.getline(buff, 1024, '"');
67             from >> std::ws;
68             from.getline(buff, 1024, '"');
69             elem.Value = buff;
70
71             AddNewEntry(type, elem);
72          }
73          from.getline(buff, 1024, '\n');
74       }
75       from.close();
76    }
77 }
78
79 /**
80  * \ingroup DicomDirElement
81  * \brief   canonical destructor 
82  */
83 DicomDirElement::~DicomDirElement()
84 {
85    DicomDirMetaList.clear();
86    DicomDirPatientList.clear();
87    DicomDirStudyList.clear();
88    DicomDirSerieList.clear();
89    DicomDirImageList.clear();
90 }
91
92 //-----------------------------------------------------------------------------
93 // Print
94 /**
95  * \ingroup DicomDirElement
96  * \brief   Print all
97  * \todo add a 'Print Level' check 
98  * @param   os The output stream to be written to.
99  */
100 void DicomDirElement::Print(std::ostream &os)
101 {
102    std::ostringstream s;
103    std::list<Element>::iterator it;
104    //char greltag[10];  //group element tag
105    std::string greltag;
106
107    s << "Meta Elements :"<<std::endl;
108    for (it = DicomDirMetaList.begin(); it != DicomDirMetaList.end(); ++it)
109    {
110       greltag = Util::Format("%04x|%04x ",it->Group,it->Elem);
111       s << "   (" << greltag << ") = " << it->Value << std::endl;
112    }
113
114    s << "Patient Elements :"<<std::endl;
115    for (it = DicomDirPatientList.begin(); it != DicomDirPatientList.end(); ++it)
116    {
117       greltag = Util::Format("%04x|%04x ",it->Group,it->Elem);
118       s << "   (" << greltag << ") = " << it->Value << std::endl;
119    }
120
121    s << "Study Elements :"<<std::endl;
122    for (it = DicomDirStudyList.begin(); it != DicomDirStudyList.end(); ++it)
123    {
124       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
125       s << "   (" << greltag << ") = " << it->Value << std::endl;
126    }
127
128    s << "Serie Elements :"<<std::endl;
129    for (it = DicomDirSerieList.begin(); it != DicomDirSerieList.end(); ++it)
130    {
131       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
132       s << "   (" << greltag << ") = " << it->Value << std::endl;
133    }
134
135    s << "Image Elements :"<<std::endl;
136    for (it = DicomDirImageList.begin(); it != DicomDirImageList.end(); ++it)
137    {
138       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
139       s << "   (" << greltag << ") = " << it->Value << std::endl;
140    }
141
142    os << s.str();
143 }
144
145 //-----------------------------------------------------------------------------
146 // Public
147
148 bool DicomDirElement::AddNewEntry(std::string const &type, 
149                                   Element const &elem)
150 {
151    if( type == "metaElem" )
152    {
153       DicomDirMetaList.push_back(elem);
154    }
155    else if( type == "patientElem" )
156    {
157       DicomDirPatientList.push_back(elem);
158    }
159    else if( type == "studyElem" )
160    {
161       DicomDirStudyList.push_back(elem);
162    }
163    else if( type == "serieElem" )
164    {
165       DicomDirSerieList.push_back(elem);
166    }
167    else if( type == "imageElem" )
168    {
169       DicomDirImageList.push_back(elem);
170    }
171    else
172    {
173      return false;
174    }
175    return true;
176 }
177 //-----------------------------------------------------------------------------
178 // Protected
179
180 //-----------------------------------------------------------------------------
181 // Private
182
183 //-----------------------------------------------------------------------------
184
185 } // end namespace gdcm