1 /*=========================================================================
4 Module: $RCSfile: gdcmValEntry.cxx,v $
6 Date: $Date: 2005/01/30 17:30:57 $
7 Version: $Revision: 1.53 $
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.
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.
17 =========================================================================*/
19 #include "gdcmValEntry.h"
22 #include "gdcmGlobal.h"
24 #include "gdcmDebug.h"
32 #define MAX_SIZE_PRINT_ELEMENT_VALUE 128
34 //-----------------------------------------------------------------------------
35 // Constructor / Destructor
37 * \brief Constructor from a given DictEntry
38 * @param e Pointer to existing dictionary entry
40 ValEntry::ValEntry(DictEntry *e) : ContentEntry(e)
45 * \brief Constructor from a given DocEntry
46 * @param e Pointer to existing Doc entry
48 ValEntry::ValEntry(DocEntry *e)
49 : ContentEntry(e->GetDictEntry())
55 * \brief Canonical destructor.
57 ValEntry::~ValEntry ()
61 //-----------------------------------------------------------------------------
64 * \brief Prints the 'std::string representable' value of ValEntry
65 * @param os ostream we want to print in
66 * @param indent Indentation string to be prepended during printing
68 void ValEntry::Print(std::ostream &os, std::string const &)
70 uint16_t g = GetGroup();
71 uint16_t e = GetElement();
80 if (g == 0xfffe) // delimiters have NO value
82 // just to avoid identing all the remaining code
86 TS * ts = Global::GetTS();
88 TSAtr v = GetValue(); // not applicable for SQ ...
89 d2 = Util::CreateCleanString(v); // replace non printable characters by '.'
90 if( GetLength() <= MAX_SIZE_PRINT_ELEMENT_VALUE
92 || d2.find(GDCM_NOTLOADED) < d2.length() )
94 s << " [" << d2 << "]";
98 s << " [gdcm::too long for print (" << GetLength() << ") ]";
101 // Display the UID value (instead of displaying only the rough code)
102 // First 'clean' trailing character (space or zero)
105 // Any more to be displayed ?
106 if ( e == 0x0010 || e == 0x0002 )
108 if ( v.length() != 0 ) // for brain damaged headers
110 if ( ! isdigit((unsigned char)v[v.length()-1]) )
112 v.erase(v.length()-1, 1);
115 s << " ==>\t[" << ts->GetValue(v) << "]";
122 if ( e == 0x0016 || e == 0x1150 )
124 if ( v.length() != 0 ) // for brain damaged headers
126 if ( ! isdigit((unsigned char)v[v.length()-1]) )
128 v.erase(v.length()-1, 1);
131 s << " ==>\t[" << ts->GetValue(v) << "]";
138 if ( e == 0x1510 || e == 0x1512 )
140 if ( v.length() != 0 ) // for brain damaged headers
142 if ( ! isdigit((unsigned char)v[v.length()-1]) )
144 v.erase(v.length()-1, 1);
147 s << " ==>\t[" << ts->GetValue(v) << "]";
152 //if (e == 0x0000) { // elem 0x0000 --> group length
153 if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
155 if (v == "4294967295") // to avoid troubles in convertion
157 st = Util::Format(" x(ffffffff)");
161 if ( GetLength() != 0 )
163 st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
167 st = Util::Format(" ");
175 //-----------------------------------------------------------------------------
179 * \brief Sets the std::string representable' value of a ValEntry
180 * @param val value to set
182 void ValEntry::SetValue(std::string const &val)
184 // Integers have a special treatement for their length:
185 int l = val.length();
186 if ( l != 0) // To avoid to be cheated by 'zero length' integers
188 const VRKey &vr = GetVR();
189 if( vr == "US" || vr == "SS" )
191 // for multivaluated items
192 l = (Util::CountSubstring(val, "\\") + 1) * 2;
193 ContentEntry::SetValue(val);
195 else if( vr == "UL" || vr == "SL" )
197 // for multivaluated items
198 l = (Util::CountSubstring(val, "\\") + 1) * 4;;
199 ContentEntry::SetValue(val);
203 std::string finalVal = Util::DicomString( val.c_str() );
204 gdcmAssertMacro( !(finalVal.size() % 2) );
206 l = finalVal.length();
207 ContentEntry::SetValue(finalVal);
212 std::string finalVal = Util::DicomString( val.c_str() );
213 gdcmAssertMacro( !(finalVal.size() % 2) );
215 l = finalVal.length();
216 ContentEntry::SetValue(finalVal);
223 * \brief Writes the std::string representable' value of a ValEntry
224 * @param fp already open ofstream pointer
225 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
227 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
229 DocEntry::WriteContent(fp, filetype);
231 if ( GetGroup() == 0xfffe )
233 return; //delimitors have NO value
236 const VRKey &vr = GetVR();
237 unsigned int lgr = GetLength();
238 if (vr == "US" || vr == "SS")
240 // some 'Short integer' fields may be multivaluated
241 // each single value is separated from the next one by '\'
242 // we split the string and write each value as a short int
243 std::vector<std::string> tokens;
244 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
245 Util::Tokenize (GetValue(), tokens, "\\");
246 for (unsigned int i=0; i<tokens.size();i++)
248 uint16_t val_uint16 = atoi(tokens[i].c_str());
249 binary_write( *fp, val_uint16);
254 if (vr == "UL" || vr == "SL")
256 // Some 'Integer' fields may be multivaluated (multiple instances
257 // of integer). But each single integer value is separated from the
258 // next one by '\' (backslash character). Hence we split the string
259 // along the '\' and write each value as an int:
260 std::vector<std::string> tokens;
261 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
262 Util::Tokenize (GetValue(), tokens, "\\");
263 for (unsigned int i=0; i<tokens.size();i++)
265 uint32_t val_uint32 = atoi(tokens[i].c_str());
266 binary_write( *fp, val_uint32);
272 gdcmAssertMacro( lgr == GetValue().length() );
273 binary_write(*fp, GetValue());
276 //-----------------------------------------------------------------------------
279 //-----------------------------------------------------------------------------
282 //-----------------------------------------------------------------------------
283 } // end namespace gdcm