| 1 |
#ifndef KSTRING_H |
| 2 |
#define KSTRING_H |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
#include <stdarg.h> |
| 7 |
|
| 8 |
#ifdef USE_MALLOC_WRAPPERS |
| 9 |
# include "malloc_wrap.h" |
| 10 |
#endif |
| 11 |
|
| 12 |
#ifndef kroundup32 |
| 13 |
#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) |
| 14 |
#endif |
| 15 |
|
| 16 |
#ifndef KSTRING_T |
| 17 |
#define KSTRING_T kstring_t |
| 18 |
typedef struct __kstring_t { |
| 19 |
size_t l, m; |
| 20 |
char *s; |
| 21 |
} kstring_t; |
| 22 |
#endif |
| 23 |
|
| 24 |
static inline void ks_resize(kstring_t *s, size_t size) |
| 25 |
{ |
| 26 |
if (s->m < size) { |
| 27 |
s->m = size; |
| 28 |
kroundup32(s->m); |
| 29 |
s->s = (char*)realloc(s->s, s->m); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
static inline int kputsn(const char *p, int l, kstring_t *s) |
| 34 |
{ |
| 35 |
if (s->l + l + 1 >= s->m) { |
| 36 |
s->m = s->l + l + 2; |
| 37 |
kroundup32(s->m); |
| 38 |
s->s = (char*)realloc(s->s, s->m); |
| 39 |
} |
| 40 |
memcpy(s->s + s->l, p, l); |
| 41 |
s->l += l; |
| 42 |
s->s[s->l] = 0; |
| 43 |
return l; |
| 44 |
} |
| 45 |
|
| 46 |
static inline int kputs(const char *p, kstring_t *s) |
| 47 |
{ |
| 48 |
return kputsn(p, strlen(p), s); |
| 49 |
} |
| 50 |
|
| 51 |
static inline int kputc(int c, kstring_t *s) |
| 52 |
{ |
| 53 |
if (s->l + 1 >= s->m) { |
| 54 |
s->m = s->l + 2; |
| 55 |
kroundup32(s->m); |
| 56 |
s->s = (char*)realloc(s->s, s->m); |
| 57 |
} |
| 58 |
s->s[s->l++] = c; |
| 59 |
s->s[s->l] = 0; |
| 60 |
return c; |
| 61 |
} |
| 62 |
|
| 63 |
static inline int kputw(int c, kstring_t *s) |
| 64 |
{ |
| 65 |
char buf[16]; |
| 66 |
int l, x; |
| 67 |
if (c == 0) return kputc('0', s); |
| 68 |
for (l = 0, x = c < 0? -c : c; x > 0; x /= 10) buf[l++] = x%10 + '0'; |
| 69 |
if (c < 0) buf[l++] = '-'; |
| 70 |
if (s->l + l + 1 >= s->m) { |
| 71 |
s->m = s->l + l + 2; |
| 72 |
kroundup32(s->m); |
| 73 |
s->s = (char*)realloc(s->s, s->m); |
| 74 |
} |
| 75 |
for (x = l - 1; x >= 0; --x) s->s[s->l++] = buf[x]; |
| 76 |
s->s[s->l] = 0; |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static inline int kputuw(unsigned c, kstring_t *s) |
| 81 |
{ |
| 82 |
char buf[16]; |
| 83 |
int l, i; |
| 84 |
unsigned x; |
| 85 |
if (c == 0) return kputc('0', s); |
| 86 |
for (l = 0, x = c; x > 0; x /= 10) buf[l++] = x%10 + '0'; |
| 87 |
if (s->l + l + 1 >= s->m) { |
| 88 |
s->m = s->l + l + 2; |
| 89 |
kroundup32(s->m); |
| 90 |
s->s = (char*)realloc(s->s, s->m); |
| 91 |
} |
| 92 |
for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i]; |
| 93 |
s->s[s->l] = 0; |
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
static inline int kputl(long c, kstring_t *s) |
| 98 |
{ |
| 99 |
char buf[32]; |
| 100 |
long l, x; |
| 101 |
if (c == 0) return kputc('0', s); |
| 102 |
for (l = 0, x = c < 0? -c : c; x > 0; x /= 10) buf[l++] = x%10 + '0'; |
| 103 |
if (c < 0) buf[l++] = '-'; |
| 104 |
if (s->l + l + 1 >= s->m) { |
| 105 |
s->m = s->l + l + 2; |
| 106 |
kroundup32(s->m); |
| 107 |
s->s = (char*)realloc(s->s, s->m); |
| 108 |
} |
| 109 |
for (x = l - 1; x >= 0; --x) s->s[s->l++] = buf[x]; |
| 110 |
s->s[s->l] = 0; |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
int bwa_kvsprintf(kstring_t *s, const char *fmt, va_list ap); |
| 115 |
|
| 116 |
static inline int ksprintf(kstring_t *s, const char *fmt, ...) |
| 117 |
{ |
| 118 |
va_list ap; |
| 119 |
int l; |
| 120 |
va_start(ap, fmt); |
| 121 |
l = bwa_kvsprintf(s, fmt, ap); |
| 122 |
va_end(ap); |
| 123 |
return l; |
| 124 |
} |
| 125 |
|
| 126 |
static inline int kvsprintf(kstring_t *s, const char *fmt, va_list ap) |
| 127 |
{ |
| 128 |
return bwa_kvsprintf(s, fmt, ap); |
| 129 |
} |
| 130 |
|
| 131 |
#endif |