]> git.sur5r.net Git - c128-kasse/blob - src/print.c
006967b28314c8f710cebc0ee921f204850574ce
[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 void print_the_buffer() {
23         BYTE c;
24 RETRY:
25         c = cbm_open((BYTE)4, (BYTE)4, (BYTE)0, NULL);
26         if (c != 0) {
27                 c128_perror(c, "cbm_open(printer)");
28                 if (retry_or_quit() == 'q')
29                         exit(1);
30
31                 goto RETRY;
32         }
33         c = cbm_write((BYTE)4, print_buffer, strlen(print_buffer));
34         if (c != strlen(print_buffer)) {
35                 c128_perror(c, "write(printer)");
36                 if (retry_or_quit() == 'q')
37                         exit(1);
38                 goto RETRY;
39         }
40         cbm_close((BYTE)4);
41         log_file(print_buffer);
42 }
43
44 void print_header() {
45         sprintf(print_buffer, "%c-----------------------------------------------------------------\r", 17);
46         print_the_buffer();
47
48         sprintf(print_buffer, "%c#kauf Uhrzeit  - Ding      - Preis    - Restguthab - # - Nickname\r", 17);
49         print_the_buffer();
50
51         sprintf(print_buffer, "%c-----------------------------------------------------------------\r", 17);
52         print_the_buffer();
53
54 }
55
56 void log_file(const char *s) {
57         /* A log-entry has usually 50 bytes, so we take 64 bytes.
58            Because files are wrapped (log.0, log.1, ...) every 100
59            lines, we don't need more than 100 * 64 bytes. */
60         char *buffer = malloc(sizeof(char) * 64 * 100);
61         char filename[8];
62         int read = 0;
63         unsigned int c;
64
65         if (buffer == NULL) {
66                 cprintf("No memory available\n");
67                 exit(1);
68         }
69
70         buffer[0] = '\0';
71         if (((++log_lines_written) % 100) == 0)
72                 log_num++;
73         sprintf(filename, "log-%d", log_num);
74         /* Don't read log if there were no lines written before */
75         if (log_lines_written != 1) {
76                 if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)0, filename)) != 0) {
77                         c128_perror(c, "cbm_open(log)");
78                         exit(1);
79                 }
80                 read = cbm_read((BYTE)8, buffer, sizeof(char) * 64 * 100);
81                 cbm_close((BYTE)8);
82                 _sysremove(filename);
83         }
84         if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, filename)) != 0) {
85                 c128_perror(c, "cbm_open(log)");
86                 exit(1);
87         }
88         if (read < 0) {
89                 cprintf("Could not read existing logfile (read returned %d)\n", read);
90                 exit(1);
91         }
92         strcpy(buffer+read, s);
93         c = cbm_write((BYTE)8, buffer, read+strlen(s));
94         if (c != (read+strlen(s))) {
95                 cprintf("Could not save logfile (wrote %d bytes, wanted %d bytes), please make sure the floppy is not full!\n", c, (read+strlen(s)));
96                 exit(1);
97         }
98         cbm_close((BYTE)8);
99         free(buffer);
100 }