]> Creatis software - gdcm.git/blob - src/gdcmValEntry.cxx
* Preparation of writing a gdcmHeader iterator: generalisation of gdcmTagKey
[gdcm.git] / src / gdcmValEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmValEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/09/13 12:10:53 $
7   Version:   $Revision: 1.25 $
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.htm 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 // CLEAN ME
25 #define MAX_SIZE_PRINT_ELEMENT_VALUE 128
26
27 //-----------------------------------------------------------------------------
28 // Constructor / Destructor
29 /**
30  * \brief   Constructor from a given gdcmDictEntry
31  * @param   e Pointer to existing dictionary entry
32  */
33 gdcmValEntry::gdcmValEntry(gdcmDictEntry* e) : gdcmDocEntry(e)
34 {
35 }
36
37 /**
38  * \brief   Constructor from a given gdcmDocEntry
39  * @param   e Pointer to existing Doc entry
40  */
41 gdcmValEntry::gdcmValEntry(gdcmDocEntry* e)
42              : gdcmDocEntry(e->GetDictEntry())
43 {
44    UsableLength = e->GetLength();
45    ReadLength   = e->GetReadLength();
46    ImplicitVR   = e->IsImplicitVR();
47    Offset       = e->GetOffset();
48    PrintLevel   = e->GetPrintLevel();
49 }
50
51
52 /**
53  * \brief   Canonical destructor.
54  */
55 gdcmValEntry::~gdcmValEntry ()
56 {
57 }
58
59 //-----------------------------------------------------------------------------
60 // Print
61 /**
62  * \brief   canonical Printer
63  */
64 void gdcmValEntry::Print(std::ostream & os)
65 {
66    uint16_t g = GetGroup();
67    uint16_t e = GetElement();
68    std::string vr = GetVR();
69    std::ostringstream s; 
70    char st[20];
71    TSKey v;
72    std::string d2;
73      
74    gdcmDocEntry::Print(os); 
75
76    if (g == 0xfffe)
77    {
78       // just to avoid identing all the remaining code     
79       return;
80    }
81    
82    gdcmTS * ts = gdcmGlobal::GetTS();
83     
84    v  = GetValue();  // not applicable for SQ ...     
85    d2 = CreateCleanString(v);  // replace non printable characters by '.'            
86    if( (GetLength()<=MAX_SIZE_PRINT_ELEMENT_VALUE) || 
87        //(PrintLevel>=3)  || (d2.find("gdcm::NotLoaded.") < d2.length()) )
88        (PrintLevel>=3)  || (d2.find(GDCM_NOTLOADED) < d2.length()) )
89    {
90       s << " [" << d2 << "]";
91    }
92    else
93    {
94       s << " [gdcm::too long for print (" << GetLength() << ") ]";
95    }
96    
97    // Display the UID value (instead of displaying only the rough code)
98    // First 'clean' trailing character (space or zero) 
99    if (g == 0x0002)
100    {
101       // Any more to be displayed ?
102       if ( (e == 0x0010) || (e == 0x0002) )
103       {
104          if ( v.length() != 0 )  // for brain damaged headers
105          {
106             if ( ! isdigit(v[v.length()-1]) )
107             {
108                v.erase(v.length()-1, 1);
109             }
110          }
111          s << "  ==>\t[" << ts->GetValue(v) << "]";
112       }
113    }
114    else
115    {
116       if (g == 0x0008)
117       {
118          if ( e == 0x0016 || e == 0x1150 )
119          {
120             if ( v.length() != 0 )  // for brain damaged headers
121             {
122                if ( ! isdigit(v[v.length()-1]) )
123                {
124                   v.erase(v.length()-1, 1);
125                }
126             }
127             s << "  ==>\t[" << ts->GetValue(v) << "]";
128          }
129       }
130       else
131       {
132          if (g == 0x0004)
133          {
134             if ( (e == 0x1510) || (e == 0x1512)  )
135             {
136                if ( v.length() != 0 )  // for brain damaged headers  
137                {
138                   if ( ! isdigit(v[v.length()-1]) )
139                   {
140                      v.erase(v.length()-1, 1);  
141                   }
142                }
143               s << "  ==>\t[" << ts->GetValue(v) << "]";
144             }
145          }     
146       }
147    }
148    //if (e == 0x0000) {        // elem 0x0000 --> group length 
149    if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
150    {
151       if (v == "4294967295") // to avoid troubles in convertion 
152       {
153          sprintf (st," x(ffffffff)");
154       }
155       else
156       {
157          if ( GetLength() !=0 )
158          {
159             sprintf(st," x(%x)", atoi(v.c_str()));//FIXME
160          }
161          else
162          {
163             sprintf(st," ");
164          }
165       }
166       s << st;
167    }
168    os << s.str();
169 }
170
171 /*
172  * \brief   canonical Writer
173  */
174 void gdcmValEntry::Write(FILE *fp, FileType filetype)
175 {
176    gdcmDocEntry::Write(fp, filetype);
177
178    //std::cout << "=====================================" << GetVR() << std::endl;
179       
180    if ( GetGroup() == 0xfffe ) 
181    {
182       return; //delimitors have NO value
183    }
184       
185    std::string vr = GetVR();
186    int lgr = GetReadLength();
187    if (vr == "US" || vr == "SS")
188    {
189       // some 'Short integer' fields may be mulivaluated
190       // each single value is separated from the next one by '\'
191       // we split the string and write each value as a short int
192       std::vector<std::string> tokens;
193       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
194       Tokenize (GetValue(), tokens, "\\");
195       for (unsigned int i=0; i<tokens.size();i++)
196       {
197          uint16_t val_uint16 = atoi(tokens[i].c_str());
198          void *ptr = &val_uint16;
199          fwrite ( ptr,(size_t)2 ,(size_t)1 ,fp);
200       }
201       tokens.clear();
202       return;
203    }
204    if (vr == "UL" || vr == "SL")
205    {
206       // Some 'Integer' fields may be multivaluated (multiple instances 
207       // of integers). But each single integer value is separated from the
208       // next one by '\' (backslash character). Hence we split the string
209       // along the '\' and write each value as an int:
210       std::vector<std::string> tokens;
211       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
212       Tokenize (GetValue(), tokens, "\\");
213       for (unsigned int i=0; i<tokens.size();i++)
214       {
215          uint32_t val_uint32 = atoi(tokens[i].c_str());
216          void *ptr = &val_uint32;
217          fwrite ( ptr,(size_t)4 ,(size_t)1 ,fp);
218       }
219       tokens.clear();
220       return;
221    } 
222           
223    fwrite (GetValue().c_str(), (size_t)lgr ,(size_t)1, fp); // Elem value
224
225
226 //-----------------------------------------------------------------------------
227 // Public
228
229 //-----------------------------------------------------------------------------
230 // Protected
231
232 //-----------------------------------------------------------------------------
233 // Private
234
235 //-----------------------------------------------------------------------------