1 /*=========================================================================
4 Module: $RCSfile: gdcmValEntry.cxx,v $
6 Date: $Date: 2005/02/01 10:29:56 $
7 Version: $Revision: 1.54 $
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"
30 //-----------------------------------------------------------------------------
31 #define MAX_SIZE_PRINT_ELEMENT_VALUE 128
33 //-----------------------------------------------------------------------------
34 // Constructor / Destructor
36 * \brief Constructor from a given DictEntry
37 * @param e Pointer to existing dictionary entry
39 ValEntry::ValEntry(DictEntry *e) : ContentEntry(e)
44 * \brief Constructor from a given DocEntry
45 * @param e Pointer to existing Doc entry
47 ValEntry::ValEntry(DocEntry *e)
48 : ContentEntry(e->GetDictEntry())
54 * \brief Canonical destructor.
56 ValEntry::~ValEntry ()
60 //-----------------------------------------------------------------------------
63 * \brief Sets the std::string representable' value of a ValEntry
64 * @param val value to set
66 void ValEntry::SetValue(std::string const &val)
68 // Integers have a special treatement for their length:
70 if ( l != 0) // To avoid to be cheated by 'zero length' integers
72 const VRKey &vr = GetVR();
73 if( vr == "US" || vr == "SS" )
75 // for multivaluated items
76 l = (Util::CountSubstring(val, "\\") + 1) * 2;
77 ContentEntry::SetValue(val);
79 else if( vr == "UL" || vr == "SL" )
81 // for multivaluated items
82 l = (Util::CountSubstring(val, "\\") + 1) * 4;;
83 ContentEntry::SetValue(val);
87 std::string finalVal = Util::DicomString( val.c_str() );
88 gdcmAssertMacro( !(finalVal.size() % 2) );
90 l = finalVal.length();
91 ContentEntry::SetValue(finalVal);
96 std::string finalVal = Util::DicomString( val.c_str() );
97 gdcmAssertMacro( !(finalVal.size() % 2) );
99 l = finalVal.length();
100 ContentEntry::SetValue(finalVal);
107 * \brief Writes the std::string representable' value of a ValEntry
108 * @param fp already open ofstream pointer
109 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
111 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
113 DocEntry::WriteContent(fp, filetype);
115 if ( GetGroup() == 0xfffe )
117 return; //delimitors have NO value
120 const VRKey &vr = GetVR();
121 unsigned int lgr = GetLength();
122 if (vr == "US" || vr == "SS")
124 // some 'Short integer' fields may be multivaluated
125 // each single value is separated from the next one by '\'
126 // we split the string and write each value as a short int
127 std::vector<std::string> tokens;
128 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
129 Util::Tokenize (GetValue(), tokens, "\\");
130 for (unsigned int i=0; i<tokens.size();i++)
132 uint16_t val_uint16 = atoi(tokens[i].c_str());
133 binary_write( *fp, val_uint16);
138 if (vr == "UL" || vr == "SL")
140 // Some 'Integer' fields may be multivaluated (multiple instances
141 // of integer). But each single integer value is separated from the
142 // next one by '\' (backslash character). Hence we split the string
143 // along the '\' and write each value as an int:
144 std::vector<std::string> tokens;
145 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
146 Util::Tokenize (GetValue(), tokens, "\\");
147 for (unsigned int i=0; i<tokens.size();i++)
149 uint32_t val_uint32 = atoi(tokens[i].c_str());
150 binary_write( *fp, val_uint32);
156 gdcmAssertMacro( lgr == GetValue().length() );
157 binary_write(*fp, GetValue());
160 //-----------------------------------------------------------------------------
163 //-----------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------
169 * \brief Prints the 'std::string representable' value of ValEntry
170 * @param os ostream we want to print in
171 * @param indent Indentation string to be prepended during printing
173 void ValEntry::Print(std::ostream &os, std::string const &)
175 uint16_t g = GetGroup();
176 uint16_t e = GetElement();
178 std::ostringstream s;
185 if (g == 0xfffe) // delimiters have NO value
187 // just to avoid identing all the remaining code
191 TS * ts = Global::GetTS();
193 TSAtr v = GetValue(); // not applicable for SQ ...
194 d2 = Util::CreateCleanString(v); // replace non printable characters by '.'
195 if( GetLength() <= MAX_SIZE_PRINT_ELEMENT_VALUE
197 || d2.find(GDCM_NOTLOADED) < d2.length() )
199 s << " [" << d2 << "]";
203 s << " [gdcm::too long for print (" << GetLength() << ") ]";
206 // Display the UID value (instead of displaying only the rough code)
207 // First 'clean' trailing character (space or zero)
210 // Any more to be displayed ?
211 if ( e == 0x0010 || e == 0x0002 )
213 if ( v.length() != 0 ) // for brain damaged headers
215 if ( ! isdigit((unsigned char)v[v.length()-1]) )
217 v.erase(v.length()-1, 1);
220 s << " ==>\t[" << ts->GetValue(v) << "]";
227 if ( e == 0x0016 || e == 0x1150 )
229 if ( v.length() != 0 ) // for brain damaged headers
231 if ( ! isdigit((unsigned char)v[v.length()-1]) )
233 v.erase(v.length()-1, 1);
236 s << " ==>\t[" << ts->GetValue(v) << "]";
243 if ( e == 0x1510 || e == 0x1512 )
245 if ( v.length() != 0 ) // for brain damaged headers
247 if ( ! isdigit((unsigned char)v[v.length()-1]) )
249 v.erase(v.length()-1, 1);
252 s << " ==>\t[" << ts->GetValue(v) << "]";
257 //if (e == 0x0000) { // elem 0x0000 --> group length
258 if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
260 if (v == "4294967295") // to avoid troubles in convertion
262 st = Util::Format(" x(ffffffff)");
266 if ( GetLength() != 0 )
268 st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
272 st = Util::Format(" ");
280 //-----------------------------------------------------------------------------
281 } // end namespace gdcm