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