forked from vivien/i3blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.c
89 lines (73 loc) · 1.94 KB
/
line.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
/*
* line.c - generic line parser
* Copyright (C) 2015-2019 Vivien Didelot
*
* 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 "line.h"
#include "log.h"
#include "sys.h"
/* Read a single character and return a negative error code if none was read */
static int line_getc(int fd, char *c)
{
return sys_read(fd, c, 1, NULL);
}
/* Read a line including the newline character and return its positive length */
static ssize_t line_gets(int fd, char *buf, size_t size)
{
size_t len = 0;
int err;
for (;;) {
if (len == size)
return -ENOSPC;
err = line_getc(fd, buf + len);
if (err)
return err;
if (buf[len++] == '\n')
break;
}
/* at least 1 */
return len;
}
/* Read a line excluding the newline character */
static int line_parse(int fd, line_cb_t *cb, size_t num, void *data)
{
char buf[BUFSIZ];
ssize_t len;
int err;
len = line_gets(fd, buf, sizeof(buf));
if (len < 0)
return len;
/* replace newline with terminating null byte */
buf[len - 1] = '\0';
debug("&%d:%.3d: %s", fd, num, buf);
if (cb) {
err = cb(buf, num, data);
if (err)
return err;
}
return 0;
}
/* Read up to count lines excluding their newline character */
int line_read(int fd, size_t count, line_cb_t *cb, void *data)
{
size_t lines = 0;
int err;
while (count--) {
err = line_parse(fd, cb, lines++, data);
if (err)
return err;
}
return 0;
}