]> Creatis software - gdcm.git/blob - src/gdcmValEntry.cxx
0ffc80910084d99d343c6216c4564c289c400f96
[gdcm.git] / src / gdcmValEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmValEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/12 17:10:15 $
7   Version:   $Revision: 1.45 $
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 /*   Length     = e->GetLength();
53    ReadLength = e->GetReadLength();
54    ImplicitVR = e->IsImplicitVR();
55    Offset     = e->GetOffset();*/
56 }
57
58
59 /**
60  * \brief   Canonical destructor.
61  */
62 ValEntry::~ValEntry ()
63 {
64 }
65
66 //-----------------------------------------------------------------------------
67 // Print
68 /**
69  * \brief   canonical Printer
70  */
71 void ValEntry::Print(std::ostream &os)
72 {
73    uint16_t g = GetGroup();
74    uint16_t e = GetElement();
75    std::string vr = GetVR();
76    std::ostringstream s; 
77    std::string st;
78    TSKey v;
79    std::string d2;
80      
81    os << "V ";
82    DocEntry::Print(os); 
83
84    if (g == 0xfffe)
85    {
86       // just to avoid identing all the remaining code     
87       return;
88    }
89    
90    TS * ts = Global::GetTS();
91     
92    v  = GetValue();  // not applicable for SQ ...     
93    d2 = Util::CreateCleanString(v);  // replace non printable characters by '.'            
94    if( (GetLength()<=MAX_SIZE_PRINT_ELEMENT_VALUE) || 
95        (PrintLevel>=3)  || (d2.find(GDCM_NOTLOADED) < d2.length()) )
96    {
97       s << " [" << d2 << "]";
98    }
99    else
100    {
101       s << " [gdcm::too long for print (" << GetLength() << ") ]";
102    }
103    
104    // Display the UID value (instead of displaying only the rough code)
105    // First 'clean' trailing character (space or zero) 
106    if (g == 0x0002)
107    {
108       // Any more to be displayed ?
109       if ( (e == 0x0010) || (e == 0x0002) )
110       {
111          if ( v.length() != 0 )  // for brain damaged headers
112          {
113             if ( ! isdigit((unsigned char)v[v.length()-1]) )
114             {
115                v.erase(v.length()-1, 1);
116             }
117          }
118          s << "  ==>\t[" << ts->GetValue(v) << "]";
119       }
120    }
121    else
122    {
123       if (g == 0x0008)
124       {
125          if ( e == 0x0016 || e == 0x1150 )
126          {
127             if ( v.length() != 0 )  // for brain damaged headers
128             {
129                if ( ! isdigit((unsigned char)v[v.length()-1]) )
130                {
131                   v.erase(v.length()-1, 1);
132                }
133             }
134             s << "  ==>\t[" << ts->GetValue(v) << "]";
135          }
136       }
137       else
138       {
139          if (g == 0x0004)
140          {
141             if ( (e == 0x1510) || (e == 0x1512)  )
142             {
143                if ( v.length() != 0 )  // for brain damaged headers  
144                {
145                   if ( ! isdigit((unsigned char)v[v.length()-1]) )
146                   {
147                      v.erase(v.length()-1, 1);  
148                   }
149                }
150               s << "  ==>\t[" << ts->GetValue(v) << "]";
151             }
152          }     
153       }
154    }
155    //if (e == 0x0000) {        // elem 0x0000 --> group length 
156    if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
157    {
158       if (v == "4294967295") // to avoid troubles in convertion 
159       {
160          st = Util::Format(" x(ffffffff)");
161       }
162       else
163       {
164          if ( GetLength() !=0 )
165          {
166             st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
167          }
168          else
169          {
170             st = Util::Format(" ");
171          }
172       }
173       s << st;
174    }
175    os << s.str();
176 }
177
178 //-----------------------------------------------------------------------------
179 // Public
180 void ValEntry::SetValue(std::string const &val)
181 {
182    // Integers have a special treatement for their length:
183    int l = val.length();
184    if ( l != 0) // To avoid to be cheated by 'zero length' integers
185    {   
186       VRKey vr = GetVR();
187       if( vr == "US" || vr == "SS" )
188       {
189          // for multivaluated items
190          l = (Util::CountSubstring(val, "\\") + 1) * 2;
191          SetValueOnly(val);
192       }
193       else if( vr == "UL" || vr == "SL" )
194       {
195          // for multivaluated items
196          l = (Util::CountSubstring(val, "\\") + 1) * 4;;
197          SetValueOnly(val);
198       }
199       else
200       {
201          std::string finalVal = Util::DicomString( val.c_str() );
202          gdcmAssertMacro( !(finalVal.size() % 2) );
203
204          l = finalVal.length();
205          SetValueOnly(finalVal);
206       }
207    }
208    else
209    {
210       std::string finalVal = Util::DicomString( val.c_str() );
211       gdcmAssertMacro( !(finalVal.size() % 2) );
212
213       l = finalVal.length();
214       SetValueOnly(finalVal);
215    }
216
217    SetLength(l);
218 }
219
220 /*
221  * \brief   canonical Writer
222  */
223 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
224 {
225    DocEntry::WriteContent(fp, filetype);
226
227    if ( GetGroup() == 0xfffe )
228    {
229       return; //delimitors have NO value
230    }
231
232    std::string vr = GetVR();
233    unsigned int lgr = GetLength();
234    //std::cout<<std::hex<<GetGroup()<<"|"<<GetElement()<<std::dec<<" : "<<GetReadLength()<<" / "<<GetLength()<<"\n";
235    if (vr == "US" || vr == "SS")
236    {
237       // some 'Short integer' fields may be mulivaluated
238       // each single value is separated from the next one by '\'
239       // we split the string and write each value as a short int
240       std::vector<std::string> tokens;
241       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
242       Util::Tokenize (GetValue(), tokens, "\\");
243       for (unsigned int i=0; i<tokens.size();i++)
244       {
245          uint16_t val_uint16 = atoi(tokens[i].c_str());
246          binary_write( *fp, val_uint16);
247       }
248       tokens.clear();
249       return;
250    }
251    if (vr == "UL" || vr == "SL")
252    {
253       // Some 'Integer' fields may be multivaluated (multiple instances 
254       // of integers). But each single integer value is separated from the
255       // next one by '\' (backslash character). Hence we split the string
256       // along the '\' and write each value as an int:
257       std::vector<std::string> tokens;
258       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
259       Util::Tokenize (GetValue(), tokens, "\\");
260       for (unsigned int i=0; i<tokens.size();i++)
261       {
262          uint32_t val_uint32 = atoi(tokens[i].c_str());
263          binary_write( *fp, val_uint32);
264       }
265       tokens.clear();
266       return;
267    } 
268
269    gdcmAssertMacro( lgr == GetValue().length() );
270    binary_write(*fp, GetValue());
271
272
273 //-----------------------------------------------------------------------------
274 // Protected
275
276 //-----------------------------------------------------------------------------
277 // Private
278
279 //-----------------------------------------------------------------------------
280 } // end namespace gdcm
281