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