switch to using FILE * in the reader

This commit is contained in:
krangelov
2021-09-15 08:06:18 +02:00
parent 9fe6ee3cce
commit c6d6914688
4 changed files with 37 additions and 32 deletions

View File

@@ -3,7 +3,6 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <iostream>
#include <exception> #include <exception>
#include <stdexcept> #include <stdexcept>

View File

@@ -36,18 +36,19 @@ PgfDB *pgf_read_pgf(const char* fpath,
PgfExn* err) PgfExn* err)
{ {
PgfDB *db = NULL; PgfDB *db = NULL;
FILE *in = NULL;
PGF_API_BEGIN { PGF_API_BEGIN {
db = new PgfDB(NULL, 0, 0); db = new PgfDB(NULL, 0, 0);
std::ifstream in(fpath, std::ios::binary); in = fopen(fpath, "rb");
if (in.fail()) { if (!in) {
throw pgf_systemerror(errno, fpath); throw pgf_systemerror(errno, fpath);
} }
{ {
DB_scope scope(db, WRITER_SCOPE); DB_scope scope(db, WRITER_SCOPE);
PgfReader rdr(&in, fpath); PgfReader rdr(in, fpath);
ref<PgfPGF> pgf = rdr.read_pgf(); ref<PgfPGF> pgf = rdr.read_pgf();
PgfDB::set_revision(pgf); PgfDB::set_revision(pgf);
@@ -58,6 +59,9 @@ PgfDB *pgf_read_pgf(const char* fpath,
} PGF_API_END } PGF_API_END
end: end:
if (in != NULL)
fclose(in);
if (db != NULL) if (db != NULL)
delete db; delete db;
@@ -70,19 +74,20 @@ PgfDB *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
PgfExn* err) PgfExn* err)
{ {
PgfDB *db = NULL; PgfDB *db = NULL;
FILE *in = NULL;
PGF_API_BEGIN { PGF_API_BEGIN {
db = new PgfDB(ngf_path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); db = new PgfDB(ngf_path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
std::ifstream in(pgf_path, std::ios::binary); in = fopen(pgf_path, "rb");
if (in.fail()) { if (!in) {
throw pgf_systemerror(errno, pgf_path); throw pgf_systemerror(errno, pgf_path);
} }
{ {
DB_scope scope(db, WRITER_SCOPE); DB_scope scope(db, WRITER_SCOPE);
PgfReader rdr(&in, pgf_path); PgfReader rdr(in, pgf_path);
ref<PgfPGF> pgf = rdr.read_pgf(); ref<PgfPGF> pgf = rdr.read_pgf();
PgfDB::set_revision(pgf); PgfDB::set_revision(pgf);
@@ -92,6 +97,9 @@ PgfDB *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
return db; return db;
} PGF_API_END } PGF_API_END
if (in != NULL)
fclose(in);
if (db != NULL) { if (db != NULL) {
delete db; delete db;
remove(ngf_path); remove(ngf_path);

View File

@@ -3,7 +3,7 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
PgfReader::PgfReader(std::istream *in, const char *filepath) PgfReader::PgfReader(FILE *in, const char *filepath)
{ {
this->in = in; this->in = in;
this->filepath = filepath; this->filepath = filepath;
@@ -12,11 +12,11 @@ PgfReader::PgfReader(std::istream *in, const char *filepath)
uint8_t PgfReader::read_uint8() uint8_t PgfReader::read_uint8()
{ {
uint8_t b; uint8_t b;
in->read((char*) &b, sizeof(b)); size_t n_bytes = fread((char*) &b, sizeof(b), 1, in);
if (in->eof()) if (feof(in))
throw pgf_error("reached end of file while reading a grammar"); throw pgf_error("reached end of file while reading a grammar");
if (in->fail()) if (ferror(in))
throw pgf_systemerror(errno, filepath); throw pgf_systemerror(ferror(in), filepath);
return b; return b;
} }
@@ -24,11 +24,11 @@ uint8_t PgfReader::read_uint8()
uint16_t PgfReader::read_u16be() uint16_t PgfReader::read_u16be()
{ {
uint8_t buf[2]; uint8_t buf[2];
in->read((char*) &buf, sizeof(buf)); size_t n_bytes = fread((char*) &buf, sizeof(buf), 1, in);
if (in->eof()) if (feof(in))
throw pgf_error("reached end of file while reading a grammar"); throw pgf_error("reached end of file while reading a grammar");
if (in->fail()) if (ferror(in))
throw pgf_systemerror(errno, filepath); throw pgf_systemerror(ferror(in), filepath);
return (((uint16_t) buf[0]) << 8 | buf[1]); return (((uint16_t) buf[0]) << 8 | buf[1]);
} }
@@ -36,11 +36,11 @@ uint16_t PgfReader::read_u16be()
uint64_t PgfReader::read_u64be() uint64_t PgfReader::read_u64be()
{ {
uint8_t buf[8]; uint8_t buf[8];
in->read((char*) &buf, sizeof(buf)); size_t n_bytes = fread((char*) &buf, sizeof(buf), 1, in);
if (in->eof()) if (feof(in))
throw pgf_error("reached end of file while reading a grammar"); throw pgf_error("reached end of file while reading a grammar");
if (in->fail()) if (ferror(in))
throw pgf_systemerror(errno, filepath); throw pgf_systemerror(ferror(in), filepath);
return (((uint64_t) buf[0]) << 56 | return (((uint64_t) buf[0]) << 56 |
((uint64_t) buf[1]) << 48 | ((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 // If reading the extra bytes causes EOF, it is an encoding
// error, not a legitimate end of character stream. // error, not a legitimate end of character stream.
in->read(ptext->text, size); fread(ptext->text, size, 1, in);
if (in->eof()) if (feof(in))
throw pgf_error("utf8 decoding error"); throw pgf_error("utf8 decoding error");
if (in->fail()) if (ferror(in))
throw pgf_systemerror(errno, filepath); throw pgf_systemerror(ferror(in), filepath);
ptext->text[size] = 0; 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 // If reading the extra bytes causes EOF, it is an encoding
// error, not a legitimate end of character stream. // error, not a legitimate end of character stream.
in->read(p, len); fread(p, len, 1, in);
if (in->eof()) if (feof(in))
throw pgf_error("utf8 decoding error"); throw pgf_error("utf8 decoding error");
if (in->fail()) if (ferror(in))
throw pgf_systemerror(errno, filepath); throw pgf_systemerror(ferror(in), filepath);
p += len; p += len;
} }

View File

@@ -1,8 +1,6 @@
#ifndef READER_H_ #ifndef READER_H_
#define READER_H_ #define READER_H_
#include <fstream>
#include <stdint.h>
#include "db.h" #include "db.h"
// reader for PGF files // reader for PGF files
@@ -10,7 +8,7 @@
class PGF_INTERNAL_DECL PgfReader class PGF_INTERNAL_DECL PgfReader
{ {
public: public:
PgfReader(std::istream *in, const char *filepath); PgfReader(FILE *in, const char *filepath);
uint8_t read_uint8(); uint8_t read_uint8();
uint16_t read_u16be(); uint16_t read_u16be();
@@ -73,7 +71,7 @@ public:
ref<PgfPGF> read_pgf(); ref<PgfPGF> read_pgf();
private: private:
std::istream *in; FILE *in;
const char* filepath; const char* filepath;
object read_name_internal(size_t struct_size); object read_name_internal(size_t struct_size);