mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-13 23:09:31 -06:00
readNGF now fails if the file doesn't exist. Instead there is newNGF
This commit is contained in:
@@ -118,31 +118,13 @@ PgfDB *pgf_read_ngf(const char *fpath,
|
||||
|
||||
bool is_new = false;
|
||||
PGF_API_BEGIN {
|
||||
db = new PgfDB(fpath, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
db = new PgfDB(fpath, O_RDWR, 0);
|
||||
|
||||
{
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
|
||||
ref<PgfPGF> pgf = PgfDB::get_revision(&master);
|
||||
if (pgf == 0) {
|
||||
is_new = true;
|
||||
pgf = PgfDB::malloc<PgfPGF>(master.size+1);
|
||||
pgf->ref_count = 1;
|
||||
pgf->major_version = PGF_MAJOR_VERSION;
|
||||
pgf->minor_version = PGF_MINOR_VERSION;
|
||||
pgf->gflags = 0;
|
||||
pgf->abstract.name = PgfDB::malloc<PgfText>();
|
||||
pgf->abstract.name->size = 0;
|
||||
pgf->abstract.aflags = 0;
|
||||
pgf->abstract.funs = 0;
|
||||
pgf->abstract.cats = 0;
|
||||
pgf->prev = 0;
|
||||
pgf->next = 0;
|
||||
memcpy(&pgf->name, &master, sizeof(PgfText)+master.size+1);
|
||||
PgfDB::set_revision(pgf);
|
||||
} else {
|
||||
Node<PgfPGF>::add_value_ref(pgf);
|
||||
}
|
||||
Node<PgfPGF>::add_value_ref(pgf);
|
||||
*revision = pgf.as_object();
|
||||
}
|
||||
|
||||
@@ -158,6 +140,49 @@ PgfDB *pgf_read_ngf(const char *fpath,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PGF_API
|
||||
PgfDB *pgf_new_ngf(PgfText *abstract_name,
|
||||
const char *fpath,
|
||||
PgfRevision *revision,
|
||||
PgfExn* err)
|
||||
{
|
||||
PgfDB *db = NULL;
|
||||
|
||||
PGF_API_BEGIN {
|
||||
db = new PgfDB(fpath, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
|
||||
{
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
|
||||
ref<PgfPGF> pgf = PgfDB::malloc<PgfPGF>(master.size+1);
|
||||
pgf->ref_count = 1;
|
||||
pgf->major_version = PGF_MAJOR_VERSION;
|
||||
pgf->minor_version = PGF_MINOR_VERSION;
|
||||
pgf->gflags = 0;
|
||||
pgf->abstract.name = PgfDB::malloc<PgfText>(abstract_name->size+1);
|
||||
memcpy(&(*pgf->abstract.name), abstract_name, sizeof(PgfText)+abstract_name->size+1);
|
||||
pgf->abstract.aflags = 0;
|
||||
pgf->abstract.funs = 0;
|
||||
pgf->abstract.cats = 0;
|
||||
pgf->prev = 0;
|
||||
pgf->next = 0;
|
||||
memcpy(&pgf->name, &master, sizeof(PgfText)+master.size+1);
|
||||
PgfDB::set_revision(pgf);
|
||||
*revision = pgf.as_object();
|
||||
}
|
||||
|
||||
return db;
|
||||
} PGF_API_END
|
||||
|
||||
if (db != NULL) {
|
||||
delete db;
|
||||
if (fpath != NULL)
|
||||
remove(fpath);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PGF_API
|
||||
void pgf_write_pgf(const char* fpath,
|
||||
PgfDB *db, PgfRevision revision,
|
||||
|
||||
@@ -237,15 +237,23 @@ PgfDB *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
|
||||
PgfExn* err);
|
||||
|
||||
/* Tries to read the grammar from an already booted NGF file.
|
||||
* If the file does not exist then a new one is created, and the
|
||||
* grammar is set to be empty. It can later be populated with
|
||||
* rules dynamically. The default grammar revision is stored
|
||||
* in *revision. */
|
||||
* The function fails if the file does not exist. The default grammar
|
||||
* revision is stored in *revision. */
|
||||
PGF_API_DECL
|
||||
PgfDB *pgf_read_ngf(const char* fpath,
|
||||
PgfRevision *revision,
|
||||
PgfExn* err);
|
||||
|
||||
/* Creates a new NGF file with a grammar with the given abstract_name.
|
||||
* Aside from the name, the grammar is otherwise empty but can be later
|
||||
* populated with new functions and categories. If fpath is NULL then
|
||||
* the file is not stored on the disk but only in memory. */
|
||||
PGF_API
|
||||
PgfDB *pgf_new_ngf(PgfText *abstract_name,
|
||||
const char *fpath,
|
||||
PgfRevision *revision,
|
||||
PgfExn* err);
|
||||
|
||||
PGF_API_DECL
|
||||
void pgf_write_pgf(const char* fpath,
|
||||
PgfDB *db, PgfRevision revision,
|
||||
|
||||
Reference in New Issue
Block a user