1 /*=========================================================================
4 Module: $RCSfile: gdcmValEntry.cxx,v $
6 Date: $Date: 2005/01/25 16:32:45 $
7 Version: $Revision: 1.52 $
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())
56 * \brief Canonical destructor.
58 ValEntry::~ValEntry ()
62 //-----------------------------------------------------------------------------
65 * \brief Prints the 'std::string representable' value of ValEntry
66 * @param os ostream we want to print in
67 * @param indent Indentation string to be prepended during printing
69 void ValEntry::Print(std::ostream &os, std::string const &)
71 uint16_t g = GetGroup();
72 uint16_t e = GetElement();
81 if (g == 0xfffe) // delimiters have NO value
83 // just to avoid identing all the remaining code
87 TS * ts = Global::GetTS();
89 TSAtr v = GetValue(); // not applicable for SQ ...
90 d2 = Util::CreateCleanString(v); // replace non printable characters by '.'
91 if( GetLength() <= MAX_SIZE_PRINT_ELEMENT_VALUE
93 || d2.find(GDCM_NOTLOADED) < d2.length() )
95 s << " [" << d2 << "]";
99 s << " [gdcm::too long for print (" << GetLength() << ") ]";
102 // Display the UID value (instead of displaying only the rough code)
103 // First 'clean' trailing character (space or zero)
106 // Any more to be displayed ?
107 if ( e == 0x0010 || e == 0x0002 )
109 if ( v.length() != 0 ) // for brain damaged headers
111 if ( ! isdigit((unsigned char)v[v.length()-1]) )
113 v.erase(v.length()-1, 1);
116 s << " ==>\t[" << ts->GetValue(v) << "]";
123 if ( e == 0x0016 || e == 0x1150 )
125 if ( v.length() != 0 ) // for brain damaged headers
127 if ( ! isdigit((unsigned char)v[v.length()-1]) )
129 v.erase(v.length()-1, 1);
132 s << " ==>\t[" << ts->GetValue(v) << "]";
139 if ( e == 0x1510 || e == 0x1512 )
141 if ( v.length() != 0 ) // for brain damaged headers
143 if ( ! isdigit((unsigned char)v[v.length()-1]) )
145 v.erase(v.length()-1, 1);
148 s << " ==>\t[" << ts->GetValue(v) << "]";
153 //if (e == 0x0000) { // elem 0x0000 --> group length
154 if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
156 if (v == "4294967295") // to avoid troubles in convertion
158 st = Util::Format(" x(ffffffff)");
162 if ( GetLength() != 0 )
164 st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
168 st = Util::Format(" ");
176 //-----------------------------------------------------------------------------
180 * \brief Sets the std::string representable' value of a ValEntry
181 * @param val value to set
184 void ValEntry::SetValue(std::string const &val)
186 // Integers have a special treatement for their length:
187 int l = val.length();
188 if ( l != 0) // To avoid to be cheated by 'zero length' integers
190 const VRKey &vr = GetVR();
191 if( vr == "US" || vr == "SS" )
193 // for multivaluated items
194 l = (Util::CountSubstring(val, "\\") + 1) * 2;
195 ContentEntry::SetValue(val);
197 else if( vr == "UL" || vr == "SL" )
199 // for multivaluated items
200 l = (Util::CountSubstring(val, "\\") + 1) * 4;;
201 ContentEntry::SetValue(val);
205 std::string finalVal = Util::DicomString( val.c_str() );
206 gdcmAssertMacro( !(finalVal.size() % 2) );
208 l = finalVal.length();
209 ContentEntry::SetValue(finalVal);
214 std::string finalVal = Util::DicomString( val.c_str() );
215 gdcmAssertMacro( !(finalVal.size() % 2) );
217 l = finalVal.length();
218 ContentEntry::SetValue(finalVal);
225 * \brief Writes the std::string representable' value of a ValEntry
226 * @param fp already open ofstream pointer
227 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
229 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
231 DocEntry::WriteContent(fp, filetype);
233 if ( GetGroup() == 0xfffe )
235 return; //delimitors have NO value
238 const VRKey &vr = GetVR();
239 unsigned int lgr = GetLength();
240 if (vr == "US" || vr == "SS")
242 // some 'Short integer' fields may be multivaluated
243 // each single value is separated from the next one by '\'
244 // we split the string and write each value as a short int
245 std::vector<std::string> tokens;
246 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
247 Util::Tokenize (GetValue(), tokens, "\\");
248 for (unsigned int i=0; i<tokens.size();i++)
250 uint16_t val_uint16 = atoi(tokens[i].c_str());
251 binary_write( *fp, val_uint16);
256 if (vr == "UL" || vr == "SL")
258 // Some 'Integer' fields may be multivaluated (multiple instances
259 // of integer). But each single integer value is separated from the
260 // next one by '\' (backslash character). Hence we split the string
261 // along the '\' and write each value as an int:
262 std::vector<std::string> tokens;
263 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
264 Util::Tokenize (GetValue(), tokens, "\\");
265 for (unsigned int i=0; i<tokens.size();i++)
267 uint32_t val_uint32 = atoi(tokens[i].c_str());
268 binary_write( *fp, val_uint32);
274 gdcmAssertMacro( lgr == GetValue().length() );
275 binary_write(*fp, GetValue());
278 //-----------------------------------------------------------------------------
281 //-----------------------------------------------------------------------------
284 //-----------------------------------------------------------------------------
285 } // end namespace gdcm