1 /*=========================================================================
4 Module: $RCSfile: gdcmValEntry.cxx,v $
6 Date: $Date: 2005/07/05 14:55:24 $
7 Version: $Revision: 1.63 $
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"
25 #include "gdcmDocument.h"
28 #include <ctype.h> // for isdigit
29 #include <stdlib.h> // for atoi
33 //-----------------------------------------------------------------------------
34 #define MAX_SIZE_PRINT_ELEMENT_VALUE 0x7fffffff
35 uint32_t ValEntry::MaxSizePrintEntry = MAX_SIZE_PRINT_ELEMENT_VALUE;
36 //-----------------------------------------------------------------------------
37 // Constructor / Destructor
39 * \brief Constructor from a given DictEntry
40 * @param e Pointer to existing dictionary entry
42 ValEntry::ValEntry(DictEntry *e)
48 * \brief Constructor from a given DocEntry
49 * @param e Pointer to existing Doc entry
51 ValEntry::ValEntry(DocEntry *e)
52 : ContentEntry(e->GetDictEntry())
58 * \brief Canonical destructor.
60 ValEntry::~ValEntry ()
64 //-----------------------------------------------------------------------------
67 * \brief Writes the std::string representable' value of a ValEntry
68 * @param fp already open ofstream pointer
69 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
71 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
73 DocEntry::WriteContent(fp, filetype);
75 if ( GetGroup() == 0xfffe )
77 return; //delimitors have NO value
80 const VRKey &vr = GetVR();
81 unsigned int lgth = GetLength();
82 if (vr == "US" || vr == "SS")
84 // some 'Short integer' fields may be multivaluated
85 // each single value is separated from the next one by '\'
86 // we split the string and write each value as a short int
87 std::vector<std::string> tokens;
88 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
89 Util::Tokenize (GetValue(), tokens, "\\");
90 for (unsigned int i=0; i<tokens.size();i++)
92 uint16_t val_uint16 = atoi(tokens[i].c_str());
93 binary_write( *fp, val_uint16);
98 if (vr == "UL" || vr == "SL")
100 // Some 'Integer' fields may be multivaluated (multiple instances
101 // of integer). But each single integer value is separated from the
102 // next one by '\' (backslash character). Hence we split the string
103 // along the '\' and write each value as an int:
104 std::vector<std::string> tokens;
105 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
106 Util::Tokenize (GetValue(), tokens, "\\");
107 for (unsigned int i=0; i<tokens.size();i++)
109 uint32_t val_uint32 = atoi(tokens[i].c_str());
110 binary_write( *fp, val_uint32);
116 gdcmAssertMacro( lgth == GetValue().length() );
117 binary_write(*fp, GetValue());
122 * \brief Header Elements too long will not be printed
123 * @param newSize new size
125 void ValEntry::SetMaxSizePrintEntry(long newSize)
131 if ((uint32_t)newSize >= (uint32_t)0xffffffff )
133 ValEntry::MaxSizePrintEntry = 0xffffffff;
136 ValEntry::MaxSizePrintEntry = newSize;
141 * \brief Sets the std::string representable' value of a ValEntry
142 * @param val value to set
144 void ValEntry::SetValue(std::string const &val)
146 // Integers have a special treatement for their length:
147 int l = val.length();
148 if ( l != 0) // To avoid to be cheated by 'zero length' integers
150 const VRKey &vr = GetVR();
151 if ( vr == "US" || vr == "SS" )
153 // for multivaluated items
154 l = (Util::CountSubstring(val, "\\") + 1) * 2;
155 ContentEntry::SetValue(val);
157 else if ( vr == "UL" || vr == "SL" )
159 // for multivaluated items
160 l = (Util::CountSubstring(val, "\\") + 1) * 4;;
161 ContentEntry::SetValue(val);
165 std::string finalVal = Util::DicomString( val.c_str() );
166 gdcmAssertMacro( !(finalVal.size() % 2) );
168 l = finalVal.length();
169 ContentEntry::SetValue(finalVal);
174 std::string finalVal = Util::DicomString( val.c_str() );
175 gdcmAssertMacro( !(finalVal.size() % 2) );
177 l = finalVal.length();
178 ContentEntry::SetValue(finalVal);
184 //-----------------------------------------------------------------------------
187 //-----------------------------------------------------------------------------
190 //-----------------------------------------------------------------------------
193 * \brief Prints the 'std::string representable' value of ValEntry
194 * @param os ostream we want to print in
195 * @param indent Indentation string to be prepended during printing
197 void ValEntry::Print(std::ostream &os, std::string const &)
199 uint16_t g = GetGroup();
200 uint16_t e = GetElement();
202 std::ostringstream s;
209 if (g == 0xfffe) // delimiters have NO value
211 // just to avoid identing all the remaining code
215 TS *ts = Global::GetTS();
217 TSAtr v = GetValue();
218 d2 = Util::CreateCleanString(v); // replace non printable characters by '.'
219 if ( (long)GetLength() <= ValEntry::GetMaxSizePrintEntry()
221 || d2.find(GDCM_NOTLOADED) < d2.length() )
223 s << " [" << d2 << "]";
227 s << " [gdcm::too long for print (" << GetLength() << ") ]";
230 // Display the UID value (instead of displaying only the rough code)
231 // First 'clean' trailing character (space or zero)
234 // Any more to be displayed ?
235 if ( e == 0x0010 || e == 0x0002 )
237 if ( v.length() != 0 ) // for brain damaged headers
239 if ( ! isdigit((unsigned char)v[v.length()-1]) )
241 v.erase(v.length()-1, 1);
244 s << " ==>\t[" << ts->GetValue(v) << "]";
251 if ( e == 0x0016 || e == 0x1150 )
253 if ( v.length() != 0 ) // for brain damaged headers
255 if ( ! isdigit((unsigned char)v[v.length()-1]) )
257 v.erase(v.length()-1, 1);
260 s << " ==>\t[" << ts->GetValue(v) << "]";
267 if ( e == 0x1510 || e == 0x1512 )
269 if ( v.length() != 0 ) // for brain damaged headers
271 if ( ! isdigit((unsigned char)v[v.length()-1]) )
273 v.erase(v.length()-1, 1);
276 s << " ==>\t[" << ts->GetValue(v) << "]";
281 //if (e == 0x0000) { // elem 0x0000 --> group length
282 if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
284 if (v == "4294967295") // to avoid troubles in convertion
290 if ( GetLength() != 0 )
292 st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
304 //-----------------------------------------------------------------------------
305 } // end namespace gdcm