]> Creatis software - gdcm.git/blob - src/gdcmDicomDirElement.cxx
ENH: Now the dictionary is compiled into gdcm lib. This is a default behavior, thus...
[gdcm.git] / src / gdcmDicomDirElement.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDicomDirElement.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/03 18:08:56 $
7   Version:   $Revision: 1.21 $
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       dbg.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          else
75          {
76             dbg.Error("DicomDirElement::DicomDirElement: Error parsing file",
77                       filename.c_str());
78             dbg.Error("Type", type.c_str(), " is not registered as valid" );
79          }
80          from.getline(buff, 1024, '\n');
81       }
82    from.close();
83    }
84 }
85
86 /**
87  * \ingroup DicomDirElement
88  * \brief   canonical destructor 
89  */
90 DicomDirElement::~DicomDirElement()
91 {
92    DicomDirMetaList.clear();
93    DicomDirPatientList.clear();
94    DicomDirStudyList.clear();
95    DicomDirSerieList.clear();
96    DicomDirImageList.clear();
97 }
98
99 //-----------------------------------------------------------------------------
100 // Print
101 /**
102  * \ingroup DicomDirElement
103  * \brief   Print all
104  * \todo add a 'Print Level' check 
105  * @param   os The output stream to be written to.
106  */
107 void DicomDirElement::Print(std::ostream &os)
108 {
109    std::ostringstream s;
110    std::list<Element>::iterator it;
111    //char greltag[10];  //group element tag
112    std::string greltag;
113
114    s << "Meta Elements :"<<std::endl;
115    for (it = DicomDirMetaList.begin(); it != DicomDirMetaList.end(); ++it)
116    {
117       greltag = Util::Format("%04x|%04x ",it->Group,it->Elem);
118       s << "   (" << greltag << ") = " << it->Value << std::endl;
119    }
120
121    s << "Patient Elements :"<<std::endl;
122    for (it = DicomDirPatientList.begin(); it != DicomDirPatientList.end(); ++it)
123    {
124       greltag = Util::Format("%04x|%04x ",it->Group,it->Elem);
125       s << "   (" << greltag << ") = " << it->Value << std::endl;
126    }
127
128    s << "Study Elements :"<<std::endl;
129    for (it = DicomDirStudyList.begin(); it != DicomDirStudyList.end(); ++it)
130    {
131       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
132       s << "   (" << greltag << ") = " << it->Value << std::endl;
133    }
134
135    s << "Serie Elements :"<<std::endl;
136    for (it = DicomDirSerieList.begin(); it != DicomDirSerieList.end(); ++it)
137    {
138       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
139       s << "   (" << greltag << ") = " << it->Value << std::endl;
140    }
141
142    s << "Image Elements :"<<std::endl;
143    for (it = DicomDirImageList.begin(); it != DicomDirImageList.end(); ++it)
144    {
145       greltag = Util::Format("%04x|%04x ", it->Group, it->Elem);
146       s << "   (" << greltag << ") = " << it->Value << std::endl;
147    }
148
149    os << s.str();
150 }
151
152 //-----------------------------------------------------------------------------
153 // Public
154
155 bool DicomDirElement::AddNewEntry(std::string const & type, 
156                                   Element const & elem)
157 {
158    if( type == "metaElem" )
159    {
160       DicomDirMetaList.push_back(elem);
161    }
162    else if( type == "patientElem" )
163    {
164       DicomDirPatientList.push_back(elem);
165    }
166    else if( type == "studyElem" )
167    {
168       DicomDirStudyList.push_back(elem);
169    }
170    else if( type == "serieElem" )
171    {
172       DicomDirSerieList.push_back(elem);
173    }
174    else if( type == "imageElem" )
175    {
176       DicomDirImageList.push_back(elem);
177    }
178    else
179    {
180      return false;
181    }
182    return true;
183 }
184 //-----------------------------------------------------------------------------
185 // Protected
186
187 //-----------------------------------------------------------------------------
188 // Private
189
190 //-----------------------------------------------------------------------------
191
192 } // end namespace gdcm