1 /*=========================================================================
4 Module: $RCSfile: gdcmValEntry.cxx,v $
6 Date: $Date: 2005/01/06 20:03:28 $
7 Version: $Revision: 1.42 $
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"
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) : DocEntry(e)
44 * \brief Constructor from a given DocEntry
45 * @param e Pointer to existing Doc entry
47 ValEntry::ValEntry(DocEntry *e)
48 : DocEntry(e->GetDictEntry())
50 UsableLength = e->GetLength();
51 ReadLength = e->GetReadLength();
52 ImplicitVR = e->IsImplicitVR();
53 Offset = e->GetOffset();
58 * \brief Canonical destructor.
60 ValEntry::~ValEntry ()
64 //-----------------------------------------------------------------------------
67 * \brief canonical Printer
69 void ValEntry::Print(std::ostream &os)
71 uint16_t g = GetGroup();
72 uint16_t e = GetElement();
73 std::string vr = GetVR();
84 // just to avoid identing all the remaining code
88 TS * ts = Global::GetTS();
90 v = GetValue(); // not applicable for SQ ...
91 d2 = Util::CreateCleanString(v); // replace non printable characters by '.'
92 if( (GetLength()<=MAX_SIZE_PRINT_ELEMENT_VALUE) ||
93 //(PrintLevel>=3) || (d2.find("gdcm::NotLoaded.") < d2.length()) )
94 (PrintLevel>=3) || (d2.find(GDCM_NOTLOADED) < d2.length()) )
96 s << " [" << d2 << "]";
100 s << " [gdcm::too long for print (" << GetLength() << ") ]";
103 // Display the UID value (instead of displaying only the rough code)
104 // First 'clean' trailing character (space or zero)
107 // Any more to be displayed ?
108 if ( (e == 0x0010) || (e == 0x0002) )
110 if ( v.length() != 0 ) // for brain damaged headers
112 if ( ! isdigit((unsigned char)v[v.length()-1]) )
114 v.erase(v.length()-1, 1);
117 s << " ==>\t[" << ts->GetValue(v) << "]";
124 if ( e == 0x0016 || e == 0x1150 )
126 if ( v.length() != 0 ) // for brain damaged headers
128 if ( ! isdigit((unsigned char)v[v.length()-1]) )
130 v.erase(v.length()-1, 1);
133 s << " ==>\t[" << ts->GetValue(v) << "]";
140 if ( (e == 0x1510) || (e == 0x1512) )
142 if ( v.length() != 0 ) // for brain damaged headers
144 if ( ! isdigit((unsigned char)v[v.length()-1]) )
146 v.erase(v.length()-1, 1);
149 s << " ==>\t[" << ts->GetValue(v) << "]";
154 //if (e == 0x0000) { // elem 0x0000 --> group length
155 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
157 if (v == "4294967295") // to avoid troubles in convertion
159 st = Util::Format(" x(ffffffff)");
163 if ( GetLength() !=0 )
165 st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
169 st = Util::Format(" ");
177 //-----------------------------------------------------------------------------
179 void ValEntry::SetValue(std::string const &val)
181 // Integers have a special treatement for their length:
182 int l = val.length();
183 if ( l != 0) // To avoid to be cheated by 'zero length' integers
186 if( vr == "US" || vr == "SS" )
188 // for multivaluated items
189 l = (Util::CountSubstring(val, "\\") + 1) * 2;
192 else if( vr == "UL" || vr == "SL" )
194 // for multivaluated items
195 l = (Util::CountSubstring(val, "\\") + 1) * 4;;
200 std::string finalVal = Util::DicomString( val.c_str() );
201 assert( !(finalVal.size() % 2) );
203 l = finalVal.length();
204 SetValueOnly(finalVal);
209 std::string finalVal = Util::DicomString( val.c_str() );
210 assert( !(finalVal.size() % 2) );
212 l = finalVal.length();
213 SetValueOnly(finalVal);
220 * \brief canonical Writer
222 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
224 DocEntry::WriteContent(fp, filetype);
226 if ( GetGroup() == 0xfffe )
228 return; //delimitors have NO value
231 std::string vr = GetVR();
232 unsigned int lgr = GetReadLength();
233 if (vr == "US" || vr == "SS")
235 // some 'Short integer' fields may be mulivaluated
236 // each single value is separated from the next one by '\'
237 // we split the string and write each value as a short int
238 std::vector<std::string> tokens;
239 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
240 Util::Tokenize (GetValue(), tokens, "\\");
241 for (unsigned int i=0; i<tokens.size();i++)
243 uint16_t val_uint16 = atoi(tokens[i].c_str());
244 binary_write( *fp, val_uint16);
249 if (vr == "UL" || vr == "SL")
251 // Some 'Integer' fields may be multivaluated (multiple instances
252 // of integers). But each single integer value is separated from the
253 // next one by '\' (backslash character). Hence we split the string
254 // along the '\' and write each value as an int:
255 std::vector<std::string> tokens;
256 tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
257 Util::Tokenize (GetValue(), tokens, "\\");
258 for (unsigned int i=0; i<tokens.size();i++)
260 uint32_t val_uint32 = atoi(tokens[i].c_str());
261 binary_write( *fp, val_uint32);
267 assert( lgr == GetValue().length() );
268 binary_write(*fp, GetValue());
271 //-----------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------
277 //-----------------------------------------------------------------------------
278 } // end namespace gdcm