]> git.sur5r.net Git - c128-kasse/blobdiff - config.c
fix saving/loading from file :)
[c128-kasse] / config.c
index de2f4f302547faf1b964c8df15b44472b5863f79..5a70352e2925b5903c39bc278b36013cee786878 100644 (file)
--- a/config.c
+++ b/config.c
+#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;
-BYTE num_credit_items = 0;
 unsigned long int items_sold = 0;
 BYTE printer_port = 4;
-struct status_t status[MAX_ITEMS+1];
-struct credits_t credits[MAX_CREDIT_ITEMS+1];
-
-void load_config();
+static bool files_existing = false;
+struct status_array_t status;
+struct credits_array_t credits;
 
-void load_items() {
-       char line[80];
-       char *sep, *newl = NULL, *lp;
+/* 
+ * 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 rc;
-       int count = 1;
+       BYTE files_existing = 0;
+       struct cbm_dirent *dirent;
 
-       if ((rc = cbm_open(lfn, (BYTE)8, (BYTE)0, "items,r")) != 0) {
-               cprintf("cannot open items\r\n");
-               return;
-       }
-       for (num_items = 0; num_items < MAX_ITEMS && count > 0;) {
-               count = cbm_read(lfn, line, 79);
-               if (count < 0) {
-                       cprintf("read error\r\n");
-                       return;
-               }
-               line[count] = '\0';
-               lp = line;
-               do {
-                       if (newl)
-                               if (newl < ((line+count)-1))
-                                       lp = newl+1;
-                               else break;
-                       sep = strchr(lp, '=');
-                       strncpy(status[num_items].item_name, lp, sep-lp);
-                       if (newl)
-                               *newl = '\0';
-                       status[num_items].price = atoi(sep+1);
-                       status[num_items].times_sold = 0; 
-                       num_items++;
-               } while (newl = strchr(lp, '\n'));
+       if (cbm_opendir(lfn, (BYTE)8) != 0) {
+               cprintf("could not open directory\r\n");
+               return false;
        }
-       cbm_close(lfn);
+       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);
 }
 
-/**
- * must be called after load_items()
- */
-void load_state(){
-    char line[80];
-    char * sep;
-    char i;
-       BYTE lfn=8;
-       BYTE rc;
-       int count=1;
+void load_items() {
+       if (files_existing)
+               cbm_load("items", (BYTE)8, &status);
+}
 
-       rc=cbm_open(lfn, (BYTE)8, (BYTE)0, "state,r");
-    if (rc!=0){
-       cprintf("cannot open state\r\n");
-       return;
-    }
-    while (count>0) {
-       count=cbm_read(lfn,line,79);
-               //fgets(line, 79, f);
-       sep = strchr(line, '=');
-       if (sep==NULL)
-               continue;
-       *(line + (sep-line)) = 0;
-        for (i=0; i< MAX_ITEMS; i++) {
-               if (strcmp(line, status[i].item_name)==0) {
-                       status[i].times_sold = atoi(sep+1);
-                       break;
-               }
-        }
-    }
-    cbm_close(lfn);
+void load_credits() {
+       if (files_existing)
+               cbm_load("credits", (BYTE)8, &credits);
 }
 
-void save_state(){
-       int i;
-       BYTE lfn=8;
-       BYTE rc;
-       int count=1;
-       int size=1;
-       char line[81];
-       
-       rc=cbm_open(lfn, (BYTE)8, (BYTE)0, "state,w");
-    if (rc!=0){
-       c128_perror(23, "cannot open state file");
-       return;
-    }
-    for (i=0;i<num_items;i++)
-       {
-               memset(line,0, 81);
-               size=sprintf(line, "%s=%d\n", status[i].item_name, status[i].times_sold);
-               cbm_write(lfn, line, size);
-       //fprintf(f, "%s=%d\n",status[i].item_name, status[i].times_sold);
-       }
-       cbm_close(lfn);
+void save_items() {
+       cbm_save("items", (BYTE)8, &status, sizeof(struct status_array_t));
+       files_existing = true;
 }
-/*
-void dump_state(){
-       FILE * f;
-       char buf[128];
-       int i, len;
-       memset(buf, 0, 128);
-       f = fopen("state", "r");
-       len = fread(buf, 1, 128, f);
-       cprintf("read %d bytes from state\n", len);
-       fclose(f);
-       for (i=0;i<len;i++)
-               printf("%x ", buf[i]);
-       cprintf("\n");
-       
+
+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();
 }