]> git.sur5r.net Git - c128-kasse/blob - src/print.c
Move logging/printing functions to src/print.c, fix compilation of itemz
[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 log_file(const char *s) {
45         /* A log-entry has usually 50 bytes, so we take 64 bytes.
46            Because files are wrapped (log.0, log.1, ...) every 100
47            lines, we don't need more than 100 * 64 bytes. */
48         char *buffer = malloc(sizeof(char) * 64 * 100);
49         char filename[8];
50         int read = 0;
51         unsigned int c;
52
53         if (buffer == NULL) {
54                 cprintf("No memory available\n");
55                 exit(1);
56         }
57
58         buffer[0] = '\0';
59         if (((++log_lines_written) % 100) == 0)
60                 log_num++;
61         sprintf(filename, "log-%d", log_num);
62         /* Don't read log if there were no lines written before */
63         if (log_lines_written != 1) {
64                 if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)0, filename)) != 0) {
65                         c128_perror(c, "cbm_open(log)");
66                         exit(1);
67                 }
68                 read = cbm_read((BYTE)8, buffer, sizeof(char) * 64 * 100);
69                 cbm_close((BYTE)8);
70                 _sysremove(filename);
71         }
72         if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, filename)) != 0) {
73                 c128_perror(c, "cbm_open(log)");
74                 exit(1);
75         }
76         if (read < 0) {
77                 cprintf("Could not read existing logfile (read returned %d)\n", read);
78                 exit(1);
79         }
80         strcpy(buffer+read, s);
81         c = cbm_write((BYTE)8, buffer, read+strlen(s));
82         if (c != (read+strlen(s))) {
83                 cprintf("Could not save logfile (wrote %d bytes, wanted %d bytes), please make sure the floppy is not full!\n", c, (read+strlen(s)));
84                 exit(1);
85         }
86         cbm_close((BYTE)8);
87         free(buffer);
88 }