Debian Codegraph @

contrib/toacme/src/io.c

raw symbols SCIP
1 // ToACME - converts other source codes to ACME format.
2 // Copyright (C) 1999-2006 Marco Baye
3 // Have a look at "main.c" for further info
4 //
5 // input/output
6
7 #include <stdio.h>
8 #include "config.h"
9 #include "io.h"
10
11
12 // variables
13 int padding_value;
14 FILE *global_input_stream;
15 FILE *global_output_stream;
16 int GotByte = 0;
17 bool IO_reached_eof = FALSE;
18
19
20 // input functions
21
22
23 // set byte sent after EOF
24 inline void IO_set_input_padding(int pad)
25 {
26 padding_value = pad;
27 }
28
29
30 // fetch and buffer byte
31 int IO_get_byte(void)
32 {
33 int w;
34
35 if (IO_reached_eof) {
36 GotByte = padding_value;
37 } else {
38 w = getc(global_input_stream);
39 if (w == EOF)
40 IO_reached_eof = TRUE;
41 GotByte = w;
42 }
43 return GotByte;
44 }
45
46
47 // read little-endian 16-bit value
48 unsigned int IO_get_le16(void)
49 {
50 unsigned int result = IO_get_byte();
51
52 // CAUTION! using
53 // return(IO_get_byte() | (IO_get_byte() << 8));
54 // would be compiler-dependent
55 return result | (IO_get_byte() << 8);
56 }
57
58
59 // read little-endian 24-bit value
60 unsigned int IO_get_le24(void)
61 {
62 unsigned int result = IO_get_le16();
63
64 // CAUTION! see above
65 return result | (IO_get_byte() << 16);
66 }
67
68
69 // output functions
70
71
72 // output string
73 inline void IO_put_string(const char string[])
74 {
75 fputs(string, global_output_stream);
76 }
77
78
79 // write byte to output file
80 inline void IO_put_byte(char b)
81 {
82 putc(b, global_output_stream);
83 }
84
85
86 // output low nibble of argument as hexadecimal digit
87 static void put_low_nibble_hex(int v)
88 {
89 putc("0123456789abcdef"[v & 15], global_output_stream);
90 }
91
92
93 // output low byte of argument as two hexadecimal digits
94 void IO_put_low_byte_hex(int v)
95 {
96 put_low_nibble_hex(v >> 4);
97 put_low_nibble_hex(v);
98 }
99
100
101 // output low 16 bits of arg as four hexadecimal digits
102 void IO_put_low_16b_hex(int w)
103 {
104 IO_put_low_byte_hex(w >> 8);
105 IO_put_low_byte_hex(w);
106 }
107
108
109 // read load address from input file and write as comment to output file
110 void IO_process_load_address(void)
111 {
112 int load_address;
113
114 load_address = IO_get_le16();
115 IO_put_string("; ToACME: Original source code file had load address $");
116 IO_put_low_16b_hex(load_address);
117 IO_put_byte('\n');
118 }