Debian Codegraph @

bcftools/cols.c

raw symbols SCIP
1 /*
2 Copyright (C) 2019 Genome Research Ltd.
3
4 Author: Petr Danecek <[email protected]>
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <string.h>
26 #include "cols.h"
27
28 cols_t *cols_split(const char *line, cols_t *cols, char delim)
29 {
30 if ( !cols ) cols = (cols_t*) calloc(1,sizeof(cols_t));
31 if ( cols->rmme ) free(cols->rmme);
32 cols->n = 0;
33 cols->rmme = strdup(line);
34 char *ss = cols->rmme;
35 while (1)
36 {
37 char *se = ss;
38 while ( *se && *se!=delim ) se++;
39 char tmp = *se;
40 *se = 0;
41 cols->n++;
42 if ( cols->n > cols->m )
43 {
44 cols->m += 10;
45 cols->off = (char**) realloc(cols->off, sizeof(*cols->off)*cols->m);
46 }
47 cols->off[ cols->n - 1 ] = ss;
48 if ( !tmp ) break;
49 ss = se + 1;
50 }
51 return cols;
52 }
53
54 void cols_append(cols_t *cols, char *str)
55 {
56 if ( cols->rmme )
57 {
58 size_t str_len = strlen(str);
59 size_t lst_len = strlen(cols->off[ cols->n - 1 ]);
60 size_t tot_len = 2 + str_len + lst_len + (cols->off[ cols->n - 1 ] - cols->rmme);
61
62 cols_t *tmp_cols = (cols_t*)calloc(1,sizeof(cols_t));
63 tmp_cols->rmme = (char*) calloc(tot_len,1);
64 tmp_cols->off = (char**) calloc(cols->n+1,sizeof(*tmp_cols->off));
65
66 char *ptr = tmp_cols->rmme;
67 int i;
68 for (i=0; i<cols->n; i++)
69 {
70 size_t len = strlen(cols->off[i]);
71 memcpy(ptr, cols->off[i], len);
72 tmp_cols->off[i] = ptr;
73 ptr += len + 1;
74 }
75 memcpy(ptr, str, str_len);
76 tmp_cols->off[i] = ptr;
77
78 free(cols->off);
79 free(cols->rmme);
80 cols->rmme = tmp_cols->rmme;
81 cols->off = tmp_cols->off;
82 cols->n = cols->n+1;
83 cols->m = cols->n;
84 free(tmp_cols);
85 return;
86 }
87 cols->n++;
88 if ( cols->n > cols->m )
89 {
90 cols->m++;
91 cols->off = (char**) realloc(cols->off,sizeof(*cols->off)*cols->m);
92 }
93 cols->off[cols->n-1] = str;
94 }
95 void cols_clear(cols_t *cols)
96 {
97 if ( !cols ) return;
98 free(cols->rmme);
99 free(cols->off);
100 cols->rmme = NULL;
101 cols->off = NULL;
102 }
103 void cols_destroy(cols_t *cols)
104 {
105 if ( !cols ) return;
106 cols_clear(cols);
107 free(cols);
108 }
109