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