Updated hresultwrap to use lib.cpp, fixing errors along the way.

This commit is contained in:
Pietro Gagliardi 2018-05-26 16:56:53 -04:00
parent a6634f775b
commit a07727515f
4 changed files with 34 additions and 32 deletions

View File

@ -1,8 +1,7 @@
// 21 may 2018
#include <vector>
#include <stdio.h>
#include <string.h>
#include "scanner.hpp"
#include "lib.hpp"
bool generate(const char *line, size_t n, FILE *fout)
{
@ -34,20 +33,20 @@ bool process(const char *line, size_t n, FILE *fout)
int main(int argc, char *argv[])
{
int fin = -1;
ReadCloser *fin = NULL;
FILE *fout = NULL;
Scanner *s = NULL;
int ret = 1;
int err;
Error *err = NULL;
if (argc != 3) {
fprintf(stderr, "usage: %s infile outfile\n", argv[0]);
return 1;
}
err = OpenForScanner(argv[1], &fin);
if (err != 0) {
fprintf(stderr, "error opening %s: %s\n", argv[1], strerror(err));
err = OpenRead(argv[1], &fin);
if (err != NULL) {
fprintf(stderr, "error opening %s: %s\n", argv[1], err->String());
goto done;
}
fout = fopen(argv[2], "wb");
@ -68,8 +67,8 @@ int main(int argc, char *argv[])
goto done;
}
}
if (s->Error() != 0) {
fprintf(stderr, "error reading from %s: %s\n", argv[1], strerror(s->Error()));
if (s->Err() != 0) {
fprintf(stderr, "error reading from %s: %s\n", argv[1], s->Err()->String());
goto done;
}
@ -79,7 +78,9 @@ done:
delete s;
if (fout != NULL)
fclose(fout);
if (fin >= 0)
CloseForScanner(fin);
if (fin != NULL)
delete fin;
if (err != NULL)
delete err;
return ret;
}

View File

@ -22,12 +22,12 @@ public:
virtual const char *String(void) const;
};
virtual ~eofError::eofError(void)
eofError::~eofError(void)
{
// do nothing
}
virtual const char *eofError::String(void) const
const char *eofError::String(void) const
{
return "EOF";
}
@ -39,12 +39,12 @@ public:
virtual const char *String(void) const;
};
virtual ~shortWriteError::shortWriteError(void)
shortWriteError::~shortWriteError(void)
{
// do nothing
}
virtual const char *shortWriteError::String(void) const
const char *shortWriteError::String(void) const
{
return "short write";
}
@ -61,7 +61,8 @@ Error *NewErrShortWrite(void)
bool IsEOF(Error *e)
{
return typeid (e) == typeid (eofError *);
// typeid does not work directly with pointers, alas (see https://stackoverflow.com/questions/4202877/typeid-for-polymorphic-types)
return typeid (*e) == typeid (eofError);
}
#define nbuf 1024
@ -73,7 +74,7 @@ Scanner::Scanner(ReadCloser *r)
this->p = this->buf;
this->n = 0;
this->line = new std::vector<char>;
this->error = NULL;
this->err = NULL;
}
Scanner::~Scanner(void)
@ -86,7 +87,7 @@ Scanner::~Scanner(void)
bool Scanner::Scan(void)
{
readtype n;
size_t n;
if (this->err != NULL)
return false;
@ -132,7 +133,7 @@ size_t Scanner::Len(void) const
return this->line->size();
}
int Scanner::Err(void) const
Error *Scanner::Err(void) const
{
if (!IsEOF(this->err))
return this->err;

View File

@ -3,7 +3,7 @@
class Error {
public:
virtual ~Error(void);
virtual ~Error(void) = default;
virtual const char *String(void) const = 0;
};
@ -14,14 +14,14 @@ extern bool IsEOF(Error *e);
class ReadCloser {
public:
virtual ~ReadCloser(void);
virtual ~ReadCloser(void) = default;
virtual Error *Read(void *buf, size_t n, size_t *actual) = 0;
};
class WriteCloser {
public:
virtual ~WriteCloser(void);
virtual ~WriteCloser(void) = default;
virtual Error *Write(void *buf, size_t n) = 0;
};

View File

@ -19,12 +19,12 @@ posixError::posixError(int error)
this->error = error;
}
virtual posixError::~posixError(void)
posixError::~posixError(void)
{
// do nothing
}
virtual const char *posixError::String(void) const
const char *posixError::String(void) const
{
return strerror(this->error);
}
@ -43,12 +43,12 @@ posixReadCloser::posixReadCloser(int fd)
this->fd = fd;
}
virtual posixReadCloser::~posixReadCloser(void)
posixReadCloser::~posixReadCloser(void)
{
close(this->fd);
}
virtual Error *posixReadCloser::Read(void *buf, size_t n, size_t *actual)
Error *posixReadCloser::Read(void *buf, size_t n, size_t *actual)
{
ssize_t ret;
@ -57,7 +57,7 @@ virtual Error *posixReadCloser::Read(void *buf, size_t n, size_t *actual)
if (ret < 0)
return new posixError(errno);
if (ret == 0)
return NewEOF(void);
return NewEOF();
*actual = ret;
return NULL;
}
@ -68,7 +68,7 @@ public:
posixWriteCloser(int fd);
virtual ~posixWriteCloser(void);
virtual Error *Write(void *buf, size_t n, size_t *actual);
virtual Error *Write(void *buf, size_t n);
};
posixWriteCloser::posixWriteCloser(int fd)
@ -76,20 +76,20 @@ posixWriteCloser::posixWriteCloser(int fd)
this->fd = fd;
}
virtual posixWriteCloser::~posixWriteCloser(void)
posixWriteCloser::~posixWriteCloser(void)
{
close(this->fd);
}
virtual Error *posixWriteCloser::Write(void *buf, size_t n)
Error *posixWriteCloser::Write(void *buf, size_t n)
{
ssize_t ret;
ret = write(this->fd, buf, n);
if (ret < 0)
return new posixError(errno);
if (ret != n)
return NewErrShortWrite(void);
if (((size_t) ret) != n)
return NewErrShortWrite();
return NULL;
}