From 250e5cbf46721b5926702fc6b2783b3ff4b7c63c Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Sat, 22 Feb 2020 21:18:53 +0100 Subject: [PATCH] implemented fdinbuf and fdistream --- fdistream.cpp | 26 ++++++++++++++++++++++++++ libscio.hpp | 24 +++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/fdistream.cpp b/fdistream.cpp index 55b6035..32848de 100644 --- a/fdistream.cpp +++ b/fdistream.cpp @@ -7,3 +7,29 @@ // #include "libscio.hpp" + +#ifdef _MSC_VER +#include +#else +#include +#endif + +using namespace sc::io; + +std::streambuf::int_type fdinbuf::uflow() { + return _ch; +} + +std::streambuf::int_type fdinbuf::underflow() { + char c; + int num = read(_fd, &c, 1); + if (num <= 0) { + _ch = EOF; + } + _ch = c; + return _ch; +} + +std::streamsize fdinbuf::xsgetn(char* s, std::streamsize n) { + return read(_fd, s, n); +} diff --git a/libscio.hpp b/libscio.hpp index 982ad85..9119401 100644 --- a/libscio.hpp +++ b/libscio.hpp @@ -32,26 +32,44 @@ namespace sc { class fdostream : public std::ostream { public: - fdostream(int fd) : std::ostream(&_buf), _buf(fd) {} + fdostream(int fd) : std::ostream(&_outbuf), _outbuf(fd) {} protected: - fdoutbuf _buf; + fdoutbuf _outbuf; }; class fdinbuf : public std::streambuf { + public: + fdinbuf(int fd) : _fd(fd) {} + protected: + int _fd; + int_type _ch {EOF}; + + int_type uflow() override; + int_type underflow() override; + std::streamsize xsgetn(char* s, std::streamsize n) override; }; class fdistream : public std::istream { + public: + fdistream(int fd) : std::istream(&_inbuf), _inbuf(fd) {} + protected: + fdinbuf _inbuf; }; class fdstream : public fdostream, public fdistream { - + public: + fdstream(int fd) : fdostream(fd), fdistream(fd) {} }; class socketstream : public fdstream { + public: + socketstream(int fd) : fdstream(fd) {} + //void make_server(const std::string& address, int port); + //void connect(const std::string& address, int port); }; struct float_80 {