| 1 |
|
| /* ------------------------------------------------------------------------ |
| 2 |
|
| @NAME : util.c |
| 3 |
|
| @DESCRIPTION: Memory allocation wrappers, the growable string buffer and |
| 4 |
|
| a locale-independent ASCII strcasecmp(). |
| 5 |
|
| -------------------------------------------------------------------------- */ |
| 6 |
|
| |
| 7 |
|
| #include <stdlib.h> |
| 8 |
|
| #include <string.h> |
| 9 |
|
| #include <stdio.h> |
| 10 |
|
| #include "bt_internal.h" |
| 11 |
|
| |
| 12 |
|
| static void |
| 13 |
0 |
| default_oom (void) |
| 14 |
|
| { |
| 15 |
0 |
| fputs ("btparse: out of memory\n", stderr); |
| 16 |
0 |
| abort (); |
| 17 |
|
| } |
| 18 |
|
| |
| 19 |
|
| static void (*oom_handler) (void) = default_oom; |
| 20 |
|
| |
| 21 |
|
| void |
| 22 |
18 |
| bt_set_oom_handler (void (*handler) (void)) |
| 23 |
|
| { |
| 24 |
18 |
▶ | oom_handler = handler ? handler : default_oom; |
Branch: 1 branch
| Branch | True | False | Total |
| oom_handler = handler ? handler : default_oom; |
18 |
0 |
18 |
|
| 25 |
18 |
| } |
| 26 |
|
| |
| 27 |
|
| static void * |
| 28 |
7951 |
| check (void *p) |
| 29 |
|
| { |
| 30 |
7951 |
▶ | if (p == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == NULL) |
0 |
7951 |
7951 |
|
| 31 |
|
| { |
| 32 |
0 |
| oom_handler (); |
| 33 |
0 |
| default_oom (); /* handler must not return */ |
| 34 |
|
| } |
| 35 |
7951 |
| return p; |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
|
| void |
| 39 |
157 |
| bt_free (void *p) |
| 40 |
|
| { |
| 41 |
157 |
| free (p); |
| 42 |
157 |
| } |
| 43 |
|
| |
| 44 |
|
| void * |
| 45 |
3784 |
| bt_xmalloc (size_t n) |
| 46 |
|
| { |
| 47 |
3784 |
▶ | return check (malloc (n ? n : 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| return check (malloc (n ? n : 1)); |
3784 |
0 |
3784 |
|
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| void * |
| 51 |
1890 |
| bt_xcalloc (size_t n, size_t size) |
| 52 |
|
| { |
| 53 |
1890 |
▶ | return check (calloc (n ? n : 1, size ? size : 1)); |
Branch: 2 branches
| Branch | True | False | Total |
| return check (calloc (n ? n : 1, size ? size : 1)); |
1890 |
0 |
1890 |
| return check (calloc (n ? n : 1, size ? size : 1)); |
1890 |
0 |
1890 |
|
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
|
| void * |
| 57 |
2277 |
| bt_xrealloc (void *p, size_t n) |
| 58 |
|
| { |
| 59 |
2277 |
▶ | return check (realloc (p, n ? n : 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| return check (realloc (p, n ? n : 1)); |
2277 |
0 |
2277 |
|
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
|
| char * |
| 63 |
1396 |
| bt_xstrdup (const char *s) |
| 64 |
|
| { |
| 65 |
1396 |
▶ | return s ? bt_xstrndup (s, strlen (s)) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return s ? bt_xstrndup (s, strlen (s)) : NULL; |
1217 |
179 |
1396 |
|
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
|
| char * |
| 69 |
2991 |
| bt_xstrndup (const char *s, size_t n) |
| 70 |
|
| { |
| 71 |
2991 |
| char * d = bt_xmalloc (n + 1); |
| 72 |
2991 |
| memcpy (d, s, n); |
| 73 |
2991 |
| d[n] = 0; |
| 74 |
2991 |
| return d; |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
|
| |
| 78 |
|
| /* ---------------------------------------------------------------------- |
| 79 |
|
| * String buffer |
| 80 |
|
| */ |
| 81 |
|
| |
| 82 |
|
| void |
| 83 |
5148 |
| bt_sbuf_init (bt_sbuf *b) |
| 84 |
|
| { |
| 85 |
5148 |
| b->s = NULL; |
| 86 |
5148 |
| b->len = 0; |
| 87 |
5148 |
| b->cap = 0; |
| 88 |
5148 |
| } |
| 89 |
|
| |
| 90 |
|
| void |
| 91 |
125 |
| bt_sbuf_free (bt_sbuf *b) |
| 92 |
|
| { |
| 93 |
125 |
| free (b->s); |
| 94 |
125 |
| bt_sbuf_init (b); |
| 95 |
125 |
| } |
| 96 |
|
| |
| 97 |
|
| void |
| 98 |
3697 |
| bt_sbuf_clear (bt_sbuf *b) |
| 99 |
|
| { |
| 100 |
3697 |
| b->len = 0; |
| 101 |
3697 |
▶ | if (b->s) |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->s) |
3624 |
73 |
3697 |
|
| 102 |
3624 |
| b->s[0] = 0; |
| 103 |
3697 |
| } |
| 104 |
|
| |
| 105 |
|
| void |
| 106 |
3943 |
| bt_sbuf_grow (bt_sbuf *b, size_t extra) |
| 107 |
|
| { |
| 108 |
3943 |
| size_t need = b->len + extra + 1; |
| 109 |
|
| size_t cap; |
| 110 |
|
| |
| 111 |
3943 |
▶ | if (need <= b->cap) |
Branch: 1 branch
| Branch | True | False | Total |
| if (need <= b->cap) |
1666 |
2277 |
3943 |
|
| 112 |
1666 |
| return; |
| 113 |
2277 |
▶ | cap = b->cap ? b->cap : 64; |
Branch: 1 branch
| Branch | True | False | Total |
| cap = b->cap ? b->cap : 64; |
32 |
2245 |
2277 |
|
| 114 |
2404 |
▶ | while (cap < need) |
Branch: 1 branch
| Branch | True | False | Total |
| while (cap < need) |
127 |
2277 |
2404 |
|
| 115 |
127 |
| cap *= 2; |
| 116 |
2277 |
| b->s = bt_xrealloc (b->s, cap); |
| 117 |
2277 |
| b->cap = cap; |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
|
| void |
| 121 |
1722 |
| bt_sbuf_putn (bt_sbuf *b, const char *s, size_t n) |
| 122 |
|
| { |
| 123 |
1722 |
| bt_sbuf_grow (b, n); |
| 124 |
1722 |
| memcpy (b->s + b->len, s, n); |
| 125 |
1722 |
| b->len += n; |
| 126 |
1722 |
| b->s[b->len] = 0; |
| 127 |
1722 |
| } |
| 128 |
|
| |
| 129 |
|
| void |
| 130 |
112673 |
| bt_sbuf_putc (bt_sbuf *b, char c) |
| 131 |
|
| { |
| 132 |
112673 |
▶ | if (b->len + 2 > b->cap) |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->len + 2 > b->cap) |
97 |
112576 |
112673 |
|
| 133 |
97 |
| bt_sbuf_grow (b, 1); |
| 134 |
112673 |
| b->s[b->len++] = c; |
| 135 |
112673 |
| b->s[b->len] = 0; |
| 136 |
112673 |
| } |
| 137 |
|
| |
| 138 |
|
| void |
| 139 |
166 |
| bt_sbuf_puts (bt_sbuf *b, const char *s) |
| 140 |
|
| { |
| 141 |
166 |
▶ | if (s) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s) |
158 |
8 |
166 |
|
| 142 |
158 |
| bt_sbuf_putn (b, s, strlen (s)); |
| 143 |
166 |
| } |
| 144 |
|
| |
| 145 |
|
| void |
| 146 |
82 |
| bt_sbuf_vprintf (bt_sbuf *b, const char *fmt, va_list ap) |
| 147 |
|
| { |
| 148 |
|
| va_list ap2; |
| 149 |
|
| int n; |
| 150 |
|
| |
| 151 |
82 |
| va_copy (ap2, ap); |
| 152 |
82 |
| n = vsnprintf (NULL, 0, fmt, ap2); |
| 153 |
82 |
| va_end (ap2); |
| 154 |
82 |
▶ | if (n < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) |
0 |
82 |
82 |
|
| 155 |
0 |
| return; |
| 156 |
82 |
| bt_sbuf_grow (b, (size_t) n); |
| 157 |
82 |
| vsnprintf (b->s + b->len, (size_t) n + 1, fmt, ap); |
| 158 |
82 |
| b->len += (size_t) n; |
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
|
| char * |
| 162 |
2124 |
| bt_sbuf_steal (bt_sbuf *b) |
| 163 |
|
| { |
| 164 |
|
| char * s; |
| 165 |
|
| |
| 166 |
2124 |
▶ | if (b->s == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->s == NULL) |
0 |
2124 |
2124 |
|
| 167 |
0 |
| bt_sbuf_grow (b, 0); |
| 168 |
2124 |
| s = b->s; |
| 169 |
2124 |
| s[b->len] = 0; |
| 170 |
2124 |
| bt_sbuf_init (b); |
| 171 |
2124 |
| return s; |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
|
| |
| 175 |
|
| int |
| 176 |
492 |
| bt_ascii_strcasecmp (const char *a, const char *b) |
| 177 |
|
| { |
| 178 |
|
| for (;;) |
| 179 |
192 |
| { |
| 180 |
684 |
▶ | int ca = BT_TOLOWER ((unsigned char) *a); |
Branch: 2 branches
| Branch | True | False | Total |
| int ca = BT_TOLOWER ((unsigned char) *a); |
656 |
28 |
684 |
| int ca = BT_TOLOWER ((unsigned char) *a); |
216 |
440 |
656 |
|
| 181 |
684 |
▶ | int cb = BT_TOLOWER ((unsigned char) *b); |
Branch: 2 branches
| Branch | True | False | Total |
| int cb = BT_TOLOWER ((unsigned char) *b); |
656 |
28 |
684 |
| int cb = BT_TOLOWER ((unsigned char) *b); |
0 |
656 |
656 |
|
| 182 |
684 |
▶ | if (ca != cb || ca == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ca != cb || ca == 0) |
220 |
464 |
684 |
| if (ca != cb || ca == 0) |
28 |
192 |
220 |
|
| 183 |
492 |
| return ca - cb; |
| 184 |
192 |
| a++; |
| 185 |
192 |
| b++; |
| 186 |
|
| } |
| 187 |
|
| } |