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