CommonLibSSE (Parapets fork)
NiBinaryStream.h
Go to the documentation of this file.
1 #pragma once
2 
3 namespace RE
4 {
6  {
7  public:
8  inline static constexpr auto RTTI = RTTI_NiBinaryStream;
9 
10  using int_type = std::int32_t;
11  using pos_type = std::uint32_t;
12  using off_type = std::int32_t;
13 
14  struct BufferInfo
15  {
16  public:
17  // members
18  void* buffer; // 00
19  std::uint32_t totalSize; // 08
20  std::uint32_t bufferAllocSize; // 0C
21  std::uint32_t bufferReadSize; // 10
22  std::uint32_t bufferPos; // 14
23  std::uint32_t streamPos; // 18
24  std::uint32_t pad1C; // 1C
25  };
26  static_assert(sizeof(BufferInfo) == 0x20);
27 
29  virtual ~NiBinaryStream(); // 00
30 
31  // add
32  [[nodiscard]] virtual bool good() const = 0; // 01
33  virtual void seek(std::int32_t a_numBytes) = 0; // 02
34  [[nodiscard]] virtual std::uint32_t tell() const; // 03 - { return _absoluteCurrentPos; }
35  virtual void get_info(BufferInfo& a_buf); // 04
36  virtual void set_endian_swap(bool a_doSwap) = 0; // 05
37 
38  template <class CharT>
39  bool get(CharT& a_ch);
40  template <class CharT>
41  bool read(CharT* a_str, std::uint32_t a_count);
42  template <class CharT>
43  bool put(CharT a_ch);
44  template <class CharT>
45  bool write(const CharT* a_str, std::uint32_t a_count);
46 
47  protected:
48  using ReadFn = std::uint32_t(NiBinaryStream* a_this, void* a_buffer, std::uint32_t a_bytes, std::uint32_t* a_componentSizes, std::uint32_t a_numComponents);
49  using WriteFn = std::uint32_t(NiBinaryStream* a_this, const void* a_buffer, std::uint32_t a_bytes, std::uint32_t* a_componentSizes, std::uint32_t a_numComponents);
50 
51  std::uint32_t binary_read(void* a_buffer, std::uint32_t a_totalBytes, std::uint32_t* a_componentSizes, std::uint32_t a_numComponents = 1);
52  std::uint32_t binary_write(const void* a_buffer, std::uint32_t a_totalBytes, std::uint32_t* a_componentSizes, std::uint32_t a_numComponents = 1);
53 
54  // members
55  std::uint32_t _absoluteCurrentPos; // 08
56  std::uint32_t _pad0C; // 0C
57  ReadFn* _readFn; // 10
58  WriteFn* _writeFn; // 18
59  };
60  static_assert(sizeof(NiBinaryStream) == 0x20);
61 
62  template <class CharT>
63  inline bool NiBinaryStream::get(CharT& a_ch)
64  {
65  return read(&a_ch, 1);
66  }
67 
68  template <class CharT>
69  inline bool NiBinaryStream::read(CharT* a_str, std::uint32_t a_count)
70  {
71  std::uint32_t size = sizeof(CharT);
72  std::uint32_t toRead = a_count * size;
73  std::uint32_t bytesRead = binary_read(a_str, toRead, &size);
74  return bytesRead == toRead;
75  }
76 
77  template <class CharT>
78  inline bool NiBinaryStream::put(CharT a_ch)
79  {
80  return write(&a_ch, 1);
81  }
82 
83  template <class CharT>
84  inline bool NiBinaryStream::write(const CharT* a_str, std::uint32_t a_count)
85  {
86  std::uint32_t size = sizeof(CharT);
87  std::uint32_t toWrite = a_count * size;
88  std::uint32_t bytesWritten = binary_write(a_str, toWrite, &size);
89  return bytesWritten == toWrite;
90  }
91 }
92 
93 namespace std
94 {
95  template <class CharT, class Traits, class Allocator>
96  inline bool getline(RE::NiBinaryStream& a_input, std::basic_string<CharT, Traits, Allocator>& a_str)
97  {
98  CharT delim = std::use_facet<std::ctype<CharT>>(std::locale()).widen('\n');
99  return getline(a_input, a_str, delim);
100  }
101 
102  template <class CharT, class Traits, class Allocator>
103  inline bool getline(RE::NiBinaryStream& a_input, std::basic_string<CharT, Traits, Allocator>& a_str, CharT a_delim)
104  {
105  a_str.erase();
106  CharT c;
107  auto notEOF = a_input.get(c);
108  if (notEOF) {
109  do {
110  if (!std::char_traits<CharT>::eq(c, a_delim)) {
111  a_str.push_back(c);
112  } else {
113  break;
114  }
115 
116  if (a_str.size() != a_str.max_size()) {
117  notEOF = a_input.get(c);
118  } else {
119  break;
120  }
121  } while (notEOF);
122  return true;
123  } else {
124  return false;
125  }
126  }
127 }
Definition: NiBinaryStream.h:6
bool get(CharT &a_ch)
Definition: NiBinaryStream.h:63
std::uint32_t(NiBinaryStream *a_this, void *a_buffer, std::uint32_t a_bytes, std::uint32_t *a_componentSizes, std::uint32_t a_numComponents) ReadFn
Definition: NiBinaryStream.h:48
WriteFn * _writeFn
Definition: NiBinaryStream.h:58
virtual void seek(std::int32_t a_numBytes)=0
std::uint32_t(NiBinaryStream *a_this, const void *a_buffer, std::uint32_t a_bytes, std::uint32_t *a_componentSizes, std::uint32_t a_numComponents) WriteFn
Definition: NiBinaryStream.h:49
virtual std::uint32_t tell() const
virtual void set_endian_swap(bool a_doSwap)=0
static constexpr auto RTTI
Definition: NiBinaryStream.h:8
std::uint32_t _pad0C
Definition: NiBinaryStream.h:56
std::uint32_t binary_write(const void *a_buffer, std::uint32_t a_totalBytes, std::uint32_t *a_componentSizes, std::uint32_t a_numComponents=1)
bool write(const CharT *a_str, std::uint32_t a_count)
Definition: NiBinaryStream.h:84
bool read(CharT *a_str, std::uint32_t a_count)
Definition: NiBinaryStream.h:69
virtual void get_info(BufferInfo &a_buf)
std::int32_t off_type
Definition: NiBinaryStream.h:12
virtual ~NiBinaryStream()
virtual bool good() const =0
ReadFn * _readFn
Definition: NiBinaryStream.h:57
std::uint32_t _absoluteCurrentPos
Definition: NiBinaryStream.h:55
bool put(CharT a_ch)
Definition: NiBinaryStream.h:78
std::int32_t int_type
Definition: NiBinaryStream.h:10
std::uint32_t binary_read(void *a_buffer, std::uint32_t a_totalBytes, std::uint32_t *a_componentSizes, std::uint32_t a_numComponents=1)
std::uint32_t pos_type
Definition: NiBinaryStream.h:11
Definition: AbsorbEffect.h:6
constexpr REL::ID RTTI_NiBinaryStream
Definition: Offsets_RTTI.h:5319
Definition: NiBinaryStream.h:94
bool getline(RE::NiBinaryStream &a_input, std::basic_string< CharT, Traits, Allocator > &a_str)
Definition: NiBinaryStream.h:96
Definition: NiBinaryStream.h:15
std::uint32_t streamPos
Definition: NiBinaryStream.h:23
std::uint32_t bufferPos
Definition: NiBinaryStream.h:22
std::uint32_t bufferAllocSize
Definition: NiBinaryStream.h:20
std::uint32_t totalSize
Definition: NiBinaryStream.h:19
void * buffer
Definition: NiBinaryStream.h:18
std::uint32_t pad1C
Definition: NiBinaryStream.h:24
std::uint32_t bufferReadSize
Definition: NiBinaryStream.h:21