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