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