annotate pyPRADA_1.2/tools/samtools-0.1.16/kstring.h @ 3:f17965495ec9 draft default tip

Uploaded
author siyuan
date Tue, 11 Mar 2014 12:14:01 -0400
parents acc2ca1a3ba4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 #ifndef KSTRING_H
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2 #define KSTRING_H
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 #include <stdlib.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 #include <stdint.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 #ifndef kroundup32
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 #define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 #ifndef KSTRING_T
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 #define KSTRING_T kstring_t
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 typedef struct __kstring_t {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15 size_t l, m;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 char *s;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 } kstring_t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 typedef struct {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 uint64_t tab[4];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 int sep, finished;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 const char *p; // end of the current token
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 } ks_tokaux_t;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 #ifdef __cplusplus
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 extern "C" {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 int ksprintf(kstring_t *s, const char *fmt, ...);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 int ksplit_core(char *s, int delimiter, int *_max, int **_offsets);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 char *kstrstr(const char *str, const char *pat, int **_prep);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 char *kstrnstr(const char *str, const char *pat, int n, int **_prep);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 void *kmemmem(const void *_str, int n, const void *_pat, int m, int **_prep);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 /* kstrtok() is similar to strtok_r() except that str is not
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 * modified and both str and sep can be NULL. For efficiency, it is
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 * actually recommended to set both to NULL in the subsequent calls
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 * if sep is not changed. */
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 char *kstrtok(const char *str, const char *sep, ks_tokaux_t *aux);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 #ifdef __cplusplus
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 #endif
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 static inline int kputsn(const char *p, int l, kstring_t *s)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 if (s->l + l + 1 >= s->m) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 s->m = s->l + l + 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 kroundup32(s->m);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 s->s = (char*)realloc(s->s, s->m);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 memcpy(s->s + s->l, p, l);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 s->l += l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 s->s[s->l] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 return l;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 static inline int kputs(const char *p, kstring_t *s)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 return kputsn(p, strlen(p), s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 static inline int kputc(int c, kstring_t *s)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 if (s->l + 1 >= s->m) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 s->m = s->l + 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 kroundup32(s->m);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 s->s = (char*)realloc(s->s, s->m);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 s->s[s->l++] = c;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 s->s[s->l] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 return c;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 static inline int kputw(int c, kstring_t *s)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 char buf[16];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 int l, x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 if (c == 0) return kputc('0', s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 for (l = 0, x = c < 0? -c : c; x > 0; x /= 10) buf[l++] = x%10 + '0';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 if (c < 0) buf[l++] = '-';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 if (s->l + l + 1 >= s->m) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 s->m = s->l + l + 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 kroundup32(s->m);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 s->s = (char*)realloc(s->s, s->m);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 for (x = l - 1; x >= 0; --x) s->s[s->l++] = buf[x];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 s->s[s->l] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 static inline int kputuw(unsigned c, kstring_t *s)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 char buf[16];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 int l, i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 unsigned x;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 if (c == 0) return kputc('0', s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 for (l = 0, x = c; x > 0; x /= 10) buf[l++] = x%10 + '0';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 if (s->l + l + 1 >= s->m) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 s->m = s->l + l + 2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 kroundup32(s->m);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 s->s = (char*)realloc(s->s, s->m);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 for (i = l - 1; i >= 0; --i) s->s[s->l++] = buf[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 s->s[s->l] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110 static inline int *ksplit(kstring_t *s, int delimiter, int *n)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 int max = 0, *offsets = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 *n = ksplit_core(s->s, delimiter, &max, &offsets);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114 return offsets;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 #endif