errno is not set for FILE I/O so we do our best

This commit is contained in:
krangelov
2021-09-17 10:06:11 +02:00
parent a8bda009a4
commit 3f31d86d0d
5 changed files with 20 additions and 24 deletions

View File

@@ -49,7 +49,7 @@ PgfDB *pgf_read_pgf(const char* fpath,
{ {
DB_scope scope(db, WRITER_SCOPE); DB_scope scope(db, WRITER_SCOPE);
PgfReader rdr(in, fpath); PgfReader rdr(in);
ref<PgfPGF> pgf = rdr.read_pgf(); ref<PgfPGF> pgf = rdr.read_pgf();
PgfDB::set_revision(pgf); PgfDB::set_revision(pgf);
@@ -88,7 +88,7 @@ PgfDB *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
{ {
DB_scope scope(db, WRITER_SCOPE); DB_scope scope(db, WRITER_SCOPE);
PgfReader rdr(in, pgf_path); PgfReader rdr(in);
ref<PgfPGF> pgf = rdr.read_pgf(); ref<PgfPGF> pgf = rdr.read_pgf();
PgfDB::set_revision(pgf); PgfDB::set_revision(pgf);
@@ -200,7 +200,7 @@ void pgf_write_pgf(const char* fpath,
DB_scope scope(db, READER_SCOPE); DB_scope scope(db, READER_SCOPE);
ref<PgfPGF> pgf = PgfDB::revision2pgf(revision); ref<PgfPGF> pgf = PgfDB::revision2pgf(revision);
PgfWriter wtr(out, fpath); PgfWriter wtr(out);
wtr.write_pgf(pgf); wtr.write_pgf(pgf);
} }
} PGF_API_END } PGF_API_END

View File

@@ -3,10 +3,9 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
PgfReader::PgfReader(FILE *in, const char *filepath) PgfReader::PgfReader(FILE *in)
{ {
this->in = in; this->in = in;
this->filepath = filepath;
} }
uint8_t PgfReader::read_uint8() uint8_t PgfReader::read_uint8()
@@ -14,9 +13,9 @@ uint8_t PgfReader::read_uint8()
uint8_t b; uint8_t b;
size_t n_bytes = fread((char*) &b, sizeof(b), 1, in); size_t n_bytes = fread((char*) &b, sizeof(b), 1, in);
if (feof(in)) if (feof(in))
throw pgf_error("reached end of file while reading a grammar"); throw pgf_error("reached end of file while reading the grammar");
if (ferror(in)) if (ferror(in))
throw pgf_systemerror(ferror(in), filepath); throw pgf_error("an error occured while reading the grammar");
return b; return b;
} }
@@ -28,7 +27,7 @@ uint16_t PgfReader::read_u16be()
if (feof(in)) 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 (ferror(in)) if (ferror(in))
throw pgf_systemerror(ferror(in), filepath); throw pgf_error("an error occured while reading the grammar");
return (((uint16_t) buf[0]) << 8 | buf[1]); return (((uint16_t) buf[0]) << 8 | buf[1]);
} }
@@ -40,7 +39,7 @@ uint64_t PgfReader::read_u64be()
if (feof(in)) 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 (ferror(in)) if (ferror(in))
throw pgf_systemerror(ferror(in), filepath); throw pgf_error("an error occured while reading the grammar");
return (((uint64_t) buf[0]) << 56 | return (((uint64_t) buf[0]) << 56 |
((uint64_t) buf[1]) << 48 | ((uint64_t) buf[1]) << 48 |
@@ -96,7 +95,7 @@ object PgfReader::read_name_internal(size_t struct_size)
if (feof(in)) if (feof(in))
throw pgf_error("utf8 decoding error"); throw pgf_error("utf8 decoding error");
if (ferror(in)) if (ferror(in))
throw pgf_systemerror(ferror(in), filepath); throw pgf_error("an error occured while reading the grammar");
ptext->text[size] = 0; ptext->text[size] = 0;
@@ -132,7 +131,7 @@ object PgfReader::read_text_internal(size_t struct_size)
if (feof(in)) if (feof(in))
throw pgf_error("utf8 decoding error"); throw pgf_error("utf8 decoding error");
if (ferror(in)) if (ferror(in))
throw pgf_systemerror(ferror(in), filepath); throw pgf_error("an error occured while reading the grammar");
p += len; p += len;
} }

View File

