From 7e7736e17c6ab73fb72f91071f5c23dec887dd60 Mon Sep 17 00:00:00 2001 From: sECuRE Date: Sun, 21 Oct 2007 12:39:34 +0000 Subject: [PATCH] use cbm_load/cbm_save; WARNING: this code _IS_ broken at the moment git-svn-id: https://shell.noname-ev.de/svn/kasse/c128@46 af93e077-1a23-4f1e-9cbe-9382a9d578f5 --- config.c | 121 +++++++++++++++-------------------------------- config.h | 5 +- credit_manager.c | 2 +- general.c | 1 + kasse.c | 30 ++++++------ 5 files changed, 58 insertions(+), 101 deletions(-) diff --git a/config.c b/config.c index de2f4f3..d85d5fc 100644 --- a/config.c +++ b/config.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "general.h" #include "config.h" @@ -12,100 +13,53 @@ 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; -void load_config(); - -void load_items() { - char line[80]; - char *sep, *newl = NULL, *lp; +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; +void load_items() { BYTE rc; - int count=1; + 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); +} - 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 save_items() { + cbm_save("items", (BYTE)8, status, sizeof(struct status_t) * MAX_ITEMS); + files_existing = true; } -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 0) { - credits[num_credit_items].nickname = nickname; + strcpy(credits[num_credit_items].nickname, nickname); credits[num_credit_items].credit = credits_int; ++num_credit_items; } diff --git a/general.c b/general.c index e7f037d..bb42e01 100644 --- a/general.c +++ b/general.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "general.h" diff --git a/kasse.c b/kasse.c index de72138..2cd714c 100644 --- a/kasse.c +++ b/kasse.c @@ -19,19 +19,18 @@ void print_screen() { BYTE i = 0; char *time = get_time(); 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")); + 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); - cprintf("\r\nBefehle: s) Daten sichern d) Drucken umschalten\r\ng) Guthabenverwaltung z) Zeit setzen\r\n"); + cprintf("\r\nBefehle: s) Daten sichern d) Drucken umschalten\r\ng) Guthabenverwaltung z) Zeit setzen\r\nn) Neues Getraenk\r\n"); } void log_file(char * s) { - FILE * f; - if (s==NULL) + FILE *f; + if (s == NULL) return; - f = fopen("log", "a"); - if (f==NULL) + if ((f = fopen("log", "a")) == NULL) c128_perror(23, "kann logfile nicht oeffnen"); fputs(s, f); fclose(f); @@ -52,13 +51,13 @@ void print_log(BYTE n, int einheiten, char *nickname) { c = cbm_open((BYTE)4, (BYTE)4, (BYTE)0, NULL); if (c != 0) { c128_perror(c, "cbm_open(printer)"); - save_state(); + save_items(); exit(1); } c = cbm_write((BYTE)4, print_buffer, strlen(print_buffer)); if (c != strlen(print_buffer)) { c128_perror(c, "write(printer)"); - save_state(); + save_items(); exit(1); } cbm_close((BYTE)4); @@ -160,12 +159,10 @@ int main() { POKE(216, 255); /* Konfigurationsdatei laden */ load_config(); - /* Einträge (=Getränke) laden */ + /* Einträge (=Getränke) und Zustand laden */ load_items(); - /* Zustand laden */ - load_state(); /* Guthaben laden */ -// load_credits(); + load_credits(); while (1) { /* Bildschirm anzeigen */ print_screen(); @@ -176,8 +173,8 @@ int main() { buy(c - 48); else if (c == 's') { /* Zustandsdatei schreiben */ - save_state(); -// save_credits(); + save_items(); + save_credits(); cprintf("Statefile/Creditfile gesichert, druecke ANYKEY...\r\n"); getchar(); } else if (c == 'd') { @@ -191,6 +188,11 @@ int main() { } else if (c == 'z') { /* 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++; } else if (c == 'q') break; } -- 2.39.5