| 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 |
#ifndef KSTRING_H |
| 28 |
#define KSTRING_H |
| 29 |
|
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
#include <stdarg.h> |
| 33 |
#include <stdint.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <limits.h> |
| 36 |
#include <sys/types.h> |
| 37 |
|
| 38 |
#include "hts_defs.h" |
| 39 |
|
| 40 |
#ifndef kroundup32 |
| 41 |
#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) |
| 42 |
#endif |
| 43 |
|
| 44 |
#ifndef kroundup_size_t |
| 45 |
#define kroundup_size_t(x) (--(x), \ |
| 46 |
(x)|=(x)>>(sizeof(size_t)/8), \ |
| 47 |
(x)|=(x)>>(sizeof(size_t)/4), \ |
| 48 |
(x)|=(x)>>(sizeof(size_t)/2), \ |
| 49 |
(x)|=(x)>>(sizeof(size_t)), \ |
| 50 |
(x)|=(x)>>(sizeof(size_t)*2), \ |
| 51 |
(x)|=(x)>>(sizeof(size_t)*4), \ |
| 52 |
++(x)) |
| 53 |
#endif |
| 54 |
|
| 55 |
#if defined __GNUC__ && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)) |
| 56 |
#ifdef __MINGW_PRINTF_FORMAT |
| 57 |
#define KS_ATTR_PRINTF(fmt, arg) __attribute__((__format__ (__MINGW_PRINTF_FORMAT, fmt, arg))) |
| 58 |
#else |
| 59 |
#define KS_ATTR_PRINTF(fmt, arg) __attribute__((__format__ (__printf__, fmt, arg))) |
| 60 |
#endif |
| 61 |
#else |
| 62 |
#define KS_ATTR_PRINTF(fmt, arg) |
| 63 |
#endif |
| 64 |
|
| 65 |
#ifndef HAVE___BUILTIN_CLZ |
| 66 |
#if defined __GNUC__ && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) |
| 67 |
#define HAVE___BUILTIN_CLZ 1 |
| 68 |
#endif |
| 69 |
#endif |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
#ifndef KSTRING_T |
| 80 |
#define KSTRING_T kstring_t |
| 81 |
typedef struct kstring_t { |
| 82 |
size_t l, m; |
| 83 |
char *s; |
| 84 |
} kstring_t; |
| 85 |
#endif |
| 86 |
|
| 87 |
typedef struct { |
| 88 |
uint64_t tab[4]; |
| 89 |
int sep, finished; |
| 90 |
const char *p; |
| 91 |
} ks_tokaux_t; |
| 92 |
|
| 93 |
#ifdef __cplusplus |
| 94 |
extern "C" { |
| 95 |
#endif |
| 96 |
|
| 97 |
HTSLIB_EXPORT |
| 98 |
int kvsprintf(kstring_t *s, const char *fmt, va_list ap) KS_ATTR_PRINTF(2,0); |
| 99 |
|
| 100 |
HTSLIB_EXPORT |
| 101 |
int ksprintf(kstring_t *s, const char *fmt, ...) KS_ATTR_PRINTF(2,3); |
| 102 |
|
| 103 |
HTSLIB_EXPORT |
| 104 |
int kputd(double d, kstring_t *s); |
| 105 |
|
| 106 |
HTSLIB_EXPORT |
| 107 |
int ksplit_core(char *s, int delimiter, int *_max, int **_offsets); |
| 108 |
|
| 109 |
HTSLIB_EXPORT |
| 110 |
char *kstrstr(const char *str, const char *pat, int **_prep); |
| 111 |
|
| 112 |
HTSLIB_EXPORT |
| 113 |
char *kstrnstr(const char *str, const char *pat, int n, int **_prep); |
| 114 |
|
| 115 |
HTSLIB_EXPORT |
| 116 |
void *kmemmem(const void *_str, int n, const void *_pat, int m, int **_prep); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
HTSLIB_EXPORT |
| 123 |
char *kstrtok(const char *str, const char *sep, ks_tokaux_t *aux); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
typedef char *kgets_func(char *, int, void *); |
| 130 |
HTSLIB_EXPORT |
| 131 |
int kgetline(kstring_t *s, kgets_func *fgets, void *fp); |
| 132 |
|
| 133 |
|
| 134 |
typedef ssize_t kgets_func2(char *, size_t, void *); |
| 135 |
HTSLIB_EXPORT |
| 136 |
int kgetline2(kstring_t *s, kgets_func2 *fgets, void *fp); |
| 137 |
|
| 138 |
#ifdef __cplusplus |
| 139 |
} |
| 140 |
#endif |
| 141 |
|
| 142 |
|
| 143 |
#define KS_INITIALIZE { 0, 0, NULL } |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static inline void ks_initialize(kstring_t *s) |
| 152 |
{ |
| 153 |
s->l = s->m = 0; |
| 154 |
s->s = NULL; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
static inline int ks_resize(kstring_t *s, size_t size) |
| 159 |
{ |
| 160 |
if (s->m < size) { |
| 161 |
char *tmp; |
| 162 |
kroundup_size_t(size); |
| 163 |
tmp = (char*)realloc(s->s, size); |
| 164 |
if (!tmp && size) |
| 165 |
return -1; |
| 166 |
s->s = tmp; |
| 167 |
s->m = size; |
| 168 |
} |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
static inline int ks_expand(kstring_t *s, size_t expansion) |
| 174 |
{ |
| 175 |
size_t new_size = s->l + expansion; |
| 176 |
|
| 177 |
if (new_size < s->l) |
| 178 |
return -1; |
| 179 |
return ks_resize(s, new_size); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
static inline char *ks_str(kstring_t *s) |
| 184 |
{ |
| 185 |
return s->s; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
static inline const char *ks_c_str(kstring_t *s) |
| 195 |
{ |
| 196 |
return s->l && s->s ? s->s : ""; |
| 197 |
} |
| 198 |
|
| 199 |
static inline size_t ks_len(kstring_t *s) |
| 200 |
{ |
| 201 |
return s->l; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
static inline kstring_t *ks_clear(kstring_t *s) |
| 211 |
{ |
| 212 |
s->l = 0; |
| 213 |
return s; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
static inline char *ks_release(kstring_t *s) |
| 221 |
{ |
| 222 |
char *ss = s->s; |
| 223 |
s->l = s->m = 0; |
| 224 |
s->s = NULL; |
| 225 |
return ss; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
static inline void ks_free(kstring_t *s) |
| 230 |
{ |
| 231 |
if (s) { |
| 232 |
free(s->s); |
| 233 |
ks_initialize(s); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
static inline int kputsn(const char *p, size_t l, kstring_t *s) |
| 238 |
{ |
| 239 |
size_t new_sz = s->l + l + 2; |
| 240 |
if (new_sz <= s->l || ks_resize(s, new_sz) < 0) |
| 241 |
return EOF; |
| 242 |
memcpy(s->s + s->l, p, l); |
| 243 |
s->l += l; |
| 244 |
s->s[s->l] = 0; |
| 245 |
return l; |
| 246 |
} |
| 247 |
|
| 248 |
static inline int kputs(const char *p, kstring_t *s) |
| 249 |
{ |
| 250 |
return kputsn(p, strlen(p), s); |
| 251 |
} |
| 252 |
|
| 253 |
static inline int kputc(int c, kstring_t *s) |
| 254 |
{ |
| 255 |
if (ks_resize(s, s->l + 2) < 0) |
| 256 |
return EOF; |
| 257 |
s->s[s->l++] = c; |
| 258 |
s->s[s->l] = 0; |
| 259 |
return (unsigned char)c; |
| 260 |
} |
| 261 |
|
| 262 |
static inline int kputc_(int c, kstring_t *s) |
| 263 |
{ |
| 264 |
if (ks_resize(s, s->l + 1) < 0) |
| 265 |
return EOF; |
| 266 |
s->s[s->l++] = c; |
| 267 |
return 1; |
| 268 |
} |
| 269 |
|
| 270 |
static inline int kputsn_(const void *p, size_t l, kstring_t *s) |
| 271 |
{ |
| 272 |
size_t new_sz = s->l + l; |
| 273 |
if (new_sz < s->l || ks_resize(s, new_sz ? new_sz : 1) < 0) |
| 274 |
return EOF; |
| 275 |
memcpy(s->s + s->l, p, l); |
| 276 |
s->l += l; |
| 277 |
return l; |
| 278 |
} |
| 279 |
|
| 280 |
static inline int kputuw(unsigned x, kstring_t *s) |
| 281 |
{ |
| 282 |
#if HAVE___BUILTIN_CLZ && UINT_MAX == 4294967295U |
| 283 |
static const unsigned int kputuw_num_digits[32] = { |
| 284 |
10, 10, 10, 9, 9, 9, 8, 8, |
| 285 |
8, 7, 7, 7, 7, 6, 6, 6, |
| 286 |
5, 5, 5, 4, 4, 4, 4, 3, |
| 287 |
3, 3, 2, 2, 2, 1, 1, 1 |
| 288 |
}; |
| 289 |
static const unsigned int kputuw_thresholds[32] = { |
| 290 |
0, 0, 1000000000U, 0, 0, 100000000U, 0, 0, |
| 291 |
10000000, 0, 0, 0, 1000000, 0, 0, 100000, |
| 292 |
0, 0, 10000, 0, 0, 0, 1000, 0, |
| 293 |
0, 100, 0, 0, 10, 0, 0, 0 |
| 294 |
}; |
| 295 |
#else |
| 296 |
uint64_t m; |
| 297 |
#endif |
| 298 |
static const char kputuw_dig2r[] = |
| 299 |
"00010203040506070809" |
| 300 |
"10111213141516171819" |
| 301 |
"20212223242526272829" |
| 302 |
"30313233343536373839" |
| 303 |
"40414243444546474849" |
| 304 |
"50515253545556575859" |
| 305 |
"60616263646566676869" |
| 306 |
"70717273747576777879" |
| 307 |
"80818283848586878889" |
| 308 |
"90919293949596979899"; |
| 309 |
unsigned int l, j; |
| 310 |
char *cp; |
| 311 |
|
| 312 |
|
| 313 |
if (x < 10) { |
| 314 |
if (ks_resize(s, s->l + 2) < 0) |
| 315 |
return EOF; |
| 316 |
s->s[s->l++] = '0'+x; |
| 317 |
s->s[s->l] = 0; |
| 318 |
return 0; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
#if HAVE___BUILTIN_CLZ && UINT_MAX == 4294967295U |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
l = __builtin_clz(x); |
| 333 |
l = kputuw_num_digits[l] - (x < kputuw_thresholds[l]); |
| 334 |
#else |
| 335 |
|
| 336 |
m = 1; |
| 337 |
l = 0; |
| 338 |
do { |
| 339 |
l++; |
| 340 |
m *= 10; |
| 341 |
} while (x >= m); |
| 342 |
#endif |
| 343 |
|
| 344 |
if (ks_resize(s, s->l + l + 2) < 0) |
| 345 |
return EOF; |
| 346 |
|
| 347 |
|
| 348 |
j = l; |
| 349 |
cp = s->s + s->l; |
| 350 |
while (x >= 10) { |
| 351 |
const char *d = &kputuw_dig2r[2*(x%100)]; |
| 352 |
x /= 100; |
| 353 |
memcpy(&cp[j-=2], d, 2); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
if (j == 1) |
| 358 |
cp[0] = x + '0'; |
| 359 |
|
| 360 |
s->l += l; |
| 361 |
s->s[s->l] = 0; |
| 362 |
return 0; |
| 363 |
} |
| 364 |
|
| 365 |
static inline int kputw(int c, kstring_t *s) |
| 366 |
{ |
| 367 |
unsigned int x = c; |
| 368 |
if (c < 0) { |
| 369 |
x = -x; |
| 370 |
if (ks_resize(s, s->l + 3) < 0) |
| 371 |
return EOF; |
| 372 |
s->s[s->l++] = '-'; |
| 373 |
} |
| 374 |
|
| 375 |
return kputuw(x, s); |
| 376 |
} |
| 377 |
|
| 378 |
static inline int kputll(long long c, kstring_t *s) |
| 379 |
{ |
| 380 |
char buf[32]; |
| 381 |
int i, l = 0; |
| 382 |
unsigned long long x = c; |
| 383 |
if (c < 0) x = -x; |
| 384 |
do { buf[l++] = x%10 + '0'; x /= 10; } while (x > 0); |
| 385 |
if (c < 0) buf[l++] = '-'; |
| 386 |
if (ks_resize(s, s->l + l + 2) < 0) |
| 387 |
return EOF; |
| 388 |
for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i]; |
| 389 |
s->s[s->l] = 0; |
| 390 |
return 0; |
| 391 |
} |
| 392 |
|
| 393 |
static inline int kputl(long c, kstring_t *s) { |
| 394 |
return kputll(c, s); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
static inline int *ksplit(kstring_t *s, int delimiter, int *n) |
| 402 |
{ |
| 403 |
int max = 0, *offsets = 0; |
| 404 |
*n = ksplit_core(s->s, delimiter, &max, &offsets); |
| 405 |
return offsets; |
| 406 |
} |
| 407 |
|
| 408 |
#endif |