]> Creatis software - gdcm.git/blob - src/gdcmValEntry.cxx
* Test/TestUtil.cxx : reformat the source code
[gdcm.git] / src / gdcmValEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmValEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/12/07 13:39:33 $
7   Version:   $Revision: 1.39 $
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
25 #include <fstream>
26
27 namespace gdcm 
28 {
29
30 // CLEAN ME
31 #define MAX_SIZE_PRINT_ELEMENT_VALUE 128
32
33 //-----------------------------------------------------------------------------
34 // Constructor / Destructor
35 /**
36  * \brief   Constructor from a given DictEntry
37  * @param   e Pointer to existing dictionary entry
38  */
39 ValEntry::ValEntry(DictEntry* e) : DocEntry(e)
40 {
41 }
42
43 /**
44  * \brief   Constructor from a given DocEntry
45  * @param   e Pointer to existing Doc entry
46  */
47 ValEntry::ValEntry(DocEntry* e)
48              : DocEntry(e->GetDictEntry())
49 {
50    UsableLength = e->GetLength();
51    ReadLength   = e->GetReadLength();
52    ImplicitVR   = e->IsImplicitVR();
53    Offset       = e->GetOffset();
54    PrintLevel   = e->GetPrintLevel();
55 }
56
57
58 /**
59  * \brief   Canonical destructor.
60  */
61 ValEntry::~ValEntry ()
62 {
63 }
64
65 //-----------------------------------------------------------------------------
66 // Print
67 /**
68  * \brief   canonical Printer
69  */
70 void ValEntry::Print(std::ostream & os)
71 {
72    uint16_t g = GetGroup();
73    uint16_t e = GetElement();
74    std::string vr = GetVR();
75    std::ostringstream s; 
76    std::string st;
77    TSKey v;
78    std::string d2;
79      
80    os << "V ";
81    DocEntry::Print(os); 
82
83    if (g == 0xfffe)
84    {
85       // just to avoid identing all the remaining code     
86       return;
87    }
88    
89    TS * ts = Global::GetTS();
90     
91    v  = GetValue();  // not applicable for SQ ...     
92    d2 = Util::CreateCleanString(v);  // replace non printable characters by '.'            
93    if( (GetLength()<=MAX_SIZE_PRINT_ELEMENT_VALUE) || 
94        //(PrintLevel>=3)  || (d2.find("gdcm::NotLoaded.") < d2.length()) )
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       }
192       else if( vr == "UL" || vr == "SL" )
193       {
194          // for multivaluated items
195          l = (Util::CountSubstring(val, "\\") + 1) * 4;;
196       }
197       SetValueOnly(val);
198    }
199    else
200    {
201       std::string finalVal = Util::DicomString( val.c_str() );
202       assert( !(finalVal.size() % 2) );
203
204       l = finalVal.length();
205       SetValueOnly(finalVal);
206    }
207
208    SetLength(l);
209 }
210
211 /*
212  * \brief   canonical Writer
213  */
214 void ValEntry::WriteContent(std::ofstream* fp, FileType filetype)
215 {
216    DocEntry::WriteContent(fp, filetype);
217
218    if ( GetGroup() == 0xfffe ) 
219    {
220       return; //delimitors have NO value
221    }
222       
223    std::string vr = GetVR();
224    unsigned int lgr = GetReadLength();
225    if (vr == "US" || vr == "SS")
226    {
227       // some 'Short integer' fields may be mulivaluated
228       // each single value is separated from the next one by '\'
229       // we split the string and write each value as a short int
230       std::vector<std::string> tokens;
231       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
232       Util::Tokenize (GetValue(), tokens, "\\");
233       for (unsigned int i=0; i<tokens.size();i++)
234       {
235          uint16_t val_uint16 = atoi(tokens[i].c_str());
236          binary_write( *fp, val_uint16);
237       }
238       tokens.clear();
239       return;
240    }
241    if (vr == "UL" || vr == "SL")
242    {
243       // Some 'Integer' fields may be multivaluated (multiple instances 
244       // of integers). But each single integer value is separated from the
245       // next one by '\' (backslash character). Hence we split the string
246       // along the '\' and write each value as an int:
247       std::vector<std::string> tokens;
248       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
249       Util::Tokenize (GetValue(), tokens, "\\");
250       for (unsigned int i=0; i<tokens.size();i++)
251       {
252          uint32_t val_uint32 = atoi(tokens[i].c_str());
253          binary_write( *fp, val_uint32);
254       }
255       tokens.clear();
256       return;
257    } 
258
259    assert( lgr == GetValue().length() );
260    binary_write(*fp, GetValue());
261
262
263 //-----------------------------------------------------------------------------
264 // Protected
265
266 //-----------------------------------------------------------------------------
267 // Private
268
269 //-----------------------------------------------------------------------------
270 } // end namespace gdcm
271