write strings
parent
dad09f5f1b
commit
29ffdaaacd
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required(VERSION 3.20)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
include(cmake/color.cmake)
|
include(cmake/color.cmake)
|
||||||
set(BLT_VERSION 5.1.13)
|
set(BLT_VERSION 5.1.14)
|
||||||
|
|
||||||
set(BLT_TARGET BLT)
|
set(BLT_TARGET BLT)
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
#define BLT_FILESYSTEM_H
|
#define BLT_FILESYSTEM_H
|
||||||
|
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
|
#include <sstream>
|
||||||
#include <blt/fs/fwddecl.h>
|
#include <blt/fs/fwddecl.h>
|
||||||
|
|
||||||
namespace blt::fs
|
namespace blt::fs
|
||||||
|
@ -37,6 +38,7 @@ namespace blt::fs
|
||||||
fstream_reader_t& operator=(const fstream_reader_t& copy) = delete;
|
fstream_reader_t& operator=(const fstream_reader_t& copy) = delete;
|
||||||
|
|
||||||
i64 read(char* buffer, size_t bytes) override;
|
i64 read(char* buffer, size_t bytes) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::istream* m_stream;
|
std::istream* m_stream;
|
||||||
};
|
};
|
||||||
|
@ -50,7 +52,7 @@ namespace blt::fs
|
||||||
|
|
||||||
fstream_writer_t& operator=(const fstream_writer_t& copy) = delete;
|
fstream_writer_t& operator=(const fstream_writer_t& copy) = delete;
|
||||||
|
|
||||||
i64 write(char* buffer, size_t bytes) override;
|
i64 write(const char* buffer, size_t bytes) override;
|
||||||
|
|
||||||
void flush() override;
|
void flush() override;
|
||||||
|
|
||||||
|
@ -62,6 +64,79 @@ namespace blt::fs
|
||||||
private:
|
private:
|
||||||
std::ostream* m_stream;
|
std::ostream* m_stream;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class reader_wrapper_t final
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit reader_wrapper_t(reader_t& reader): m_reader(&reader)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void read(T& out)
|
||||||
|
{
|
||||||
|
if (!m_reader->read(reinterpret_cast<char*>(&out), sizeof(T)))
|
||||||
|
throw std::runtime_error("Failed to read from reader");
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
friend reader_wrapper_t& operator>>(reader_wrapper_t& reader, T& t)
|
||||||
|
{
|
||||||
|
reader.read(t);
|
||||||
|
return reader;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
reader_t* m_reader;
|
||||||
|
};
|
||||||
|
|
||||||
|
class writer_wrapper_t final
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit writer_wrapper_t(writer_t& writer): m_writer(&writer)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void write(const T& t)
|
||||||
|
{
|
||||||
|
m_writer->write(reinterpret_cast<char*>(&t), sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
friend writer_wrapper_t& operator<<(writer_wrapper_t& writer, const T& t)
|
||||||
|
{
|
||||||
|
writer.write(t);
|
||||||
|
return writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
writer_t* m_writer;
|
||||||
|
};
|
||||||
|
|
||||||
|
class writer_string_wrapper_t final
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit writer_string_wrapper_t(writer_t& writer): m_writer(&writer)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void write(const T& t)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << t;
|
||||||
|
const auto str = ss.str();
|
||||||
|
m_writer->write(str.data(), str.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
friend writer_string_wrapper_t& operator<<(writer_string_wrapper_t& writer, const T& t)
|
||||||
|
{
|
||||||
|
writer.write(t);
|
||||||
|
return writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
writer_t* m_writer;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //BLT_FILESYSTEM_H
|
#endif //BLT_FILESYSTEM_H
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#ifndef BLT_FS_FWDDECL_H
|
#ifndef BLT_FS_FWDDECL_H
|
||||||
#define BLT_FS_FWDDECL_H
|
#define BLT_FS_FWDDECL_H
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
#include <blt/std/types.h>
|
#include <blt/std/types.h>
|
||||||
|
|
||||||
namespace blt::fs
|
namespace blt::fs
|
||||||
|
@ -64,7 +65,7 @@ namespace blt::fs
|
||||||
* @param bytes number of bytes to write
|
* @param bytes number of bytes to write
|
||||||
* @return number of bytes, or negative value if error. Zero is also a valid return, not indicating error in itself but can be the result of one.
|
* @return number of bytes, or negative value if error. Zero is also a valid return, not indicating error in itself but can be the result of one.
|
||||||
*/
|
*/
|
||||||
virtual i64 write(char* buffer, size_t bytes) = 0;
|
virtual i64 write(const char* buffer, size_t bytes) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optional flush command which syncs the underlying objects
|
* Optional flush command which syncs the underlying objects
|
||||||
|
|
|
@ -32,7 +32,7 @@ namespace blt::fs
|
||||||
fstream_writer_t::fstream_writer_t(std::ostream& stream): m_stream{&stream}
|
fstream_writer_t::fstream_writer_t(std::ostream& stream): m_stream{&stream}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
i64 fstream_writer_t::write(char* buffer, size_t bytes)
|
i64 fstream_writer_t::write(const char* buffer, const size_t bytes)
|
||||||
{
|
{
|
||||||
m_stream->write(buffer, static_cast<std::streamsize>(bytes));
|
m_stream->write(buffer, static_cast<std::streamsize>(bytes));
|
||||||
return static_cast<i64>(bytes);
|
return static_cast<i64>(bytes);
|
||||||
|
|
|
@ -15,15 +15,16 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
#include <chrono>
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <thread>
|
||||||
|
#include <blt/fs/filesystem.h>
|
||||||
#include <blt/logging/ansi.h>
|
#include <blt/logging/ansi.h>
|
||||||
#include <blt/logging/logging.h>
|
#include <blt/logging/logging.h>
|
||||||
#include <blt/std/assert.h>
|
#include <blt/std/assert.h>
|
||||||
#include <blt/std/utility.h>
|
#include <blt/std/utility.h>
|
||||||
#include <thread>
|
|
||||||
#include <chrono>
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
struct some_silly_type_t
|
struct some_silly_type_t
|
||||||
{};
|
{};
|
||||||
|
@ -89,43 +90,6 @@ std::pair<bool, std::string> compare_strings(const std::string& s1, const std::s
|
||||||
return {true, ""};
|
return {true, ""};
|
||||||
}
|
}
|
||||||
|
|
||||||
struct seqbuf final : std::streambuf
|
|
||||||
{
|
|
||||||
seqbuf(std::streambuf* destBuf, std::size_t& charCount) : m_dest(destBuf), m_count(charCount)
|
|
||||||
{}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
int_type overflow(int_type ch) override
|
|
||||||
{
|
|
||||||
if (traits_type::eq_int_type(ch, traits_type::eof()))
|
|
||||||
return traits_type::eof();
|
|
||||||
++m_count;
|
|
||||||
blt::logging::println("We are printing a character {:c}", ch);
|
|
||||||
return m_dest->sputc(static_cast<char>(ch));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::streamsize xsputn(const char* s, std::streamsize count) override
|
|
||||||
{
|
|
||||||
blt::logging::println("We are printing a series of characters {} {}", count, std::string_view{s, static_cast<size_t>(count)});
|
|
||||||
m_count += static_cast<std::size_t>(count);
|
|
||||||
return m_dest->sputn(s, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
int_type underflow() override
|
|
||||||
{
|
|
||||||
return m_dest->sgetc();
|
|
||||||
}
|
|
||||||
|
|
||||||
int sync() override
|
|
||||||
{
|
|
||||||
return m_dest->pubsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::streambuf* m_dest;
|
|
||||||
std::size_t& m_count;
|
|
||||||
};
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
@ -195,12 +159,17 @@ int main()
|
||||||
|
|
||||||
|
|
||||||
std::ofstream os("test.txt");
|
std::ofstream os("test.txt");
|
||||||
|
blt::fs::fstream_writer_t wtr(os);
|
||||||
|
blt::fs::writer_wrapper_t writer(wtr);
|
||||||
|
|
||||||
|
writer.write("This is a println with a stream\n");
|
||||||
|
writer.write("This is a mixed print");
|
||||||
|
writer.write(std::to_string(25));
|
||||||
|
writer.write(" with multiple types ");
|
||||||
|
writer.write(std::to_string(34.23340));
|
||||||
|
writer.write('\n');
|
||||||
|
writer.write("What about just a new line character?\n");
|
||||||
size_t charCount = 0;
|
size_t charCount = 0;
|
||||||
seqbuf hi{os.rdbuf(), charCount};
|
|
||||||
dynamic_cast<std::ostream&>(os).rdbuf(&hi);
|
|
||||||
os << "This is a println with a stream" << std::endl;
|
|
||||||
os << "This is a mixed print " << 25 << " with multiple types " << 34.23340 << std::endl;
|
|
||||||
os << "What about just a new line character?\n";
|
|
||||||
|
|
||||||
blt::logging::println("Logged {} characters", charCount);
|
blt::logging::println("Logged {} characters", charCount);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue