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

View File

@ -22,12 +22,12 @@ public:
virtual const char *String(void) const; virtual const char *String(void) const;
}; };
virtual ~eofError::eofError(void) eofError::~eofError(void)
{ {
// do nothing // do nothing
} }
virtual const char *eofError::String(void) const const char *eofError::String(void) const
{ {
return "EOF"; return "EOF";
} }
@ -39,12 +39,12 @@ public:
virtual const char *String(void) const; virtual const char *String(void) const;
}; };
virtual ~shortWriteError::shortWriteError(void) shortWriteError::~shortWriteError(void)
{ {
// do nothing // do nothing
} }
virtual const char *shortWriteError::String(void) const const char *shortWriteError::String(void) const
{ {
return "short write"; return "short write";
} }
@ -61,7 +61,8 @@ Error *NewErrShortWrite(void)
bool IsEOF(Error *e) 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 #define nbuf 1024
@ -73,7 +74,7 @@ Scanner::Scanner(ReadCloser *r)
this->p = this->buf; this->p = this->buf;
this->n = 0; this->n = 0;
this->line = new std::vector<char>; this->line = new std::vector<char>;
this->error = NULL; this->err = NULL;
} }
Scanner::~Scanner(void) Scanner::~Scanner(void)
@ -86,7 +87,7 @@ Scanner::~Scanner(void)
bool Scanner::Scan(void) bool Scanner::Scan(void)
{ {
readtype n; size_t n;
if (this->err != NULL) if (this->err != NULL)
return false; return false;
@ -132,7 +133,7 @@ size_t Scanner::Len(void) const
return this->line->size(); return this->line->size();
} }
int Scanner::Err(void) const Error *Scanner::Err(void) const
{ {
if (!IsEOF(this->err)) if (!IsEOF(this->err))
return this->err; return this->err;

View File

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

View File

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