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