1 /*=========================================================================
4 Module: $RCSfile: gdcmValEntry.cxx,v $
6 Date: $Date: 2005/01/19 15:58:01 $
7 Version: $Revision: 1.48 $
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) : DocEntry(e)
45 * \brief Constructor from a given DocEntry
46 * @param e Pointer to existing Doc entry
48 ValEntry::ValEntry(DocEntry *e)
49 : DocEntry(e->GetDictEntry())
56 * \brief Canonical destructor.
58 ValEntry::~ValEntry ()
62 //-----------------------------------------------------------------------------
65 * \brief canonical Printer
67 void ValEntry::Print(std::ostream &os, std::string const &)
69 uint16_t g = GetGroup();
70 uint16_t e = GetElement();
81 // just to avoid identing all the remaining code
85 TS * ts = Global::GetTS();
87 TSAtr v = GetValue(); // not applicable for SQ ...
88 d2 = Util::CreateCleanString(v); // replace non printable characters by '.'
89 if( GetLength() <= MAX_SIZE_PRINT_ELEMENT_VALUE
91 || d2.find(GDCM_NOTLOADED) < d2.length() )
93 s << " [" << d2 << "]";
97 s << " [gdcm::too long for print (" << GetLength() << ") ]";
100 // Display the UID value (instead of displaying only the rough code)
101 // First 'clean' trailing character (space or zero)
104 // Any more to be displayed ?
105 if ( e == 0x0010 || e == 0x0002 )
107 if ( v.length() != 0 ) // for brain damaged headers
109 if ( ! isdigit((unsigned char)v[v.length()-1]) )
111 v.erase(v.length()-1, 1);
114 s << " ==>\t[" << ts->GetValue(v) << "]";
121 if ( e == 0x0016 || e == 0x1150 )
123 if ( v.length() != 0 ) // for brain damaged headers
125 if ( ! isdigit((unsigned char)v[v.length()-1]) )
127 v.erase(v.length()-1, 1);
130 s << " ==>\t[" << ts->GetValue(v) << "]";
137 if ( e == 0x1510 || e == 0x1512 )
139 if ( v.length() != 0 ) // for brain damaged headers
141 if ( ! isdigit((unsigned char)v[v.length()-1]) )
143 v.erase(v.length()-1, 1);
146 s << " ==>\t[" << ts->GetValue(v) << "]";
151 //if (e == 0x0000) { // elem 0x0000 --> group length
152 if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
154 if (v == "4294967295") // to avoid troubles in convertion
156 st = Util::Format(" x(ffffffff)");
160 if ( GetLength() != 0 )
162 st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
166 st = Util::Format(" ");
174 //-----------------------------------------------------------------------------
176 void ValEntry::SetValue(std::string const &val)
178 // Integers have a special treatement for their length:
179 int l = val.length();
180 if ( l != 0) // To avoid to be cheated by 'zero length' integers
182 const VRKey &vr = GetVR();
183 if( vr == "US" || vr == "SS" )
185 // for multivaluated items
186 l = (Util::CountSubstring(val, "\\") + 1) * 2;
189 else if( vr == "UL" || vr == "SL" )
191 // for multivaluated items
192 l = (Util::CountSubstring(val, "\\") + 1) * 4;;
197 std::string finalVal = Util::DicomString( val.c_str() );
198 gdcmAssertMacro( !(finalVal.size() % 2) );
200 l = finalVal.length();
201 SetValueOnly(finalVal);
206 std::string finalVal = Util::DicomString( val.c_str() );
207 gdcmAssertMacro( !(finalVal.size() % 2) );
209 l = finalVal.length();
210 SetValueOnly(finalVal);
217 * \brief canonical Writer
219 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
221 DocEntry::WriteContent(fp, filetype);
223 if ( GetGroup() == 0xfffe )
225 return; //delimitors have NO value
228 const VRKey &vr = GetVR();
229 unsigned int lgr = GetLength();
230 //std::cout<<std::hex<<GetGroup()<<"|"<<GetElement()<<std::dec<<" : "<<GetReadLength()<<" / "<<GetLength()<<"\n";
231 if (vr == "US" || vr == "SS")
233 // some 'Short integer' fields may be mulivaluated
234 // each single value is separated from the next one by '\'
235 // we split the string and write each value as a short int
236 std::vector<std::string> tokens;
237 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
238 Util::Tokenize (GetValue(), tokens, "\\");
239 for (unsigned int i=0; i<tokens.size();i++)
241 uint16_t val_uint16 = atoi(tokens[i].c_str());
242 binary_write( *fp, val_uint16);
247 if (vr == "UL" || vr == "SL")
249 // Some 'Integer' fields may be multivaluated (multiple instances
250 // of integers). But each single integer value is separated from the
251 // next one by '\' (backslash character). Hence we split the string
252 // along the '\' and write each value as an int:
253 std::vector<std::string> tokens;
254 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
255 Util::Tokenize (GetValue(), tokens, "\\");
256 for (unsigned int i=0; i<tokens.size();i++)
258 uint32_t val_uint32 = atoi(tokens[i].c_str());
259 binary_write( *fp, val_uint32);
265 gdcmAssertMacro( lgr == GetValue().length() );
266 binary_write(*fp, GetValue());
269 //-----------------------------------------------------------------------------
272 //-----------------------------------------------------------------------------
275 //-----------------------------------------------------------------------------
276 } // end namespace gdcm