Commit | Line | Data |
---|---|---|
a24bd519 LM |
1 | #include <malloc.h> |
2 | #include <stdio.h> | |
3 | ||
4 | #include "alloc.h" | |
5 | #include "debug.h" | |
6 | #include "format.h" | |
7 | ||
8 | #include "storage.h" | |
9 | ||
10 | /* global variables */ | |
11 | ||
12 | #define DEFAULT_STORAGE_SIZE 10 | |
13 | double *storage = NULL; | |
14 | ||
15 | int storage_size = -1; | |
16 | ||
17 | /* storage functions */ | |
18 | ||
19 | void memory (int nb) | |
20 | { | |
21 | int i, l; | |
22 | double *tmp = NULL; | |
23 | if (nb != storage_size) { | |
24 | l = (nb < storage_size) ? nb : storage_size; | |
25 | tmp = (double *) callocordie (nb, sizeof (double)); | |
26 | for (i = 0; i < l; i++) { | |
27 | tmp[i] = storage[i]; | |
28 | } | |
29 | if (storage != NULL) { | |
30 | free (storage); | |
31 | } | |
32 | storage = tmp; | |
33 | storage_size = nb; | |
34 | } | |
35 | } | |
36 | ||
37 | double store (int index, double value) | |
38 | { | |
39 | if (storage_size == -1) { | |
40 | memory (DEFAULT_STORAGE_SIZE); | |
41 | } | |
42 | if ((index > 0) && (index <= storage_size)) { | |
43 | storage[index - 1] = value; | |
44 | } else { | |
45 | VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size)); | |
46 | } | |
47 | return value; | |
48 | } | |
49 | ||
50 | double recall (int index) | |
51 | { | |
52 | if (storage_size == -1) { | |
53 | memory (DEFAULT_STORAGE_SIZE); | |
54 | } | |
55 | if ((index > 0) && (index <= storage_size)) { | |
56 | return storage[index - 1]; | |
57 | } else { | |
58 | VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size)); | |
59 | } | |
60 | return 0; | |
61 | } | |
62 | ||
63 | double increase (int index) | |
64 | { | |
65 | if (storage_size == -1) { | |
66 | memory (DEFAULT_STORAGE_SIZE); | |
67 | } | |
68 | if ((index > 0) && (index <= storage_size)) { | |
69 | return storage[index - 1]++; | |
70 | } else { | |
71 | VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size)); | |
72 | } | |
73 | return 0; | |
74 | } | |
75 | ||
76 | double decrease (int index) | |
77 | { | |
78 | if (storage_size == -1) { | |
79 | memory (DEFAULT_STORAGE_SIZE); | |
80 | } | |
81 | if ((index > 0) && (index <= storage_size)) { | |
82 | return storage[index - 1]--; | |
83 | } else { | |
84 | VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size)); | |
85 | } | |
86 | return 0; | |
87 | } | |
88 | ||
89 | void display (void) | |
90 | { | |
91 | int i; | |
92 | if (storage_size == -1) { | |
93 | memory (DEFAULT_STORAGE_SIZE); | |
94 | } | |
95 | fprintf (stdout, "storage:"); | |
96 | for (i = 0; i < storage_size; i++) { | |
97 | fprintf (stdout, " "); | |
98 | fprintf (stdout, minform, storage[i]); | |
99 | } | |
100 | fprintf (stdout, "\n"); | |
101 | } | |
102 | ||
103 | void clear () | |
104 | { | |
105 | int i; | |
106 | for (i = 0; i < storage_size; i++) { | |
107 | storage[i] = 0; | |
108 | } | |
109 | } | |
110 | ||
111 | /* vim: set ts=4 sw=4 et: */ |