From 5391e76a448825b4c687c1f47c3d8b71aa6b453a Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 1 Nov 2009 20:46:29 +0100 Subject: [PATCH] Move logging/printing functions to src/print.c, fix compilation of itemz --- include/general.h | 3 +- include/kasse.h | 7 ---- include/print.h | 18 +++++++++ src/config.c | 1 + src/credit_manager.c | 1 + src/general.c | 9 +++++ src/itemz.c | 4 -- src/kasse.c | 84 +----------------------------------------- src/print.c | 88 ++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 120 insertions(+), 95 deletions(-) create mode 100644 include/print.h create mode 100644 src/print.c diff --git a/include/general.h b/include/general.h index 0ddefcf..4e45b29 100644 --- a/include/general.h +++ b/include/general.h @@ -2,7 +2,8 @@ #define GENERAL_H_ typedef unsigned char BYTE; char *get_input(); -char * format_euro(char * s, int maxlen, int cent); +char retry_or_quit(); +char *format_euro(char * s, int maxlen, int cent); void c128_perror(BYTE, char*); extern BYTE _oserror; #define POKE(addr,val) (*(unsigned char*) (addr) = (val)) diff --git a/include/kasse.h b/include/kasse.h index 2546c96..76d414b 100644 --- a/include/kasse.h +++ b/include/kasse.h @@ -5,12 +5,5 @@ void print_the_buffer(); #ifdef _IS_KASSE BYTE printing = 1; -char print_buffer[81]; -char log_num = 0; -int log_lines_written = 0; -#else -extern int log_num; -extern int log_lines_written; -extern char print_buffer[81]; #endif #endif diff --git a/include/print.h b/include/print.h new file mode 100644 index 0000000..4ccde07 --- /dev/null +++ b/include/print.h @@ -0,0 +1,18 @@ +#ifndef _PRINT_H +#define _PRINT_H + +void print_the_buffer(); +void log_file(const char *s); + +#ifdef _IS_PRINT +char print_buffer[81]; +char log_num = 0; +int log_lines_written = 0; +#else +extern char print_buffer[81]; +extern int log_num; +extern int log_lines_written; + +#endif + +#endif diff --git a/src/config.c b/src/config.c index 3ee69bd..4c2e1e6 100644 --- a/src/config.c +++ b/src/config.c @@ -15,6 +15,7 @@ #include "kasse.h" #include "general.h" #include "config.h" +#include "print.h" /* NOTE: undocumented function which scratches files We need to use this function because linking unistd.h diff --git a/src/credit_manager.c b/src/credit_manager.c index 51620e3..d4caea7 100644 --- a/src/credit_manager.c +++ b/src/credit_manager.c @@ -13,6 +13,7 @@ #include "general.h" #include "kasse.h" #include "c128time.h" +#include "print.h" static char *filter = NULL; static BYTE filter_len; diff --git a/src/general.c b/src/general.c index 3708d46..aeb1f16 100644 --- a/src/general.c +++ b/src/general.c @@ -34,6 +34,15 @@ char *get_input() { return output; } +char retry_or_quit() { + char *c; + do { + cprintf("\r\nr)etry or q)uit?\r\n"); + c = get_input(); + } while ((*c != 'r') && (*c != 'q')); + return *c; +} + char *format_euro(char *s, int maxlen, int cent){ int tmp = cent; int len = strlen(",EUR"); diff --git a/src/itemz.c b/src/itemz.c index 4558fc5..aec8f4c 100644 --- a/src/itemz.c +++ b/src/itemz.c @@ -16,10 +16,6 @@ #include "config.h" #include "credit_manager.h" -/* NOTE: dummy entry to fix linking */ -int log_num = 0; -int log_lines_written = 0; - static void itemz_print_screen() { BYTE i; char buffer[10]; diff --git a/src/kasse.c b/src/kasse.c index c792c60..b347397 100644 --- a/src/kasse.c +++ b/src/kasse.c @@ -16,15 +16,10 @@ #include "kasse.h" #include "credit_manager.h" #include "c128time.h" +#include "print.h" // 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(); @@ -53,58 +48,6 @@ g) Guthabenverwaltung z) Zeit setzen\r\ f) Freitext verkaufen 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]; - int read = 0; - unsigned int c; - 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)"); - sane_exit(); - } - if (read < 0) { - cprintf("Could not read existing logfile (read returned %d)\n", read); - sane_exit(); - } - strcpy(buffer+read, s); - c = cbm_write((BYTE)8, buffer, read+strlen(s)); - if (c != (read+strlen(s))) { - cprintf("Could not save logfile (wrote %d bytes, wanted %d bytes), please make sure the floppy is not full!\n", c, (read+strlen(s))); - sane_exit(); - } - cbm_close((BYTE)8); - free(buffer); -} - -static char retry_or_quit() { - char *c; - do { - cprintf("\r\nr)etry or q)uit?\r\n"); - c = get_input(); - } while ((*c != 'r') && (*c != 'q')); - return *c; -} - /* Prints a line and logs it to file */ static void print_log(char *name, int item_price, int einheiten, char *nickname, char *rest) { char *time = get_time(); @@ -132,31 +75,6 @@ static void print_log(char *name, int item_price, int einheiten, char *nickname, print_the_buffer(); } -void print_the_buffer() { - BYTE c; -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') - sane_exit(); - - goto RETRY; - } - c = cbm_write((BYTE)4, print_buffer, strlen(print_buffer)); - if (c != strlen(print_buffer)) { - c128_perror(c, "write(printer)"); - if (retry_or_quit() == 'q') { - save_items(); - save_credits(); - exit(1); - } - goto RETRY; - } - cbm_close((BYTE)4); - log_file(print_buffer); -} - /* dialog which is called for each bought item */ static signed int buy(char *name, unsigned int price) { int negative = 1; diff --git a/src/print.c b/src/print.c new file mode 100644 index 0000000..edd295e --- /dev/null +++ b/src/print.c @@ -0,0 +1,88 @@ +/* + * © 2007-2009 phil_fry, sECuRE, sur5r + * See LICENSE for license information + * + */ +#include +#include +#include +#include +#include + +#include "general.h" +#define _IS_PRINT +#include "print.h" + +/* 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); + +void print_the_buffer() { + BYTE c; +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') + exit(1); + + goto RETRY; + } + c = cbm_write((BYTE)4, print_buffer, strlen(print_buffer)); + if (c != strlen(print_buffer)) { + c128_perror(c, "write(printer)"); + if (retry_or_quit() == 'q') + exit(1); + goto RETRY; + } + cbm_close((BYTE)4); + log_file(print_buffer); +} + +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]; + int read = 0; + unsigned int c; + + if (buffer == NULL) { + cprintf("No memory available\n"); + exit(1); + } + + 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)"); + exit(1); + } + 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)"); + exit(1); + } + if (read < 0) { + cprintf("Could not read existing logfile (read returned %d)\n", read); + exit(1); + } + strcpy(buffer+read, s); + c = cbm_write((BYTE)8, buffer, read+strlen(s)); + if (c != (read+strlen(s))) { + cprintf("Could not save logfile (wrote %d bytes, wanted %d bytes), please make sure the floppy is not full!\n", c, (read+strlen(s))); + exit(1); + } + cbm_close((BYTE)8); + free(buffer); +} -- 2.39.5