From: sECuRE Date: Sun, 21 Oct 2007 13:34:35 +0000 (+0000) Subject: fix saving/loading from file :) X-Git-Tag: rgb2rv6~62 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=adce5afb5bf582726091bb9689d94fb19ea85220;p=c128-kasse fix saving/loading from file :) git-svn-id: https://shell.noname-ev.de/svn/kasse/c128@47 af93e077-1a23-4f1e-9cbe-9382a9d578f5 --- diff --git a/Makefile b/Makefile index 56dc731..18ad9b8 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,20 @@ +# Note: this are my paths to cc65 as there is no gentoo ebuild, sorry for that. +# Please use the path below if you've installed cc65 system-wide +CC=~/customSoftware/cc65-2.11.0/src/cc65/cc65 -I ~/customSoftware/cc65-2.11.0/include +CA=~/customSoftware/cc65-2.11.0/src/ca65/ca65 +CL=~/customSoftware/cc65-2.11.0/src/cl65/cl65 +# CC=cc65 +# CA=ca65 +# CL=cl65 + %.o: %.c - cc65 -t c128 $< - ca65 -t c128 $$(basename $< .c).s + cp /tmp/cc65/lib/c128* . + ${CC} -t c128 $< + ${CA} -t c128 $$(basename $< .c).s all: config.o kasse.o general.o credit_manager.o time.o - cl65 -t c128 *.o -o kasse + # See above, please just kill the PATH-definition + PATH=${PATH}:~/customSoftware/cc65-2.11.0/src/ld65:/tmp/cc65/lib ${CL} -t c128 *.o -o kasse package: all c1541 -attach kasse.d64 -delete state || exit 0 @@ -14,7 +25,7 @@ package: all c1541 -attach kasse.d64 -write items || exit 0 test: config.o test.o general.o - cl65 -t c128 config.o test.o general.o -o test + ${CL} -t c128 config.o test.o general.o -o test test-package: test c1541 -attach test.d64 -delete state || exit 0 diff --git a/config.c b/config.c index d85d5fc..5a70352 100644 --- a/config.c +++ b/config.c @@ -1,3 +1,5 @@ +#define _IS_CONFIG_C + #include #include #include @@ -7,14 +9,18 @@ #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]; 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; @@ -29,53 +35,29 @@ bool lookup_needed_files() { strcasecmp(dirent->name, "credits") == 0) files_existing++; cbm_closedir(lfn); - return (files_existing == 2); + return (files_existing >= 2); } void load_items() { - BYTE rc; - char *nn; - cprintf("load_items()\r\n"); - if (files_existing) { - rc = cbm_load("items", (BYTE)8, status); - cprintf("rc = %d\r\n", rc); - nn = get_input(); - } -// cbm_load("items", (BYTE)8, NULL); -} - -void save_items() { - cbm_save("items", (BYTE)8, status, sizeof(struct status_t) * MAX_ITEMS); - files_existing = true; + if (files_existing) + cbm_load("items", (BYTE)8, &status); } void load_credits() { if (files_existing) - cbm_load("credits", (BYTE)8, credits); + cbm_load("credits", (BYTE)8, &credits); } -void save_credits() { - cbm_save("credits", (BYTE)8, credits, sizeof(struct credits_t) * MAX_CREDIT_ITEMS); +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 0) { - strcpy(credits[num_credit_items].nickname, nickname); - credits[num_credit_items].credit = credits_int; - ++num_credit_items; + strcpy(credits.credits[credits.num_items].nickname, nickname); + credits.credits[credits.num_items].credit = credits_int; + ++credits.num_items; } if (negative == -1) --credits_input; diff --git a/kasse.c b/kasse.c index 2cd714c..2979fd6 100644 --- a/kasse.c +++ b/kasse.c @@ -21,8 +21,9 @@ void print_screen() { clrscr(); cprintf("C128-Kassenprogramm\r\n\r\nUhrzeit: %s (wird nicht aktualisiert)\r\nEingenommen: %ld Cents, Verkauft: %ld Flaschen, Drucken: %s\r\n\r\n", time, money, items_sold, (printing == 1 ? "ein" : "aus")); free(time); - for (; i < num_items; ++i) - cprintf("Eintrag %x: %s (%d Cents, %d mal verkauft)\r\n", i, status[i].item_name, status[i].price, status[i].times_sold); + for (; i < status.num_items; ++i) + cprintf("Eintrag %x: %s (%d Cents, %d mal verkauft)\r\n", + i, status.status[i].item_name, status.status[i].price, status.status[i].times_sold); cprintf("\r\nBefehle: s) Daten sichern d) Drucken umschalten\r\ng) Guthabenverwaltung z) Zeit setzen\r\nn) Neues Getraenk\r\n"); } @@ -72,10 +73,10 @@ void buy(BYTE n) { BYTE c, nickname_len, single_match; int einheiten; char *nickname; - if (status[n].item_name == NULL) + if (status.status[n].item_name == NULL) cprintf("FEHLER: Diese Einheit existiert nicht.\r\n"); else { - cprintf("Wieviel Einheiten \"%s\"?\r\n", status[n].item_name); + cprintf("Wieviel Einheiten \"%s\"?\r\n", status.status[n].item_name); while (1) { c = getchar(); if (c == 13) @@ -95,21 +96,22 @@ void buy(BYTE n) { nickname_len = strlen(nickname); /* go through credits and remove the amount of money or set nickname * to NULL if no such credit could be found */ - for (c = 0; c < num_credit_items; ++c) - if (strncmp(nickname, credits[c].nickname, nickname_len) == 0) { + for (c = 0; c < credits.num_items; ++c) + if (strncmp(nickname, credits.credits[c].nickname, nickname_len) == 0) { if (++matches == 2) break; else single_match = c; } if (matches == 1) { - if (credits[single_match].credit < (status[n].price * einheiten)) { + if (credits.credits[single_match].credit < (status.status[n].price * einheiten)) { cprintf("Sorry, %s hat nicht genug Geld :-(\r\n", nickname); free(nickname); return; } else { /* Geld abziehen */ - credits[single_match].credit -= (status[n].price * einheiten); - cprintf("\r\nVerbleibendes Guthaben fuer %s: %d Cents. Druecke ANYKEY...\r\n", nickname, credits[single_match].credit); + credits.credits[single_match].credit -= (status.status[n].price * einheiten); + cprintf("\r\nVerbleibendes Guthaben fuer %s: %d Cents. Druecke ANYKEY...\r\n", + nickname, credits.credits[single_match].credit); getchar(); } } else if (matches == 0) { @@ -119,8 +121,8 @@ void buy(BYTE n) { nickname = NULL; } } - status[n].times_sold += einheiten; - money += status[n].price * einheiten; + status.status[n].times_sold += einheiten; + money += status.status[n].price * einheiten; items_sold += einheiten; if (printing == 1) print_log(n, einheiten, nickname); @@ -189,10 +191,10 @@ int main() { /* Zeit setzen */ set_time_interactive(); } else if (c == 'n') { - strcpy(status[num_items].item_name, "mate"); - status[num_items].price = 23; - status[num_items].times_sold = 5; - num_items++; + strcpy(status.status[status.num_items].item_name, "mate"); + status.status[status.num_items].price = 23; + status.status[status.num_items].times_sold = 5; + status.num_items++; } else if (c == 'q') break; }