GDCM  2.4.5
gdcmByteValue.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: GDCM (Grassroots DICOM). A DICOM library
4 
5  Copyright (c) 2006-2011 Mathieu Malaterre
6  All rights reserved.
7  See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8 
9  This software is distributed WITHOUT ANY WARRANTY; without even
10  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  PURPOSE. See the above copyright notice for more information.
12 
13 =========================================================================*/
14 #ifndef GDCMBYTEVALUE_H
15 #define GDCMBYTEVALUE_H
16 
17 #include "gdcmValue.h"
18 #include "gdcmTrace.h"
19 #include "gdcmVL.h"
20 
21 #include <vector>
22 #include <iterator>
23 #include <iomanip>
24 #include <algorithm>
25 
26 namespace gdcm_ns
27 {
28 #if !defined(SWIGPYTHON) && !defined(SWIGCSHARP) && !defined(SWIGJAVA) && !defined(SWIGPHP)
29 using namespace gdcm;
30 #endif
31 
35 class GDCM_EXPORT ByteValue : public Value
36 {
37 public:
38  ByteValue(const char* array = 0, VL const &vl = 0):
39  Internal(array, array+vl),Length(vl) {
40  if( vl.IsOdd() )
41  {
42  gdcmDebugMacro( "Odd length" );
43  Internal.resize(vl+1);
44  Length++;
45  }
46  }
47 
49  ByteValue(std::vector<char> &v):Internal(v),Length((uint32_t)v.size()) {}
50  //ByteValue(std::ostringstream const &os) {
51  // (void)os;
52  // assert(0); // TODO
53  //}
55  Internal.clear();
56  }
57 
58  // When 'dumping' dicom file we still have some information from
59  // Either the VR: eg LO (private tag)
60  void PrintASCII(std::ostream &os, VL maxlength ) const;
61 
62  void PrintHex(std::ostream &os, VL maxlength) const;
63 
64  // Either from Element Number (== 0x0000)
65  void PrintGroupLength(std::ostream &os) {
66  assert( Length == 2 );
67  (void)os;
68  }
69 
70  bool IsEmpty() const {
71 #if 0
72  if( Internal.empty() ) assert( Length == 0 );
73  return Internal.empty();
74 #else
75  return Length == 0;
76 #endif
77  }
78  VL GetLength() const { return Length; }
79 
80  VL ComputeLength() const { return Length + Length % 2; }
81  // Does a reallocation
82  void SetLength(VL vl);
83 
84  operator const std::vector<char>& () const { return Internal; }
85 
86  ByteValue &operator=(const ByteValue &val) {
87  Internal = val.Internal;
88  Length = val.Length;
89  return *this;
90  }
91 
92  bool operator==(const ByteValue &val) const {
93  if( Length != val.Length )
94  return false;
95  if( Internal == val.Internal )
96  return true;
97  return false;
98  }
99  bool operator==(const Value &val) const
100  {
101  const ByteValue &bv = dynamic_cast<const ByteValue&>(val);
102  return Length == bv.Length && Internal == bv.Internal;
103  }
104 
105  void Append(ByteValue const & bv);
106 
107  void Clear() {
108  Internal.clear();
109  }
110  // Use that only if you understand what you are doing
111  const char *GetPointer() const {
112  if(!Internal.empty()) return &Internal[0];
113  return 0;
114  }
115  void Fill(char c) {
116  //if( Internal.empty() ) return;
117  std::vector<char>::iterator it = Internal.begin();
118  for(; it != Internal.end(); ++it) *it = c;
119  }
120  bool GetBuffer(char *buffer, unsigned long length) const;
121  bool WriteBuffer(std::ostream &os) const {
122  if( Length ) {
123  //assert( Internal.size() <= Length );
124  assert( !(Internal.size() % 2) );
125  os.write(&Internal[0], Internal.size() );
126  }
127  return true;
128  }
129 
130  template <typename TSwap, typename TType>
131  std::istream &Read(std::istream &is, bool readvalues = true) {
132  // If Length is odd we have detected that in SetLength
133  // and calling std::vector::resize make sure to allocate *AND*
134  // initialize values to 0 so we are sure to have a \0 at the end
135  // even in this case
136  if(Length)
137  {
138  if( readvalues )
139  {
140  is.read(&Internal[0], Length);
141  assert( Internal.size() == Length || Internal.size() == Length + 1 );
142  TSwap::SwapArray((TType*)&Internal[0], Internal.size() / sizeof(TType) );
143  }
144  else
145  {
146  is.seekg(Length, std::ios::cur);
147  }
148  }
149  return is;
150  }
151 
152  template <typename TSwap>
153  std::istream &Read(std::istream &is) {
154  return Read<TSwap,uint8_t>(is);
155  }
156 
157 
158  template <typename TSwap, typename TType>
159  std::ostream const &Write(std::ostream &os) const {
160  assert( !(Internal.size() % 2) );
161  if( !Internal.empty() ) {
162  //os.write(&Internal[0], Internal.size());
163  std::vector<char> copy = Internal;
164  TSwap::SwapArray((TType*)&copy[0], Internal.size() / sizeof(TType) );
165  os.write(&copy[0], copy.size());
166  }
167  return os;
168  }
169 
170  template <typename TSwap>
171  std::ostream const &Write(std::ostream &os) const {
172  return Write<TSwap,uint8_t>(os);
173  }
174 
181  bool IsPrintable(VL length) const {
182  assert( length <= Length );
183  for(unsigned int i=0; i<length; i++)
184  {
185  if ( i == (length-1) && Internal[i] == '\0') continue;
186  if ( !( isprint((unsigned char)Internal[i]) || isspace((unsigned char)Internal[i]) ) )
187  {
188  //gdcmWarningMacro( "Cannot print :" << i );
189  return false;
190  }
191  }
192  return true;
193  }
194 
196  void PrintPNXML(std::ostream &os) const;
197  void PrintASCIIXML(std::ostream &os) const;
198  void PrintHexXML(std::ostream &os) const;
199 protected:
200  void Print(std::ostream &os) const {
201  // This is perfectly valid to have a Length = 0 , so we cannot check
202  // the length for printing
203  if( !Internal.empty() )
204  {
205  if( IsPrintable(Length) )
206  {
207  // WARNING: Internal.end() != Internal.begin()+Length
208  std::vector<char>::size_type length = Length;
209  if( Internal.back() == 0 ) --length;
210  std::copy(Internal.begin(), Internal.begin()+length,
211  std::ostream_iterator<char>(os));
212  }
213  else
214  os << "Loaded:" << Internal.size();
215  }
216  else
217  {
218  //os << "Not Loaded";
219  os << "(no value available)";
220  }
221  }
222 /*
223 //Introduce check for invalid XML characters
224 friend std::ostream& operator<<(std::ostream &os,const char c);
225 */
226 
227  void SetLengthOnly(VL vl) {
228  Length = vl;
229  }
230 
231 private:
232  std::vector<char> Internal;
233 
234  // WARNING Length IS NOT Internal.size() some *featured* DICOM
235  // implementation define odd length, we always load them as even number
236  // of byte, so we need to keep the right Length
237  VL Length;
238 };
239 
240 } // end namespace gdcm_ns
241 
242 #endif //GDCMBYTEVALUE_H
ByteValue(std::vector< char > &v)
Definition: gdcmByteValue.h:49
std::istream & Read(std::istream &is, bool readvalues=true)
Definition: gdcmByteValue.h:131
bool IsPrintable(VL length) const
Checks whether a 'ByteValue' is printable or not (in order to avoid corrupting the terminal of invoca...
Definition: gdcmByteValue.h:181
Class to represent the value of a Data Element.
Definition: gdcmValue.h:31
VL GetLength() const
Definition: gdcmByteValue.h:78
~ByteValue()
Definition: gdcmByteValue.h:54
std::ostream const & Write(std::ostream &os) const
Definition: gdcmByteValue.h:159
#define GDCM_EXPORT
Definition: gdcmWin32.h:34
bool operator==(const ByteValue &val) const
Definition: gdcmByteValue.h:92
void Print(std::ostream &os) const
Definition: gdcmByteValue.h:200
Value Length.
Definition: gdcmVL.h:29
#define gdcmDebugMacro(msg)
Debug.
Definition: gdcmTrace.h:119
ByteValue(const char *array=0, VL const &vl=0)
Definition: gdcmByteValue.h:38
void Fill(char c)
Definition: gdcmByteValue.h:115
bool operator==(const Value &val) const
Definition: gdcmByteValue.h:99
Class to represent binary value (array of bytes)
Definition: gdcmByteValue.h:35
void PrintGroupLength(std::ostream &os)
Definition: gdcmByteValue.h:65
bool WriteBuffer(std::ostream &os) const
Definition: gdcmByteValue.h:121
std::ostream const & Write(std::ostream &os) const
Definition: gdcmByteValue.h:171
VL ComputeLength() const
Definition: gdcmByteValue.h:80
void SetLengthOnly(VL vl)
Definition: gdcmByteValue.h:227
std::istream & Read(std::istream &is)
Definition: gdcmByteValue.h:153
void Clear()
Definition: gdcmByteValue.h:107
bool IsEmpty() const
Definition: gdcmByteValue.h:70
const char * GetPointer() const
Definition: gdcmByteValue.h:111
Definition: gdcmASN1.h:20
ByteValue & operator=(const ByteValue &val)
Definition: gdcmByteValue.h:86

Generated on Fri Sep 25 2015 17:58:22 for GDCM by doxygen 1.8.9.1
SourceForge.net Logo