]> Creatis software - gdcm.git/blob - src/gdcmHeaderEntry.cxx
* src/gdcmUtil.[cxx|h] _cleanString C style function (replaced with
[gdcm.git] / src / gdcmHeaderEntry.cxx
1 // gdcmHeaderEntry.cxx
2 //-----------------------------------------------------------------------------
3 //
4 #include "gdcmHeaderEntry.h"
5 #include "gdcmTS.h"
6 #include "gdcmUtil.h"
7
8 #ifdef GDCM_NO_ANSI_STRING_STREAM
9 #  include <strstream>
10 #  define  ostringstream ostrstream
11 # else
12 #  include <sstream>
13 #endif
14 #include <iomanip> // for std::ios::left, ...
15
16
17 #define MAX_SIZE_PRINT_ELEMENT_VALUE 64
18
19 //-----------------------------------------------------------------------------
20 // Constructor / Destructor
21 /**
22  * \ingroup gdcmHeaderEntry
23  * \brief   Constructor from a given gdcmDictEntry
24  * @param   in Pointer to existing dictionary entry
25  */
26 gdcmHeaderEntry::gdcmHeaderEntry(gdcmDictEntry* in) {
27         ImplicitVR = false;
28         voidArea = NULL; // unsecure memory area to hold 'non string' values
29         entry = in;
30 }
31
32 //-----------------------------------------------------------------------------
33 // Print
34 /**
35  * \ingroup gdcmHeaderEntry
36  * \brief   canonical Printer
37  */
38 void gdcmHeaderEntry::Print(std::ostream & os) {
39    size_t o;
40    unsigned short int g, e;
41    char st[20];
42    TSKey v;
43    std::string d2, vr;
44    gdcmTS * ts = gdcmGlobal::GetTS();
45    std::ostringstream s;
46    guint32 lgth;
47    char greltag[10];  //group element tag
48
49    g  = GetGroup();
50    e  = GetElement();
51    v  = GetValue();
52    o  = GetOffset();
53    vr = GetVR();
54    sprintf(greltag,"%04x|%04x ",g,e);           
55    s << greltag ;
56        
57    d2 = CreateCleanString(v);  // replace non printable characters by '.'
58    if (printLevel>=2) { 
59       s << "lg : ";
60       lgth = GetReadLength(); // ReadLength, as opposed to UsableLength
61       if (lgth == 0xffffffff) {
62          sprintf(st,"x(ffff)");  // I said : "x(ffff)" !
63          s.setf(std::ios::left);
64          s << std::setw(10-strlen(st)) << " ";  
65          s << st << " ";
66          s.setf(std::ios::left);
67          s << std::setw(8) << "-1";      
68       } else {
69          sprintf(st,"x(%x)",lgth);
70          s.setf(std::ios::left);
71          s << std::setw(10-strlen(st)) << " ";  
72          s << st << " ";
73          s.setf(std::ios::left);
74          s << std::setw(8) << lgth; 
75       }
76       s << " Off.: ";
77       sprintf(st,"x(%x)",o); 
78       s << std::setw(10-strlen(st)) << " ";
79       s << st << " ";
80       s << std::setw(8) << o; 
81    }
82
83    s << "[" << vr  << "] ";
84
85    if (printLevel>=1) {      
86       s.setf(std::ios::left);
87       s << std::setw(66-GetName().length()) << " ";              
88    }
89     
90    s << "[" << GetName()<< "]";
91
92    if (voidArea != NULL) {
93        s << " [gdcm::Non String Data Loaded in Unsecure Area (" 
94          << GetLength() << ") ]";
95    } 
96    
97    else {             
98       if( (GetLength()<MAX_SIZE_PRINT_ELEMENT_VALUE) || 
99           (printLevel>=3)  || 
100           (d2.find("gdcm::NotLoaded.") < d2.length()) )
101          s << " [" << d2 << "]";
102       else 
103          s << " [gdcm::too long for print (" << GetLength() << ") ]";
104    }
105    
106    // Display the UID value (instead of displaying the rough code)  
107    if (g == 0x0002) {  // Any more to be displayed ?
108       if ( (e == 0x0010) || (e == 0x0002) )
109          s << "  ==>\t[" << ts->GetValue(v) << "]";
110    } else {
111       if (g == 0x0008) {
112          if ( (e == 0x0016) || (e == 0x1150)  )
113             s << "  ==>\t[" << ts->GetValue(v) << "]";
114       } else {
115          if (g == 0x0004) {
116             if ( (e == 0x1510) || (e == 0x1512)  )
117                s << "  ==>\t[" << ts->GetValue(v) << "]";
118          }     
119       }
120    }
121    //if (e == 0x0000) {        // elem 0x0000 --> group length 
122    if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") ) {
123       if (v == "4294967295") // to avoid troubles in convertion 
124          sprintf (st," x(ffffffff)");
125       else      
126          sprintf(st," x(%x)",(unsigned long)atof(v.c_str()));
127       s << st;
128    }
129    s << std::endl;
130    os << s.str();
131 }
132
133 //-----------------------------------------------------------------------------
134 // Public
135
136 //-----------------------------------------------------------------------------
137 // Protected
138
139 //-----------------------------------------------------------------------------
140 // Private
141
142 //-----------------------------------------------------------------------------