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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/*
Line based I/O over fds
Copyright (C) Amitay Isaacs 2018
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 "replace.h"
#include <talloc.h>
#include "lib/util/sys_rw.h"
#include "common/line.h"
struct line_read_state {
line_process_fn_t callback;
void *private_data;
char *buf;
size_t hint, len, offset;
int num_lines;
};
static bool line_read_one(char *buf, size_t start, size_t len, size_t *pos)
{
size_t i;
for (i=start; i<len; i++) {
if (buf[i] == '\n' || buf[i] == '\0') {
*pos = i;
return true;
}
}
return false;
}
static int line_read_process(struct line_read_state *state)
{
size_t start = 0;
size_t pos = 0;
while (1) {
int ret;
bool ok;
ok = line_read_one(state->buf, start, state->offset, &pos);
if (! ok) {
break;
}
state->buf[pos] = '\0';
state->num_lines += 1;
ret = state->callback(state->buf + start, state->private_data);
if (ret != 0) {
return ret;
}
start = pos+1;
}
if (pos > 0) {
if (pos+1 < state->offset) {
memmove(state->buf,
state->buf + pos+1,
state->offset - (pos+1));
}
state->offset -= (pos+1);
}
return 0;
}
int line_read(int fd,
size_t length,
TALLOC_CTX *mem_ctx,
line_process_fn_t callback,
void *private_data,
int *num_lines)
{
struct line_read_state state;
if (length < 32) {
length = 32;
}
state = (struct line_read_state) {
.callback = callback,
.private_data = private_data,
.hint = length,
};
while (1) {
ssize_t n;
int ret;
if (state.offset == state.len) {
state.len += state.hint;
state.buf = talloc_realloc_size(mem_ctx,
state.buf,
state.len);
if (state.buf == NULL) {
return ENOMEM;
}
}
n = sys_read(fd,
state.buf + state.offset,
state.len - state.offset);
if (n < 0) {
return errno;
}
if (n == 0) {
break;
}
state.offset += n;
ret = line_read_process(&state);
if (ret != 0) {
if (num_lines != NULL) {
*num_lines = state.num_lines;
}
return ret;
}
}
if (num_lines != NULL) {
*num_lines = state.num_lines;
}
return 0;
}
|