]> git.sur5r.net Git - c128-kasse/blob - src/config.c
save file when exiting credit manager
[c128-kasse] / src / config.c
1 /* 
2  * RGB2R-C128-Kassenprogramm
3  * (c) 2007-2009 phil_fry, sECuRE, sur5r
4  * See LICENSE for license information
5  *
6  */
7 #define _IS_CONFIG_C
8
9 #include <stdlib.h>
10 #include <conio.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdbool.h>
14
15 #include "kasse.h"
16 #include "general.h"
17 #include "config.h"
18
19 /* NOTE: undocumented function which scratches files
20    We need to use this function because linking unistd.h
21    makes our program break at runtime.
22  */
23 unsigned char __fastcall__ _sysremove(const char *name);
24
25 unsigned long int money = 0;
26 unsigned long int items_sold = 0;
27 BYTE printer_port = 4;
28 static bool items_exists = false;
29 static bool credits_exists = false;
30 struct status_array_t status;
31 struct credits_array_t credits;
32
33 /* 
34  * Checks if items/credits-files are existing to avoid having to recover
35  * the error state of the drive (we'd have to if we would just access the
36  * files directly)
37  *
38  */
39 static void lookup_needed_files() {
40         BYTE lfn = 8, c;
41         struct cbm_dirent dirent;
42         char *buffer = malloc(sizeof(char) * 64 * 100);
43         char *walk;
44         char filename[8];
45         int n;
46
47         if (buffer == NULL) {
48                 cprintf("Not enough memory available\r\n");
49                 exit(1);
50         }
51
52         if (cbm_opendir(lfn, (BYTE)8) != 0) {
53                 cprintf("could not open directory\r\n");
54                 return;
55         }
56         while (cbm_readdir(lfn, &dirent) == 0) {
57                 /* NOTE: You MUST NOT delete any logfiles. This does only work
58                  * under the assumption that logfiles are named continuously */
59                 if (strncmp(dirent.name, "log", 3) == 0)
60                         log_num++;
61                 if (strcasecmp(dirent.name, "items") == 0)
62                         items_exists = true;
63                 if (strcasecmp(dirent.name, "credits") == 0)
64                         credits_exists = true;
65         }
66         cbm_closedir(lfn);
67
68         /* Try to find out how many lines the last logfile got to seamlessly
69          * append to it, if we got more than one logfile. */
70         if (log_num > 0) {
71                 log_num--;
72
73                 sprintf(filename, "log-%d", log_num);
74                 if ((c = cbm_open(lfn, (BYTE)8, (BYTE)CBM_READ, filename)) != 0) {
75                         c128_perror(c, "cbm_open(log) for reading");
76                         exit(1);
77                 }
78                 n = cbm_read(lfn, buffer, sizeof(char) * 64 * 100);
79                 if (n < 0) {
80                         cprintf("error while cbm_read()ing the logfile\r\n");
81                         exit(1);
82                 }
83                 buffer[n] = '\0';
84                 for (walk = buffer; (walk - buffer) < n; walk++) {
85                         if (*walk == '\r' || *walk == '\n')
86                                 log_lines_written++;
87                 }
88                 cbm_close(lfn);
89         }
90
91         free(buffer);
92 }
93
94 void load_items() {
95         BYTE c;
96
97         if (items_exists) {
98                 items_sold = 0;
99                 cbm_load("items", (BYTE)8, &status);
100                 for (c = 0; c < status.num_items; c++)
101                         items_sold += status.status[c].times_sold;
102         } else
103                 memset(&status, 0, sizeof(struct status_array_t));
104 }
105
106 void load_credits() {
107         if (credits_exists)
108                 cbm_load("credits", (BYTE)8, &credits);
109         else
110                 memset(&credits, 0, sizeof(struct credits_array_t));
111 }
112
113 void save_items() {
114         if (items_exists)
115                 _sysremove("items");
116         cbm_save("items", (BYTE)8, &status, sizeof(struct status_array_t));
117         items_exists = true;
118 }
119
120 void save_credits() {
121         if (credits_exists)
122                 _sysremove("credits");
123         cbm_save("credits", (BYTE)8, &credits, sizeof(struct credits_array_t));
124         credits_exists = true;
125 }
126
127 void load_config() {
128         lookup_needed_files();
129 }