]> Creatis software - gdcm.git/blob - src/gdcmDicomDirElement.cxx
ENH: Guess what ! Still some cosmetic cleanup
[gdcm.git] / src / gdcmDicomDirElement.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDicomDirElement.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/08/01 02:39:09 $
7   Version:   $Revision: 1.14 $
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.htm 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 <fstream>
20 #include <stdio.h>    // For sprintf
21 #include <iostream>
22
23 #include "gdcmDicomDirElement.h"
24 #include "gdcmUtil.h"
25 #include "gdcmDebug.h"
26 #include "gdcmDictSet.h"
27
28
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
31
32 /**
33  * \brief   constructor : populates the chained lists 
34  *          from the file 'Dicts/DicomDir.dic'
35  */
36 gdcmDicomDirElement::gdcmDicomDirElement()
37 {
38    std::string filename = gdcmDictSet::BuildDictPath() + std::string(DICT_ELEM);
39    std::ifstream from(filename.c_str());
40    dbg.Error(!from, "gdcmDicomDirElement::gdcmDicomDirElement: can't open dictionary",filename.c_str());
41
42    char buff[1024];
43    std::string type;
44    gdcmElement elem;
45
46    while (!from.eof())
47    {
48       eatwhite(from);
49       from.getline(buff, 1024, ' ');
50       type = buff;
51
52       if( (type=="metaElem")  || (type=="patientElem") || 
53           (type=="studyElem") || (type=="serieElem")   || 
54           (type=="imageElem") )
55       {
56          from >> std::hex >> elem.group >> elem.elem;
57
58          eatwhite(from);
59          from.getline(buff, 1024, '"');
60          eatwhite(from);
61          from.getline(buff, 1024, '"');
62          elem.value = buff;
63
64          if( type == "metaElem" )
65          {
66             DicomDirMetaList.push_back(elem);
67          }
68          else if( type == "patientElem" )
69          {
70             DicomDirPatientList.push_back(elem);
71          }
72          else if( type == "studyElem" )
73          {
74             DicomDirStudyList.push_back(elem);
75          }
76          else if( type == "serieElem" )
77          {
78             DicomDirSerieList.push_back(elem);
79          }
80          else if( type == "imageElem" )
81          {
82             DicomDirImageList.push_back(elem);
83          }
84       }
85       from.getline(buff, 1024, '\n');
86    }
87    from.close();
88 }
89
90 /**
91  * \ingroup gdcmDicomDirElement
92  * \brief   canonical destructor 
93  */
94 gdcmDicomDirElement::~gdcmDicomDirElement()
95 {
96    DicomDirMetaList.clear();
97    DicomDirPatientList.clear();
98    DicomDirStudyList.clear();
99    DicomDirSerieList.clear();
100    DicomDirImageList.clear();
101 }
102
103 //-----------------------------------------------------------------------------
104 // Print
105 /**
106  * \ingroup gdcmDicomDirElement
107  * \brief   Print all
108  * \todo add a 'Print Level' check 
109  * @param   os The output stream to be written to.
110  */
111 void gdcmDicomDirElement::Print(std::ostream &os)
112 {
113    std::ostringstream s;
114    std::list<gdcmElement>::iterator it;
115    char greltag[10];  //group element tag
116
117    s << "Meta Elements :"<<std::endl;
118    for (it = DicomDirMetaList.begin(); it != DicomDirMetaList.end(); ++it)
119    {
120       sprintf(greltag,"%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       sprintf(greltag,"%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       sprintf(greltag,"%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       sprintf(greltag,"%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       sprintf(greltag,"%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 //-----------------------------------------------------------------------------