]> Creatis software - gdcm.git/blob - src/gdcmValEntry.cxx
* src/gdcmBinEntry.cxx, gdcmSeqEntry.cxx, gdcmSQItem.cxx, gdcmValEntry.cxx :
[gdcm.git] / src / gdcmValEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmValEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/24 16:39:19 $
7   Version:   $Revision: 1.37 $
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 "gdcmTS.h"
21 #include "gdcmGlobal.h"
22 #include "gdcmUtil.h"
23
24 #include <fstream>
25
26 namespace gdcm 
27 {
28
29 // CLEAN ME
30 #define MAX_SIZE_PRINT_ELEMENT_VALUE 128
31
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 /**
35  * \brief   Constructor from a given DictEntry
36  * @param   e Pointer to existing dictionary entry
37  */
38 ValEntry::ValEntry(DictEntry* e) : DocEntry(e)
39 {
40 }
41
42 /**
43  * \brief   Constructor from a given DocEntry
44  * @param   e Pointer to existing Doc entry
45  */
46 ValEntry::ValEntry(DocEntry* e)
47              : DocEntry(e->GetDictEntry())
48 {
49    UsableLength = e->GetLength();
50    ReadLength   = e->GetReadLength();
51    ImplicitVR   = e->IsImplicitVR();
52    Offset       = e->GetOffset();
53    PrintLevel   = e->GetPrintLevel();
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  * \brief   canonical Writer
179  */
180 void ValEntry::Write(std::ofstream* fp, FileType filetype)
181 {
182    DocEntry::Write(fp, filetype);
183
184    //std::cout << "=====================================" << GetVR() << std::endl;
185       
186    if ( GetGroup() == 0xfffe ) 
187    {
188       return; //delimitors have NO value
189    }
190       
191    std::string vr = GetVR();
192    unsigned int lgr = GetReadLength();
193    if (vr == "US" || vr == "SS")
194    {
195       // some 'Short integer' fields may be mulivaluated
196       // each single value is separated from the next one by '\'
197       // we split the string and write each value as a short int
198       std::vector<std::string> tokens;
199       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
200       Util::Tokenize (GetValue(), tokens, "\\");
201       for (unsigned int i=0; i<tokens.size();i++)
202       {
203          uint16_t val_uint16 = atoi(tokens[i].c_str());
204          binary_write( *fp, val_uint16);
205       }
206       tokens.clear();
207       return;
208    }
209    if (vr == "UL" || vr == "SL")
210    {
211       // Some 'Integer' fields may be multivaluated (multiple instances 
212       // of integers). But each single integer value is separated from the
213       // next one by '\' (backslash character). Hence we split the string
214       // along the '\' and write each value as an int:
215       std::vector<std::string> tokens;
216       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
217       Util::Tokenize (GetValue(), tokens, "\\");
218       for (unsigned int i=0; i<tokens.size();i++)
219       {
220          uint32_t val_uint32 = atoi(tokens[i].c_str());
221          binary_write( *fp, val_uint32);
222       }
223       tokens.clear();
224       return;
225    } 
226
227    assert( lgr == GetValue().size() ); 
228    binary_write(*fp, GetValue());
229
230
231 //-----------------------------------------------------------------------------
232 // Public
233
234 //-----------------------------------------------------------------------------
235 // Protected
236
237 //-----------------------------------------------------------------------------
238 // Private
239
240 //-----------------------------------------------------------------------------
241 } // end namespace gdcm
242