]> Creatis software - gdcm.git/blob - src/gdcmValEntry.cxx
COMP: gcc 4.0 dont like ; in #define
[gdcm.git] / src / gdcmValEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmValEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/04/24 16:21:44 $
7   Version:   $Revision: 1.60 $
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 #include "gdcmDocument.h"
26
27 #include <fstream>
28
29 namespace gdcm 
30 {
31 //-----------------------------------------------------------------------------
32 #define MAX_SIZE_PRINT_ELEMENT_VALUE 0x7fffffff
33 uint32_t ValEntry::MaxSizePrintEntry = MAX_SIZE_PRINT_ELEMENT_VALUE;
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) 
41         : ContentEntry(e)
42 {
43 }
44
45 /**
46  * \brief   Constructor from a given DocEntry
47  * @param   e Pointer to existing Doc entry
48  */
49 ValEntry::ValEntry(DocEntry *e)
50         : ContentEntry(e->GetDictEntry())
51 {
52    Copy(e);
53 }
54
55 /**
56  * \brief   Canonical destructor.
57  */
58 ValEntry::~ValEntry ()
59 {
60 }
61
62 //-----------------------------------------------------------------------------
63 // Public
64 /**
65  * \brief   Writes the std::string representable' value of a ValEntry
66  * @param fp already open ofstream pointer
67  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
68  */
69 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
70 {
71    DocEntry::WriteContent(fp, filetype);
72
73    if ( GetGroup() == 0xfffe )
74    {
75       return; //delimitors have NO value
76    }
77
78    const VRKey &vr = GetVR();
79    unsigned int lgth = GetLength();
80    if (vr == "US" || vr == "SS")
81    {
82       // some 'Short integer' fields may be multivaluated
83       // each single value is separated from the next one by '\'
84       // we split the string and write each value as a short int
85       std::vector<std::string> tokens;
86       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
87       Util::Tokenize (GetValue(), tokens, "\\");
88       for (unsigned int i=0; i<tokens.size();i++)
89       {
90          uint16_t val_uint16 = atoi(tokens[i].c_str());
91          binary_write( *fp, val_uint16);
92       }
93       tokens.clear();
94       return;
95    }
96    if (vr == "UL" || vr == "SL")
97    {
98       // Some 'Integer' fields may be multivaluated (multiple instances 
99       // of integer). But each single integer value is separated from the
100       // next one by '\' (backslash character). Hence we split the string
101       // along the '\' and write each value as an int:
102       std::vector<std::string> tokens;
103       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
104       Util::Tokenize (GetValue(), tokens, "\\");
105       for (unsigned int i=0; i<tokens.size();i++)
106       {
107          uint32_t val_uint32 = atoi(tokens[i].c_str());
108          binary_write( *fp, val_uint32);
109       }
110       tokens.clear();
111       return;
112    } 
113
114    gdcmAssertMacro( lgth == GetValue().length() );
115    binary_write(*fp, GetValue());
116
117
118
119 /**
120  * \brief Header Elements too long will not be printed
121  * @param newSize new size
122  */ 
123 void ValEntry::SetMaxSizePrintEntry(long newSize) 
124 {
125    if ( newSize < 0 )
126    {
127       return;
128    }
129    if ((uint32_t)newSize >= (uint32_t)0xffffffff )
130    {
131       ValEntry::MaxSizePrintEntry = 0xffffffff;
132       return;
133    }
134    ValEntry::MaxSizePrintEntry = newSize;
135 }
136
137
138 /**
139  * \brief   Sets the std::string representable' value of a ValEntry
140  * @param  val value to set 
141  */
142 void ValEntry::SetValue(std::string const &val)
143 {
144    // Integers have a special treatement for their length:
145    int l = val.length();
146    if ( l != 0) // To avoid to be cheated by 'zero length' integers
147    {   
148       const VRKey &vr = GetVR();
149       if( vr == "US" || vr == "SS" )
150       {
151          // for multivaluated items
152          l = (Util::CountSubstring(val, "\\") + 1) * 2;
153          ContentEntry::SetValue(val);
154       }
155       else if( vr == "UL" || vr == "SL" )
156       {
157          // for multivaluated items
158          l = (Util::CountSubstring(val, "\\") + 1) * 4;;
159          ContentEntry::SetValue(val);
160       }
161       else
162       {
163          std::string finalVal = Util::DicomString( val.c_str() );
164          gdcmAssertMacro( !(finalVal.size() % 2) );
165
166          l = finalVal.length();
167          ContentEntry::SetValue(finalVal);
168       }
169    }
170    else
171    {
172       std::string finalVal = Util::DicomString( val.c_str() );
173       gdcmAssertMacro( !(finalVal.size() % 2) );
174
175       l = finalVal.length();
176       ContentEntry::SetValue(finalVal);
177    }
178
179    SetLength(l);
180 }
181
182 //-----------------------------------------------------------------------------
183 // Protected
184
185 //-----------------------------------------------------------------------------
186 // Private
187
188 //-----------------------------------------------------------------------------
189 // Print
190 /**
191  * \brief   Prints the 'std::string representable' value of ValEntry
192  * @param   os ostream we want to print in
193  * @param indent Indentation string to be prepended during printing
194  */
195 void ValEntry::Print(std::ostream &os, std::string const &)
196 {
197    uint16_t g = GetGroup();
198    uint16_t e = GetElement();
199    VRKey vr   = GetVR();
200    std::ostringstream s; 
201    std::string st;
202    std::string d2;
203      
204    os << "V ";
205    DocEntry::Print(os); 
206
207    if (g == 0xfffe) // delimiters have NO value
208    {
209       // just to avoid identing all the remaining code     
210       return;
211    }
212    
213    TS *ts = Global::GetTS();
214     
215    TSAtr v  = GetValue();     
216    d2 = Util::CreateCleanString(v);  // replace non printable characters by '.'            
217    if( (long)GetLength() <= ValEntry::GetMaxSizePrintEntry()
218     || PrintLevel >= 3
219     || d2.find(GDCM_NOTLOADED) < d2.length() )
220    {
221       s << " [" << d2 << "]";
222    }
223    else
224    {
225       s << " [gdcm::too long for print (" << GetLength() << ") ]";
226    }
227    
228    // Display the UID value (instead of displaying only the rough code)
229    // First 'clean' trailing character (space or zero) 
230    if (g == 0x0002)
231    {
232       // Any more to be displayed ?
233       if ( e == 0x0010 || e == 0x0002 )
234       {
235          if ( v.length() != 0 )  // for brain damaged headers
236          {
237             if ( ! isdigit((unsigned char)v[v.length()-1]) )
238             {
239                v.erase(v.length()-1, 1);
240             }
241          }
242          s << "  ==>\t[" << ts->GetValue(v) << "]";
243       }
244    }
245    else
246    {
247       if (g == 0x0008)
248       {
249          if ( e == 0x0016 || e == 0x1150 )
250          {
251             if ( v.length() != 0 )  // for brain damaged headers
252             {
253                if ( ! isdigit((unsigned char)v[v.length()-1]) )
254                {
255                   v.erase(v.length()-1, 1);
256                }
257             }
258             s << "  ==>\t[" << ts->GetValue(v) << "]";
259          }
260       }
261       else
262       {
263          if (g == 0x0004)
264          {
265             if ( e == 0x1510 || e == 0x1512  )
266             {
267                if ( v.length() != 0 )  // for brain damaged headers  
268                {
269                   if ( ! isdigit((unsigned char)v[v.length()-1]) )
270                   {
271                      v.erase(v.length()-1, 1);  
272                   }
273                }
274               s << "  ==>\t[" << ts->GetValue(v) << "]";
275             }
276          }     
277       }
278    }
279    //if (e == 0x0000) {        // elem 0x0000 --> group length 
280    if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
281    {
282       if (v == "4294967295") // to avoid troubles in convertion 
283       {
284          st = Util::Format(" x(ffffffff)");
285       }
286       else
287       {
288          if ( GetLength() != 0 )
289          {
290             st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
291          }
292          else
293          {
294             st = Util::Format(" ");
295          }
296       }
297       s << st;
298    }
299    os << s.str();
300 }
301
302 //-----------------------------------------------------------------------------
303 } // end namespace gdcm
304