X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=storage.c;h=dda1856155387d8f8d5b5afbd9697eabfaee2215;hb=9e527b672cea0530effc91bc13e0ce49d552ecdd;hp=80ebf0af96dac923f4de3ddd4724c1e23a984661;hpb=a24bd5195f9990159a974c98751f1473f29b9fa4;p=calc.git diff --git a/storage.c b/storage.c index 80ebf0a..dda1856 100644 --- a/storage.c +++ b/storage.c @@ -1,110 +1,90 @@ #include +#include #include #include "alloc.h" #include "debug.h" #include "format.h" +#include "tabular.h" #include "storage.h" /* global variables */ #define DEFAULT_STORAGE_SIZE 10 -double *storage = NULL; - -int storage_size = -1; +tab_t *storage = NULL; /* storage functions */ void memory (int nb) { - int i, l; - double *tmp = NULL; - if (nb != storage_size) { - l = (nb < storage_size) ? nb : storage_size; - tmp = (double *) callocordie (nb, sizeof (double)); - for (i = 0; i < l; i++) { - tmp[i] = storage[i]; - } - if (storage != NULL) { - free (storage); - } - storage = tmp; - storage_size = nb; + if (nb != size_tab (storage)) { + storage = resize_tab (storage, nb); } } -double store (int index, double value) +double store (int id, double value) { - if (storage_size == -1) { + if (storage == NULL) { memory (DEFAULT_STORAGE_SIZE); } - if ((index > 0) && (index <= storage_size)) { - storage[index - 1] = value; - } else { - VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size)); - } - return value; + return set_tab (storage, id, value); } -double recall (int index) +double recall (int id) { - if (storage_size == -1) { + if (storage == NULL) { memory (DEFAULT_STORAGE_SIZE); } - if ((index > 0) && (index <= storage_size)) { - return storage[index - 1]; - } else { - VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size)); - } - return 0; + return get_tab (storage, id); } -double increase (int index) +double increase (int id) { - if (storage_size == -1) { + if (storage == NULL) { memory (DEFAULT_STORAGE_SIZE); } - if ((index > 0) && (index <= storage_size)) { - return storage[index - 1]++; - } else { - VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size)); + double val = get_tab (storage, id); + if (!isnan (val)) { + set_tab (storage, id, ++val); } - return 0; + return val; } -double decrease (int index) +double decrease (int id) { - if (storage_size == -1) { + if (storage == NULL) { memory (DEFAULT_STORAGE_SIZE); } - if ((index > 0) && (index <= storage_size)) { - return storage[index - 1]--; - } else { - VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size)); + double val = get_tab (storage, id); + if (!isnan (val)) { + set_tab (storage, id, --val); } - return 0; + return val; } void display (void) { - int i; - if (storage_size == -1) { + if (storage == NULL) { memory (DEFAULT_STORAGE_SIZE); } + int i, n = size_tab (storage); fprintf (stdout, "storage:"); - for (i = 0; i < storage_size; i++) { + for (i = 0; i < n; i++) { fprintf (stdout, " "); - fprintf (stdout, minform, storage[i]); + fprintf (stdout, minform, get_tab (storage, i + 1)); } fprintf (stdout, "\n"); } void clear () { - int i; - for (i = 0; i < storage_size; i++) { - storage[i] = 0; + if (storage == NULL) { + memory (DEFAULT_STORAGE_SIZE); + } + int i, n = size_tab (storage); + for (i = 0; i < n; i++) { + set_tab (storage, i, 0); } }