From 43c2f26de372e37e9747c9140f3c95a41dd5de73 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 25 May 2018 00:33:56 -0400 Subject: [PATCH] Split Scanner into its own files. --- windows/tools/hresultwrap.cpp | 128 ++-------------------------------- windows/tools/scanner.cpp | 117 +++++++++++++++++++++++++++++++ windows/tools/scanner.hpp | 23 ++++++ 3 files changed, 146 insertions(+), 122 deletions(-) create mode 100644 windows/tools/scanner.cpp create mode 100644 windows/tools/scanner.hpp diff --git a/windows/tools/hresultwrap.cpp b/windows/tools/hresultwrap.cpp index 9fa92072..04daad5f 100644 --- a/windows/tools/hresultwrap.cpp +++ b/windows/tools/hresultwrap.cpp @@ -1,125 +1,8 @@ // 21 may 2018 -#ifdef _WIN32 -#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1 -#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT 1 -#include -#include -#define openfunc _open -#define openflags (_O_RDONLY | _O_BINARY) -#define openmode (_S_IREAD) -#define readfunc _read -#define readtype int -#define closefunc _close -#else -#include -#include -#define openfunc open -#define openflags (O_RDONLY) -#define openmode 0644 -#define readfunc read -#define readtype ssize_t -#define closefunc close -#endif #include #include #include -#include - -class Scanner { - int fd; - char *buf; - const char *p; - size_t n; - std::vector *line; - bool eof; - int error; -public: - Scanner(int fd); - ~Scanner(void); - - bool Scan(void); - const char *Bytes(void) const; - size_t Len(void) const; - int Error(void) const; -}; - -#define nbuf 1024 - -Scanner::Scanner(int fd) -{ - this->fd = fd; - this->buf = new char[nbuf]; - this->p = this->buf; - this->n = 0; - this->line = new std::vector; - this->eof = false; - this->error = 0; -} - -Scanner::~Scanner(void) -{ - delete this->line; - delete[] this->buf; -} - -bool Scanner::Scan(void) -{ - readtype n; - - if (this->eof || this->error != 0) - return false; - this->line->clear(); - for (;;) { - if (this->n > 0) { - size_t j; - bool haveline; - - haveline = false; - for (j = 0; j < this->n; j++) - if (this->p[j] == '\n') { - haveline = true; - break; - } - this->line->insert(this->line->end(), this->p, this->p + j); - this->p += j; - this->n -= j; - if (haveline) { - // swallow the \n for the next time through - this->p++; - this->n--; - return true; - } - // otherwise, the buffer was exhausted in the middle of a line, so fall through - } - // need to refill the buffer - n = readfunc(this->fd, this->buf, nbuf * sizeof (char)); - if (n < 0) { - this->error = errno; - return false; - } - if (n == 0) { - this->eof = true; - return false; - } - this->p = this->buf; - this->n = n; - } -} - -const char *Scanner::Bytes(void) const -{ - return this->line->data(); -} - -size_t Scanner::Len(void) const -{ - return this->line->size(); -} - -int Scanner::Error(void) const -{ - return this->error; -} +#include "scanner.hpp" bool generate(const char *line, size_t n, FILE *fout) { @@ -155,15 +38,16 @@ int main(int argc, char *argv[]) FILE *fout = NULL; Scanner *s = NULL; int ret = 1; + int err; if (argc != 3) { fprintf(stderr, "usage: %s infile outfile\n", argv[0]); return 1; } - fin = openfunc(argv[1], openflags, openmode); - if (fin < 0) { - fprintf(stderr, "error opening %s: %s\n", argv[1], strerror(errno)); + err = OpenForScanner(argv[1], &fin); + if (err != 0) { + fprintf(stderr, "error opening %s: %s\n", argv[1], strerror(err)); goto done; } fout = fopen(argv[2], "wb"); @@ -196,6 +80,6 @@ done: if (fout != NULL) fclose(fout); if (fin >= 0) - closefunc(fin); + CloseForScanner(fin); return ret; } diff --git a/windows/tools/scanner.cpp b/windows/tools/scanner.cpp new file mode 100644 index 00000000..3af2ec89 --- /dev/null +++ b/windows/tools/scanner.cpp @@ -0,0 +1,117 @@ +// 21 may 2018 +#ifdef _WIN32 +#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1 +#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT 1 +#include +#include +#define openfunc _open +#define openflags (_O_RDONLY | _O_BINARY) +#define openmode (_S_IREAD) +#define readfunc _read +#define readtype int +#define closefunc _close +#else +#include +#include +#define openfunc open +#define openflags (O_RDONLY) +#define openmode 0644 +#define readfunc read +#define readtype ssize_t +#define closefunc close +#endif +#include +#include "scanner.hpp" + +#define nbuf 1024 + +Scanner::Scanner(int fd) +{ + this->fd = fd; + this->buf = new char[nbuf]; + this->p = this->buf; + this->n = 0; + this->line = new std::vector; + this->eof = false; + this->error = 0; +} + +Scanner::~Scanner(void) +{ + delete this->line; + delete[] this->buf; +} + +bool Scanner::Scan(void) +{ + readtype n; + + if (this->eof || this->error != 0) + return false; + this->line->clear(); + for (;;) { + if (this->n > 0) { + size_t j; + bool haveline; + + haveline = false; + for (j = 0; j < this->n; j++) + if (this->p[j] == '\n') { + haveline = true; + break; + } + this->line->insert(this->line->end(), this->p, this->p + j); + this->p += j; + this->n -= j; + if (haveline) { + // swallow the \n for the next time through + this->p++; + this->n--; + return true; + } + // otherwise, the buffer was exhausted in the middle of a line, so fall through + } + // need to refill the buffer + n = readfunc(this->fd, this->buf, nbuf * sizeof (char)); + if (n < 0) { + this->error = errno; + return false; + } + if (n == 0) { + this->eof = true; + return false; + } + this->p = this->buf; + this->n = n; + } +} + +const char *Scanner::Bytes(void) const +{ + return this->line->data(); +} + +size_t Scanner::Len(void) const +{ + return this->line->size(); +} + +int Scanner::Error(void) const +{ + return this->error; +} + +int OpenForScanner(const char *filename, int *fd) +{ + *fd = openfunc(filename, openflags, openmode); + if (*fd < 0) + return errno; + return 0; +} + +int CloseForScanner(int fd) +{ + if (closefunc(fd) < 0) + return errno; + return 0; +} diff --git a/windows/tools/scanner.hpp b/windows/tools/scanner.hpp new file mode 100644 index 00000000..0c83c3d6 --- /dev/null +++ b/windows/tools/scanner.hpp @@ -0,0 +1,23 @@ +// 21 may 2018 +#include + +class Scanner { + int fd; + char *buf; + const char *p; + size_t n; + std::vector *line; + bool eof; + int error; +public: + Scanner(int fd); + ~Scanner(void); + + bool Scan(void); + const char *Bytes(void) const; + size_t Len(void) const; + int Error(void) const; +}; + +extern int OpenForScanner(const char *filename, int *fd); +extern int CloseForScanner(int fd);