changed spaces to tabs

This commit is contained in:
Bob Polis 2020-02-19 18:51:39 +01:00
parent 77ea0fb08e
commit 15f402e5ff
2 changed files with 151 additions and 151 deletions

View File

@ -16,92 +16,92 @@ using namespace sc::io;
// data_streamer --------------------------------------------------------------- // data_streamer ---------------------------------------------------------------
byte_order data_streamer::cur_byte_order() { byte_order data_streamer::cur_byte_order() {
uint8_t ch[2] = {0x12, 0x34}; uint8_t ch[2] = {0x12, 0x34};
uint16_t* p = reinterpret_cast<uint16_t*>(ch); uint16_t* p = reinterpret_cast<uint16_t*>(ch);
return *p == 0x1234 ? byte_order::big_endian : byte_order::little_endian; return *p == 0x1234 ? byte_order::big_endian : byte_order::little_endian;
} }
std::string data_streamer::get4chars(std::istream& file) const { std::string data_streamer::get4chars(std::istream& file) const {
char buf[4]; char buf[4];
file.read(buf, 4); file.read(buf, 4);
return std::string(buf, 4); return std::string(buf, 4);
} }
uint32_t data_streamer::getui24(std::istream& file) const { uint32_t data_streamer::getui24(std::istream& file) const {
char buf[4] = {0}; char buf[4] = {0};
file.read(&buf[1], 3); file.read(&buf[1], 3);
if (cur_byte_order() != _target_byte_order) { if (cur_byte_order() != _target_byte_order) {
char tmp = buf[1]; char tmp = buf[1];
buf[1] = buf[3]; buf[1] = buf[3];
buf[3] = tmp; buf[3] = tmp;
} }
return *reinterpret_cast<uint32_t*>(buf); return *reinterpret_cast<uint32_t*>(buf);
} }
int32_t data_streamer::getsi24(std::istream& file) const { int32_t data_streamer::getsi24(std::istream& file) const {
return static_cast<int32_t>(getui24(file)); return static_cast<int32_t>(getui24(file));
} }
double data_streamer::getf80(std::istream& file) const { double data_streamer::getf80(std::istream& file) const {
float_80 val; float_80 val;
file.read(reinterpret_cast<char*>(val._val), sizeof(val._val)); file.read(reinterpret_cast<char*>(val._val), sizeof(val._val));
if (_target_byte_order != byte_order::big_endian) { if (_target_byte_order != byte_order::big_endian) {
byte_swap(val._val, sizeof(val._val)); byte_swap(val._val, sizeof(val._val));
} }
return val; return val;
} }
void data_streamer::put4chars(const std::string& s, std::ostream& file) const { void data_streamer::put4chars(const std::string& s, std::ostream& file) const {
if (s.size() < 4) { if (s.size() < 4) {
throw std::runtime_error("data_streamer::put4chars: string too short"); throw std::runtime_error("data_streamer::put4chars: string too short");
} }
file.write(s.c_str(), 4); file.write(s.c_str(), 4);
} }
void data_streamer::putui24(uint32_t val, std::ostream& file) const { void data_streamer::putui24(uint32_t val, std::ostream& file) const {
char* buf = reinterpret_cast<char*>(&val); char* buf = reinterpret_cast<char*>(&val);
if (cur_byte_order() != _target_byte_order) { if (cur_byte_order() != _target_byte_order) {
char tmp = buf[1]; char tmp = buf[1];
buf[1] = buf[3]; buf[1] = buf[3];
buf[3] = tmp; buf[3] = tmp;
} }
file.write(&buf[1], 3); file.write(&buf[1], 3);
} }
void data_streamer::putsi24(int32_t val, std::ostream& file) const { void data_streamer::putsi24(int32_t val, std::ostream& file) const {
putui24(static_cast<uint32_t>(val), file); putui24(static_cast<uint32_t>(val), file);
} }
// byte_order_changer ---------------------------------------------------------- // byte_order_changer ----------------------------------------------------------
byte_order_changer::byte_order_changer(data_streamer* ds, byte_order order) : _data_streamer {ds} { byte_order_changer::byte_order_changer(data_streamer* ds, byte_order order) : _data_streamer {ds} {
if (ds == nullptr) { if (ds == nullptr) {
std::cerr << "byte_order_changer: no data streamer given\n"; std::cerr << "byte_order_changer: no data streamer given\n";
return; return;
} }
_saved_byte_order = _data_streamer->target_byte_order(); _saved_byte_order = _data_streamer->target_byte_order();
_data_streamer->target_byte_order(order); _data_streamer->target_byte_order(order);
} }
byte_order_changer::~byte_order_changer() { byte_order_changer::~byte_order_changer() {
if (_data_streamer) { if (_data_streamer) {
_data_streamer->target_byte_order(_saved_byte_order); _data_streamer->target_byte_order(_saved_byte_order);
} }
} }
byte_order_changer::byte_order_changer(byte_order_changer&& other) { byte_order_changer::byte_order_changer(byte_order_changer&& other) {
_data_streamer = other._data_streamer; _data_streamer = other._data_streamer;
_saved_byte_order = other._saved_byte_order; _saved_byte_order = other._saved_byte_order;
other._data_streamer = nullptr; other._data_streamer = nullptr;
} }
byte_order_changer& byte_order_changer::operator=(byte_order_changer&& other) { byte_order_changer& byte_order_changer::operator=(byte_order_changer&& other) {
if (&other != this) { if (&other != this) {
_data_streamer = other._data_streamer; _data_streamer = other._data_streamer;
_saved_byte_order = other._saved_byte_order; _saved_byte_order = other._saved_byte_order;
other._data_streamer = nullptr; other._data_streamer = nullptr;
} }
return *this; return *this;
} }
// float_80 -------------------------------------------------------------------- // float_80 --------------------------------------------------------------------

