1 /*=========================================================================
4 Module: $RCSfile: gdcmValEntry.cxx,v $
6 Date: $Date: 2005/04/24 16:21:44 $
7 Version: $Revision: 1.60 $
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"
31 //-----------------------------------------------------------------------------
32 #define MAX_SIZE_PRINT_ELEMENT_VALUE 0x7fffffff
33 uint32_t ValEntry::MaxSizePrintEntry = MAX_SIZE_PRINT_ELEMENT_VALUE;
34 //-----------------------------------------------------------------------------
35 // Constructor / Destructor
37 * \brief Constructor from a given DictEntry
38 * @param e Pointer to existing dictionary entry
40 ValEntry::ValEntry(DictEntry *e)
46 * \brief Constructor from a given DocEntry
47 * @param e Pointer to existing Doc entry
49 ValEntry::ValEntry(DocEntry *e)
50 : ContentEntry(e->GetDictEntry())
56 * \brief Canonical destructor.
58 ValEntry::~ValEntry ()
62 //-----------------------------------------------------------------------------
65 * \brief Writes the std::string representable' value of a ValEntry
66 * @param fp already open ofstream pointer
67 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
69 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
71 DocEntry::WriteContent(fp, filetype);
73 if ( GetGroup() == 0xfffe )
75 return; //delimitors have NO value
78 const VRKey &vr = GetVR();
79 unsigned int lgth = GetLength();
80 if (vr == "US" || vr == "SS")
82 // some 'Short integer' fields may be multivaluated
83 // each single value is separated from the next one by '\'
84 // we split the string and write each value as a short int
85 std::vector<std::string> tokens;
86 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
87 Util::Tokenize (GetValue(), tokens, "\\");
88 for (unsigned int i=0; i<tokens.size();i++)
90 uint16_t val_uint16 = atoi(tokens[i].c_str());
91 binary_write( *fp, val_uint16);
96 if (vr == "UL" || vr == "SL")
98 // Some 'Integer' fields may be multivaluated (multiple instances
99 // of integer). But each single integer value is separated from the
100 // next one by '\' (backslash character). Hence we split the string
101 // along the '\' and write each value as an int:
102 std::vector<std::string> tokens;
103 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
104 Util::Tokenize (GetValue(), tokens, "\\");
105 for (unsigned int i=0; i<tokens.size();i++)
107 uint32_t val_uint32 = atoi(tokens[i].c_str());
108 binary_write( *fp, val_uint32);
114 gdcmAssertMacro( lgth == GetValue().length() );
115 binary_write(*fp, GetValue());
120 * \brief Header Elements too long will not be printed
121 * @param newSize new size
123 void ValEntry::SetMaxSizePrintEntry(long newSize)
129 if ((uint32_t)newSize >= (uint32_t)0xffffffff )
131 ValEntry::MaxSizePrintEntry = 0xffffffff;
134 ValEntry::MaxSizePrintEntry = newSize;
139 * \brief Sets the std::string representable' value of a ValEntry
140 * @param val value to set
142 void ValEntry::SetValue(std::string const &val)
144 // Integers have a special treatement for their length:
145 int l = val.length();
146 if ( l != 0) // To avoid to be cheated by 'zero length' integers
148 const VRKey &vr = GetVR();
149 if( vr == "US" || vr == "SS" )
151 // for multivaluated items
152 l = (Util::CountSubstring(val, "\\") + 1) * 2;
153 ContentEntry::SetValue(val);
155 else if( vr == "UL" || vr == "SL" )
157 // for multivaluated items
158 l = (Util::CountSubstring(val, "\\") + 1) * 4;;
159 ContentEntry::SetValue(val);
163 std::string finalVal = Util::DicomString( val.c_str() );
164 gdcmAssertMacro( !(finalVal.size() % 2) );
166 l = finalVal.length();
167 ContentEntry::SetValue(finalVal);
172 std::string finalVal = Util::DicomString( val.c_str() );
173 gdcmAssertMacro( !(finalVal.size() % 2) );
175 l = finalVal.length();
176 ContentEntry::SetValue(finalVal);
182 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
191 * \brief Prints the 'std::string representable' value of ValEntry
192 * @param os ostream we want to print in
193 * @param indent Indentation string to be prepended during printing
195 void ValEntry::Print(std::ostream &os, std::string const &)
197 uint16_t g = GetGroup();
198 uint16_t e = GetElement();
200 std::ostringstream s;
207 if (g == 0xfffe) // delimiters have NO value
209 // just to avoid identing all the remaining code
213 TS *ts = Global::GetTS();
215 TSAtr v = GetValue();
216 d2 = Util::CreateCleanString(v); // replace non printable characters by '.'
217 if( (long)GetLength() <= ValEntry::GetMaxSizePrintEntry()
219 || d2.find(GDCM_NOTLOADED) < d2.length() )
221 s << " [" << d2 << "]";
225 s << " [gdcm::too long for print (" << GetLength() << ") ]";
228 // Display the UID value (instead of displaying only the rough code)
229 // First 'clean' trailing character (space or zero)
232 // Any more to be displayed ?
233 if ( e == 0x0010 || e == 0x0002 )
235 if ( v.length() != 0 ) // for brain damaged headers
237 if ( ! isdigit((unsigned char)v[v.length()-1]) )
239 v.erase(v.length()-1, 1);
242 s << " ==>\t[" << ts->GetValue(v) << "]";
249 if ( e == 0x0016 || e == 0x1150 )
251 if ( v.length() != 0 ) // for brain damaged headers
253 if ( ! isdigit((unsigned char)v[v.length()-1]) )
255 v.erase(v.length()-1, 1);
258 s << " ==>\t[" << ts->GetValue(v) << "]";
265 if ( e == 0x1510 || e == 0x1512 )
267 if ( v.length() != 0 ) // for brain damaged headers
269 if ( ! isdigit((unsigned char)v[v.length()-1]) )
271 v.erase(v.length()-1, 1);
274 s << " ==>\t[" << ts->GetValue(v) << "]";
279 //if (e == 0x0000) { // elem 0x0000 --> group length
280 if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
282 if (v == "4294967295") // to avoid troubles in convertion
284 st = Util::Format(" x(ffffffff)");
288 if ( GetLength() != 0 )
290 st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
294 st = Util::Format(" ");
302 //-----------------------------------------------------------------------------
303 } // end namespace gdcm