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