]> git.sur5r.net Git - c128-kasse/blob - src/print.c
kasse: allow starting up without a printer
[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 #include "globals.h"
16
17 /* NOTE: undocumented function which scratches files
18    We need to use this function because linking unistd.h
19    makes our program break at runtime.
20  */
21 unsigned char __fastcall__ _sysremove(const char *name);
22
23 /* 8192 bytes of log buffer on the heap used for storing one entire logfile in
24  * memory before writing (unless flushed). */
25 char *log_heap_buf;
26 int log_heap_offset = 0;
27 int log_heap_flushed = 0;
28
29 const int LOG_SIZE = 8192;
30
31 void init_log(void) {
32   log_heap_buf = malloc(sizeof(char) * LOG_SIZE);
33   if (log_heap_buf == NULL) {
34     cprintf("malloc(log_heap_buf) failed");
35     exit(1);
36   }
37 }
38
39 void print_the_buffer(void) {
40   BYTE status;
41   char *c;
42
43   if (!printing)
44     return;
45
46 RETRY:
47   status = cbm_open((BYTE)4, (BYTE)4, (BYTE)0, NULL);
48   if (status != 0) {
49     c128_perror(status, "cbm_open(printer)");
50
51     do {
52       cprintf("\r\nr)etry, c)ontinue or q)uit?\r\n");
53       c = get_input();
54     } while ((*c != 'r') && (*c != 'c') && (*c != 'q'));
55
56     if (*c == 'q')
57       exit(1);
58     else if (*c == 'c') {
59       printing = 0;
60       return;
61     }
62
63     goto RETRY;
64   }
65   status = cbm_write((BYTE)4, print_buffer, strlen(print_buffer));
66   if (status != strlen(print_buffer)) {
67     c128_perror(status, "write(printer)");
68     if (retry_or_quit() == 'q')
69       exit(1);
70     goto RETRY;
71   }
72   cbm_close((BYTE)4);
73   log_file(print_buffer);
74 }
75
76 void print_header(void) {
77   sprintf(print_buffer, "%c----------------------------------------------------"
78                         "----------------------------\r",
79           17);
80   print_the_buffer();
81
82   sprintf(
83       print_buffer,
84       "%c#kauf Uhrzeit  - Ding      -     Preis - Restguthbn - # - Nickname\r",
85       17);
86   print_the_buffer();
87
88   sprintf(print_buffer, "%c----------------------------------------------------"
89                         "----------------------------\r",
90           17);
91   print_the_buffer();
92 }
93
94 /*
95  * Flushes the current log buffer to disk. Called either by log_file() when one
96  * entire log file is completed or interactively.
97  *
98  */
99 void log_flush(void) {
100   int c;
101   static char filename[8];
102   sprintf(filename, "log-%d", log_num);
103
104   /* If we have written to this logfile before, we need to remove it first */
105   if (log_heap_flushed > 0)
106     _sysremove(filename);
107
108   if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, filename)) != 0) {
109     c128_perror(c, "cbm_open(log)");
110     exit(1);
111   }
112   c = cbm_write((BYTE)8, log_heap_buf, log_heap_offset);
113   if (c != log_heap_offset) {
114     textcolor(TC_LIGHT_RED);
115     cprintf("\r\nCould not save logfile (wrote %d bytes, wanted %d bytes), "
116             "please make sure the floppy is not full!\n",
117             c, log_heap_offset);
118     c128_perror(c, "cbm_write");
119     exit(1);
120   }
121   cbm_close((BYTE)8);
122
123   log_heap_flushed = log_heap_offset;
124 }
125
126 /*
127  * Logs to memory and eventually to file (log_flush() is called when one entire
128  * logfile was completed in memory).
129  *
130  */
131 void log_file(const char *s) {
132   strcpy(log_heap_buf + log_heap_offset, s);
133   log_heap_offset += strlen(s);
134
135   /* Force a flush when there are only five lines left */
136   if (log_heap_offset > (LOG_SIZE - (5 * 80))) {
137     log_flush();
138     log_num++;
139     log_heap_offset = 0;
140     log_heap_flushed = 0;
141   }
142 }