#ifndef GDCMBYTEVALUE_H
#define GDCMBYTEVALUE_H
#include "gdcmValue.h"
#include "gdcmTrace.h"
#include "gdcmVL.h"
#include <vector>
#include <iterator>
#include <iomanip>
#include <algorithm>
#include <cstring>
namespace gdcm_ns
{
#if !defined(SWIGPYTHON) && !defined(SWIGCSHARP) && !defined(SWIGJAVA) && !defined(SWIGPHP)
using namespace gdcm;
#endif
class GDCM_EXPORT ByteValue : public Value
{
public:
ByteValue(const char* array = nullptr, VL const &vl = 0): Length(vl) {
VL bytes_count_to_copy = Length;
if( vl.IsOdd() )
{
gdcmDebugMacro( "Odd length" );
Internal.resize(vl+1);
++Length;
}
Internal.resize(Length);
if( array )
std::memcpy(Internal.data(), array, bytes_count_to_copy);
}
ByteValue(std::vector<char> &v):Internal(v),Length((uint32_t)v.size()) {}
~ByteValue() override {
Internal.clear();
}
void PrintASCII(std::ostream &os, VL maxlength ) const;
void PrintHex(std::ostream &os, VL maxlength) const;
void PrintGroupLength(std::ostream &os) {
gdcm_assert( Length == 2 );
(void)os;
}
bool IsEmpty() const {
#if 0#else
return Length == 0;
#endif
}
VL GetLength() const override { return Length; }
VL ComputeLength() const { return Length + Length % 2; }
void SetLength(VL vl) override;
operator const std::vector<char>& () const { return Internal; }
ByteValue &operator=(const ByteValue &val) {
Internal = val.Internal;
Length = val.Length;
return *this;
}
bool operator==(const ByteValue &val) const {
if( Length != val.Length )
return false;
if( Internal == val.Internal )
return true;
return false;
}
bool operator==(const Value &val) const override
{
const ByteValue &bv = dynamic_cast<const ByteValue&>(val);
return Length == bv.Length && Internal == bv.Internal;
}
void Append(ByteValue const & bv);
void Clear() override {
Internal.clear();
}
const char *GetPointer() const {
if(!Internal.empty()) return &Internal[0];
return nullptr;
}
const void *GetVoidPointer() const {
if(!Internal.empty()) return &Internal[0];
return nullptr;
}
void *GetVoidPointer() {
if(!Internal.empty()) return &Internal[0];
return nullptr;
}
void Fill(char c) {
std::vector<char>::iterator it = Internal.begin();
for(; it != Internal.end(); ++it) *it = c;
}
bool GetBuffer(char *buffer, unsigned long length) const;
bool WriteBuffer(std::ostream &os) const {
if( Length ) {
gdcm_assert( !(Internal.size() % 2) );
os.write(&Internal[0], Internal.size() );
}
return true;
}
template <typename TSwap, typename TType>
std::istream &Read(std::istream &is, bool readvalues = true) {
if(Length)
{
if( readvalues )
{
is.read(&Internal[0], Length);
gdcm_assert( Internal.size() == Length || Internal.size() == Length + 1 );
TSwap::SwapArray((TType*)GetVoidPointer(), Internal.size() / sizeof(TType) );
}
else
{
is.seekg(Length, std::ios::cur);
}
}
return is;
}
template <typename TSwap>
std::istream &Read(std::istream &is) {
return Read<TSwap,uint8_t>(is);
}
template <typename TSwap, typename TType>
std::ostream const &Write(std::ostream &os) const {
gdcm_assert( !(Internal.size() % 2) );
if( !Internal.empty() ) {
std::vector<char> copy = Internal;
TSwap::SwapArray((TType*)(void*)©[0], Internal.size() / sizeof(TType) );
os.write(©[0], copy.size());
}
return os;
}
template <typename TSwap>
std::ostream const &Write(std::ostream &os) const {
return Write<TSwap,uint8_t>(os);
}
bool IsPrintable(VL length) const {
gdcm_assert( length <= Length );
for(unsigned int i=0; i<length; i++)
{
if ( i == (length-1) && Internal[i] == '\0') continue;
if ( !( isprint((unsigned char)Internal[i]) || isspace((unsigned char)Internal[i]) ) )
{
return false;
}
}
return true;
}
void PrintPNXML(std::ostream &os) const;
void PrintASCIIXML(std::ostream &os) const;
void PrintHexXML(std::ostream &os) const;
protected:
void Print(std::ostream &os) const override {
if( !Internal.empty() )
{
if( IsPrintable(Length) )
{
std::vector<char>::size_type length = Length;
if( Internal.back() == 0 ) --length;
std::copy(Internal.begin(), Internal.begin()+length,
std::ostream_iterator<char>(os));
}
else
os << "Loaded:" << Internal.size();
}
else
{
os << "(no value available)";
}
}
void SetLengthOnly(VL vl) override {
Length = vl;
}
private:
std::vector<char> Internal;
VL Length;
};
}
#endif