1 /*=========================================================================
4 Module: $RCSfile: gdcmValEntry.cxx,v $
6 Date: $Date: 2005/02/14 10:45:47 $
7 Version: $Revision: 1.58 $
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)
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 Writes the std::string representable' value of a ValEntry
65 * @param fp already open ofstream pointer
66 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
68 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
70 DocEntry::WriteContent(fp, filetype);
72 if ( GetGroup() == 0xfffe )
74 return; //delimitors have NO value
77 const VRKey &vr = GetVR();
78 unsigned int lgth = GetLength();
79 if (vr == "US" || vr == "SS")
81 // some 'Short integer' fields may be multivaluated
82 // each single value is separated from the next one by '\'
83 // we split the string and write each value as a short int
84 std::vector<std::string> tokens;
85 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
86 Util::Tokenize (GetValue(), tokens, "\\");
87 for (unsigned int i=0; i<tokens.size();i++)
89 uint16_t val_uint16 = atoi(tokens[i].c_str());
90 binary_write( *fp, val_uint16);
95 if (vr == "UL" || vr == "SL")
97 // Some 'Integer' fields may be multivaluated (multiple instances
98 // of integer). But each single integer value is separated from the
99 // next one by '\' (backslash character). Hence we split the string
100 // along the '\' and write each value as an int:
101 std::vector<std::string> tokens;
102 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
103 Util::Tokenize (GetValue(), tokens, "\\");
104 for (unsigned int i=0; i<tokens.size();i++)
106 uint32_t val_uint32 = atoi(tokens[i].c_str());
107 binary_write( *fp, val_uint32);
113 gdcmAssertMacro( lgth == GetValue().length() );
114 binary_write(*fp, GetValue());
118 * \brief Sets the std::string representable' value of a ValEntry
119 * @param val value to set
121 void ValEntry::SetValue(std::string const &val)
123 // Integers have a special treatement for their length:
124 int l = val.length();
125 if ( l != 0) // To avoid to be cheated by 'zero length' integers
127 const VRKey &vr = GetVR();
128 if( vr == "US" || vr == "SS" )
130 // for multivaluated items
131 l = (Util::CountSubstring(val, "\\") + 1) * 2;
132 ContentEntry::SetValue(val);
134 else if( vr == "UL" || vr == "SL" )
136 // for multivaluated items
137 l = (Util::CountSubstring(val, "\\") + 1) * 4;;
138 ContentEntry::SetValue(val);
142 std::string finalVal = Util::DicomString( val.c_str() );
143 gdcmAssertMacro( !(finalVal.size() % 2) );
145 l = finalVal.length();
146 ContentEntry::SetValue(finalVal);
151 std::string finalVal = Util::DicomString( val.c_str() );
152 gdcmAssertMacro( !(finalVal.size() % 2) );
154 l = finalVal.length();
155 ContentEntry::SetValue(finalVal);
161 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
167 //-----------------------------------------------------------------------------
170 * \brief Prints the 'std::string representable' value of ValEntry
171 * @param os ostream we want to print in
172 * @param indent Indentation string to be prepended during printing
174 void ValEntry::Print(std::ostream &os, std::string const &)
176 uint16_t g = GetGroup();
177 uint16_t e = GetElement();
179 std::ostringstream s;
186 if (g == 0xfffe) // delimiters have NO value
188 // just to avoid identing all the remaining code
192 TS *ts = Global::GetTS();
194 TSAtr v = GetValue();
195 d2 = Util::CreateCleanString(v); // replace non printable characters by '.'
196 if( GetLength() <= MAX_SIZE_PRINT_ELEMENT_VALUE
198 || d2.find(GDCM_NOTLOADED) < d2.length() )
200 s << " [" << d2 << "]";
204 s << " [gdcm::too long for print (" << GetLength() << ") ]";
207 // Display the UID value (instead of displaying only the rough code)
208 // First 'clean' trailing character (space or zero)
211 // Any more to be displayed ?
212 if ( e == 0x0010 || e == 0x0002 )
214 if ( v.length() != 0 ) // for brain damaged headers
216 if ( ! isdigit((unsigned char)v[v.length()-1]) )
218 v.erase(v.length()-1, 1);
221 s << " ==>\t[" << ts->GetValue(v) << "]";
228 if ( e == 0x0016 || e == 0x1150 )
230 if ( v.length() != 0 ) // for brain damaged headers
232 if ( ! isdigit((unsigned char)v[v.length()-1]) )
234 v.erase(v.length()-1, 1);
237 s << " ==>\t[" << ts->GetValue(v) << "]";
244 if ( e == 0x1510 || e == 0x1512 )
246 if ( v.length() != 0 ) // for brain damaged headers
248 if ( ! isdigit((unsigned char)v[v.length()-1]) )
250 v.erase(v.length()-1, 1);
253 s << " ==>\t[" << ts->GetValue(v) << "]";
258 //if (e == 0x0000) { // elem 0x0000 --> group length
259 if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
261 if (v == "4294967295") // to avoid troubles in convertion
263 st = Util::Format(" x(ffffffff)");
267 if ( GetLength() != 0 )
269 st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
273 st = Util::Format(" ");
281 //-----------------------------------------------------------------------------
282 } // end namespace gdcm