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