00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "gdcmValEntry.h"
00020 #include "gdcmVR.h"
00021 #include "gdcmTS.h"
00022 #include "gdcmGlobal.h"
00023 #include "gdcmUtil.h"
00024 #include "gdcmDebug.h"
00025
00026 #include <fstream>
00027
00028 namespace gdcm
00029 {
00030
00031 #define MAX_SIZE_PRINT_ELEMENT_VALUE 128
00032
00033
00034
00039 ValEntry::ValEntry(DictEntry *e)
00040 : ContentEntry(e)
00041 {
00042 }
00043
00048 ValEntry::ValEntry(DocEntry *e)
00049 : ContentEntry(e->GetDictEntry())
00050 {
00051 Copy(e);
00052 }
00053
00057 ValEntry::~ValEntry ()
00058 {
00059 }
00060
00061
00062
00068 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
00069 {
00070 DocEntry::WriteContent(fp, filetype);
00071
00072 if ( GetGroup() == 0xfffe )
00073 {
00074 return;
00075 }
00076
00077 const VRKey &vr = GetVR();
00078 unsigned int lgth = GetLength();
00079 if (vr == "US" || vr == "SS")
00080 {
00081
00082
00083
00084 std::vector<std::string> tokens;
00085 tokens.erase(tokens.begin(),tokens.end());
00086 Util::Tokenize (GetValue(), tokens, "\\");
00087 for (unsigned int i=0; i<tokens.size();i++)
00088 {
00089 uint16_t val_uint16 = atoi(tokens[i].c_str());
00090 binary_write( *fp, val_uint16);
00091 }
00092 tokens.clear();
00093 return;
00094 }
00095 if (vr == "UL" || vr == "SL")
00096 {
00097
00098
00099
00100
00101 std::vector<std::string> tokens;
00102 tokens.erase(tokens.begin(),tokens.end());
00103 Util::Tokenize (GetValue(), tokens, "\\");
00104 for (unsigned int i=0; i<tokens.size();i++)
00105 {
00106 uint32_t val_uint32 = atoi(tokens[i].c_str());
00107 binary_write( *fp, val_uint32);
00108 }
00109 tokens.clear();
00110 return;
00111 }
00112
00113 gdcmAssertMacro( lgth == GetValue().length() );
00114 binary_write(*fp, GetValue());
00115 }
00116
00121 void ValEntry::SetValue(std::string const &val)
00122 {
00123
00124 int l = val.length();
00125 if ( l != 0)
00126 {
00127 const VRKey &vr = GetVR();
00128 if( vr == "US" || vr == "SS" )
00129 {
00130
00131 l = (Util::CountSubstring(val, "\\") + 1) * 2;
00132 ContentEntry::SetValue(val);
00133 }
00134 else if( vr == "UL" || vr == "SL" )
00135 {
00136
00137 l = (Util::CountSubstring(val, "\\") + 1) * 4;;
00138 ContentEntry::SetValue(val);
00139 }
00140 else
00141 {
00142 std::string finalVal = Util::DicomString( val.c_str() );
00143 gdcmAssertMacro( !(finalVal.size() % 2) );
00144
00145 l = finalVal.length();
00146 ContentEntry::SetValue(finalVal);
00147 }
00148 }
00149 else
00150 {
00151 std::string finalVal = Util::DicomString( val.c_str() );
00152 gdcmAssertMacro( !(finalVal.size() % 2) );
00153
00154 l = finalVal.length();
00155 ContentEntry::SetValue(finalVal);
00156 }
00157
00158 SetLength(l);
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00174 void ValEntry::Print(std::ostream &os, std::string const &)
00175 {
00176 uint16_t g = GetGroup();
00177 uint16_t e = GetElement();
00178 VRKey vr = GetVR();
00179 std::ostringstream s;
00180 std::string st;
00181 std::string d2;
00182
00183 os << "V ";
00184 DocEntry::Print(os);
00185
00186 if (g == 0xfffe)
00187 {
00188
00189 return;
00190 }
00191
00192 TS *ts = Global::GetTS();
00193
00194 TSAtr v = GetValue();
00195 d2 = Util::CreateCleanString(v);
00196 if( GetLength() <= MAX_SIZE_PRINT_ELEMENT_VALUE
00197 || PrintLevel >= 3
00198 || d2.find(GDCM_NOTLOADED) < d2.length() )
00199 {
00200 s << " [" << d2 << "]";
00201 }
00202 else
00203 {
00204 s << " [gdcm::too long for print (" << GetLength() << ") ]";
00205 }
00206
00207
00208
00209 if (g == 0x0002)
00210 {
00211
00212 if ( e == 0x0010 || e == 0x0002 )
00213 {
00214 if ( v.length() != 0 )
00215 {
00216 if ( ! isdigit((unsigned char)v[v.length()-1]) )
00217 {
00218 v.erase(v.length()-1, 1);
00219 }
00220 }
00221 s << " ==>\t[" << ts->GetValue(v) << "]";
00222 }
00223 }
00224 else
00225 {
00226 if (g == 0x0008)
00227 {
00228 if ( e == 0x0016 || e == 0x1150 )
00229 {
00230 if ( v.length() != 0 )
00231 {
00232 if ( ! isdigit((unsigned char)v[v.length()-1]) )
00233 {
00234 v.erase(v.length()-1, 1);
00235 }
00236 }
00237 s << " ==>\t[" << ts->GetValue(v) << "]";
00238 }
00239 }
00240 else
00241 {
00242 if (g == 0x0004)
00243 {
00244 if ( e == 0x1510 || e == 0x1512 )
00245 {
00246 if ( v.length() != 0 )
00247 {
00248 if ( ! isdigit((unsigned char)v[v.length()-1]) )
00249 {
00250 v.erase(v.length()-1, 1);
00251 }
00252 }
00253 s << " ==>\t[" << ts->GetValue(v) << "]";
00254 }
00255 }
00256 }
00257 }
00258
00259 if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
00260 {
00261 if (v == "4294967295")
00262 {
00263 st = Util::Format(" x(ffffffff)");
00264 }
00265 else
00266 {
00267 if ( GetLength() != 0 )
00268 {
00269 st = Util::Format(" x(%x)", atoi(v.c_str()));
00270 }
00271 else
00272 {
00273 st = Util::Format(" ");
00274 }
00275 }
00276 s << st;
00277 }
00278 os << s.str();
00279 }
00280
00281
00282 }
00283