From c6d6914688bdab46e0d330677067832be8a1deba Mon Sep 17 00:00:00 2001 From: krangelov Date: Wed, 15 Sep 2021 08:06:18 +0200 Subject: [PATCH] switch to using FILE * in the reader --- src/runtime/c/pgf/data.h | 1 - src/runtime/c/pgf/pgf.cxx | 20 +++++++++++------ src/runtime/c/pgf/reader.cxx | 42 ++++++++++++++++++------------------ src/runtime/c/pgf/reader.h | 6 ++---- 4 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/runtime/c/pgf/data.h b/src/runtime/c/pgf/data.h index c7e5089c7..054a2bce7 100644 --- a/src/runtime/c/pgf/data.h +++ b/src/runtime/c/pgf/data.h @@ -3,7 +3,6 @@ #include #include -#include #include #include diff --git a/src/runtime/c/pgf/pgf.cxx b/src/runtime/c/pgf/pgf.cxx index 50cabdc23..da1b59674 100644 --- a/src/runtime/c/pgf/pgf.cxx +++ b/src/runtime/c/pgf/pgf.cxx @@ -36,18 +36,19 @@ PgfDB *pgf_read_pgf(const char* fpath, PgfExn* err) { PgfDB *db = NULL; + FILE *in = NULL; PGF_API_BEGIN { db = new PgfDB(NULL, 0, 0); - std::ifstream in(fpath, std::ios::binary); - if (in.fail()) { + in = fopen(fpath, "rb"); + if (!in) { throw pgf_systemerror(errno, fpath); } { DB_scope scope(db, WRITER_SCOPE); - PgfReader rdr(&in, fpath); + PgfReader rdr(in, fpath); ref pgf = rdr.read_pgf(); PgfDB::set_revision(pgf); @@ -58,6 +59,9 @@ PgfDB *pgf_read_pgf(const char* fpath, } PGF_API_END end: + if (in != NULL) + fclose(in); + if (db != NULL) delete db; @@ -70,19 +74,20 @@ PgfDB *pgf_boot_ngf(const char* pgf_path, const char* ngf_path, PgfExn* err) { PgfDB *db = NULL; + FILE *in = NULL; PGF_API_BEGIN { db = new PgfDB(ngf_path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); - std::ifstream in(pgf_path, std::ios::binary); - if (in.fail()) { + in = fopen(pgf_path, "rb"); + if (!in) { throw pgf_systemerror(errno, pgf_path); } { DB_scope scope(db, WRITER_SCOPE); - PgfReader rdr(&in, pgf_path); + PgfReader rdr(in, pgf_path); ref pgf = rdr.read_pgf(); PgfDB::set_revision(pgf); @@ -92,6 +97,9 @@ PgfDB *pgf_boot_ngf(const char* pgf_path, const char* ngf_path, return db; } PGF_API_END + if (in != NULL) + fclose(in); + if (db != NULL) { delete db; remove(ngf_path); diff --git a/src/runtime/c/pgf/reader.cxx b/src/runtime/c/pgf/reader.cxx index 4d57ac280..45ea438d7 100644 --- a/src/runtime/c/pgf/reader.cxx +++ b/src/runtime/c/pgf/reader.cxx @@ -3,7 +3,7 @@ #include #include -PgfReader::PgfReader(std::istream *in, const char *filepath) +PgfReader::PgfReader(FILE *in, const char *filepath) { this->in = in; this->filepath = filepath; @@ -12,11 +12,11 @@ PgfReader::PgfReader(std::istream *in, const char *filepath) uint8_t PgfReader::read_uint8() { uint8_t b; - in->read((char*) &b, sizeof(b)); - if (in->eof()) + size_t n_bytes = fread((char*) &b, sizeof(b), 1, in); + if (feof(in)) throw pgf_error("reached end of file while reading a grammar"); - if (in->fail()) - throw pgf_systemerror(errno, filepath); + if (ferror(in)) + throw pgf_systemerror(ferror(in), filepath); return b; } @@ -24,11 +24,11 @@ uint8_t PgfReader::read_uint8() uint16_t PgfReader::read_u16be() { uint8_t buf[2]; - in->read((char*) &buf, sizeof(buf)); - if (in->eof()) + size_t n_bytes = fread((char*) &buf, sizeof(buf), 1, in); + if (feof(in)) throw pgf_error("reached end of file while reading a grammar"); - if (in->fail()) - throw pgf_systemerror(errno, filepath); + if (ferror(in)) + throw pgf_systemerror(ferror(in), filepath); return (((uint16_t) buf[0]) << 8 | buf[1]); } @@ -36,11 +36,11 @@ uint16_t PgfReader::read_u16be() uint64_t PgfReader::read_u64be() { uint8_t buf[8]; - in->read((char*) &buf, sizeof(buf)); - if (in->eof()) + size_t n_bytes = fread((char*) &buf, sizeof(buf), 1, in); + if (feof(in)) throw pgf_error("reached end of file while reading a grammar"); - if (in->fail()) - throw pgf_systemerror(errno, filepath); + if (ferror(in)) + throw pgf_systemerror(ferror(in), filepath); return (((uint64_t) buf[0]) << 56 | ((uint64_t) buf[1]) << 48 | @@ -92,11 +92,11 @@ object PgfReader::read_name_internal(size_t struct_size) // If reading the extra bytes causes EOF, it is an encoding // error, not a legitimate end of character stream. - in->read(ptext->text, size); - if (in->eof()) + fread(ptext->text, size, 1, in); + if (feof(in)) throw pgf_error("utf8 decoding error"); - if (in->fail()) - throw pgf_systemerror(errno, filepath); + if (ferror(in)) + throw pgf_systemerror(ferror(in), filepath); ptext->text[size] = 0; @@ -128,11 +128,11 @@ object PgfReader::read_text_internal(size_t struct_size) ); // If reading the extra bytes causes EOF, it is an encoding // error, not a legitimate end of character stream. - in->read(p, len); - if (in->eof()) + fread(p, len, 1, in); + if (feof(in)) throw pgf_error("utf8 decoding error"); - if (in->fail()) - throw pgf_systemerror(errno, filepath); + if (ferror(in)) + throw pgf_systemerror(ferror(in), filepath); p += len; } diff --git a/src/runtime/c/pgf/reader.h b/src/runtime/c/pgf/reader.h index de335ff96..b14bc6665 100644 --- a/src/runtime/c/pgf/reader.h +++ b/src/runtime/c/pgf/reader.h @@ -1,8 +1,6 @@ #ifndef READER_H_ #define READER_H_ -#include -#include #include "db.h" // reader for PGF files @@ -10,7 +8,7 @@ class PGF_INTERNAL_DECL PgfReader { public: - PgfReader(std::istream *in, const char *filepath); + PgfReader(FILE *in, const char *filepath); uint8_t read_uint8(); uint16_t read_u16be(); @@ -73,7 +71,7 @@ public: ref read_pgf(); private: - std::istream *in; + FILE *in; const char* filepath; object read_name_internal(size_t struct_size);