-
Notifications
You must be signed in to change notification settings - Fork 24
/
linebuf.c
89 lines (78 loc) · 2.49 KB
/
linebuf.c
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
/* PANDAseq -- Assemble paired FASTQ Illumina reads and strip the region between amplification primers.
Copyright (C) 2011-2012 Andre Masella
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "pandaseq.h"
#include "misc.h"
struct panda_linebuf {
char data[10 * MAX_LEN];
size_t data_length;
size_t offset;
MANAGED_MEMBER(
PandaBufferRead,
read);
};
PandaLineBuf panda_linebuf_new(
PandaBufferRead read,
void *read_data,
PandaDestroy read_destroy) {
PandaLineBuf buffer;
if (read == NULL)
return NULL;
buffer = malloc(sizeof(struct panda_linebuf));
buffer->data_length = 0;
buffer->offset = 0;
buffer->read = read;
buffer->read_data = read_data;
buffer->read_destroy = read_destroy;
return buffer;
}
void panda_linebuf_free(
PandaLineBuf linebuf) {
if (linebuf == NULL)
return;
DESTROY_MEMBER(linebuf, read);
free(linebuf);
}
const char *panda_linebuf_next(
PandaLineBuf linebuf) {
char *end;
if (linebuf->offset > 0) {
memmove(linebuf->data, linebuf->data + linebuf->offset, linebuf->data_length - linebuf->offset);
linebuf->data_length -= linebuf->offset;
linebuf->offset = 0;
}
while ((end = memchr(linebuf->data, '\n', linebuf->data_length)) == NULL && linebuf->data_length < sizeof(linebuf->data)) {
size_t new_bytes = 0;
if (!linebuf->read(linebuf->data + linebuf->data_length, sizeof(linebuf->data) - linebuf->data_length, &new_bytes, linebuf->read_data)) {
return NULL;
}
if (new_bytes == 0) {
end = linebuf->data + linebuf->data_length + 1;
break;
}
linebuf->data_length += new_bytes;
}
if (end == NULL || linebuf->data_length == 0 || *end == '\0') {
return NULL;
}
/* White out any carriage returns if we get DOS-formatted files. */
if (end != linebuf->data && end[-1] == '\r') {
end[-1] = '\0';
}
*end = '\0';
linebuf->offset = end - linebuf->data + 1;
return linebuf->data;
}