BashSpark
Loading...
Searching...
No Matches
fakestream.h
Go to the documentation of this file.
1
46#pragma once
47
48#include <cstdio>
49#include <cstdlib>
50#include <cstring>
51#include <fstream>
52#include <string>
53#include <string_view>
54#include <new>
55
56#ifndef ALWAYS_INLINE
57#if defined(__GNUC__) || defined(__clang__)
58#define ALWAYS_INLINE inline __attribute__((always_inline))
59#else
60#define ALWAYS_INLINE
61#endif
62#endif
63
64namespace bs {
69 template<typename char_t>
71 public:
73 using traits_type = std::char_traits<char_t>;
75 using char_type = traits_type::char_type;
77 using int_type = traits_type::int_type;
78
80 static constexpr auto EOF_VALUE = traits_type::eof();
81
85 ALWAYS_INLINE basic_ifakestream()
86 : m_nPos(0), m_pData(nullptr), m_nSize(0) {
87 }
88
94 ALWAYS_INLINE basic_ifakestream(const char *const pData, const std::size_t nSize)
95 : m_nPos(0), m_pData(pData), m_nSize(nSize) {
96 }
97
102 ALWAYS_INLINE basic_ifakestream(const std::string_view &sText)
103 : m_nPos(0), m_pData(sText.data()), m_nSize(sText.length()) {
104 }
105
106 // Deleted copy and move constructors and assignment operators.
107 basic_ifakestream(const basic_ifakestream &) = delete;
108
110
111 basic_ifakestream &operator=(const basic_ifakestream &) = delete;
112
113 basic_ifakestream &operator=(basic_ifakestream &&) = delete;
114
119 ALWAYS_INLINE int_type get() noexcept {
120 auto nChar = m_nPos < m_nSize ? m_pData[m_nPos] : EOF_VALUE;
121 this->m_nPos += 1;
122 return nChar;
123 }
124
129 ALWAYS_INLINE int_type peek() noexcept {
130 return m_nPos < m_nSize ? m_pData[m_nPos] : EOF_VALUE;
131 }
132
137 ALWAYS_INLINE int_type prev() noexcept {
138 return m_nPos > 0 ? m_nPos <= m_nSize ? m_pData[m_nPos - 1] : EOF_VALUE : EOF_VALUE;
139 }
140
144 ALWAYS_INLINE void put_back() noexcept {
145 if (m_nPos > 0)--m_nPos;
146 }
147
154 ALWAYS_INLINE std::size_t read(char_type *const pBuffer, const std::size_t nCount) noexcept {
155 const std::size_t nRead = m_nPos + nCount > m_nSize
156 ? nCount + m_nPos - m_nSize
157 : nCount;
158 std::memcpy(pBuffer, this->m_pData + this->m_nPos, nRead * sizeof(char_type));
159 this->m_nPos += nRead;
160 return nRead;
161 }
162
167 [[nodiscard]] ALWAYS_INLINE bool eof() const noexcept {
168 return this->m_nPos >= m_nSize;
169 }
170
175 [[nodiscard]] ALWAYS_INLINE std::size_t tell() const noexcept {
176 return this->m_nPos;
177 }
178
183 ALWAYS_INLINE void seek(const std::size_t pos) noexcept {
184 this->m_nPos = pos;
185 }
186
191 [[nodiscard]] ALWAYS_INLINE std::size_t size() const noexcept {
192 return this->m_nSize;
193 }
194
199 [[nodiscard]] ALWAYS_INLINE std::basic_string_view<char_type> view() const noexcept {
200 return {this->m_pData, this->m_nSize};
201 }
202
207 [[nodiscard]] ALWAYS_INLINE std::basic_string_view<char_type> sub_view(
208 const std::size_t nBegin, std::size_t nLength) const noexcept {
209 nLength = nBegin + nLength > m_nSize
210 ? nBegin + nLength - m_nSize
211 : nLength;
212 return {this->m_pData + nBegin, nLength};
213 }
214
219 [[nodiscard]] ALWAYS_INLINE std::basic_string<char_type> str() const {
220 return {this->m_pData, this->m_nSize};
221 }
222
227 [[nodiscard]] ALWAYS_INLINE std::basic_string_view<char_type> remaining_view() const noexcept {
228 if (this->m_nPos >= this->m_nSize)return "";
229 return {this->m_pData + this->m_nPos, this->m_nSize - this->m_nPos};
230 }
231
236 [[nodiscard]] ALWAYS_INLINE std::basic_string<char_type> remaining_str() const {
237 if (this->m_nPos >= this->m_nSize)return "";
238 return {this->m_pData + this->m_nPos, this->m_nSize - this->m_nPos};
239 }
240
241 private:
242 std::size_t m_nPos;
243 const char_type *const m_pData;
244 const std::size_t m_nSize;
245 };
246
257
262 template<typename char_t>
264 public:
266 using traits_type = std::char_traits<char_t>;
268 using char_type = traits_type::char_type;
270 using int_type = traits_type::int_type;
271
273 static constexpr auto DEFAULT_BUFFER_SIZE = 64;
275 static constexpr auto EOF_VALUE = traits_type::eof();
276
280 ALWAYS_INLINE basic_ofakestream()
281 : m_pData(static_cast<char_type *>(
282 std::malloc(DEFAULT_BUFFER_SIZE * sizeof(char_type))
283 )),
284 m_nSize(DEFAULT_BUFFER_SIZE),
285 m_nPos(0) {
286 if (this->m_pData == nullptr) throw std::bad_alloc();
287 }
288
289 // Deleted copy and move constructors and assignment operators.
290 basic_ofakestream(const basic_ofakestream &) = delete;
291
293
294 basic_ofakestream &operator=(const basic_ofakestream &) = delete;
295
296 basic_ofakestream &operator=(basic_ofakestream &&) = delete;
297
301 ALWAYS_INLINE ~basic_ofakestream() {
302 std::free(this->m_pData);
303 }
304
309 ALWAYS_INLINE void put(const char_type cChar) {
310 if (m_nPos + 1 >= m_nSize) realloc(m_nPos + 1);
311 m_pData[m_nPos++] = cChar;
312 }
313
319 ALWAYS_INLINE void write(const char_type *const pData, const std::size_t nLength) {
320 if (m_nPos + nLength >= m_nSize) realloc(m_nPos + nLength);
321 std::memcpy(this->m_pData + this->m_nPos, pData, nLength * sizeof(char_type));
322 this->m_nPos += nLength;
323 }
324
329 [[nodiscard]] ALWAYS_INLINE bool empty() const noexcept {
330 return this->m_nPos == 0;
331 }
332
337 [[nodiscard]] ALWAYS_INLINE std::size_t size() const noexcept {
338 return this->m_nPos;
339 }
340
344 ALWAYS_INLINE void clear() noexcept {
345 this->m_nPos = 0;
346 }
347
352 ALWAYS_INLINE void operator<<(const std::basic_string<char_type> &sString) {
353 const std::size_t nLength = sString.length();
354 if (m_nPos + nLength >= m_nSize) realloc(m_nPos + nLength);
355 std::memcpy(this->m_pData + this->m_nPos, sString.data(), nLength * sizeof(char_type));
356 this->m_nPos += nLength;
357 }
358
363 ALWAYS_INLINE void operator<<(const std::basic_string_view<char_type> &sString) {
364 const std::size_t nLength = sString.length();
365 if (m_nPos + nLength >= m_nSize) realloc(m_nPos + nLength);
366 std::memcpy(this->m_pData + this->m_nPos, sString.data(), nLength * sizeof(char_type));
367 this->m_nPos += nLength;
368 }
369
374 [[nodiscard]] ALWAYS_INLINE std::basic_string_view<char_type> view() const noexcept {
375 return {this->m_pData, m_nPos};
376 }
377
382 [[nodiscard]] ALWAYS_INLINE std::basic_string<char_type> str() const {
383 return {this->m_pData, m_nPos};
384 }
385
390 [[nodiscard]] ALWAYS_INLINE std::basic_string<char_type> str_reset() {
391 std::basic_string<char_type> sResult{this->m_pData, m_nPos};
392 this->m_nPos = 0;
393 return sResult;
394 }
395
403 [[nodiscard]] ALWAYS_INLINE std::basic_string<char_type> sub_str(
404 const std::size_t nBegin, std::size_t nLength) const noexcept {
405 nLength = nBegin + nLength >= m_nSize
406 ? nBegin + nLength - m_nSize
407 : nLength;
408 return {this->m_pData + nBegin, nLength};
409 }
410
411 private:
413 char_type *m_pData;
415 std::size_t m_nSize;
417 std::size_t m_nPos;
418
423 void realloc(const std::size_t nRequestedSize) {
424 // Calculate new size
425 std::size_t nNewSize = m_nSize * 2;
426 while (nNewSize < nRequestedSize) nNewSize *= 2;
427
428 // Malloc new memory
429 const auto pNewData = static_cast<char *>(std::realloc(this->m_pData, nNewSize * sizeof(char_type)));
430 if (pNewData == nullptr) throw std::bad_alloc();
431
432 // Swap to the new pointer
433 this->m_pData = pNewData;
434 this->m_nSize = nNewSize;
435 }
436 };
437
448}
A class for input stream behavior with a character type.
Definition fakestream.h:70
ALWAYS_INLINE std::basic_string< char_type > str() const
Converts the data to a string.
Definition fakestream.h:219
ALWAYS_INLINE int_type prev() noexcept
Retrieves the previous character from the stream. Does not move stream position.
Definition fakestream.h:137
ALWAYS_INLINE basic_ifakestream(const char *const pData, const std::size_t nSize)
Constructs an input stream with data and size.
Definition fakestream.h:94
traits_type::int_type int_type
Type for representing character values.
Definition fakestream.h:77
ALWAYS_INLINE std::basic_string_view< char_type > sub_view(const std::size_t nBegin, std::size_t nLength) const noexcept
Gets a read-only view of the stream data.
Definition fakestream.h:207
static constexpr auto EOF_VALUE
End-of-file marker.
Definition fakestream.h:80
ALWAYS_INLINE basic_ifakestream()
Constructs an empty input stream.
Definition fakestream.h:85
ALWAYS_INLINE std::basic_string_view< char_type > view() const noexcept
Gets a read-only view of the stream data.
Definition fakestream.h:199
ALWAYS_INLINE void put_back() noexcept
Returns the last read character to the stream.
Definition fakestream.h:144
ALWAYS_INLINE int_type get() noexcept
Retrieves the next character character from the stream and moves to the next.
Definition fakestream.h:119
ALWAYS_INLINE std::basic_string_view< char_type > remaining_view() const noexcept
Gets a read-only view of the stream remaining data.
Definition fakestream.h:227
ALWAYS_INLINE std::size_t tell() const noexcept
Gets the current position in the stream.
Definition fakestream.h:175
ALWAYS_INLINE void seek(const std::size_t pos) noexcept
Seeks to a specified position in the stream.
Definition fakestream.h:183
ALWAYS_INLINE std::size_t size() const noexcept
Gets the stream data size.
Definition fakestream.h:191
std::char_traits< char_t > traits_type
Type traits for the character type.
Definition fakestream.h:73
ALWAYS_INLINE int_type peek() noexcept
Retrieves the next character from the stream. Does not increment stream position.
Definition fakestream.h:129
ALWAYS_INLINE std::size_t read(char_type *const pBuffer, const std::size_t nCount) noexcept
Reads a specified number of characters into a buffer.
Definition fakestream.h:154
traits_type::char_type char_type
Fundamental character type.
Definition fakestream.h:75
ALWAYS_INLINE std::basic_string< char_type > remaining_str() const
Converts the remaining data to a string.
Definition fakestream.h:236
ALWAYS_INLINE basic_ifakestream(const std::string_view &sText)
Constructs an input stream with string view.
Definition fakestream.h:102
ALWAYS_INLINE bool eof() const noexcept
Checks if the end of the stream has been reached.
Definition fakestream.h:167
A class for output stream behavior with a character type.
Definition fakestream.h:263
ALWAYS_INLINE void operator<<(const std::basic_string< char_type > &sString)
Writes a string to the stream using the << operator.
Definition fakestream.h:352
ALWAYS_INLINE basic_ofakestream()
Constructs an output stream with a default buffer.
Definition fakestream.h:280
ALWAYS_INLINE std::size_t size() const noexcept
Gets the stream size.
Definition fakestream.h:337
ALWAYS_INLINE void write(const char_type *const pData, const std::size_t nLength)
Writes a block of data to the stream.
Definition fakestream.h:319
traits_type::char_type char_type
Fundamental character type.
Definition fakestream.h:268
ALWAYS_INLINE std::basic_string_view< char_type > view() const noexcept
Gets a read-only view of the current state of the stream.
Definition fakestream.h:374
ALWAYS_INLINE std::basic_string< char_type > sub_str(const std::size_t nBegin, std::size_t nLength) const noexcept
Converts a portion of the current data to a string.
Definition fakestream.h:403
static constexpr auto EOF_VALUE
End-of-file marker.
Definition fakestream.h:275
ALWAYS_INLINE std::basic_string< char_type > str() const
Converts the current data to a string.
Definition fakestream.h:382
std::char_traits< char_t > traits_type
Type traits for the character type.
Definition fakestream.h:266
ALWAYS_INLINE void operator<<(const std::basic_string_view< char_type > &sString)
Writes a string view to the stream using the << operator.
Definition fakestream.h:363
ALWAYS_INLINE ~basic_ofakestream()
Destructor that frees allocated memory.
Definition fakestream.h:301
static constexpr auto DEFAULT_BUFFER_SIZE
Default buffer size.
Definition fakestream.h:273
ALWAYS_INLINE std::basic_string< char_type > str_reset()
Resets the stream and returns the current data as a string.
Definition fakestream.h:390
ALWAYS_INLINE void clear() noexcept
Clears the stream by resetting the position.
Definition fakestream.h:344
ALWAYS_INLINE void put(const char_type cChar)
Writes a single character to the stream.
Definition fakestream.h:309
ALWAYS_INLINE bool empty() const noexcept
Checks if the stream is empty.
Definition fakestream.h:329
traits_type::int_type int_type
Type for representing character values.
Definition fakestream.h:270
BashSpark main namespace.
Definition command.h:39