]> Creatis software - gdcm.git/blob - src/gdcmValEntry.cxx
* src/gdcmDocEntry.h : DocEntry is now secure. User can't change the
[gdcm.git] / src / gdcmValEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmValEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/19 15:58:01 $
7   Version:   $Revision: 1.48 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmValEntry.h"
20 #include "gdcmVR.h"
21 #include "gdcmTS.h"
22 #include "gdcmGlobal.h"
23 #include "gdcmUtil.h"
24 #include "gdcmDebug.h"
25
26 #include <fstream>
27
28 namespace gdcm 
29 {
30
31 // CLEAN ME
32 #define MAX_SIZE_PRINT_ELEMENT_VALUE 128
33
34 //-----------------------------------------------------------------------------
35 // Constructor / Destructor
36 /**
37  * \brief   Constructor from a given DictEntry
38  * @param   e Pointer to existing dictionary entry
39  */
40 ValEntry::ValEntry(DictEntry *e) : DocEntry(e)
41 {
42 }
43
44 /**
45  * \brief   Constructor from a given DocEntry
46  * @param   e Pointer to existing Doc entry
47  */
48 ValEntry::ValEntry(DocEntry *e)
49              : DocEntry(e->GetDictEntry())
50 {
51    Copy(e);
52 }
53
54
55 /**
56  * \brief   Canonical destructor.
57  */
58 ValEntry::~ValEntry ()
59 {
60 }
61
62 //-----------------------------------------------------------------------------
63 // Print
64 /**
65  * \brief   canonical Printer
66  */
67 void ValEntry::Print(std::ostream &os, std::string const &)
68 {
69    uint16_t g = GetGroup();
70    uint16_t e = GetElement();
71    VRKey vr = GetVR();
72    std::ostringstream s; 
73    std::string st;
74    std::string d2;
75      
76    os << "V ";
77    DocEntry::Print(os); 
78
79    if (g == 0xfffe)
80    {
81       // just to avoid identing all the remaining code     
82       return;
83    }
84    
85    TS * ts = Global::GetTS();
86     
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
90     || PrintLevel >= 3
91     || d2.find(GDCM_NOTLOADED) < d2.length() )
92    {
93       s << " [" << d2 << "]";
94    }
95    else
96    {
97       s << " [gdcm::too long for print (" << GetLength() << ") ]";
98    }
99    
100    // Display the UID value (instead of displaying only the rough code)
101    // First 'clean' trailing character (space or zero) 
102    if (g == 0x0002)
103    {
104       // Any more to be displayed ?
105       if ( e == 0x0010 || e == 0x0002 )
106       {
107          if ( v.length() != 0 )  // for brain damaged headers
108          {
109             if ( ! isdigit((unsigned char)v[v.length()-1]) )
110             {
111                v.erase(v.length()-1, 1);
112             }
113          }
114          s << "  ==>\t[" << ts->GetValue(v) << "]";
115       }
116    }
117    else
118    {
119       if (g == 0x0008)
120       {
121          if ( e == 0x0016 || e == 0x1150 )
122          {
123             if ( v.length() != 0 )  // for brain damaged headers
124             {
125                if ( ! isdigit((unsigned char)v[v.length()-1]) )
126                {
127                   v.erase(v.length()-1, 1);
128                }
129             }
130             s << "  ==>\t[" << ts->GetValue(v) << "]";
131          }
132       }
133       else
134       {
135          if (g == 0x0004)
136          {
137             if ( e == 0x1510 || e == 0x1512  )
138             {
139                if ( v.length() != 0 )  // for brain damaged headers  
140                {
141                   if ( ! isdigit((unsigned char)v[v.length()-1]) )
142                   {
143                      v.erase(v.length()-1, 1);  
144                   }
145                }
146               s << "  ==>\t[" << ts->GetValue(v) << "]";
147             }
148          }     
149       }
150    }
151    //if (e == 0x0000) {        // elem 0x0000 --> group length 
152    if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
153    {
154       if (v == "4294967295") // to avoid troubles in convertion 
155       {
156          st = Util::Format(" x(ffffffff)");
157       }
158       else
159       {
160          if ( GetLength() != 0 )
161          {
162             st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
163          }
164          else
165          {
166             st = Util::Format(" ");
167          }
168       }
169       s << st;
170    }
171    os << s.str();
172 }
173
174 //-----------------------------------------------------------------------------
175 // Public
176 void ValEntry::SetValue(std::string const &val)
177 {
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
181    {   
182       const VRKey &vr = GetVR();
183       if( vr == "US" || vr == "SS" )
184       {
185          // for multivaluated items
186          l = (Util::CountSubstring(val, "\\") + 1) * 2;
187          SetValueOnly(val);
188       }
189       else if( vr == "UL" || vr == "SL" )
190       {
191          // for multivaluated items
192          l = (Util::CountSubstring(val, "\\") + 1) * 4;;
193          SetValueOnly(val);
194       }
195       else
196       {
197          std::string finalVal = Util::DicomString( val.c_str() );
198          gdcmAssertMacro( !(finalVal.size() % 2) );
199
200          l = finalVal.length();
201          SetValueOnly(finalVal);
202       }
203    }
204    else
205    {
206       std::string finalVal = Util::DicomString( val.c_str() );
207       gdcmAssertMacro( !(finalVal.size() % 2) );
208
209       l = finalVal.length();
210       SetValueOnly(finalVal);
211    }
212
213    SetLength(l);
214 }
215
216 /*
217  * \brief   canonical Writer
218  */
219 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
220 {
221    DocEntry::WriteContent(fp, filetype);
222
223    if ( GetGroup() == 0xfffe )
224    {
225       return; //delimitors have NO value
226    }
227
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")
232    {
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++)
240       {
241          uint16_t val_uint16 = atoi(tokens[i].c_str());
242          binary_write( *fp, val_uint16);
243       }
244       tokens.clear();
245       return;
246    }
247    if (vr == "UL" || vr == "SL")
248    {
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++)
257       {
258          uint32_t val_uint32 = atoi(tokens[i].c_str());
259          binary_write( *fp, val_uint32);
260       }
261       tokens.clear();
262       return;
263    } 
264
265    gdcmAssertMacro( lgr == GetValue().length() );
266    binary_write(*fp, GetValue());
267
268
269 //-----------------------------------------------------------------------------
270 // Protected
271
272 //-----------------------------------------------------------------------------
273 // Private
274
275 //-----------------------------------------------------------------------------
276 } // end namespace gdcm
277