-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.c
176 lines (145 loc) · 3.85 KB
/
utils.c
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
#include "utils.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*
* Add a string
*/
int addstrold(char **strp, size_t *strs, const char *format, ...) {
va_list val;
size_t written;
va_start(val, format);
written = vsnprintf(*strp, *strs, format, val);
va_end(val);
if (written >= *strs) {
// buffer full
*strs = 0;
return (1);
}
*strs = (*strs - written);
*strp = (*strp + written);
return (0);
}
int addstr(String *str, const char *format, ...) {
va_list val;
size_t written;
size_t available;
char *pos;
available = str->max - str->len;
pos = str->text + str->len;
va_start(val, format);
written = vsnprintf(pos, available, format, val);
va_end(val);
if (written >= available) {
/* buffer full */
str->text[(str->max)] = 0;
str->len = str->max;
return (1);
}
str->len = str->len + written;
return (0);
}
/*
* Replace troublesome characters in a string with underscores
* - only use for strings we already know the size of */
void strcpy_nospaces(char *dest, char *src) {
static unsigned char allowed[256] = "_________________________________!_#_%__()*+,-.-0123456789_____?@"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ[_]^__abcdefghijklmnopqrstuvwxyz{_}_________"
"__"
"______________________________________________________________________"
"__"
"_______________________________________________";
while (*src) {
*(dest++) = allowed[(unsigned char)*(src++)];
}
*dest = 0;
}
/*
* convert a (possibly large) integer to a string with unit suffix
*
* add the following check to configure.ac
* AC_SEARCH_LIBS(pow, [c m], AC_DEFINE([HAVE_POW]))
*/
int gauge_to_si(u64 bignum, char **str) {
long unsigned int i = 0;
u64 tmpll;
static char units[] = "kMGTPE";
tmpll = bignum;
while ((tmpll /= 1000ULL) && (i < (sizeof(units) - 1))) {
i++;
}
#ifdef HAVE_POW
if (i) {
return asprintf(str, "%0.2f%c", ((double)bignum / pow(1000, i)), units[i - 1]);
}
return asprintf(str, "%Ld", bignum);
#else
return asprintf(str, "%Ld", bignum);
#endif
}
static struct timespec benchmark_start_time;
static char *benchmark_task;
void benchmark_start(char const *format, ...) {
{
va_list args;
va_start(args, format);
int benchmark_task_length = vsnprintf(NULL, 0U, format, args);
va_end(args);
benchmark_task = (char *)malloc(benchmark_task_length + 1);
benchmark_task[benchmark_task_length] = 0;
}
{
va_list args;
va_start(args, format);
vsprintf(benchmark_task, format, args);
va_end(args);
}
fprintf(stderr, "[Starting benchmark] %s\n", benchmark_task);
clock_gettime(CLOCK_MONOTONIC, &benchmark_start_time);
}
void benchmark_end(void) {
{
struct timespec benchmark_end_time;
clock_gettime(CLOCK_MONOTONIC, &benchmark_end_time);
fprintf(stderr, "[Finished benchmark after %f ms] %s\n",
((double)benchmark_end_time.tv_sec * 1000.0 + (double)benchmark_end_time.tv_nsec / 1000000.0) -
((double)benchmark_start_time.tv_sec * 1000.0 + (double)benchmark_start_time.tv_nsec / 1000000.0),
benchmark_task);
}
free(benchmark_task);
}
char *implode(char const *glue, char **pieces) {
size_t total_len = 0U;
char **walk_pieces = pieces;
while (*walk_pieces != NULL) {
total_len += strlen(*walk_pieces++);
}
ptrdiff_t walk_pieces_diff = walk_pieces - pieces;
if (walk_pieces_diff >= 2) {
total_len += strlen(glue) * (size_t)(walk_pieces_diff - 1);
}
char *result = (char *)malloc(total_len + 1U);
if (walk_pieces_diff > 0) {
strcpy(result, *pieces);
if (walk_pieces_diff >= 2) {
char *walk_result = result;
walk_pieces = pieces + 1;
while (*walk_pieces != NULL) {
while (*walk_result) {
++walk_result;
}
strcpy(walk_result, glue);
while (*walk_result) {
++walk_result;
}
strcpy(walk_result, *walk_pieces++);
}
}
} else {
*result = 0;
}
return result;
}