Root/
| 1 | /* |
| 2 | * param.h - Parameters and values |
| 3 | * |
| 4 | * Copyright 2012 by Werner Almesberger |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | #ifndef PARAM_H |
| 14 | #define PARAM_H |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | |
| 18 | #include "bitset.h" |
| 19 | #include "relop.h" |
| 20 | |
| 21 | |
| 22 | /* |
| 23 | * Parameter types: |
| 24 | * |
| 25 | * "name" arbitrary name |
| 26 | * "set" names from an ordered list |
| 27 | * "abs" absolute numeric value |
| 28 | * "rel" numeric value relative to another parameter |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | struct equiv { |
| 33 | const char *name; |
| 34 | struct equiv *next; |
| 35 | }; |
| 36 | |
| 37 | struct names { |
| 38 | struct equiv *equiv; /* equivalent value(s) */ |
| 39 | struct names *next; /* next/greater value */ |
| 40 | }; |
| 41 | |
| 42 | struct param_ops; |
| 43 | |
| 44 | struct format { |
| 45 | const struct param_ops *ops; |
| 46 | union { |
| 47 | const struct names *set; |
| 48 | const char *abs; /* unit name; NULL if dimensionless */ |
| 49 | const struct format *rel; |
| 50 | } u; |
| 51 | }; |
| 52 | |
| 53 | struct value { |
| 54 | union { |
| 55 | const char *s; |
| 56 | struct bitset set; |
| 57 | double abs; |
| 58 | struct rel_value { |
| 59 | double plus, minus; /* rel: 100% = 1 */ |
| 60 | int fract; /* 0: abs. offset; 1: fract. offs. */ |
| 61 | } rel; |
| 62 | } u; |
| 63 | }; |
| 64 | |
| 65 | /* |
| 66 | * When using "struct param" to store (string) variables not associated to a |
| 67 | * field, then u.name contains the variable name, "op" contains the relational |
| 68 | * operator (default rel_eq), and value.u.s contains the variable's value. |
| 69 | * |
| 70 | * If the variable is "$", then u.name is set to NULL. |
| 71 | */ |
| 72 | |
| 73 | struct param { |
| 74 | union { |
| 75 | const char *name; |
| 76 | const struct field *field; |
| 77 | } u; |
| 78 | enum relop op; |
| 79 | struct value value; |
| 80 | struct param *next; |
| 81 | }; |
| 82 | |
| 83 | struct param_ops { |
| 84 | int (*eval)(const struct format *fmt, const char *s, |
| 85 | struct value *res); |
| 86 | int (*comp)(const struct value *a, enum relop relop, |
| 87 | const struct value *b); |
| 88 | void (*dump)(FILE *file, const struct format *fmt, |
| 89 | const struct value *v); |
| 90 | }; |
| 91 | |
| 92 | |
| 93 | void register_nameset(const char *name, const struct names *set); |
| 94 | const struct names *find_nameset(const char *name); |
| 95 | const char *nameset_rev_lookup(const struct names *first_name); |
| 96 | |
| 97 | int eval_name(const struct format *fmt, const char *s, struct value *res); |
| 98 | int eval_set(const struct format *fmt, const char *s, struct value *res); |
| 99 | int eval_abs(const struct format *fmt, const char *s, struct value *res); |
| 100 | int eval_rel(const struct format *fmt, const char *s, struct value *res); |
| 101 | int evaluate(const struct format *fmt, const char *s, struct value *res); |
| 102 | |
| 103 | int comp_name(const struct value *a, enum relop relop, const struct value *b); |
| 104 | int comp_set(const struct value *a, enum relop relop, const struct value *b); |
| 105 | int comp_abs(const struct value *a, enum relop relop, const struct value *b); |
| 106 | int comp_rel(const struct value *a, enum relop relop, const struct value *b); |
| 107 | int compare(const struct format *fmt, |
| 108 | const struct value *a, enum relop relop, const struct value *b); |
| 109 | |
| 110 | void dump_name(FILE *file, const struct format *fmt, const struct value *v); |
| 111 | void dump_set(FILE *file, const struct format *fmt, const struct value *v); |
| 112 | void dump_abs(FILE *file, const struct format *fmt, const struct value *v); |
| 113 | void dump_rel(FILE *file, const struct format *fmt, const struct value *v); |
| 114 | void dump_param(FILE *file, const struct format *fmt, const struct value *v); |
| 115 | |
| 116 | extern const struct param_ops param_ops_name; |
| 117 | extern const struct param_ops param_ops_set; |
| 118 | extern const struct param_ops param_ops_abs; |
| 119 | extern const struct param_ops param_ops_rel; |
| 120 | |
| 121 | struct param *make_var(const char *name, enum relop op, const char *val); |
| 122 | const char *var_lookup(const struct param *vars, const char *name); |
| 123 | struct param *merge_vars(const struct param *a, const struct param *b); |
| 124 | void free_vars(struct param *vars); |
| 125 | |
| 126 | #endif /* !PARAM_H */ |
| 127 |
Branches:
master
