]> git.sur5r.net Git - c128-kasse/blob - config.c
fix saving/loading from file :)
[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 unsigned long int money = 0;
12 unsigned long int items_sold = 0;
13 BYTE printer_port = 4;
14 static bool files_existing = false;
15 struct status_array_t status;
16 struct credits_array_t credits;
17
18 /* 
19  * Checks if items/credits-files are existing to avoid having to recover
20  * the error state of the drive (we'd have to if we would just access the
21  * files directly)
22  *
23  */
24 bool lookup_needed_files() {
25         BYTE lfn = 8;
26         BYTE files_existing = 0;
27         struct cbm_dirent *dirent;
28
29         if (cbm_opendir(lfn, (BYTE)8) != 0) {
30                 cprintf("could not open directory\r\n");
31                 return false;
32         }
33         while (cbm_readdir(lfn, dirent) == 0)
34                 if (    strcasecmp(dirent->name, "items") == 0 ||
35                         strcasecmp(dirent->name, "credits") == 0)
36                         files_existing++;
37         cbm_closedir(lfn);
38         return (files_existing >= 2);
39 }
40
41 void load_items() {
42         if (files_existing)
43                 cbm_load("items", (BYTE)8, &status);
44 }
45
46 void load_credits() {
47         if (files_existing)
48                 cbm_load("credits", (BYTE)8, &credits);
49 }
50
51 void save_items() {
52         cbm_save("items", (BYTE)8, &status, sizeof(struct status_array_t));
53         files_existing = true;
54 }
55
56 void save_credits() {
57         cbm_save("credits", (BYTE)8, &credits, sizeof(struct credits_array_t));
58         files_existing = true;
59 }
60
61 void load_config() {
62         files_existing = lookup_needed_files();
63 }