-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.c
More file actions
188 lines (172 loc) · 3.99 KB
/
print.c
File metadata and controls
188 lines (172 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2024 Fabian Stiewitz <fabian (at) stiewitz.pw>
// Licensed under the EUPL-1.2
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<assert.h>
static long c = 0;
#define D 6
#define E 2
#define LT 16
#define ST 4
#define LL (5 * LT)
void PRINT__nl() {
c = 0;
fputc('\n', stdout);
fflush(stdout);
}
void PRINT__string(char* str) {
if(!str) return;
long s = strlen(str);
if(c + s > LL) {
fputc('\n', stdout);
c = s;
} else {
c += s;
}
fputs(str, stdout);
fflush(stdout);
}
static void PRINT__slurp_zeroes_e(char* buffer) {
char* e = strchr(buffer, 'E');
if(!e) return;
char* b = e + 1;
int i;
int j = 1;
if(*b == '+' || *b == '-') {
++b;
++j;
}
while(*b && *b == '0') ++b;
if(*b && *(b - 1) == '0') {
for(i = 0; i < (b - e); ++i) {
*(e + j + i) = *(b + i);
}
*(e + j + i) = 0;
}
for (b = e - 1; b != buffer; --b) {
if (*b != '0') break;
}
++b;
long d = e - b;
if(d == 0) return;
for(i = 0; i < strlen(e); ++i) {
*(b + i) = *(e + i);
}
*(e + strlen(e) - d) = 0;
}
static void PRINT__slurp_zeroes(char* buffer) {
char* e = strchr(buffer, '.');
if(!e) return;
for (char* b = buffer + strlen(buffer) - 2; b != buffer; --b) {
if (*b == '0') {
*b = ' ';
*(b + 1) = 0;
} else break;
}
e = buffer + strlen(buffer) - 2;
if(*e == '.') {
*e = ' ';
*(e + 1) = 0;
}
}
void PRINT__numberd(double f) {
if(isinf(f)) {
if(f < 0) PRINT__string("-INF ");
else PRINT__string("INF ");
} else if(isnan(f)) {
PRINT__string("NAN ");
} else if(f == 0) {
PRINT__string(" 0 ");
} else {
char buffer[64];
char *b = buffer;
if (f < 0) {
*(b++) = '-';
f = -f;
} else {
*(b++) = ' ';
}
snprintf(b, 64 - (b - buffer), "%#.10E ", (double)f);
char* d = strchr(b, '.');
char* e = strchr(b, 'E');
assert(d && e);
long exp = atol(e + 1);
if(exp < 0 && (e - d - 6) - exp <= D) {
snprintf(b, 64 - (b - buffer), "%#6.6F", f);
d = strchr(b, '.');
memmove(b, d, strlen(b) - (d - b));
PRINT__slurp_zeroes(buffer);
} else if(exp >= 0 && exp + 1 <= D) {
snprintf(b, 64 - (b - buffer), "%#6.6F", f);
PRINT__slurp_zeroes(buffer);
} else {
snprintf(b, 64 - (b - buffer), "%#6.7E ", f);
PRINT__slurp_zeroes_e(b);
}
PRINT__string(buffer);
}
fflush(stdout);
}
void PRINT__numberl(long l) {
PRINT__numberd((double)l);
fflush(stdout);
}
void PRINT__tab() {
long d = LT;
if(c % LT) {
d = LT - (c % LT);
}
c += d;
if(c >= LL) {
fputc('\n', stdout);
c = 0;
} else {
for (long i = 0; i < d; ++i) {
fputc(' ', stdout);
}
}
fflush(stdout);
}
void TAB__l(long t) {
if(t <= 0) {
fprintf(stderr, "warning: invalid TAB argument (%li)\n", t);
t = 1;
}
--t;
if(c > t) {
fputc('\n', stdout);
c = 0;
}
if(c < t) {
for(long i = 0; i < (t - c); ++i) {
fputc(' ', stdout);
}
c = t;
}
}
void TAB__d(double d) {
long t = round(d);
if(d < 0) {
fprintf(stderr, "warning: invalid TAB argument (%li)\n", t);
t = 1;
}
if(isinf(d)) {
fprintf(stderr, "warning: invalid TAB argument (%f)\n", d);
t = 1;
}
if(isnan(d)) {
fprintf(stderr, "warning: invalid TAB argument (%f)\n", d);
t = 1;
}
if(t <= 0) {
fprintf(stderr, "warning: invalid TAB argument (%li)\n", t);
t = 1;
}
TAB__l(t);
}
double DBG_print(double a, double b, double c, double d) {
fprintf(stdout, "DBG %f\t%f\t%f\t%f\n", a, b, c, d);
return a;
}