From 261e2fd5e7461bba107033d3a68096327e46e8d1 Mon Sep 17 00:00:00 2001 From: Noel Berry Date: Thu, 14 Jan 2021 12:21:59 -0800 Subject: [PATCH] stream.h cleanup & commenting --- include/blah/streams/stream.h | 50 ++++++----------------------------- src/streams/stream.cpp | 43 +++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 43 deletions(-) diff --git a/include/blah/streams/stream.h b/include/blah/streams/stream.h index c49af69..369dade 100644 --- a/include/blah/streams/stream.h +++ b/include/blah/streams/stream.h @@ -43,45 +43,20 @@ namespace Blah int64_t read(void* buffer, int64_t length) { return read_into(buffer, length); } // reads a string. if length < 0, assumes null-terminated - String read_string(int length = -1) - { - String result; + String read_string(int length = -1); - if (length < 0) - { - char next; - while (read(&next, 1) && next != '\0') - result.append(next); - } - else - { - result.set_length(length); - read_into(result.cstr(), length); - } - - return result; - } - - String read_line() - { - String result; - - char next; - while (read(&next, 1) && next != '\n' && next != '\0') - result.append(next); - - return result; - } + // reads a string until a newline '\n' or null-terminator '\0' is found + String read_line(); // reads a number - template + template::value, T>::type> T read() { return read(Endian::Little); } // reads a number - template + template::value, T>::type> T read(Endian endian) { T result; @@ -92,16 +67,10 @@ namespace Blah } // writes the amount of bytes to the stream from the given buffer, and returns the amount written - int64_t write(const void* buffer, int64_t length) - { - return write_from(buffer, length); - } + int64_t write(const void* buffer, int64_t length); // writes the contents of a string to the stream - int64_t write(const String& string) - { - return write_from(string.begin(), string.length()); - } + int64_t write(const String& string); // writes a number template::value, T>::type> @@ -129,7 +98,4 @@ namespace Blah // writes from the stream from the given buffer, and returns the number of bytes written virtual int64_t write_from(const void* buffer, int64_t length) = 0; }; -} - -#undef BLAH_SWAP_ENDIAN -#undef BLAH_BIG_ENDIAN \ No newline at end of file +} \ No newline at end of file diff --git a/src/streams/stream.cpp b/src/streams/stream.cpp index 682b0a6..b393537 100644 --- a/src/streams/stream.cpp +++ b/src/streams/stream.cpp @@ -26,4 +26,45 @@ int64_t Stream::pipe(Stream& stream, int64_t length) } return result; -} \ No newline at end of file +} + +// reads a string. if length < 0, assumes null-terminated +String Stream::read_string(int length) +{ + String result; + + if (length < 0) + { + char next; + while (read(&next, 1) && next != '\0') + result.append(next); + } + else + { + result.set_length(length); + read_into(result.cstr(), length); + } + + return result; +} + +String Stream::read_line() +{ + String result; + + char next; + while (read(&next, 1) && next != '\n' && next != '\0') + result.append(next); + + return result; +} + +int64_t Stream::write(const void* buffer, int64_t length) +{ + return write_from(buffer, length); +} + +int64_t Stream::write(const String& string) +{ + return write_from(string.begin(), string.length()); +}