/* * param.h - Parameters and values * * Copyright 2012 by Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. */ #ifndef PARAM_H #define PARAM_H #include #include "bitset.h" #include "relop.h" /* * Parameter types: * * "name" arbitrary name * "set" names from an ordered list * "abs" absolute numeric value * "rel" numeric value relative to another parameter */ struct equiv { const char *name; struct equiv *next; }; struct names { struct equiv *equiv; /* equivalent value(s) */ struct names *next; /* next/greater value */ }; struct param_ops; struct format { const struct param_ops *ops; union { const struct names *set; const char *abs; /* unit name */ const struct format *rel; } u; }; struct value { union { const char *s; struct bitset set; double abs; struct rel_value { double plus, minus; /* rel: 100% = 1 */ int fract; /* 0: abs. offset; 1: fract. offs. */ } rel; } u; }; /* * When using "struct param" to store (string) variables not associated to a * field, then u.name contains the variable name, "op" contains the relational * operator (default rel_eq), and value.u.s contains the variable's value. */ struct param { union { const char *name; const struct field *field; } u; enum relop op; struct value value; struct param *next; }; struct param_ops { int (*eval)(const struct format *fmt, const char *s, struct value *res); int (*comp)(const struct value *a, enum relop relop, const struct value *b); void (*dump)(FILE *file, const struct format *fmt, const struct value *v); }; void register_nameset(const char *name, const struct names *set); const struct names *find_nameset(const char *name); const char *nameset_rev_lookup(const struct names *first_name); int eval_name(const struct format *fmt, const char *s, struct value *res); int eval_set(const struct format *fmt, const char *s, struct value *res); int eval_abs(const struct format *fmt, const char *s, struct value *res); int eval_rel(const struct format *fmt, const char *s, struct value *res); int evaluate(const struct format *fmt, const char *s, struct value *res); int comp_name(const struct value *a, enum relop relop, const struct value *b); int comp_set(const struct value *a, enum relop relop, const struct value *b); int comp_abs(const struct value *a, enum relop relop, const struct value *b); int comp_rel(const struct value *a, enum relop relop, const struct value *b); int compare(const struct format *fmt, const struct value *a, enum relop relop, const struct value *b); void dump_name(FILE *file, const struct format *fmt, const struct value *v); void dump_set(FILE *file, const struct format *fmt, const struct value *v); void dump_abs(FILE *file, const struct format *fmt, const struct value *v); void dump_rel(FILE *file, const struct format *fmt, const struct value *v); void dump(FILE *file, const struct format *fmt, const struct value *v); extern const struct param_ops param_ops_name; extern const struct param_ops param_ops_set; extern const struct param_ops param_ops_abs; extern const struct param_ops param_ops_rel; struct param *make_var(const char *name, enum relop op, const char *val); const char *var_lookup(const struct param *vars, const char *name); void free_vars(struct param *vars); #endif /* !PARAM_H */