]> Creatis software - gdcm.git/blob - src/gdcmVR.cxx
- Avoid useless creation of std::string
[gdcm.git] / src / gdcmVR.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmVR.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/07/10 08:27:27 $
7   Version:   $Revision: 1.51 $
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    char *VRvalues = "AEASCSDADSFLFDISLOLTPNSHSLSSSTTMUIULUSUTOBOWOFATUNSQ";
75 }
76
77 /**
78  * \brief Destructor
79  */
80 VR::~VR()
81 {
82    vr.clear();
83 }
84
85 //-----------------------------------------------------------------------------
86 // Public
87
88 /**
89  * \brief   Simple predicate that checks whether the given argument
90  *          corresponds to the Value Representation of a \ref DataEntry .
91  * @param   tested value representation to check for.
92  */
93 bool VR::IsVROfBinaryRepresentable(VRKey const &tested)
94 {
95    if ( IsVROfStringRepresentable(tested) )
96       return false;
97
98    if ( IsVROfSequence(tested) )
99       return false;
100
101    return true;
102 }
103
104 /**
105  * \brief   Simple predicate that checks whether the given argument
106  *          corresponds to the Value Representation of a
107  *          'std::string representable' value.
108  * @param   tested value representation to be checked.
109  */
110 bool VR::IsVROfStringRepresentable(VRKey const &tested)
111 {
112    return tested == "AE" ||
113           tested == "AS" ||
114           tested == "CS" ||
115           tested == "DA" ||
116           tested == "DS" ||
117           tested == "FL" ||
118           tested == "FD" ||
119           tested == "IS" || 
120           tested == "LO" ||
121           tested == "LT" ||
122           tested == "PN" ||
123           tested == "SH" ||
124           tested == "SL" ||
125           tested == "SS" ||
126           tested == "ST" ||
127           tested == "TM" ||
128           tested == "UI" ||
129           tested == "UL" ||
130           tested == "US" ||
131           tested == "UT";
132
133    // Should be quicker
134    // --> will *never* work : any rotten value would be considered as OK !
135 /*
136    return tested != "OB" &&
137           tested != "OW" &&
138           tested != "OF" &&
139           tested != "AT" && // Attribute Tag ?!? contain no printable character
140           tested != "UN" && // UN is an actual VR !
141           tested != "SQ" ;
142 */
143 }
144 /// \brief returns the length of a elementary elem whose VR is passed
145 unsigned short VR::GetAtomicElementLength(VRKey const &tested)
146 {
147    // Unsigned & signed short
148    if( tested == "US" || tested == "SS" )
149       return 2;
150    // Unsigned & signed long
151    if( tested == "UL" || tested == "SL" )
152       return 4;
153    // Float
154    if( tested == "FL" )
155       return 4;
156    // Double
157    if( tested == "FD" )
158       return 8;
159    // Word string
160    if( tested == "OW" )
161       return 2;
162    // Float string
163    if( tested == "OF" )
164       return 4;   
165    return 1;
166 }
167
168 // VS6 need a single implementation in the dll
169 #if defined(_MSC_VER) && (_MSC_VER == 1200)
170 /// \brief checks is a supposed-to-be VR is a 'legal' one.
171 bool VR::IsValidVR(VRKey const &key)
172 {
173 //  return vr.find(key) != vr.end();
174
175   int nbVal=26;
176   char *pt = VRvalues;
177   for (int i=0;i<nbVal;i++)
178   {
179      if(tested[0] == *pt++)
180        if(tested[1] == *pt++)
181           return true;       
182   }
183   return false;
184
185 }
186 #endif
187
188 //-----------------------------------------------------------------------------
189 // Protected
190
191 //-----------------------------------------------------------------------------
192 // Private
193
194 //-----------------------------------------------------------------------------
195 // Print
196 /**
197  * \brief   Print all 
198  * @param   os The output stream to be written to.
199  */
200 void VR::Print(std::ostream &os,std::string const &) 
201 {
202    for (VRHT::iterator it = vr.begin(); it != vr.end(); ++it)
203    {
204       os << "VR : " << it->first << " = " << it->second << std::endl;
205    }
206 }
207
208 //-----------------------------------------------------------------------------
209 } // end namespace gdcm