+#define _IS_CONFIG_C
+
#include <stdlib.h>
-#include <stdio.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];
-
-#ifdef REAL_DATA
-void load_config();
-
-
-void load_items(){
- FILE* f;
- char line[80];
- char * sep;
- f = fopen("items", "r");
- for (num_items=0; num_items < MAX_ITEMS && !feof(f); num_items++) {
- fgets(line, 79, f);
- sep = strchr(line, '=');
- strncpy(status[num_items].item_name, line, sep-line);
- status[num_items].price = atoi(sep+1);
- status[num_items].times_sold = 0;
- }
-
-}
-
-/**
- * must be called after load_items()
+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)
+ *
*/
-void load_state(){
- FILE* f;
- char line[80];
- char * sep;
- char i, j;
- f = fopen("state", "r");
- if (f==NULL){
- printf("cannot open state\n");
- return;
- }
- while (!feof(f)) {
- fgets(line, 79, f);
- sep = strchr(line, '=');
- *(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;
- }
- }
- }
-}
-
-void save_state(){}
-
-void load_config() {
-}
-
-#else
-
-void load_config() {
+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() {
- BYTE c;
- num_items=2;
- strcpy(status[0].item_name, "cola");
- status[0].price = 230;
- status[0].times_sold = 0;
- strcpy(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[0] = 0;
+ if (files_existing)
+ cbm_load("items", (BYTE)8, &status);
}
-void load_state() {
- status[0].times_sold=23;
- status[1].times_sold=42;
+void load_credits() {
+ if (files_existing)
+ cbm_load("credits", (BYTE)8, &credits);
}
-void load_credits() {
+void save_items() {
+ cbm_save("items", (BYTE)8, &status, sizeof(struct status_array_t));
+ files_existing = true;
}
-void save_state() {
+void save_credits() {
+ cbm_save("credits", (BYTE)8, &credits, sizeof(struct credits_array_t));
+ files_existing = true;
}
-void save_credits() {}
-#endif
+void load_config() {
+ files_existing = lookup_needed_files();
+}