]> git.sur5r.net Git - c128-kasse/blob - config.c
523e8e445d8c2b266ab2bae5c2ab6524cbb9ed4e
[c128-kasse] / config.c
1 #define _IS_CONFIG_C
2
3 #include <stdlib.h>
4 #include <conio.h>
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdbool.h>
8 #include "general.h"
9 #include "config.h"
10
11 /* undocumented function which scratches files */
12 unsigned char __fastcall__ _sysremove(const char *name);
13
14 unsigned long int money = 0;
15 unsigned long int items_sold = 0;
16 BYTE printer_port = 4;
17 static bool files_existing = false;
18 struct status_array_t status;
19 struct credits_array_t credits;
20
21 /* 
22  * Checks if items/credits-files are existing to avoid having to recover
23  * the error state of the drive (we'd have to if we would just access the
24  * files directly)
25  *
26  */
27 bool lookup_needed_files() {
28         BYTE lfn = 8;
29         BYTE files_existing = 0;
30         struct cbm_dirent *dirent;
31
32         if (cbm_opendir(lfn, (BYTE)8) != 0) {
33                 cprintf("could not open directory\r\n");
34                 return false;
35         }
36         while (cbm_readdir(lfn, dirent) == 0)
37                 if (    strcasecmp(dirent->name, "items") == 0 ||
38                         strcasecmp(dirent->name, "credits") == 0)
39                         files_existing++;
40         cbm_closedir(lfn);
41         return (files_existing >= 2);
42 }
43
44 void load_items() {
45         if (files_existing)
46                 cbm_load("items", (BYTE)8, &status);
47 }
48
49 void load_credits() {
50         if (files_existing)
51                 cbm_load("credits", (BYTE)8, &credits);
52 }
53
54 void save_items() {
55         if (files_existing)
56                 _sysremove("items");
57         cbm_save("items", (BYTE)8, &status, sizeof(struct status_array_t));
58         files_existing = true;
59 }
60
61 void save_credits() {
62         if (files_existing)
63                 _sysremove("credits");
64         cbm_save("credits", (BYTE)8, &credits, sizeof(struct credits_array_t));
65         files_existing = true;
66 }
67
68 void load_config() {
69         files_existing = lookup_needed_files();
70 }