From: sECuRE Date: Sat, 15 Nov 2008 23:44:47 +0000 (+0000) Subject: Write log in files with 100 lines each (append mode emulation) X-Git-Tag: rgb2rv6~27 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=2848f2de1f65a04328ebf6a7be467ea00d57e57b;p=c128-kasse Write log in files with 100 lines each (append mode emulation) Extend copyright to 2008 Make comments english (consistent) Fix spelling errors Check for not existant items correctly Cleanup makefile git-svn-id: https://shell.noname-ev.de/svn/kasse/c128@82 af93e077-1a23-4f1e-9cbe-9382a9d578f5 --- diff --git a/LICENSE b/LICENSE index f55fecf..0bbefd7 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2007 Jakob 'sur5r' Haufe, Matthias 'phil_fry' Schütz, Michael 'sECuRE' Stapelberg +Copyright (c) 2007-2008 Jakob 'sur5r' Haufe, Matthias 'phil_fry' Schütz, Michael 'sECuRE' Stapelberg All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that diff --git a/Makefile b/Makefile index c957d26..40ab3c0 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,19 @@ -CC=cc65 -CA=ca65 -CL=cl65 - src/%.o: src/%.c include/%.h - ${CC} -O -I include -t c128 $< - ${CA} -I include -t c128 src/$$(basename $< .c).s + cc65 -O -I include -t c128 $< + ca65 -I include -t c128 src/$$(basename $< .c).s test/%.o: test/%.c - ${CC} -O -I include -t c128 $< - ${CA} -I include -t c128 test/$$(basename $< .c).s + cc65 -O -I include -t c128 $< + ca65 -I include -t c128 test/$$(basename $< .c).s kasse: src/config.o src/kasse.o src/general.o src/credit_manager.o src/c128time.o - ${CL} -t c128 src/c128time.o src/config.o src/kasse.o src/general.o src/credit_manager.o -o kasse + cl65 -t c128 src/c128time.o src/config.o src/kasse.o src/general.o src/credit_manager.o -o kasse itemz: src/config.o src/itemz.o src/general.o src/credit_manager.o - ${CL} -t c128 src/config.o src/itemz.o src/general.o src/credit_manager.o -o itemz + cl65 -t c128 src/config.o src/itemz.o src/general.o src/credit_manager.o -o itemz cat: src/general.o src/cat.o - ${CL} -t c128 src/general.o src/cat.o -o cat + cl65 -t c128 src/general.o src/cat.o -o cat all: kasse itemz @@ -33,7 +29,7 @@ package: all # c1541 -attach kasse.d64 -write items || exit 0 test: src/config.o test/test.o src/general.o - ${CL} -t c128 src/config.o test/test.o src/general.o -o test + cl65 -t c128 src/config.o test/test.o src/general.o -o test test-package: test c1541 -attach test.d64 -delete state || exit 0 diff --git a/include/kasse.h b/include/kasse.h index 7556749..4b76410 100644 --- a/include/kasse.h +++ b/include/kasse.h @@ -4,7 +4,8 @@ #ifdef _IS_KASSE BYTE printing = 1; char print_buffer[81]; -int log_num = 0; +char log_num = 0; +int log_lines_written = 0; #else extern int log_num; #endif diff --git a/src/c128time.c b/src/c128time.c index 3678392..ef0ebc2 100644 --- a/src/c128time.c +++ b/src/c128time.c @@ -1,6 +1,6 @@ /* * RGB2R-C128-Kassenprogramm - * (c) 2007 phil_fry, sECuRE, sur5r + * (c) 2007-2008 phil_fry, sECuRE, sur5r * See LICENSE for license information * */ diff --git a/src/config.c b/src/config.c index b1e6a58..00dceb1 100644 --- a/src/config.c +++ b/src/config.c @@ -1,6 +1,6 @@ /* * RGB2R-C128-Kassenprogramm - * (c) 2007 phil_fry, sECuRE, sur5r + * (c) 2007-2008 phil_fry, sECuRE, sur5r * See LICENSE for license information * */ @@ -11,14 +11,16 @@ #include #include #include -#include #include "kasse.h" #include "general.h" #include "config.h" -/* undocumented function which scratches files */ -//unsigned char __fastcall__ _sysremove(const char *name); +/* NOTE: undocumented function which scratches files + We need to use this function because linking unistd.h + makes our program break at runtime. + */ +unsigned char __fastcall__ _sysremove(const char *name); unsigned long int money = 0; unsigned long int items_sold = 0; @@ -69,14 +71,14 @@ void load_credits() { void save_items() { if (items_exists) - unlink("items"); + _sysremove("items"); cbm_save("items", (BYTE)8, &status, sizeof(struct status_array_t)); items_exists = true; } void save_credits() { if (credits_exists) - unlink("credits"); + _sysremove("credits"); cbm_save("credits", (BYTE)8, &credits, sizeof(struct credits_array_t)); credits_exists = true; } diff --git a/src/credit_manager.c b/src/credit_manager.c index 8198d7e..54099b0 100644 --- a/src/credit_manager.c +++ b/src/credit_manager.c @@ -1,6 +1,6 @@ /* * RGB2R-C128-Kassenprogramm - * (c) 2007 phil_fry, sECuRE, sur5r + * (c) 2007-2008 phil_fry, sECuRE, sur5r * See LICENSE for license information * */ diff --git a/src/general.c b/src/general.c index eca154a..8a9041d 100644 --- a/src/general.c +++ b/src/general.c @@ -1,6 +1,6 @@ /* * RGB2R-C128-Kassenprogramm - * (c) 2007 phil_fry, sECuRE, sur5r + * (c) 2007-2008 phil_fry, sECuRE, sur5r * See LICENSE for license information * */ diff --git a/src/itemz.c b/src/itemz.c index fc70c6f..76d8022 100644 --- a/src/itemz.c +++ b/src/itemz.c @@ -1,6 +1,6 @@ /* * RGB2R-C128-Kassenprogramm - * (c) 2007 phil_fry, sECuRE, sur5r + * (c) 2007-2008 phil_fry, sECuRE, sur5r * See LICENSE for license information * * itemz.c: Verwaltet die ITEMS- und CREDITS-datei diff --git a/src/kasse.c b/src/kasse.c index 0d95aea..c2c02a6 100644 --- a/src/kasse.c +++ b/src/kasse.c @@ -1,6 +1,6 @@ /* * RGB2R-C128-Kassenprogramm - * (c) 2007 phil_fry, sECuRE, sur5r + * (c) 2007-2008 phil_fry, sECuRE, sur5r * See LICENSE for license information * */ @@ -19,6 +19,17 @@ // drucker 4 oder 5 // graphic 4,0,10 +/* NOTE: undocumented function which scratches files + We need to use this function because linking unistd.h + makes our program break at runtime. + */ +unsigned char __fastcall__ _sysremove(const char *name); + +static void sane_exit() { + save_items(); + save_credits(); + exit(1); +} /* Hauptbildschirm ausgeben */ static void print_screen() { @@ -27,7 +38,7 @@ static void print_screen() { char profit[10]; clrscr(); if (format_euro(profit, 10, money) == NULL) { - cprintf("Einname %ld konnte nicht umgerechnet werden\r\n", money); + cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money); exit(1); } cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r)\r\ @@ -43,23 +54,43 @@ q) Beenden\r\n"); } static void log_file(const char *s) { + /* A log-entry has usually 50 bytes, so we take 64 bytes. + Because files are wrapped (log.0, log.1, ...) every 100 + lines, we don't need more than 100 * 64 bytes. */ + char *buffer = malloc(sizeof(char) * 64 * 100); char filename[8]; + char read = 0; BYTE c; - sprintf(filename, "log%4d", log_num++); + if (buffer == NULL) { + cprintf("No memory available\n"); + } + buffer[0] = '\0'; + if (((++log_lines_written) % 100) == 0) + log_num++; + sprintf(filename, "log-%d", log_num); + /* Don't read log if there were no lines written before */ + if (log_lines_written != 1) { + if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)0, filename)) != 0) { + c128_perror(c, "cbm_open(log)"); + sane_exit(); + } + read = cbm_read((BYTE)8, buffer, sizeof(char) * 64 * 100); + cbm_close((BYTE)8); + _sysremove(filename); + } if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, filename)) != 0) { c128_perror(c, "cbm_open(log)"); - save_items(); - save_credits(); - exit(1); + sane_exit(); } - c = cbm_write((BYTE)8, s, strlen(s)); - if (c != strlen(s)) { + /* TODO: read < 0? */ + strcpy(buffer+read, s); + c = cbm_write((BYTE)8, buffer, read+strlen(s)); + if (c != (read+strlen(s))) { cprintf("Could not save logfile, please make sure the floppy is not full!\n"); - save_items(); - save_credits(); - exit(1); + sane_exit(); } cbm_close((BYTE)8); + free(buffer); } static char retry_or_quit() { @@ -71,7 +102,7 @@ static char retry_or_quit() { return *c; } -/* Druckt eine entsprechende Zeile aus */ +/* Prints a line and logs it to file */ static void print_log(BYTE n, int einheiten, char *nickname) { BYTE c; char *time = get_time(); @@ -92,15 +123,13 @@ static void print_log(BYTE n, int einheiten, char *nickname) { sprintf(print_buffer, "%c[%lu] %s - %s - %s - %d - an %s\r\n", 17, items_sold, time, status.status[n].item_name, price, einheiten, (*nickname != '\0' ? nickname : "Unbekannt")); - RETRY:; +RETRY: c = cbm_open((BYTE)4, (BYTE)4, (BYTE)0, NULL); if (c != 0) { c128_perror(c, "cbm_open(printer)"); - if (retry_or_quit() == 'q') { - save_items(); - save_credits(); - exit(1); - } + if (retry_or_quit() == 'q') + sane_exit(); + goto RETRY; } c = cbm_write((BYTE)4, print_buffer, strlen(print_buffer)); @@ -117,7 +146,7 @@ static void print_log(BYTE n, int einheiten, char *nickname) { log_file(print_buffer); } -/* Dialog, der einen durch's Abrechnen der Einträge führt */ +/* dialog which is called for each bought item */ void buy(BYTE n) { int negative = 1; char entered[5] = {'1', 0, 0, 0, 0}; @@ -128,8 +157,9 @@ void buy(BYTE n) { char nickname[11]; struct credits_t *credit; - if (status.status[n].item_name == NULL) { + if (n >= status.num_items || status.status[n].item_name == NULL) { cprintf("FEHLER: Diese Einheit existiert nicht.\r\n"); + get_input(); return; } cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", status.status[n].item_name); @@ -169,9 +199,10 @@ void buy(BYTE n) { if (credit != NULL) { if ((signed int)credit->credit < ((signed int)status.status[n].price * einheiten)) { cprintf("Sorry, %s hat nicht genug Geld :-(\r\n", nickname); + get_input(); return; } - /* Geld abziehen */ + /* substract money */ credit->credit -= (status.status[n].price * einheiten); cprintf("\r\nVerbleibendes Guthaben fuer %s: %d Cents. Druecke RETURN...\r\n", nickname, credit->credit); @@ -223,47 +254,42 @@ int main() { if (VIDEOMODE == 40) toggle_videomode(); - /* Zeit erstmalig setzen */ + /* Set time initially, c128 doesn't know it */ set_time_interactive(); POKE(216, 255); - /* Konfigurationsdatei laden */ + /* Load configuration */ load_config(); cprintf("got %d logfiles\r\n", log_num); - /* Einträge (=Getränke) und Zustand laden */ + /* Load items (= drinks) */ load_items(); - /* Guthaben laden */ + /* Load credits */ load_credits(); while (1) { - /* Bildschirm anzeigen */ print_screen(); - /* Tastatureingaben abfragen */ c = get_input(); - /* und eventuell weitere Dialoge anzeigen */ + /* ...display dialogs eventually */ if (*c > 47 && *c < 58) { buy((*c) - 48); toggle_videomode(); clrscr(); toggle_videomode(); } else if (*c == 's') { - /* Zustandsdatei schreiben */ save_items(); save_credits(); cprintf("Statefile/Creditfile gesichert, druecke RETURN...\r\n"); get_input(); } else if (*c == 'd') { - /* Drucken an- oder ausschalten */ + /* enable/disable printing */ printing = (printing == 1 ? 0 : 1); cprintf("Drucken ist nun %s, druecke RETURN...\r\n", (printing == 1 ? "eingeschaltet" : "ausgeschaltet")); get_input(); } else if (*c == 'g') { - /* Guthabenverwalter aufrufen */ credit_manager(); } else if (*c == 'z') { - /* Zeit setzen */ set_time_interactive(); } else if (*c == 'q') break;