]> Creatis software - gdcm.git/blob - src/gdcmDicomDirElement.cxx
* src/gdcmDicomDir*.[h|cxx] : rename methods to be logik in their name.
[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 11:09:23 $
7   Version:   $Revision: 1.28 $
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 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  * \brief   canonical destructor 
81  */
82 DicomDirElement::~DicomDirElement()
83 {
84    DicomDirMetaList.clear();
85    DicomDirPatientList.clear();
86    DicomDirStudyList.clear();
87    DicomDirSerieList.clear();
88    DicomDirImageList.clear();
89 }
90
91 //-----------------------------------------------------------------------------
92 // Print
93 /**
94  * \brief   Print all
95  * \todo add a 'Print Level' check 
96  * @param   os The output stream to be written to.
97  */
98 void DicomDirElement::Print(std::ostream &os)
99 {
100    std::ostringstream s;
101    std::list<Element>::iterator it;
102    //char greltag[10];  //group element tag
103    std::string greltag;
104
105    s << "Meta Elements :"<<std::endl;
106    for (it = DicomDirMetaList.begin(); it != DicomDirMetaList.end(); ++it)
107    {
108       greltag = Util::Format("%04x|%04x ",it->Group,it->Elem);
109       s << "   (" << greltag << ") = " << it->Value << std::endl;
110    }
111
112    s << "Patient Elements :"<<std::endl;
113    for (it = DicomDirPatientList.begin(); it != DicomDirPatientList.end(); ++it)
114    {
115       greltag = Util::Format("%04x|%04x ",it->Group,it->Elem);
116       s << "   (" << greltag << ") = " << it->Value << std::endl;
117    }
118
119    s << "Study Elements :"<<std::endl;
120    for (it = DicomDirStudyList.begin(); it != DicomDirStudyList.end(); ++it)
121    {
122       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
123       s << "   (" << greltag << ") = " << it->Value << std::endl;
124    }
125
126    s << "Serie Elements :"<<std::endl;
127    for (it = DicomDirSerieList.begin(); it != DicomDirSerieList.end(); ++it)
128    {
129       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
130       s << "   (" << greltag << ") = " << it->Value << std::endl;
131    }
132
133    s << "Image Elements :"<<std::endl;
134    for (it = DicomDirImageList.begin(); it != DicomDirImageList.end(); ++it)
135    {
136       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
137       s << "   (" << greltag << ") = " << it->Value << std::endl;
138    }
139
140    os << s.str();
141 }
142
143 //-----------------------------------------------------------------------------
144 // Public
145
146 /**
147  * \brief AddNewEntry 
148  * @param  type type
149  * @param elem elem
150  */
151 bool DicomDirElement::AddNewEntry(std::string const &type, 
152                                   Element const &elem)
153 {
154    if( type == "metaElem" )
155    {
156       DicomDirMetaList.push_back(elem);
157    }
158    else if( type == "patientElem" )
159    {
160       DicomDirPatientList.push_back(elem);
161    }
162    else if( type == "studyElem" )
163    {
164       DicomDirStudyList.push_back(elem);
165    }
166    else if( type == "serieElem" )
167    {
168       DicomDirSerieList.push_back(elem);
169    }
170    else if( type == "imageElem" )
171    {
172       DicomDirImageList.push_back(elem);
173    }
174    else
175    {
176      return false;
177    }
178    return true;
179 }
180 //-----------------------------------------------------------------------------
181 // Protected
182
183 //-----------------------------------------------------------------------------
184 // Private
185
186 //-----------------------------------------------------------------------------
187
188 } // end namespace gdcm