////////////////////////////////////////////////////////////////////////////////// // If variable "var" already instantiated, returns -1. Otherwise, // instantiates the unification variable "var" to category "cat" and value "val". // Returns 1 or -1 in case of error, 0 otherwise. int unif_instantiate(unif_vars_T* UNIF_VARS,unichar* var, l_category_T* cat, unichar* val) { int i; int v; //Check if not yet instantiated. if (unif_instantiated(UNIF_VARS,var)) return -1; i = UNIF_VARS->no_vars; //Category UNIF_VARS->vars[i].cat = cat; //value v = is_valid_val(cat,val); if (v == -1) { error("Instantiation impossible: %S is an invalid value in category %S.\n",val,cat->name); return 1; } UNIF_VARS->vars[i].val = v; //id UNIF_VARS->vars[i].id = u_strdup(var); UNIF_VARS->no_vars++; return 0; }
////////////////////////////////////////////////////////////////////////////////// // If variable "var" already instantiated, returns -1. Otherwise, // instantiates the unification variable "var" to category "cat" and value "val". // Returns 1 or -1 in case of error, 0 otherwise. int unif_instantiate(MultiFlex_ctx* p_multiFlex_ctx,unichar* var, l_category_T* cat, unichar* val) { int i; int v; //Check if not yet instantiated. if (unif_instantiated(p_multiFlex_ctx,var)) return -1; i = (p_multiFlex_ctx->UNIF_VARS).no_vars; //Category (p_multiFlex_ctx->UNIF_VARS).vars[i].cat = cat; //value v = is_valid_val(cat,val); if (v == -1) { error("Instantiation impossible: %S is an invalid value in category %S.\n",val,cat->name); return 1; } (p_multiFlex_ctx->UNIF_VARS).vars[i].val = v; //id (p_multiFlex_ctx->UNIF_VARS).vars[i].id = u_strdup(var); (p_multiFlex_ctx->UNIF_VARS).no_vars++; return 0; }
static int bisect_val(int min, int max, bool is_valid_val(int val)) { bool valid; while (1) { int try_val = min + (max - min + 1) / 2; valid = is_valid_val(try_val); if (min == max) break; if (valid) min = try_val; else max = try_val - 1; } return valid ? min : -1; }
int dump_by_meta(const char *metafile) { void *val; size_t val_len; item_value_u val_history; hfs_trend_t val_trends; int fd, i; char *datafile = NULL; hfs_meta_t *meta = NULL; hfs_meta_item_t *ip = NULL; hfs_time_t ts; hfs_off_t ofs; if ((meta = read_metafile(metafile, NULL)) == NULL) return -1; // Somethig real bad happend :( if (meta->blocks == 0) { fprintf(stderr, "%s: No data!\n", metafile); free_meta(meta); return -1; } if (is_trend_type(meta->last_type)) { val = &val_trends; val_len = sizeof(val_trends); } else { val = &val_history; val_len = sizeof(val_history); } datafile = get_datafile(metafile); if ((fd = open (datafile, O_RDONLY)) == -1) { fprintf(stderr, "%s: file open failed: %s\n", datafile, strerror(errno)); free_meta(meta); free(datafile); return -1; } for (i = 0; i < meta->blocks; i++) { ip = meta->meta + i; ts = ip->start; if ((ofs = find_meta_ofs (ts, meta, NULL)) == -1) { fprintf(stderr, "%s: %d: unable to get offset in file\n", datafile, (int)ts); free_meta(meta); free(datafile); close(fd); return -1; } if (lseek (fd, ofs, SEEK_SET) == -1) { fprintf(stderr, "%s: unable to change file offset: %s\n", datafile, strerror(errno)); free_meta(meta); free(datafile); close(fd); return -1; } while (read (fd, val, val_len) > 0) { if (!is_valid_val(val, val_len)) { ts += ip->delay; continue; } printf("time=%d\tdelay=%d\ttype=%d\t", (int)ts, ip->delay, ip->type); if (is_trend_type(ip->type)) { printf("count=%d\tmax=", val_trends.count); show_value(ip->type, val_trends.max); printf("\tmin="); show_value(ip->type, val_trends.min); printf("\tavg="); show_value(ip->type, val_trends.avg); } else { printf("value="); show_value(ip->type, val_history); } printf("\n"); ts += ip->delay; if (ts > ip->end) break; } } free_meta(meta); free(datafile); close(fd); return 0; }