split code into multiple files
[calc.git] / storage.c
diff --git a/storage.c b/storage.c
new file mode 100644 (file)
index 0000000..80ebf0a
--- /dev/null
+++ b/storage.c
@@ -0,0 +1,111 @@
+#include <malloc.h>
+#include <stdio.h>
+
+#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: */