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