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