Tries module: when loading a trie from a file, we now check if the file is corrupted before starting loading.

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@2232 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
ricroc 2008-05-07 14:21:39 +00:00
parent da7a287e49
commit ed339dcb12
5 changed files with 46 additions and 14 deletions

View File

@ -369,12 +369,15 @@ TrEntry itrie_load(FILE *file) {
TrNode node;
new_itrie_entry(itrie, NULL);
CURRENT_ITRIE = itrie;
if (!(node = core_trie_load(ITRIE_ENGINE, file, &itrie_data_load))) {
free_itrie_entry(itrie);
return NULL;
}
TrEntry_trie(itrie) = node;
if (FIRST_ITRIE)
TrEntry_previous(FIRST_ITRIE) = itrie;
FIRST_ITRIE = itrie;
CURRENT_ITRIE = itrie;
node = core_trie_load(ITRIE_ENGINE, file, &itrie_data_load);
TrEntry_trie(itrie) = node;
return itrie;
}

View File

@ -240,12 +240,15 @@ TrEntry trie_load(FILE *file) {
TrNode node;
new_trie_entry(trie, NULL);
CURRENT_TRIE = trie;
if (!(node = core_trie_load(TRIE_ENGINE, file, &trie_data_load))) {
free_trie_entry(trie);
return NULL;
}
TrEntry_trie(trie) = node;
if (FIRST_TRIE)
TrEntry_previous(FIRST_TRIE) = trie;
FIRST_TRIE = trie;
CURRENT_TRIE = trie;
node = core_trie_load(TRIE_ENGINE, file, &trie_data_load);
TrEntry_trie(trie) = node;
return trie;
}

View File

@ -429,19 +429,41 @@ TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode,
TrNode node;
char version[15];
fscanf(file, "%14s", version);
if (!strcmp(version, "BEGIN_TRIE_v2")) {
fseek(file, -11, SEEK_END);
fscanf(file, "%s", version);
if (strcmp(version, "END_TRIE_v2")) {
fprintf(stderr, "******************************************\n");
fprintf(stderr, " Tries core module: trie file corrupted\n");
fprintf(stderr, "******************************************\n");
return NULL;
}
fseek(file, 13, SEEK_SET);
CURRENT_LOAD_VERSION = 2;
} else if (!strcmp(version, "BEGIN_TRIE")) {
fseek(file, -8, SEEK_END);
fscanf(file, "%s", version);
if (strcmp(version, "END_TRIE")) {
fprintf(stderr, "******************************************\n");
fprintf(stderr, " Tries core module: trie file corrupted\n");
fprintf(stderr, "******************************************\n");
return NULL;
}
fseek(file, 10, SEEK_SET);
CURRENT_LOAD_VERSION = 1;
} else {
fprintf(stderr, "****************************************\n");
fprintf(stderr, " Tries core module: invalid trie file\n");
fprintf(stderr, "****************************************\n");
return NULL;
}
CURRENT_TRIE_ENGINE = engine;
CURRENT_INDEX = -1;
CURRENT_DEPTH = 0;
DATA_LOAD_FUNCTION = load_function;
node = core_trie_open(engine);
fscanf(file, "%s", version);
if (!strcmp(version, "BEGIN_TRIE_v2")) {
CURRENT_LOAD_VERSION = 2;
traverse_and_load(node, file);
} else if (!strcmp(version, "BEGIN_TRIE")) {
CURRENT_LOAD_VERSION = 1;
traverse_and_load(node, file);
}
traverse_and_load(node, file);
return node;
}

View File

@ -579,6 +579,8 @@ static int p_itrie_load(void) {
itrie = itrie_load(file);
if (fclose(file))
return FALSE;
if (!itrie)
return FALSE;
return YAP_Unify(arg_itrie, YAP_MkIntTerm((YAP_Int) itrie));
}
#undef arg_itrie

View File

@ -527,6 +527,8 @@ static int p_trie_load(void) {
data = trie_load(file);
if (fclose(file))
return FALSE;
if (!data)
return FALSE;
return YAP_Unify(arg_trie, YAP_MkIntTerm((YAP_Int) data));
}
#undef arg_trie