|
|
tutil.c - ploot - simple plotting tools |
|
|
 |
git clone git://bitreich.org/ploot git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ploot (git://bitreich.org) |
|
|
 |
Log |
|
|
 |
Files |
|
|
 |
Refs |
|
|
 |
Tags |
|
|
 |
README |
|
|
|
--- |
|
|
|
tutil.c (1510B) |
|
|
|
--- |
|
|
|
1 #include "util.h" |
|
|
|
2 |
|
|
|
3 #include <ctype.h> |
|
|
|
4 #include <errno.h> |
|
|
|
5 #include <limits.h> |
|
|
|
6 #include <stdarg.h> |
|
|
|
7 #include <stdio.h> |
|
|
|
8 #include <stdlib.h> |
|
|
|
9 #include <string.h> |
|
|
|
10 |
|
|
|
11 size_t |
|
|
|
12 strlcpy(char *buf, const char *str, size_t sz) |
|
|
|
13 { |
|
|
|
14 size_t len, cpy; |
|
|
|
15 |
|
|
|
16 cpy = ((len = strlen(str)) > sz) ? (sz) : (len); |
|
|
|
17 memcpy(buf, str, cpy); |
|
|
|
18 buf[sz - 1] = '\0'; |
|
|
|
19 return len; |
|
|
|
20 } |
|
|
|
21 |
|
|
|
22 void |
|
|
|
23 put3utf(long rune) |
|
|
|
24 { |
|
|
|
25 putchar((char)(0xe0 | (0x0f & (rune >> 12)))); /* 1110xxxx */ |
|
|
|
26 putchar((char)(0x80 | (0x3f & (rune >> 6)))); /* 10xxxxxx */ |
|
|
|
27 putchar((char)(0x80 | (0x3f & (rune)))); /* 10xxxxxx */ |
|
|
|
28 } |
|
|
|
29 |
|
|
|
30 char * |
|
|
|
31 strsep(char **strp, const char *sep) |
|
|
|
32 { |
|
|
|
33 char *s, *prev; |
|
|
|
34 |
|
|
|
35 if (*strp == NULL) |
|
|
|
36 return NULL; |
|
|
|
37 for (s = prev = *strp; strchr(sep, *s) == NULL; s++); |
|
|
|
38 if (*s == '\0') { |
|
|
|
39 *strp = NULL; |
|
|
|
40 return prev; |
|
|
|
41 } |
|
|
|
42 *s = '\0'; |
|
|
|
43 *strp = s + 1; |
|
|
|
44 |
|
|
|
45 return prev; |
|
|
|
46 } |
|
|
|
47 |
|
|
|
48 void |
|
|
|
49 strchomp(char *s) |
|
|
|
50 { |
|
|
|
51 char *x = s + strlen(s); |
|
|
|
52 |
|
|
|
53 while (--x >= s && (*x == '\r' || *x == '\n')) |
|
|
|
54 *x = '\0'; |
|
|
|
55 } |
|
|
|
56 |
|
|
|
57 /* |
|
|
|
58 * Set 'str' to a human-readable form of 'num' with always a width of 8 (+1 for |
|
|
|
59 * the '\0' terminator). Buffer overflow is ensured not to happen due to the |
|
|
|
60 * max size of a double. Return the exponent. |
|
|
|
61 */ |
|
|
|
62 int |
|
|
|
63 humanize(char *str, double val) |
|
|
|
64 { |
|
|
|
65 int exp, precision; |
|
|
|
66 char label[] = { '\0', 'M', 'G', 'T', 'E' }; |
|
|
|
67 |
|
|
|
68 for (exp = 0; ABS(val) > 1000; exp++) |
|
|
|
69 val /= 1000; |
|
|
|
70 |
|
|
|
71 precision = (ABS(val) < 10) ? 2 : (ABS(val) < 100) ? 1 : 0; |
|
|
|
72 precision += (exp == 0); |
|
|
|
73 |
|
|
|
74 snprintf(str, 9, "%+.*f %c", precision, val, label[exp]); |
|
|
|
75 str[8] = '\0'; |
|
|
|
76 if (val >= 0) |
|
|
|
77 str[0] = ' '; |
|
|
|
78 |
|
|
|
79 return exp * 3; |
|
|
|
80 } |
|