]> Creatis software - gdcm.git/blob - src/gdcmVR.cxx
* Remove compilation warnings
[gdcm.git] / src / gdcmVR.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmVR.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/26 08:04:16 $
7   Version:   $Revision: 1.47 $
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 #include <string.h>
27
28 namespace gdcm 
29 {
30 //-----------------------------------------------------------------------------
31 /// \brief auto generated function, to fill up the 'Value Representation'
32 ///        Dictionnary, if relevant file is not found on user's disk
33 void FillDefaultVRDict(VRHT &vr);
34
35 //-----------------------------------------------------------------------------
36 // Constructor / Destructor
37 /**
38  * \brief Constructor
39  */
40 VR::VR() 
41 {
42    std::string filename = DictSet::BuildDictPath() + DICT_VR;
43    std::ifstream from(filename.c_str());
44    if ( !from )
45    {
46       gdcmWarningMacro("Can't open dictionary " << filename.c_str());
47       FillDefaultVRDict(vr);
48    }
49    else
50    {
51       char buff[1024];
52       VRKey key;
53       VRAtr name;
54    
55       while (!from.eof()) 
56       {
57          from >> std::ws;
58          from.getline(buff, 1024, ' ');
59          if( strcmp(buff,"") == 0)
60             continue;
61
62          key = buff;
63          from >> std::ws;
64          from.getline(buff, 1024, ';');
65          name = buff;
66    
67          from >> std::ws;
68          from.getline(buff, 1024, '\n');
69    
70          vr[key] = name;
71       }
72       from.close();
73    }
74 }
75
76 /**
77  * \brief Destructor
78  */
79 VR::~VR()
80 {
81    vr.clear();
82 }
83
84 //-----------------------------------------------------------------------------
85 // Public
86
87 /**
88  * \brief   Simple predicate that checks whether the given argument
89  *          corresponds to the Value Representation of a \ref DataEntry .
90  * @param   tested value representation to check for.
91  */
92 bool VR::IsVROfBinaryRepresentable(VRKey const &tested)
93 {
94    if ( IsVROfStringRepresentable(tested) )
95       return false;
96
97    if ( IsVROfSequence(tested) )
98       return false;
99
100    return true;
101 }
102
103 /**
104  * \brief   Simple predicate that checks whether the given argument
105  *          corresponds to the Value Representation of a
106  *          'std::string representable' value.
107  * @param   tested value representation to be checked.
108  */
109 bool VR::IsVROfStringRepresentable(VRKey const &tested)
110 {
111    //FIXME : either you consider than US, UL, SS, SL *are* string representable
112    //                            and you have to add FD and FL
113    //        or  you consider they are not, and you have to remove them
114    // (I cannot guess your point, reading gdcmDataEntry code :-( )  JPR
115  
116    return tested == "AE" ||
117           tested == "AS" ||
118           tested == "CS" ||
119           tested == "DA" ||
120           tested == "DS" ||
121           tested == "IS" || 
122           tested == "LO" ||
123           tested == "LT" ||
124           tested == "PN" ||
125           tested == "SH" ||
126           tested == "SL" ||
127           tested == "SS" ||
128           tested == "ST" ||
129           tested == "TM" ||
130           tested == "UI" ||
131           tested == "UL" ||
132           tested == "US" ||
133           tested == "UT";
134
135    // Should be quicker --> But it doesn't work : revert to old code
136 /*
137    return tested != "FL" &&
138           tested != "FD" &&
139           tested != "OB" &&
140           tested != "OW" &&
141           tested != "AT" && // Attribute Tag ?!?
142           tested != "UN" && // UN is an actual VR !
143           tested != "SQ" ;
144 */
145 }
146 /// \brief returns the length of a elementary elem whose VR is passed
147 unsigned short VR::GetAtomicElementLength(VRKey const &tested)
148 {
149    // Unsigned & signed short
150    if( tested == "US" || tested == "SS" )
151       return 2;
152    // Unsigned & signed long
153    if( tested == "UL" || tested == "SL" )
154       return 4;
155    // Float
156    if( tested == "FL" )
157       return 4;
158    // Double
159    if( tested == "FD" )
160       return 8;
161    // Word string
162    if( tested == "OW" )
163       return 2;
164    return 1;
165 }
166
167 // VS6 need a single implementation in the dll
168 #if defined(_MSC_VER) && (_MSC_VER == 1200)
169 /// \brief checks is a supposed-to-be VR is a 'legal' one.
170 bool VR::IsValidVR(VRKey const &key)
171 {
172   return vr.find(key) != vr.end();
173 }
174 #endif
175
176 //-----------------------------------------------------------------------------
177 // Protected
178
179 //-----------------------------------------------------------------------------
180 // Private
181
182 //-----------------------------------------------------------------------------
183 // Print
184 /**
185  * \brief   Print all 
186  * @param   os The output stream to be written to.
187  */
188 void VR::Print(std::ostream &os,std::string const &) 
189 {
190    for (VRHT::iterator it = vr.begin(); it != vr.end(); ++it)
191    {
192       os << "VR : " << it->first << " = " << it->second << std::endl;
193    }
194 }
195
196 //-----------------------------------------------------------------------------
197 } // end namespace gdcm