Add file and string streams

This commit is contained in:
drmortalwombat 2023-07-21 22:07:19 +02:00
parent bd6db60802
commit fbde581475
8 changed files with 223 additions and 108 deletions

21
include/opp/ifstream.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "ifstream.h"
#include <c64/kernalio.h>
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);
}

24
include/opp/ifstream.h Normal file
View File

@ -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

View File

@ -1,4 +1,6 @@
#include "iostream.h"
#include <math.h>
#include <stdio.h>
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<mBFill; i++)
b[i] = mBuffer[i];
free(mBuffer);
mBuffer = b;
}
mBuffer[mBFill++] = ch;
}
string ostringstream::str(void) const
{
return string(mBuffer, mBFill);
}
void ostringstream::str(const string & str)
{
mBFill = str.size();
if (mBFill > 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++];
}
}

View File

@ -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 {

29
include/opp/ofstream.cpp Normal file
View File

@ -0,0 +1,29 @@
#include "ofstream.h"
#include <c64/kernalio.h>
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;
}
}

27
include/opp/ofstream.h Normal file
View File

@ -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

81
include/opp/sstream.cpp Normal file
View File

@ -0,0 +1,81 @@
#include "sstream.h"
#include <stdlib.h>
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<mBFill; i++)
b[i] = mBuffer[i];
free(mBuffer);
mBuffer = b;
}
mBuffer[mBFill++] = ch;
}
string ostringstream::str(void) const
{
return string(mBuffer, mBFill);
}
void ostringstream::str(const string & str)
{
mBFill = str.size();
if (mBFill > 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++];
}
}

39
include/opp/sstream.h Normal file
View File

@ -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