]> git.sur5r.net Git - c128-kasse/blob - src/print.c
log to memory
[c128-kasse] / src / print.c
1 /*
2  * © 2007-2009 phil_fry, sECuRE, sur5r
3  * See LICENSE for license information
4  *
5  */
6 #include <stdio.h>
7 #include <conio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <cbm.h>
11
12 #include "general.h"
13 #define _IS_PRINT
14 #include "print.h"
15
16 /* NOTE: undocumented function which scratches files
17    We need to use this function because linking unistd.h
18    makes our program break at runtime.
19  */
20 unsigned char __fastcall__ _sysremove(const char *name);
21
22 /* 8192 bytes of log buffer on the heap used for storing one entire logfile in
23  * memory before writing (unless flushed). */
24 char *log_heap_buf;
25 int log_heap_offset = 0;
26 int log_heap_flushed = 0;
27
28 const int LOG_SIZE = 8192;
29
30 void init_log() {
31         log_heap_buf = malloc(sizeof(char) * LOG_SIZE);
32         if (log_heap_buf == NULL) {
33                 cprintf("malloc(log_heap_buf) failed");
34                 exit(1);
35         }
36 }
37
38 void print_the_buffer() {
39         BYTE c;
40 RETRY:
41         c = cbm_open((BYTE)4, (BYTE)4, (BYTE)0, NULL);
42         if (c != 0) {
43                 c128_perror(c, "cbm_open(printer)");
44                 if (retry_or_quit() == 'q')
45                         exit(1);
46
47                 goto RETRY;
48         }
49         c = cbm_write((BYTE)4, print_buffer, strlen(print_buffer));
50         if (c != strlen(print_buffer)) {
51                 c128_perror(c, "write(printer)");
52                 if (retry_or_quit() == 'q')
53                         exit(1);
54                 goto RETRY;
55         }
56         cbm_close((BYTE)4);
57         log_file(print_buffer);
58 }
59
60 void print_header() {
61         sprintf(print_buffer, "%c--------------------------------------------------------------------------------\r", 17);
62         print_the_buffer();
63
64         sprintf(print_buffer, "%c#kauf Uhrzeit  - Ding      -     Preis - Restguthbn - # - Nickname\r", 17);
65         print_the_buffer();
66
67         sprintf(print_buffer, "%c--------------------------------------------------------------------------------\r", 17);
68         print_the_buffer();
69
70 }
71
72 /*
73  * Flushes the current log buffer to disk. Called either by log_file() when one
74  * entire log file is completed or interactively.
75  *
76  */
77 void log_flush(void) {
78         int c;
79         static char filename[8];
80         sprintf(filename, "log-%d", log_num);
81
82         /* If we have written to this logfile before, we need to remove it first */
83         if (log_heap_flushed > 0)
84                 _sysremove(filename);
85
86         if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, filename)) != 0) {
87                 c128_perror(c, "cbm_open(log)");
88                 exit(1);
89         }
90         c = cbm_write((BYTE)8, log_heap_buf, log_heap_offset);
91         if (c != log_heap_offset) {
92                 textcolor(TC_LIGHT_RED);
93                 cprintf("\r\nCould not save logfile (wrote %d bytes, wanted %d bytes), please make sure the floppy is not full!\n", c, log_heap_offset);
94                 c128_perror(c, "cbm_write");
95                 exit(1);
96         }
97         cbm_close((BYTE)8);
98
99         log_heap_flushed = log_heap_offset;
100 }
101
102 /*
103  * Logs to memory and eventually to file (log_flush() is called when one entire
104  * logfile was completed in memory).
105  *
106  */
107 void log_file(const char *s) {
108         strcpy(log_heap_buf+log_heap_offset, s);
109         log_heap_offset += strlen(s);
110
111         /* Force a flush when there are only five lines left */
112         if (log_heap_offset > (LOG_SIZE - (5 * 80))) {
113                 log_flush();
114                 log_num++;
115                 log_heap_offset = 0;
116                 log_heap_flushed = 0;
117         }
118 }