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