X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=storage.c;fp=storage.c;h=80ebf0af96dac923f4de3ddd4724c1e23a984661;hb=a24bd5195f9990159a974c98751f1473f29b9fa4;hp=0000000000000000000000000000000000000000;hpb=a70d1cdb22b2aa77a34d483403a182e335babf8e;p=calc.git diff --git a/storage.c b/storage.c new file mode 100644 index 0000000..80ebf0a --- /dev/null +++ b/storage.c @@ -0,0 +1,111 @@ +#include +#include + +#include "alloc.h" +#include "debug.h" +#include "format.h" + +#include "storage.h" + +/* global variables */ + +#define DEFAULT_STORAGE_SIZE 10 +double *storage = NULL; + +int storage_size = -1; + +/* 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; + } +} + +double store (int index, double value) +{ + if (storage_size == -1) { + 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; +} + +double recall (int index) +{ + if (storage_size == -1) { + 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; +} + +double increase (int index) +{ + if (storage_size == -1) { + 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; +} + +double decrease (int index) +{ + if (storage_size == -1) { + 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; +} + +void display (void) +{ + int i; + if (storage_size == -1) { + memory (DEFAULT_STORAGE_SIZE); + } + fprintf (stdout, "storage:"); + for (i = 0; i < storage_size; i++) { + fprintf (stdout, " "); + fprintf (stdout, minform, storage[i]); + } + fprintf (stdout, "\n"); +} + +void clear () +{ + int i; + for (i = 0; i < storage_size; i++) { + storage[i] = 0; + } +} + +/* vim: set ts=4 sw=4 et: */