181 lines
5.9 KiB
C++
181 lines
5.9 KiB
C++
//
|
|
// libscio.hpp
|
|
// libscio
|
|
//
|
|
// Created by Bob Polis at 2020-02-14
|
|
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
|
//
|
|
|
|
#ifndef _libscio_H_
|
|
#define _libscio_H_
|
|
|
|
#include <iostream>
|
|
#include <streambuf>
|
|
#include <cstdio>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
|
|
namespace sc {
|
|
|
|
namespace io {
|
|
|
|
class fdoutbuf : public std::streambuf {
|
|
public:
|
|
fdoutbuf(int fd): _fd(fd) {}
|
|
|
|
protected:
|
|
int _fd;
|
|
|
|
int_type overflow(int_type c) override;
|
|
std::streamsize xsputn(const char* s, std::streamsize num) override;
|
|
};
|
|
|
|
class fdostream : public std::ostream {
|
|
public:
|
|
fdostream(int fd) : std::ostream(&_buf), _buf(fd) {}
|
|
|
|
protected:
|
|
fdoutbuf _buf;
|
|
};
|
|
|
|
class fdinbuf : public std::streambuf {
|
|
|
|
};
|
|
|
|
class fdistream : public std::istream {
|
|
|
|
};
|
|
|
|
class fdstream : public fdostream, public fdistream {
|
|
|
|
};
|
|
|
|
class socketstream : public fdstream {
|
|
|
|
};
|
|
|
|
class meminbuf : public std::streambuf {
|
|
|
|
};
|
|
|
|
class memistream : public std::istream {
|
|
|
|
};
|
|
|
|
class memoutbuf : public std::streambuf {
|
|
|
|
};
|
|
|
|
class memostream : public std::ostream {
|
|
|
|
};
|
|
|
|
class memstream : public memostream, public memistream {
|
|
|
|
};
|
|
|
|
struct float_80 {
|
|
unsigned char _val[10]; // big endian
|
|
|
|
float_80();
|
|
float_80(double val);
|
|
operator double() const;
|
|
};
|
|
|
|
enum class byte_order {
|
|
undefined,
|
|
little_endian,
|
|
big_endian
|
|
};
|
|
|
|
class data_streamer {
|
|
public:
|
|
static byte_order cur_byte_order();
|
|
static inline void byte_swap(void* val, size_t n) {
|
|
char* p = reinterpret_cast<char*>(val);
|
|
for (size_t i = 0; i < n / 2; ++i) {
|
|
std::swap(p[i], p[n - 1 - i]);
|
|
}
|
|
}
|
|
template<class T>
|
|
inline T get(std::istream& file) const {
|
|
char buf[sizeof(T)];
|
|
file.read(buf, sizeof(T));
|
|
if (cur_byte_order() != _target_byte_order) {
|
|
byte_swap(buf, sizeof(T));
|
|
}
|
|
return *reinterpret_cast<T*>(buf);
|
|
}
|
|
template<class T>
|
|
inline void put(T val, std::ostream& file) const {
|
|
if (cur_byte_order() != _target_byte_order) {
|
|
byte_swap(&val, sizeof(T));
|
|
}
|
|
file.write(reinterpret_cast<char*>(&val), sizeof(T));
|
|
}
|
|
|
|
data_streamer() = default;
|
|
|
|
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; }
|
|
|
|
std::string get4chars(std::istream& file) const;
|
|
|
|
uint8_t getui8(std::istream& file) const { return static_cast<uint8_t>(file.get()); }
|
|
int8_t getsi8(std::istream& file) const { return file.get(); }
|
|
uint16_t getui16(std::istream& file) const { return get<uint16_t>(file); }
|
|
int16_t getsi16(std::istream& file) const { return get<int16_t>(file); }
|
|
uint32_t getui24(std::istream& file) const;
|
|
int32_t getsi24(std::istream& file) const;
|
|
uint32_t getui32(std::istream& file) const { return get<uint32_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); }
|
|
int64_t getsi64(std::istream& file) const { return get<int64_t>(file); }
|
|
|
|
float getf32(std::istream& file) const { return get<float>(file); }
|
|
double getf64(std::istream& file) const { return get<double>(file); }
|
|
double getf80(std::istream& 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 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 putsi16(int16_t val, std::ostream& file) const { put<int16_t>(val, file); }
|
|
void putui24(uint32_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 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 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 putf64(double val, std::ostream& file) const { put<double>(val, file); }
|
|
void putf80(double val, std::ostream& file) const;
|
|
|
|
private:
|
|
byte_order _target_byte_order {cur_byte_order()};
|
|
};
|
|
|
|
class byte_order_changer {
|
|
public:
|
|
byte_order_changer(data_streamer* ds, byte_order order);
|
|
~byte_order_changer();
|
|
|
|
byte_order_changer(byte_order_changer&& other);
|
|
byte_order_changer& operator=(byte_order_changer&& other);
|
|
|
|
byte_order_changer(const byte_order_changer&) = delete;
|
|
byte_order_changer& operator=(const byte_order_changer&) = delete;
|
|
|
|
private:
|
|
data_streamer* _data_streamer {nullptr};
|
|
byte_order _saved_byte_order {byte_order::undefined};
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // _libscio_H_
|