mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
switch to using FILE * in the reader
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
@@ -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<PgfPGF> 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<PgfPGF> 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);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#ifndef READER_H_
|
||||
#define READER_H_
|
||||
|
||||
#include <fstream>
|
||||
#include <stdint.h>
|
||||
#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<PgfPGF> read_pgf();
|
||||
|
||||
private:
|
||||
std::istream *in;
|
||||
FILE *in;
|
||||
const char* filepath;
|
||||
|
||||
object read_name_internal(size_t struct_size);
|
||||
|
||||
Reference in New Issue
Block a user