View File

@ -38,140 +38,140 @@ namespace sc {
fdoutbuf _buf; fdoutbuf _buf;
}; };
class fdinbuf : public std::streambuf { class fdinbuf : public std::streambuf {
}; };
class fdistream : public std::istream { class fdistream : public std::istream {
}; };
class fdstream : public fdostream, public fdistream { class fdstream : public fdostream, public fdistream {
}; };
class socketstream : public fdstream { class socketstream : public fdstream {
}; };
class meminbuf : public std::streambuf { class meminbuf : public std::streambuf {
}; };
class memistream : public std::istream { class memistream : public std::istream {
}; };
class memoutbuf : public std::streambuf { class memoutbuf : public std::streambuf {
}; };
class memostream : public std::ostream { class memostream : public std::ostream {
}; };
class memstream : public memostream, public memistream { class memstream : public memostream, public memistream {
}; };
struct float_80 { struct float_80 {
unsigned char _val[10]; // big endian unsigned char _val[10]; // big endian
float_80(); float_80();
float_80(double val); float_80(double val);
operator double() const; operator double() const;
}; };
enum class byte_order { enum class byte_order {
undefined, undefined,
little_endian, little_endian,
big_endian big_endian
}; };
class data_streamer { class data_streamer {
public: public:
static byte_order cur_byte_order(); static byte_order cur_byte_order();
static inline void byte_swap(void* val, size_t n) { static inline void byte_swap(void* val, size_t n) {
char* p = reinterpret_cast<char*>(val); char* p = reinterpret_cast<char*>(val);
for (size_t i = 0; i < n / 2; ++i) { for (size_t i = 0; i < n / 2; ++i) {
std::swap(p[i], p[n - 1 - i]); std::swap(p[i], p[n - 1 - i]);
} }
} }
template<class T> template<class T>
inline T get(std::istream& file) const { inline T get(std::istream& file) const {
char buf[sizeof(T)]; char buf[sizeof(T)];
file.read(buf, sizeof(T)); file.read(buf, sizeof(T));
if (cur_byte_order() != _target_byte_order) { if (cur_byte_order() != _target_byte_order) {
byte_swap(buf, sizeof(T)); byte_swap(buf, sizeof(T));
} }
return *reinterpret_cast<T*>(buf); return *reinterpret_cast<T*>(buf);
} }
template<class T> template<class T>
inline void put(T val, std::ostream& file) const { inline void put(T val, std::ostream& file) const {
if (cur_byte_order() != _target_byte_order) { if (cur_byte_order() != _target_byte_order) {
byte_swap(&val, sizeof(T)); byte_swap(&val, sizeof(T));
} }
file.write(reinterpret_cast<char*>(&val), sizeof(T)); file.write(reinterpret_cast<char*>(&val), sizeof(T));
} }
data_streamer() = default; data_streamer() = default;
byte_order target_byte_order() const { return _target_byte_order; } byte_order target_byte_order() const { return _target_byte_order; }
void target_byte_order(byte_order new_byte_order) { _target_byte_order = new_byte_order; } void target_byte_order(byte_order new_byte_order) { _target_byte_order = new_byte_order; }
std::string get4chars(std::istream& file) const; std::string get4chars(std::istream& file) const;
uint8_t getui8(std::istream& file) const { return static_cast<uint8_t>(file.get()); } uint8_t getui8(std::istream& file) const { return static_cast<uint8_t>(file.get()); }
int8_t getsi8(std::istream& file) const { return file.get(); } int8_t getsi8(std::istream& file) const { return file.get(); }
uint16_t getui16(std::istream& file) const { return get<uint16_t>(file); } uint16_t getui16(std::istream& file) const { return get<uint16_t>(file); }
int16_t getsi16(std::istream& file) const { return get<int16_t>(file); } int16_t getsi16(std::istream& file) const { return get<int16_t>(file); }
uint32_t getui24(std::istream& file) const; uint32_t getui24(std::istream& file) const;
int32_t getsi24(std::istream& file) const; int32_t getsi24(std::istream& file) const;
uint32_t getui32(std::istream& file) const { return get<uint32_t>(file); } uint32_t getui32(std::istream& file) const { return get<uint32_t>(file); }
int32_t getsi32(std::istream& file) const { return get<int32_t>(file); } int32_t getsi32(std::istream& file) const { return get<int32_t>(file); }
uint64_t getui64(std::istream& file) const { return get<uint64_t>(file); } uint64_t getui64(std::istream& file) const { return get<uint64_t>(file); }
int64_t getsi64(std::istream& file) const { return get<int64_t>(file); } int64_t getsi64(std::istream& file) const { return get<int64_t>(file); }
float getf32(std::istream& file) const { return get<float>(file); } float getf32(std::istream& file) const { return get<float>(file); }
double getf64(std::istream& file) const { return get<double>(file); } double getf64(std::istream& file) const { return get<double>(file); }
double getf80(std::istream& file) const; double getf80(std::istream& file) const;
void put4chars(const std::string& s, std::ostream& file) const; void put4chars(const std::string& s, std::ostream& file) const;
void putui8(uint8_t val, std::ostream& file) const { file.put(val); } void putui8(uint8_t val, std::ostream& file) const { file.put(val); }
void putsi8(int8_t val, std::ostream& file) const { file.put(val); } void putsi8(int8_t val, std::ostream& file) const { file.put(val); }
void putui16(uint16_t val, std::ostream& file) const { put<uint16_t>(val, file); } void putui16(uint16_t val, std::ostream& file) const { put<uint16_t>(val, file); }
void putsi16(int16_t val, std::ostream& file) const { put<int16_t>(val, file); } void putsi16(int16_t val, std::ostream& file) const { put<int16_t>(val, file); }
void putui24(uint32_t val, std::ostream& file) const; void putui24(uint32_t val, std::ostream& file) const;
void putsi24(int32_t val, std::ostream& file) const; void putsi24(int32_t val, std::ostream& file) const;
void putui32(uint32_t val, std::ostream& file) const { put<uint32_t>(val, file); } void putui32(uint32_t val, std::ostream& file) const { put<uint32_t>(val, file); }
void putsi32(int32_t val, std::ostream& file) const { put<int32_t>(val, file); } void putsi32(int32_t val, std::ostream& file) const { put<int32_t>(val, file); }
void putui64(uint64_t val, std::ostream& file) const { put<uint64_t>(val, file); } void putui64(uint64_t val, std::ostream& file) const { put<uint64_t>(val, file); }
void putsi64(int64_t val, std::ostream& file) const { put<int64_t>(val, file); } void putsi64(int64_t val, std::ostream& file) const { put<int64_t>(val, file); }
void putf32(float val, std::ostream& file) const { put<float>(val, file); } void putf32(float val, std::ostream& file) const { put<float>(val, file); }
void putf64(double val, std::ostream& file) const { put<double>(val, file); } void putf64(double val, std::ostream& file) const { put<double>(val, file); }
void putf80(double val, std::ostream& file) const; void putf80(double val, std::ostream& file) const;
private: private:
byte_order _target_byte_order {cur_byte_order()}; byte_order _target_byte_order {cur_byte_order()};
}; };
class byte_order_changer { class byte_order_changer {
public: public:
byte_order_changer(data_streamer* ds, byte_order order); byte_order_changer(data_streamer* ds, byte_order order);
~byte_order_changer(); ~byte_order_changer();
byte_order_changer(byte_order_changer&& other); byte_order_changer(byte_order_changer&& other);
byte_order_changer& operator=(byte_order_changer&& other); byte_order_changer& operator=(byte_order_changer&& other);
byte_order_changer(const byte_order_changer&) = delete; byte_order_changer(const byte_order_changer&) = delete;
byte_order_changer& operator=(const byte_order_changer&) = delete; byte_order_changer& operator=(const byte_order_changer&) = delete;
private: private:
data_streamer* _data_streamer {nullptr}; data_streamer* _data_streamer {nullptr};
byte_order _saved_byte_order {byte_order::undefined}; byte_order _saved_byte_order {byte_order::undefined};
}; };
} }