From 3e55aa442452d6a38a67466cb2ef30e1ce736bb5 Mon Sep 17 00:00:00 2001 From: Krasimir Angelov Date: Wed, 13 Sep 2017 09:30:52 +0200 Subject: [PATCH] added function pgf_write --- src/runtime/c/Makefile.am | 2 ++ src/runtime/c/pgf/pgf.c | 23 +++++++++++++++++++++++ src/runtime/c/pgf/pgf.h | 3 +++ 3 files changed, 28 insertions(+) diff --git a/src/runtime/c/Makefile.am b/src/runtime/c/Makefile.am index f275731fe..edc4f88b2 100644 --- a/src/runtime/c/Makefile.am +++ b/src/runtime/c/Makefile.am @@ -76,6 +76,8 @@ libpgf_la_SOURCES = \ pgf/literals.h \ pgf/reader.h \ pgf/reader.c \ + pgf/writer.h \ + pgf/writer.c \ pgf/linearizer.c \ pgf/typechecker.c \ pgf/reasoner.c \ diff --git a/src/runtime/c/pgf/pgf.c b/src/runtime/c/pgf/pgf.c index a1649b9ff..af046d63c 100644 --- a/src/runtime/c/pgf/pgf.c +++ b/src/runtime/c/pgf/pgf.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +45,28 @@ pgf_read_in(GuIn* in, return pgf; } +PGF_API_DECL void +pgf_write(PgfPGF* pgf, const char* fpath, GuExn* err) +{ + FILE* outfile = fopen(fpath, "wb"); + if (outfile == NULL) { + gu_raise_errno(err); + return; + } + + GuPool* tmp_pool = gu_local_pool(); + + // Create an input stream from the input file + GuOut* out = gu_file_out(outfile, tmp_pool); + + PgfWriter* wtr = pgf_new_writer(out, tmp_pool, err); + pgf_write_pgf(pgf, wtr); + + gu_pool_free(tmp_pool); + + fclose(outfile); +} + PGF_API GuString pgf_abstract_name(PgfPGF* pgf) { diff --git a/src/runtime/c/pgf/pgf.h b/src/runtime/c/pgf/pgf.h index ceeb1f54b..c7a14dceb 100644 --- a/src/runtime/c/pgf/pgf.h +++ b/src/runtime/c/pgf/pgf.h @@ -57,6 +57,9 @@ pgf_concrete_load(PgfConcr* concr, GuIn* in, GuExn* err); PGF_API_DECL void pgf_concrete_unload(PgfConcr* concr); +PGF_API_DECL void +pgf_write(PgfPGF* pgf, const char* fpath, GuExn* err); + PGF_API_DECL GuString pgf_abstract_name(PgfPGF*);