]> git.sur5r.net Git - c128-kasse/commitdiff
fix saving/loading from file :)
authorsECuRE <sECuRE@af93e077-1a23-4f1e-9cbe-9382a9d578f5>
Sun, 21 Oct 2007 13:34:35 +0000 (13:34 +0000)
committersECuRE <sECuRE@af93e077-1a23-4f1e-9cbe-9382a9d578f5>
Sun, 21 Oct 2007 13:34:35 +0000 (13:34 +0000)
git-svn-id: https://shell.noname-ev.de/svn/kasse/c128@47 af93e077-1a23-4f1e-9cbe-9382a9d578f5

Makefile
config.c
config.h
credit_manager.c
kasse.c

index 56dc731637cadf8a8c2d1cc273226b92e5511ba2..18ad9b82494b7cbdc4706e5ce07ab2c6b4b64e01 100644 (file)
--- 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 
index d85d5fca74e4959d53013e8075466393c25109e2..5a70352e2925b5903c39bc278b36013cee786878 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1,3 +1,5 @@
+#define _IS_CONFIG_C
+
 #include <stdlib.h>
 #include <conio.h>
 #include <string.h>
@@ -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<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();
 }
index 0263b9e175ab6e4f18e90c2d879b796eb88fc8d7..2eef4da748c9b516e9519dd2387033fbf1f29c1b 100644 (file)
--- a/config.h
+++ b/config.h
@@ -3,10 +3,11 @@
 #ifndef CONFIG_H_
 #define CONFIG_H_
 
+#define MAX_ITEMS 15
+#define MAX_CREDIT_ITEMS 75
+
 /* Eingenommes Geld in Cent */
 extern unsigned long int money;
-extern unsigned long int num_items;
-extern unsigned char num_credit_items;
 extern unsigned long int items_sold;
 extern BYTE printer_port;
 
@@ -20,8 +21,10 @@ struct status_t {
        unsigned int times_sold;
 };
 
-#define MAX_ITEMS 15
-extern struct status_t status[MAX_ITEMS+1];
+struct status_array_t {
+       BYTE num_items;
+       struct status_t status[MAX_ITEMS];
+};
 
 /* Datenstruktur für die Guthaben */
 struct credits_t {
@@ -30,8 +33,15 @@ struct credits_t {
        unsigned int credit;
 };
 
-#define MAX_CREDIT_ITEMS 75
-extern struct credits_t credits[MAX_CREDIT_ITEMS+1];
+struct credits_array_t {
+       BYTE num_items;
+       struct credits_t credits[MAX_CREDIT_ITEMS];
+};
+
+#ifndef _IS_CONFIG_C
+extern struct status_array_t status;
+extern struct credits_array_t credits;
+#endif
 
 /* Lädt Dinge wie die Druckeradresse */
 void load_config();
index f4e7dbeadf9495499be329c7f3b85772fd2fd510..154cccf2b71c3a6baca58b98ddfce683e0988848 100644 (file)
@@ -13,9 +13,9 @@ void print_credit_screen() {
        BYTE i = 0;
        clrscr();
        cprintf("C128-Kassenprogramm: Credit Manager\r\n\r\n");
-       for (; i < num_credit_items; ++i)
-               if (filter == NULL || strncmp(credits[i].nickname, filter, filter_len) == 0)
-                       cprintf("Guthaben %s: %d Cents\r\n", credits[i].nickname, credits[i].credit);
+       for (; i < credits.num_items; ++i)
+               if (filter == NULL || strncmp(credits.credits[i].nickname, filter, filter_len) == 0)
+                       cprintf("Guthaben %s: %d Cents\r\n", credits.credits[i].nickname, credits.credits[i].credit);
        cprintf("\r\nBefehle: n) Neues Guthaben f) Filtern z) Zurueck\r\n");
 }
 
@@ -48,9 +48,9 @@ void credit_manager() {
                        }
                        credits_int = atoi(credits_input) * negative;
                        if (credits_int > 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 2cd714ceab75fe10939559c4527bfc5c4d9a671e..2979fd6940b6063c03036ef4e13efb11a9968028 100644 (file)
--- 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;
        }