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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
// find string in
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
char **load_mers_from_file(FILE *fh, ssize_t *len) {
char line[4096];
size_t realloc_size = 1;
*len = 0;
char **mers = NULL;
while((fgets(line, 4096, fh)) != NULL) {
size_t line_len = strlen(line);
if(line_len == 0)
continue;
line[line_len - 1] = '\0';
mers = realloc(mers, sizeof(char *) * realloc_size);
if(mers == NULL) {
fprintf(stderr, "could not realloc mers\n");
exit(EXIT_FAILURE);
}
char *cpy = malloc(line_len + 1);
if(cpy == NULL) {
fprintf(stderr, "could not alloc mers\n");
exit(EXIT_FAILURE);
}
strncpy(cpy, line, line_len);
mers[*len] = cpy;
*len += 1;
realloc_size++;
}
if(*len != 0)
return mers;
*len = -1;
return NULL;
}
int main(int argc, char **argv){
char buffer[BUFSIZ + 1] = { 0 };
char *buf, *start;
size_t len = 0;
ssize_t mer_len = 0;
int save_size = 0;
int cpy = 0;
unsigned long long pos = 0;
unsigned long long cpy_size = 0;
int i = 0;
if(argc != 2) {
fprintf(stderr, "usage: strstream merlist.txt\n");
exit(EXIT_FAILURE);
}
// load mers
FILE *fh = fopen(argv[1], "r");
if(fh == NULL) {
fprintf(stderr, "could not open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
char **mers = load_mers_from_file(fh, &mer_len);
if(mers == NULL) {
fprintf(stderr, "could not load mers from %s\n", argv[1]);
exit(EXIT_FAILURE);
}
// get max argument length
for(i = 0; i < mer_len; i++) {
int current_len = strlen(mers[i]);
if( current_len > save_size)
save_size = current_len;
}
cpy = save_size - 1;
cpy_size = BUFSIZ - cpy;
buf = buffer;
start = buf + cpy;
// copy our first cpy length into the first part of our buffer
len = fread(buffer, 1, cpy, stdin);
if(len == 0)
exit(EXIT_FAILURE);
// read into "start" (buf + cpy) from tdin
while((len = fread(start, 1, cpy_size, stdin)) != 0) {
// strstr isn't smart, and fread doesn't set a '\0', so manually set end of string
if(len < cpy_size)
start[len] = '\0';
for(i = 0; i < mer_len; i++) {
char *p = buffer;
while((p = strstr(p, mers[i])) != NULL) {
printf("%d %llu\n", i, pos + (p - buffer));
p++;
}
}
memcpy(buffer, buffer + len, cpy);
pos = pos + len;
}
return 0;
}
|