From fbde581475984bcec8844c97c9125517ee694e7e Mon Sep 17 00:00:00 2001 From: drmortalwombat <90205530+drmortalwombat@users.noreply.github.com> Date: Fri, 21 Jul 2023 22:07:19 +0200 Subject: [PATCH] Add file and string streams --- include/opp/ifstream.cpp | 21 +++++++++++ include/opp/ifstream.h | 24 ++++++++++++ include/opp/iostream.cpp | 80 +-------------------------------------- include/opp/iostream.h | 30 --------------- include/opp/ofstream.cpp | 29 ++++++++++++++ include/opp/ofstream.h | 27 ++++++++++++++ include/opp/sstream.cpp | 81 ++++++++++++++++++++++++++++++++++++++++ include/opp/sstream.h | 39 +++++++++++++++++++ 8 files changed, 223 insertions(+), 108 deletions(-) create mode 100644 include/opp/ifstream.cpp create mode 100644 include/opp/ifstream.h create mode 100644 include/opp/ofstream.cpp create mode 100644 include/opp/ofstream.h create mode 100644 include/opp/sstream.cpp create mode 100644 include/opp/sstream.h diff --git a/include/opp/ifstream.cpp b/include/opp/ifstream.cpp new file mode 100644 index 0000000..5807711 --- /dev/null +++ b/include/opp/ifstream.cpp @@ -0,0 +1,21 @@ +#include "ifstream.h" +#include + +ifstream::ifstream(char fnum, char device, char channel, const string & name) +{ + this->fnum = fnum; + krnio_setnam(name); + krnio_open(fnum, device, channel); +} + +ifstream::~ifstream(void) +{ + krnio_close(fnum); +} + +void ifstream::refill(void) +{ + mBufferPos = 0; + mBufferFill = krnio_read(fnum, mBuffer, 32); +} + diff --git a/include/opp/ifstream.h b/include/opp/ifstream.h new file mode 100644 index 0000000..42d59cc --- /dev/null +++ b/include/opp/ifstream.h @@ -0,0 +1,24 @@ +#ifndef OPP_IFSTREAM_H +#define OPP_IFSTREAM_H + +#include "iostream.h" +#include "string.h" + + +class ifstream : public istream +{ +public: + ifstream(char fnum, char device, char channel, const string & name); + ~ifstream(void); + +protected: + virtual void refill(void); + + char fnum; +}; + + + +#pragma compile("ifstream.cpp") + +#endif \ No newline at end of file diff --git a/include/opp/iostream.cpp b/include/opp/iostream.cpp index 88cc988..01ad8ef 100644 --- a/include/opp/iostream.cpp +++ b/include/opp/iostream.cpp @@ -1,4 +1,6 @@ #include "iostream.h" +#include +#include ios::ios(void) : mFlags(0), mState(0), mWidth(0), mPrecision(6), mFill(' ') @@ -966,81 +968,3 @@ void cistream::refill(void) } } -ostringstream::ostringstream(void) -{ - mBuffer = nullptr; - mBFill = mBSize = 0; -} - -ostringstream::~ostringstream(void) -{ - free(mBuffer); -} - -void ostringstream::bput(char ch) -{ - if (!mBuffer) - { - mBSize = 16; - mBFill = 0; - mBuffer = malloc(16); - } - else if (mBFill == mBSize) - { - mBSize *= 2; - char * b = malloc(mBSize); - for(char i=0; i mBSize) - { - free(mBuffer); - mBSize = mBFill; - mBuffer = malloc(mBSize); - } - str.copyseg(mBuffer, 0, mBFill); -} - -istringstream::istringstream(const string & str) - : mString(str), mSPos(0) -{} - -istringstream::~istringstream(void) -{} - -string istringstream::str(void) const -{ - return mString; -} - -void istringstream::str(const string & str) -{ - mString = str; - mSPos = 0; -} - - -void istringstream::refill(void) -{ - mBufferFill = 0; - mBufferPos = 0; - - char ch; - while (mSPos < mString.size() && mBufferFill < 32) - { - mBuffer[mBufferFill++] = mString[mSPos++]; - } -} diff --git a/include/opp/iostream.h b/include/opp/iostream.h index b2cb0eb..00577d8 100644 --- a/include/opp/iostream.h +++ b/include/opp/iostream.h @@ -162,36 +162,6 @@ protected: void refill(void); }; -class ostringstream : public ostream -{ -public: - ostringstream(void); - ~ostringstream(void); - - string str(void) const; - void str(const string & str); -protected: - void bput(char ch); - - char * mBuffer; - char mBFill, mBSize; -}; - -class istringstream : public istream -{ -public: - istringstream(const string & str); - ~istringstream(void); - - string str(void) const; - void str(const string & str); -protected: - virtual void refill(void); - - string mString; - char mSPos; -}; - ostream & endl(ostream & os); struct iosetf { diff --git a/include/opp/ofstream.cpp b/include/opp/ofstream.cpp new file mode 100644 index 0000000..a447870 --- /dev/null +++ b/include/opp/ofstream.cpp @@ -0,0 +1,29 @@ +#include "ofstream.h" +#include + +ofstream::ofstream(char fnum, char device, char channel, const string & name) +{ + this->fnum = fnum; + krnio_setnam(name.tocstr()); + krnio_open(fnum, device, channel); + + mBufferFill = 0; +} + +ofstream::~ofstream(void) +{ + if (mBufferFill > 0) + krnio_write(fnum, mBuffer, mBufferFill); + krnio_close(fnum); +} + +void ofstream::bput(char ch) +{ + mBuffer[mBufferFill++] = ch; + if (mBufferFill == 32) + { + krnio_write(fnum, mBuffer, mBufferFill); + mBufferFill = 0; + } +} + diff --git a/include/opp/ofstream.h b/include/opp/ofstream.h new file mode 100644 index 0000000..65b47d9 --- /dev/null +++ b/include/opp/ofstream.h @@ -0,0 +1,27 @@ +#ifndef OPP_OFSTREAM_H +#define OPP_OFSTREAM_H + +#include "iostream.h" +#include "string.h" + + +class ofstream : public ostream +{ +public: + ofstream(char fnum, char device, char channel, const string & name); + ~ofstream(void); + +protected: + virtual void bput(char ch); + + char mBuffer[32]; + char mBufferFill; + + char fnum; +}; + + + +#pragma compile("ofstream.cpp") + +#endif diff --git a/include/opp/sstream.cpp b/include/opp/sstream.cpp new file mode 100644 index 0000000..ddf06ab --- /dev/null +++ b/include/opp/sstream.cpp @@ -0,0 +1,81 @@ +#include "sstream.h" +#include + +ostringstream::ostringstream(void) +{ + mBuffer = nullptr; + mBFill = mBSize = 0; +} + +ostringstream::~ostringstream(void) +{ + free(mBuffer); +} + +void ostringstream::bput(char ch) +{ + if (!mBuffer) + { + mBSize = 16; + mBFill = 0; + mBuffer = malloc(16); + } + else if (mBFill == mBSize) + { + mBSize *= 2; + char * b = malloc(mBSize); + for(char i=0; i mBSize) + { + free(mBuffer); + mBSize = mBFill; + mBuffer = malloc(mBSize); + } + str.copyseg(mBuffer, 0, mBFill); +} + +istringstream::istringstream(const string & str) + : mString(str), mSPos(0) +{} + +istringstream::~istringstream(void) +{} + +string istringstream::str(void) const +{ + return mString; +} + +void istringstream::str(const string & str) +{ + mString = str; + mSPos = 0; +} + + +void istringstream::refill(void) +{ + mBufferFill = 0; + mBufferPos = 0; + + char ch; + while (mSPos < mString.size() && mBufferFill < 32) + { + mBuffer[mBufferFill++] = mString[mSPos++]; + } +} diff --git a/include/opp/sstream.h b/include/opp/sstream.h new file mode 100644 index 0000000..686f65a --- /dev/null +++ b/include/opp/sstream.h @@ -0,0 +1,39 @@ +#ifndef OPP_SSTREAM_H +#define OPP_SSTREAM_H + +#include "iostream.h" + +class ostringstream : public ostream +{ +public: + ostringstream(void); + ~ostringstream(void); + + string str(void) const; + void str(const string & str); +protected: + void bput(char ch); + + char * mBuffer; + char mBFill, mBSize; +}; + +class istringstream : public istream +{ +public: + istringstream(const string & str); + ~istringstream(void); + + string str(void) const; + void str(const string & str); +protected: + virtual void refill(void); + + string mString; + char mSPos; +}; + + +#pragma compile("sstream.cpp") + +#endif