]> Creatis software - gdcm.git/blob - src/gdcmDicomDirElement.cxx
ENH: Encapsulate global functions within gdcmUtil class as static functions. No diffe...
[gdcm.git] / src / gdcmDicomDirElement.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDicomDirElement.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/10/10 00:42:54 $
7   Version:   $Revision: 1.19 $
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 //-----------------------------------------------------------------------------
28 // Constructor / Destructor
29
30 /**
31  * \brief   constructor : populates the chained lists 
32  *          from the file 'Dicts/DicomDir.dic'
33  */
34 gdcmDicomDirElement::gdcmDicomDirElement()
35 {
36    std::string filename = gdcmDictSet::BuildDictPath() + std::string(DICT_ELEM);
37    std::ifstream from(filename.c_str());
38    dbg.Error(!from, "gdcmDicomDirElement::gdcmDicomDirElement: can't open dictionary",
39               filename.c_str());
40
41    char buff[1024];
42    std::string type;
43    gdcmElement elem;
44
45    while (!from.eof())
46    {
47       from >> std::ws;
48       from.getline(buff, 1024, ' ');
49       type = buff;
50
51       if( (type=="metaElem")  || (type=="patientElem") || 
52           (type=="studyElem") || (type=="serieElem")   || 
53           (type=="imageElem") )
54       {
55          from >> std::hex >> elem.Group >> elem.Elem;
56
57          from >> std::ws;
58          from.getline(buff, 1024, '"');
59          from >> std::ws;
60          from.getline(buff, 1024, '"');
61          elem.Value = buff;
62
63          if( type == "metaElem" )
64          {
65             DicomDirMetaList.push_back(elem);
66          }
67          else if( type == "patientElem" )
68          {
69             DicomDirPatientList.push_back(elem);
70          }
71          else if( type == "studyElem" )
72          {
73             DicomDirStudyList.push_back(elem);
74          }
75          else if( type == "serieElem" )
76          {
77             DicomDirSerieList.push_back(elem);
78          }
79          else if( type == "imageElem" )
80          {
81             DicomDirImageList.push_back(elem);
82          }
83       }
84       from.getline(buff, 1024, '\n');
85    }
86    from.close();
87 }
88
89 /**
90  * \ingroup gdcmDicomDirElement
91  * \brief   canonical destructor 
92  */
93 gdcmDicomDirElement::~gdcmDicomDirElement()
94 {
95    DicomDirMetaList.clear();
96    DicomDirPatientList.clear();
97    DicomDirStudyList.clear();
98    DicomDirSerieList.clear();
99    DicomDirImageList.clear();
100 }
101
102 //-----------------------------------------------------------------------------
103 // Print
104 /**
105  * \ingroup gdcmDicomDirElement
106  * \brief   Print all
107  * \todo add a 'Print Level' check 
108  * @param   os The output stream to be written to.
109  */
110 void gdcmDicomDirElement::Print(std::ostream &os)
111 {
112    std::ostringstream s;
113    std::list<gdcmElement>::iterator it;
114    //char greltag[10];  //group element tag
115    std::string greltag;
116
117    s << "Meta Elements :"<<std::endl;
118    for (it = DicomDirMetaList.begin(); it != DicomDirMetaList.end(); ++it)
119    {
120       greltag = gdcmUtil::Format("%04x|%04x ",it->Group,it->Elem);
121       s << "   (" << greltag << ") = " << it->Value << std::endl;
122    }
123
124    s << "Patient Elements :"<<std::endl;
125    for (it = DicomDirPatientList.begin(); it != DicomDirPatientList.end(); ++it)
126    {
127       greltag = gdcmUtil::Format("%04x|%04x ",it->Group,it->Elem);
128       s << "   (" << greltag << ") = " << it->Value << std::endl;
129    }
130
131    s << "Study Elements :"<<std::endl;
132    for (it = DicomDirStudyList.begin(); it != DicomDirStudyList.end(); ++it)
133    {
134       greltag = gdcmUtil::Format("%04x|%04x ", it->Group, it->Elem);
135       s << "   (" << greltag << ") = " << it->Value << std::endl;
136    }
137
138    s << "Serie Elements :"<<std::endl;
139    for (it = DicomDirSerieList.begin(); it != DicomDirSerieList.end(); ++it)
140    {
141       greltag = gdcmUtil::Format("%04x|%04x ", it->Group, it->Elem);
142       s << "   (" << greltag << ") = " << it->Value << std::endl;
143    }
144
145    s << "Image Elements :"<<std::endl;
146    for (it = DicomDirImageList.begin(); it != DicomDirImageList.end(); ++it)
147    {
148       greltag = gdcmUtil::Format("%04x|%04x ", it->Group, it->Elem);
149       s << "   (" << greltag << ") = " << it->Value << std::endl;
150    }
151
152    os << s.str();
153 }
154
155 //-----------------------------------------------------------------------------
156 // Public
157
158 //-----------------------------------------------------------------------------
159 // Protected
160
161 //-----------------------------------------------------------------------------
162 // Private
163
164 //-----------------------------------------------------------------------------