From 995b82cc6ecd741ddc7ff669a75056558b8bbe3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Manuel=20de=20Morais=20Santos=20Costa?= Date: Tue, 3 Nov 2009 14:37:12 +0000 Subject: [PATCH] Nuno Fonseca's trie writing updates. --- library/itries.yap | 5 +++- library/tries/core_tries.c | 7 ++++-- library/tries/itries.c | 48 +++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/library/itries.yap b/library/itries.yap index 14f38e83f..323dc2daa 100644 --- a/library/itries.yap +++ b/library/itries.yap @@ -31,7 +31,10 @@ itrie_stats/4, itrie_max_stats/4, itrie_usage/4, - itrie_print/1 + itrie_print/1, + %added by nf + itrie_save2stream/2, + itrie_loadFromstream/2 ]). :- load_foreign_files([itries], [], init_itries). diff --git a/library/tries/core_tries.c b/library/tries/core_tries.c index dd289539c..424a5f40c 100644 --- a/library/tries/core_tries.c +++ b/library/tries/core_tries.c @@ -428,8 +428,11 @@ inline TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode, YAP_Int, FILE *)) { TrNode node; char version[15]; + fpos_t curpos; fscanf(file, "%14s", version); + if (fgetpos(file, &curpos) ) return NULL; + if (!strcmp(version, "BEGIN_TRIE_v2")) { fseek(file, -11, SEEK_END); fscanf(file, "%s", version); @@ -439,7 +442,7 @@ TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode, fprintf(stderr, "******************************************\n"); return NULL; } - fseek(file, 13, SEEK_SET); + if (fsetpos(file, &curpos) ) return NULL; CURRENT_LOAD_VERSION = 2; } else if (!strcmp(version, "BEGIN_TRIE")) { fseek(file, -8, SEEK_END); @@ -450,7 +453,7 @@ TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode, fprintf(stderr, "******************************************\n"); return NULL; } - fseek(file, 10, SEEK_SET); + if (fsetpos(file, &curpos) ) return NULL; CURRENT_LOAD_VERSION = 1; } else { fprintf(stderr, "****************************************\n"); diff --git a/library/tries/itries.c b/library/tries/itries.c index 72f2b46c9..5a1b66e9e 100644 --- a/library/tries/itries.c +++ b/library/tries/itries.c @@ -51,7 +51,9 @@ static int p_itrie_stats(void); static int p_itrie_max_stats(void); static int p_itrie_usage(void); static int p_itrie_print(void); - +//nf +static int p_itrie_loadFromStream(void); +static int p_itrie_save2stream(void); /* -------------------------- */ @@ -87,6 +89,9 @@ void init_itries(void) { YAP_UserCPredicate("itrie_max_stats", p_itrie_max_stats, 4); YAP_UserCPredicate("itrie_usage", p_itrie_usage, 4); YAP_UserCPredicate("itrie_print", p_itrie_print, 1); + // nf + YAP_UserCPredicate("itrie_save2stream", p_itrie_save2stream, 2); + YAP_UserCPredicate("itrie_loadFromstream", p_itrie_loadFromStream, 2); return; } @@ -679,3 +684,44 @@ static int p_itrie_print(void) { return TRUE; } #undef arg_itrie + +/* added by nf: itrie_save2stream(+Itrie,+Stream) */ +#define arg_itrie YAP_ARG1 +#define arg_stream YAP_ARG2 +static int p_itrie_save2stream(void) { + FILE *file; + + /* check args */ + if (!YAP_IsIntTerm(arg_itrie)) + return FALSE; + if ((file=(FILE*)YAP_FileDescriptorFromStream(arg_stream))==NULL) + return FALSE; + + /* save itrie and close file */ + itrie_save((TrEntry) YAP_IntOfTerm(arg_itrie), file); + return TRUE; +} +#undef arg_itrie +#undef arg_stream + +/* added by nf: itrie_loadFromStream(-Itrie,+Stream) */ +#define arg_itrie YAP_ARG1 +#define arg_stream YAP_ARG2 +static int p_itrie_loadFromStream(void) { + TrEntry itrie; + FILE *file; + + /* check args */ + if (!YAP_IsVarTerm(arg_itrie)) + return FALSE; + if (!(file=(FILE*)Yap_FileDescriptorFromStream(arg_stream))) + return FALSE; + + /* load itrie and close file */ + itrie = itrie_load(file); + if (!itrie) + return FALSE; + return YAP_Unify(arg_itrie, YAP_MkIntTerm((YAP_Int) itrie)); +} +#undef arg_itrie +#undef arg_stream