forked from BRL-CAD/brlcad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnum.c
More file actions
184 lines (157 loc) · 4.43 KB
/
Copy pathnum.c
File metadata and controls
184 lines (157 loc) · 4.43 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
/* N U M . C
* BRL-CAD
*
* Copyright (c) 2019-2026 United States Government as represented by
* the U.S. Army Research Laboratory.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this file; see the file named COPYING for more
* information.
*/
#include "common.h"
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <stdio.h>
#include <math.h>
static size_t
char_length(int64_t num)
{
uint64_t pos = labs((long)num);
return
((pos < 10L) ? 1 :
((pos < 100L) ? 2 :
((pos < 1000L) ? 3 :
((pos < 10000L) ? 4 :
((pos < 100000L) ? 5 :
((pos < 1000000L) ? 6 :
((pos < 10000000L) ? 7 :
((pos < 100000000L) ? 8 :
((pos < 1000000000L) ? 9 :
((pos < 10000000000L) ? 10 :
((pos < 100000000000L) ? 11 :
((pos < 1000000000000L) ? 12 :
((pos < 10000000000000L) ? 13 :
((pos < 100000000000000L) ? 14 :
((pos < 1000000000000000L) ? 15 :
((pos < 10000000000000000L) ? 16 :
((pos < 100000000000000000L) ? 17 :
((pos < 1000000000000000000L) ? 18 :
19)))))))))))))))))) +
((num < 0) ? 1 : 0);
}
void
bu_num_print(const double *vals, size_t nvals, size_t cols, const char *tbl_start, const char *row_start, const char *num_fmt, const char *col_sep, const char *row_end, const char *tbl_end)
{
size_t i;
size_t j;
#define MAXCOLS 64
size_t preallocated_colsizes[MAXCOLS] = {0};
size_t preallocated_colchars[MAXCOLS] = {0};
size_t *colsizes = preallocated_colsizes;
size_t *colchars = preallocated_colchars;
size_t padding = 0;
if (!vals || nvals == 0 || cols == 0)
return;
if (!col_sep)
col_sep = " ";
if (cols > MAXCOLS) {
/* resort to dynamic memory if necessary */
colsizes = (size_t *)calloc(cols, sizeof(size_t));
colchars = (size_t *)calloc(cols, sizeof(size_t));
}
if (tbl_start) {
printf("%s", tbl_start);
while (tbl_start[strlen(tbl_start)-(padding+1)] != '\n' && padding != strlen(tbl_start)) {
padding++;
}
}
if ((row_start && strchr(row_start, '\n')) || (row_end && strchr(row_end, '\n'))) {
/* if spanning lines, get column sizes for pretty-printing */
i = 0;
do {
char num[3 + DBL_MANT_DIG - DBL_MIN_EXP + 1]; /* max double digits plus nul */
size_t numlen;
size_t charlen;
if (num_fmt) {
snprintf(num, sizeof(num), num_fmt, vals[i]);
} else {
snprintf(num, sizeof(num), "%.17g", vals[i]);
}
numlen = strlen(num);
charlen = char_length(lround(floor(vals[i])));
if (colsizes[i % cols] < numlen)
colsizes[i % cols] = numlen;
if (colchars[i % cols] < charlen)
colchars[i % cols] = charlen;
i++;
} while (i < nvals);
}
/* print our values */
i = 0;
do {
char ifmt[128] = "%%%d.17g";
char ofmt[128] = {0};
if ((i % cols == 0) && row_start) {
if (i > 0) {
for (j = 0; j < padding; j++) {
printf(" ");
}
}
printf("%s", row_start);
}
/* if we run out of values, pad with negative zeros */
if (num_fmt) {
printf(num_fmt, (i < nvals) ? vals[i] : -0.0);
} else {
snprintf(ofmt, sizeof(ofmt), ifmt, colsizes[i % cols]);
printf(ofmt, (i < nvals) ? vals[i] : -0.0);
}
i++;
if (i < nvals || i % cols != 0) {
if ((i % cols == 0) && row_end) {
printf("%s", row_end);
} else if (col_sep) {
printf("%s", col_sep);
}
}
} while (i < nvals || i % cols != 0);
/* finish the row if partial */
while (i % cols != 0) {
printf("X");
i++;
if (i < nvals) {
if ((i % cols == 0) && row_end) {
printf("%s", row_end);
} else if (col_sep) {
printf("%s", col_sep);
}
}
}
if (tbl_end)
printf("%s", tbl_end);
/* release if we had to resort to dynamic memory */
if (colsizes != preallocated_colsizes)
free(colsizes);
if (colchars != preallocated_colchars)
free(colchars);
return;
}
/*
* Local Variables:
* mode: C
* tab-width: 8
* indent-tabs-mode: t
* c-file-style: "stroustrup"
* End:
* ex: shiftwidth=4 tabstop=8
*/