]> git.sur5r.net Git - c128-kasse/blob - src/config.c
README: remove emulator configuration, we use -config vicerc
[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 int32_t money = 0;
27 int32_t 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(void) {
41   BYTE lfn = 8, c;
42   struct cbm_dirent dirent;
43   char filename[8];
44
45   if (cbm_opendir(lfn, (BYTE)8) != 0) {
46     cprintf("could not open directory\r\n");
47     return;
48   }
49   while (cbm_readdir(lfn, &dirent) == 0) {
50     /* NOTE: You MUST NOT delete any logfiles. This does only work
51      * under the assumption that logfiles are named continuously */
52     if (strncmp(dirent.name, "log", 3) == 0)
53       log_num++;
54     if (strcasecmp(dirent.name, "items") == 0)
55       items_exists = true;
56     if (strcasecmp(dirent.name, "credits") == 0)
57       credits_exists = true;
58   }
59   cbm_closedir(lfn);
60
61   /* Try to find out how many lines the last logfile got to seamlessly
62    * append to it, if we got more than one logfile. */
63   if (log_num > 0) {
64     log_num--;
65
66     sprintf(filename, "log-%u", log_num);
67     if ((c = cbm_open(lfn, (BYTE)8, (BYTE)CBM_READ, filename)) != 0) {
68       c128_perror(c, "cbm_open(log) for reading");
69       exit(1);
70     }
71     log_heap_offset = cbm_read(lfn, log_heap_buf, LOG_SIZE);
72     if (log_heap_offset < 0) {
73       cprintf("error while cbm_read()ing the logfile\r\n");
74       exit(1);
75     }
76     log_heap_flushed = log_heap_offset;
77     log_heap_buf[log_heap_offset] = '\0';
78     cbm_close(lfn);
79   }
80 }
81
82 void load_items(void) {
83   BYTE c;
84
85   if (items_exists) {
86     items_sold = 0;
87     money = 0;
88     cbm_load("items", (BYTE)8, &status);
89     for (c = 0; c < status.num_items; c++) {
90       items_sold += status.status[c].times_sold;
91       money += (((int32_t)status.status[c].price) *
92                 ((int32_t)status.status[c].times_sold));
93     }
94   } else
95     memset(&status, 0, sizeof(struct status_array_t));
96 }
97
98 void load_credits(void) {
99   if (credits_exists)
100     cbm_load("credits", (BYTE)8, &credits);
101   else
102     memset(&credits, 0, sizeof(struct credits_array_t));
103 }
104
105 void save_items(void) {
106   if (items_exists)
107     _sysremove("items");
108   cbm_save("items", (BYTE)8, &status, sizeof(struct status_array_t));
109   items_exists = true;
110 }
111
112 void save_credits(void) {
113   if (credits_exists)
114     _sysremove("credits");
115   cbm_save("credits", (BYTE)8, &credits, sizeof(struct credits_array_t));
116   credits_exists = true;
117 }
118
119 void load_config(void) { lookup_needed_files(); }