From f9bd4784d55c6e2afe4c102a1212a7661ac0ef80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Fri, 15 May 2015 01:38:34 +0200 Subject: Rewrite write_config solution check and fix apply solution.apply failed in case of last solution. --- scripts/shared/build_files.h | 3 +- scripts/solution.py | 11 +++- scripts/write_config/solution.c | 119 ++++++++++++++++++++++++++-------------- scripts/write_config/solution.h | 10 +++- scripts/write_config/write.c | 26 ++++++--- 5 files changed, 117 insertions(+), 52 deletions(-) (limited to 'scripts') diff --git a/scripts/shared/build_files.h b/scripts/shared/build_files.h index 9907309..948ebce 100644 --- a/scripts/shared/build_files.h +++ b/scripts/shared/build_files.h @@ -2,4 +2,5 @@ #define DEFAULT_VARIABLE_COUNT_FILE "variable_count" #define DEFAULT_SYMBOL_MAP_FILE "symbol_map" #define DEFAULT_DEF_CONFIG_FILE "def_config" -#define DEFAULT_SOLUTION_FILE "solution" +#define DEFAULT_CONFIG_MAP_FILE "config_map" +#define DEFAULT_CONFIG_SOLVED_FILE "config_solved" diff --git a/scripts/solution.py b/scripts/solution.py index 37db112..2eed129 100644 --- a/scripts/solution.py +++ b/scripts/solution.py @@ -107,15 +107,24 @@ def apply(): solved.add(ln.strip()) # Load one solution if it is not in solved + hash = '' with open(sf(conf.config_map_file)) as f: while True: w = f.readline().split(sep=':') + if not w[0]: + break if not w[0] in solved: solution = utils.config_strtoint(w[1]) - break; + hash = w[0] + break if not solution: raise exceptions.NoApplicableSolution() + # Write hash to config_solved + with open(sf(conf.config_solved_file), 'a') as f: + f.write(hash) + f.write('\n') + # Load variable count with open(sf(conf.symbol_map_file)) as f: for var_num, l in enumerate(f): diff --git a/scripts/write_config/solution.c b/scripts/write_config/solution.c index f575864..c2007a4 100644 --- a/scripts/write_config/solution.c +++ b/scripts/write_config/solution.c @@ -1,50 +1,89 @@ #include "solution.h" extern int exit_status; -void solution_check(struct symlist *sl, FILE * f) { + +struct solution *solution_load(FILE * fmap, FILE * fsolved) { + char buffer[BUFFER_SIZE]; + size_t buff_pos; + int c; - // skip first line - do - c = fgetc(f); - while (c != EOF && c != '\n'); + buff_pos = 0; + size_t buff_pos_old; + while (1) { + c = fgetc(fsolved); + if (c == '\n') { + buff_pos_old = buff_pos; + buff_pos = 0; + } else if (c == EOF) { + break; + } else { + buffer[buff_pos++] = (char) c; + } + } + buffer[buff_pos_old++] = '\0'; + char *hash; + hash = malloc(buff_pos_old * sizeof(char)); + memcpy(hash, buffer, buff_pos_old * sizeof(char)); - char *w; - size_t w_size = 1, w_pos = 0; - w = malloc((w_size + 1) * sizeof(char)); - do { - c = fgetc(f); + while (1) { + do { + c = fgetc(fmap); + buffer[buff_pos++] = (char) c; + } while (c != ':'); + buffer[buff_pos] = '\0'; + if (!strcmp(buffer, hash)) + break; + do { + c = fgetc(fmap); + } while (c != '\n'); + } + + size_t sz = 2; + struct solution *sol; + sol = malloc(sizeof(struct solution)); + sol->sol = malloc(sz * sizeof(int)); + sol->size = 0; + while (1) { + c = fgetc(fmap); if (c == ' ' || c == '\n') { - w[w_pos] = '\0'; - w_pos = 0; - char *ww = w; - bool neg = false; - if (w[0] == '-') { - neg = true; - ww = w + 1; - } - int id = atoi(ww); - if ((unsigned) id > sl->maxid) - break; - if (id == 0) - continue; - if (sl->array[id - 1].sym == NULL) - continue; - if (neg == - (sym_get_tristate_value(sl->array[id - 1].sym) == - no ? true : false)) - { - } else { - printf("Problem %s=%d/%d\n", sl->array[id - 1].sym->name, - !neg, - sym_get_tristate_value(sl->array[id - 1].sym)); - exit_status++; + buffer[buff_pos] = '\0'; + if (sol->size >= sz) { + sz *= 2; + sol->sol = realloc(sol->sol, sz * sizeof(int)); } + sol->sol[sol->size++] = atoi(buffer); } else { - if (w_pos >= w_size) { - w_size *= 2; - w = realloc(w, (w_size + 1) * sizeof(char)); - } - w[w_pos++] = (char) c; + buffer[buff_pos++] = (char) c; + } + if (c == '\n') + break; + } + + return sol; +} + +void solution_check(struct symlist *sl, struct solution *s) { + unsigned i; + for (i = 0; i < s->size; s++) { + bool neg = false; + if (s->sol[i] < 0) { + neg = true; + s->sol[i] *= -1; + } + if ((unsigned) s->sol[i] > sl->maxid) + break; + if (s->sol[i] == 0) + continue; + if (sl->array[s->sol[i] - 1].sym == NULL) + continue; + if (neg == + (sym_get_tristate_value(sl->array[s->sol[i] - 1].sym) == + no ? true : false)) { + } else { + printf("Problem %s=%d/%d\n", + sl->array[s->sol[i] - 1].sym->name, !neg, + sym_get_tristate_value(sl->array[s->sol[i] - 1].sym)); + exit_status++; } - } while (c != EOF && c != '\n'); + } } diff --git a/scripts/write_config/solution.h b/scripts/write_config/solution.h index 91db619..100a9fe 100644 --- a/scripts/write_config/solution.h +++ b/scripts/write_config/solution.h @@ -8,6 +8,14 @@ #ifndef _SOLUTION_H_ #define _SOLUTION_H_ -void solution_check(struct symlist *sl, FILE * f); +#define BUFFER_SIZE 32 + +struct solution { + int *sol; + size_t size; +}; + +struct solution *solution_load(FILE *fmap, FILE *fsolved); +void solution_check(struct symlist *sl, struct solution *s); #endif /* _SOLUTION_H_ */ diff --git a/scripts/write_config/write.c b/scripts/write_config/write.c index 29fcd60..5402286 100644 --- a/scripts/write_config/write.c +++ b/scripts/write_config/write.c @@ -39,11 +39,12 @@ int main(int argc, char **argv) { exit(3); } - char *rules_file, *symbol_map_file, *def_config_file, *solution_file; + char *rules_file, *symbol_map_file, *def_config_file, *config_map, *config_solved; asprintf(&rules_file, "%s/%s", folder, DEFAULT_RULES_FILE); asprintf(&symbol_map_file, "%s/%s", folder, DEFAULT_SYMBOL_MAP_FILE); asprintf(&def_config_file, "%s/%s", folder, DEFAULT_DEF_CONFIG_FILE); - asprintf(&solution_file, "%s/%s", folder, DEFAULT_SOLUTION_FILE); + asprintf(&config_map, "%s/%s", folder, DEFAULT_CONFIG_MAP_FILE); + asprintf(&config_solved, "%s/%s", folder, DEFAULT_CONFIG_SOLVED_FILE); setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); @@ -57,18 +58,25 @@ int main(int argc, char **argv) { FILE *f = fopen(symbol_map_file, "r"); if (f == NULL) { Eprintf("Can't open file: %s\n", symbol_map_file); - exit(1); + exit(-1); } struct symlist *sl = symlist_read(f); fclose(f); - f = fopen(solution_file, "r"); - if (f == NULL) { - Eprintf("Can't open file: %s\n", solution_file); - exit(2); + FILE *fconfig_map = fopen(config_map, "r"); + if (fconfig_map == NULL) { + Eprintf("Can't open file: %s\n", config_map); + exit(-2); } - solution_check(sl, f); - fclose(f); + FILE *fconfig_solved = fopen(config_solved, "r"); + if (fconfig_map == NULL) { + Eprintf("Can't open file: %s\n", config_solved); + exit(-3); + } + //struct solution *sol = solution_load(fconfig_map, fconfig_solved); + //solution_check(sl, sol); + fclose(fconfig_map); + fclose(fconfig_solved); conf_write(".config"); -- cgit v1.2.3