]> git.sur5r.net Git - c128-kasse/blob - src/print.c
Format code using clang-format-3.9 (“make format”)
[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(void) {
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(void) {
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(void) {
61   sprintf(print_buffer, "%c----------------------------------------------------"
62                         "----------------------------\r",
63           17);
64   print_the_buffer();
65
66   sprintf(
67       print_buffer,
68       "%c#kauf Uhrzeit  - Ding      -     Preis - Restguthbn - # - Nickname\r",
69       17);
70   print_the_buffer();
71
72   sprintf(print_buffer, "%c----------------------------------------------------"
73                         "----------------------------\r",
74           17);
75   print_the_buffer();
76 }
77
78 /*
79  * Flushes the current log buffer to disk. Called either by log_file() when one
80  * entire log file is completed or interactively.
81  *
82  */
83 void log_flush(void) {
84   int c;
85   static char filename[8];
86   sprintf(filename, "log-%d", log_num);
87
88   /* If we have written to this logfile before, we need to remove it first */
89   if (log_heap_flushed > 0)
90     _sysremove(filename);
91
92   if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, filename)) != 0) {
93     c128_perror(c, "cbm_open(log)");
94     exit(1);
95   }
96   c = cbm_write((BYTE)8, log_heap_buf, log_heap_offset);
97   if (c != log_heap_offset) {
98     textcolor(TC_LIGHT_RED);
99     cprintf("\r\nCould not save logfile (wrote %d bytes, wanted %d bytes), "
100             "please make sure the floppy is not full!\n",
101             c, log_heap_offset);
102     c128_perror(c, "cbm_write");
103     exit(1);
104   }
105   cbm_close((BYTE)8);
106
107   log_heap_flushed = log_heap_offset;
108 }
109
110 /*
111  * Logs to memory and eventually to file (log_flush() is called when one entire
112  * logfile was completed in memory).
113  *
114  */
115 void log_file(const char *s) {
116   strcpy(log_heap_buf + log_heap_offset, s);
117   log_heap_offset += strlen(s);
118
119   /* Force a flush when there are only five lines left */
120   if (log_heap_offset > (LOG_SIZE - (5 * 80))) {
121     log_flush();
122     log_num++;
123     log_heap_offset = 0;
124     log_heap_flushed = 0;
125   }
126 }