]> Creatis software - gdcm.git/blob - src/gdcmVR.cxx
BUG: Apparently on Borland uses unsigned char to store boolean, all other plateformns...
[gdcm.git] / src / gdcmVR.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmVR.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/05 01:37:09 $
7   Version:   $Revision: 1.35 $
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 "gdcmVR.h"
20 #include "gdcmUtil.h"
21 #include "gdcmDictSet.h"
22 #include "gdcmDebug.h"
23
24 #include <fstream>
25 #include <iostream>
26
27 namespace gdcm 
28 {
29 //-----------------------------------------------------------------------------
30 void FillDefaultVRDict(VRHT &vr);
31
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 /**
35  * \brief Constructor
36  */
37 VR::VR() 
38 {
39    std::string filename = DictSet::BuildDictPath() + DICT_VR;
40    std::ifstream from(filename.c_str());
41    if(!from)
42    {
43       gdcmWarningMacro("Can't open dictionary" << filename.c_str());
44       FillDefaultVRDict(vr);
45    }
46    else
47    {
48       char buff[1024];
49       VRKey key;
50       VRAtr name;
51    
52       while (!from.eof()) 
53       {
54          from >> std::ws;
55          from.getline(buff, 1024, ' ');
56          key = buff;
57          from >> std::ws;
58          from.getline(buff, 1024, ';');
59          name = buff;
60    
61          from >> std::ws;
62          from.getline(buff, 1024, '\n');
63    
64          if(key != "")
65          {
66             vr[key] = name;
67          }
68       }
69       from.close();
70    }
71 }
72
73 /**
74  * \brief Destructor
75  */
76 VR::~VR()
77 {
78    vr.clear();
79 }
80
81 //-----------------------------------------------------------------------------
82 // Public
83 /**
84  * \brief   Get the count for an element
85  * @param   key key to count
86  */
87 int VR::Count(VRKey const &key) 
88 {
89    return vr.count(key);
90 }
91
92 /**
93  * \brief   Simple predicate that checks whether the given argument
94  *          corresponds to the Value Representation of a \ref BinEntry .
95  * @param   tested value representation to check for.
96  */
97 bool VR::IsVROfBinaryRepresentable(VRKey const &tested)
98 {
99    if ( tested == GDCM_UNKNOWN)
100       return true;
101
102    if ( IsVROfStringRepresentable(tested) )
103       return false;
104
105    if ( IsVROfSequence(tested) )
106       return false;
107
108    return true;
109 }
110
111 /**
112  * \brief   Simple predicate that checks whether the given argument
113  *          corresponds to the Value Representation of a \ref ValEntry
114  *          but NOT a \ref BinEntry.
115  * @param   tested value representation to be checked.
116  */
117 bool VR::IsVROfStringRepresentable(VRKey const &tested)
118 {
119    return tested == "AE" ||
120           tested == "AS" ||
121           tested == "CS" ||
122           tested == "DA" ||
123           tested == "DS" ||
124           tested == "IS" || 
125           tested == "LO" ||
126           tested == "LT" ||
127           tested == "PN" ||
128           tested == "SH" ||
129           tested == "SL" ||
130           tested == "SS" ||
131           tested == "ST" ||
132           tested == "TM" ||
133           tested == "UI" ||
134           tested == "UL" ||
135           tested == "UN" ||
136           tested == "US";
137 }
138
139 /**
140  * \brief   Simple predicate that checks whether the given argument
141  *          corresponds to the Value Representation of a \ref SeqEntry
142  * @param   tested value representation to check for.
143  */
144 bool VR::IsVROfSequence(VRKey const &tested)
145 {
146    return tested == "SQ";
147 }
148
149 bool VR::IsValidVR(VRKey const &key)
150 {
151    return vr.find(key) != vr.end();
152 }
153
154 //-----------------------------------------------------------------------------
155 // Protected
156
157 //-----------------------------------------------------------------------------
158 // Private
159
160 //-----------------------------------------------------------------------------
161 // Print
162 /**
163  * \brief   Print all 
164  * @param   os The output stream to be written to.
165  */
166 void VR::Print(std::ostream &os) 
167 {
168    std::ostringstream s;
169
170    for (VRHT::iterator it = vr.begin(); it != vr.end(); ++it)
171    {
172       s << "VR : " << it->first << " = " << it->second << std::endl;
173    }
174    os << s.str();
175 }
176
177 //-----------------------------------------------------------------------------
178 } // end namespace gdcm