@@ -8,7 +8,7 @@
class PGF_INTERNAL_DECL PgfReader class PGF_INTERNAL_DECL PgfReader
{ {
public: public:
PgfReader(FILE *in, const char *filepath); PgfReader(FILE *in);
uint8_t read_uint8(); uint8_t read_uint8();
uint16_t read_u16be(); uint16_t read_u16be();
@@ -72,7 +72,6 @@ public:
private: private:
FILE *in; FILE *in;
const char* filepath;
object read_name_internal(size_t struct_size); object read_name_internal(size_t struct_size);
object read_text_internal(size_t struct_size); object read_text_internal(size_t struct_size);

View File

@@ -2,10 +2,9 @@
#include "data.h" #include "data.h"
#include "writer.h" #include "writer.h"
PgfWriter::PgfWriter(FILE *out, const char *filepath) PgfWriter::PgfWriter(FILE *out)
{ {
this->out = out; this->out = out;
this->filepath = filepath;
this->abstract = 0; this->abstract = 0;
} }
@@ -13,7 +12,7 @@ void PgfWriter::write_uint8(uint8_t b)
{ {
size_t n_items = fwrite(&b, sizeof(b), 1, out); size_t n_items = fwrite(&b, sizeof(b), 1, out);
if (ferror(out)) if (ferror(out))
throw pgf_systemerror(ferror(out), filepath); throw pgf_error("an error occured while writing out the grammar");
if (n_items != 1) if (n_items != 1)
throw pgf_error("couldn't write to the output file"); throw pgf_error("couldn't write to the output file");
} }
@@ -26,7 +25,7 @@ void PgfWriter::write_u16be(uint16_t u)
size_t n_items = fwrite(&buf, sizeof(buf), 1, out); size_t n_items = fwrite(&buf, sizeof(buf), 1, out);
if (ferror(out)) if (ferror(out))
throw pgf_systemerror(ferror(out), filepath); throw pgf_error("an error occured while writing out the grammar");
if (n_items != 1) if (n_items != 1)
throw pgf_error("couldn't write to the output file"); throw pgf_error("couldn't write to the output file");
} }
@@ -45,7 +44,7 @@ void PgfWriter::write_u64be(uint64_t u)
size_t n_items = fwrite(&buf, sizeof(buf), 1, out); size_t n_items = fwrite(&buf, sizeof(buf), 1, out);
if (ferror(out)) if (ferror(out))
throw pgf_systemerror(ferror(out), filepath); throw pgf_error("an error occured while writing out the grammar");
if (n_items != 1) if (n_items != 1)
throw pgf_error("couldn't write to the output file"); throw pgf_error("couldn't write to the output file");
} }
@@ -90,7 +89,7 @@ void PgfWriter::write_uint(uint64_t u)
if (u == 0) { if (u == 0) {
size_t n_items = fwrite(&b, sizeof(b), 1, out); size_t n_items = fwrite(&b, sizeof(b), 1, out);
if (ferror(out)) if (ferror(out))
throw pgf_systemerror(ferror(out), filepath); throw pgf_error("an error occured while writing out the grammar");
if (n_items != 1) if (n_items != 1)
throw pgf_error("couldn't write to the output file"); throw pgf_error("couldn't write to the output file");
@@ -100,7 +99,7 @@ void PgfWriter::write_uint(uint64_t u)
size_t n_items = fwrite(&b, sizeof(b), 1, out); size_t n_items = fwrite(&b, sizeof(b), 1, out);
if (ferror(out)) if (ferror(out))
throw pgf_systemerror(ferror(out), filepath); throw pgf_error("an error occured while writing out the grammar");
if (n_items != 1) if (n_items != 1)
throw pgf_error("couldn't write to the output file"); throw pgf_error("couldn't write to the output file");
} }
@@ -112,7 +111,7 @@ void PgfWriter::write_name(PgfText *text)
write_len(text->size); write_len(text->size);
size_t n_items = fwrite(&text->text, text->size, 1, out); size_t n_items = fwrite(&text->text, text->size, 1, out);
if (ferror(out)) if (ferror(out))
throw pgf_systemerror(ferror(out), filepath); throw pgf_error("an error occured while writing out the grammar");
if (n_items != 1) if (n_items != 1)
throw pgf_error("couldn't write to the output file"); throw pgf_error("couldn't write to the output file");
} }
@@ -128,7 +127,7 @@ void PgfWriter::write_text(PgfText *text)
write_len(len); write_len(len);
size_t n_items = fwrite(&text->text, text->size, 1, out); size_t n_items = fwrite(&text->text, text->size, 1, out);
if (ferror(out)) if (ferror(out))
throw pgf_systemerror(ferror(out), filepath); throw pgf_error("an error occured while writing out the grammar");
if (n_items != 1) if (n_items != 1)
throw pgf_error("couldn't write to the output file"); throw pgf_error("couldn't write to the output file");
} }

View File

@@ -4,7 +4,7 @@
class PGF_INTERNAL_DECL PgfWriter class PGF_INTERNAL_DECL PgfWriter
{ {
public: public:
PgfWriter(FILE *out, const char *filepath); PgfWriter(FILE *out);
void write_uint8(uint8_t b); void write_uint8(uint8_t b);
void write_u16be(uint16_t u); void write_u16be(uint16_t u);
@@ -49,7 +49,6 @@ private:
void write_namespace_helper(Namespace<V> nmsp, void (PgfWriter::*write_value)(ref<V>)); void write_namespace_helper(Namespace<V> nmsp, void (PgfWriter::*write_value)(ref<V>));
FILE *out; FILE *out;
const char* filepath;
ref<PgfAbstr> abstract; ref<PgfAbstr> abstract;
}; };