]> git.sur5r.net Git - c128-kasse/blobdiff - config.c
fix saving/loading from file :)
[c128-kasse] / config.c
index 681f2ff1fc23419275718a554bfd0da0c3dce075..5a70352e2925b5903c39bc278b36013cee786878 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1,25 +1,63 @@
+#define _IS_CONFIG_C
+
+#include <stdlib.h>
+#include <conio.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include "general.h"
 #include "config.h"
 
 unsigned long int money = 0;
-unsigned long int num_items = 0;
 unsigned long int items_sold = 0;
+BYTE printer_port = 4;
+static bool files_existing = false;
+struct status_array_t status;
+struct credits_array_t credits;
+
+/* 
+ * Checks if items/credits-files are existing to avoid having to recover
+ * the error state of the drive (we'd have to if we would just access the
+ * files directly)
+ *
+ */
+bool lookup_needed_files() {
+       BYTE lfn = 8;
+       BYTE files_existing = 0;
+       struct cbm_dirent *dirent;
+
+       if (cbm_opendir(lfn, (BYTE)8) != 0) {
+               cprintf("could not open directory\r\n");
+               return false;
+       }
+       while (cbm_readdir(lfn, dirent) == 0)
+               if (    strcasecmp(dirent->name, "items") == 0 ||
+                       strcasecmp(dirent->name, "credits") == 0)
+                       files_existing++;
+       cbm_closedir(lfn);
+       return (files_existing >= 2);
+}
+
+void load_items() {
+       if (files_existing)
+               cbm_load("items", (BYTE)8, &status);
+}
 
-void load_config();
-
-void load_items(){
-       num_items=3;
-       status[0].item_name = "cola";
-       status[0].price = 230;
-       status[0].times_sold = 0;
-       status[1].item_name = "mate";
-       status[1].price = 150;
-       status[1].times_sold = 0;
-       for (c = 2; c < MAX_ITEMS; ++c)
-               status[c].item_name = NULL;
-       
+void load_credits() {
+       if (files_existing)
+               cbm_load("credits", (BYTE)8, &credits);
 }
-void load_state(){
-       status[0].times_sold=23;        
-       status[1].times_sold=42;        
+
+void save_items() {
+       cbm_save("items", (BYTE)8, &status, sizeof(struct status_array_t));
+       files_existing = true;
+}
+
+void save_credits() {
+       cbm_save("credits", (BYTE)8, &credits, sizeof(struct credits_array_t));
+       files_existing = true;
+}
+
+void load_config() {
+       files_existing = lookup_needed_files();
 }
-void save_state(){}