-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_buffer.c
186 lines (153 loc) · 3.87 KB
/
read_buffer.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "read_buffer.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fcntl.h>
#include <unistd.h>
#define likely(x) __builtin_expect((x), 1)
#define unlikely(x) __builtin_expect((x), 0)
#define BUFFER_SIZE 64
static char buffer[BUFFER_SIZE];
static int pos;
static int filled;
static int current_fd = -1;
static read_result_t last_error;
read_result_t fill_buffer() {
filled = read(current_fd, buffer, BUFFER_SIZE);
if (unlikely(filled < 0)) {
return read_result_error;
}
if (filled == 0) {
return read_result_eof;
}
pos = 0;
return read_result_ok;
}
inline static int is_number(char c) {
return c >= '0' && c <= '9';
}
inline static int is_whitespace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
inline static char get() {
return buffer[pos];
}
inline static read_result_t next() {
pos++;
if (pos >= filled)
return last_error = fill_buffer();
return last_error = read_result_ok;
}
inline static char next_char() {
pos++;
return get();
}
inline static read_result_t skip_whitespaces() {
while (true) {
if (!is_whitespace(get()))
return read_result_ok;
if (next() != read_result_ok)
return last_error;
};
return read_result_ok;
}
read_result_t read_start(int fd) {
current_fd = fd;
return fill_buffer();
}
read_result_t read_next_uint32(uint32_t *out) {
if (skip_whitespaces() != read_result_ok)
return last_error;
unsigned int res = 0;
while (true) {
if (!is_number(get())) {
*out = res;
return read_result_ok;
}
res *= 10;
res += buffer[pos] - '0';
if (next() != read_result_ok) {
if (last_error == read_result_eof) {
*out = res;
return read_result_ok;
}
return last_error;
}
}
}
read_result_t read_next_uint64(uint64_t *out) {
if (skip_whitespaces() != read_result_ok)
return last_error;
uint64_t res = 0;
while (true) {
if (!is_number(get())) {
*out = res;
return read_result_ok;
}
res *= 10;
res += buffer[pos] - '0';
if (next() != read_result_ok) {
if (last_error == read_result_eof) {
*out = res;
return read_result_ok;
}
return last_error;
}
}
}
read_result_t read_next_int64(int64_t *out) {
if (skip_whitespaces() != read_result_ok)
return last_error;
uint64_t res = 0;
bool neg = (get() == '-');
while (true) {
if (!is_number(get())) {
*out = res;
return read_result_ok;
}
res *= 10;
res += buffer[pos] - '0';
if (next() != read_result_ok) {
if (last_error == read_result_eof) {
*out = res;
return read_result_ok;
}
return last_error;
}
}
}
read_result_t next_line() {
while (true) {
if (get() == '\n')
return next();
if (next() != read_result_ok)
return last_error;
}
}
read_result_t read_next_string(char *dst, int n) {
if (skip_whitespaces() != read_result_ok)
return last_error;
if (n == 0)
return read_result_error;
n--;
while (true) {
if (is_whitespace(get()) || !(n--)) {
*dst = 0;
return read_result_ok;
}
*dst++ = buffer[pos];
if (next() != read_result_ok)
return last_error;
}
}
read_result_t skip_next() {
if (skip_whitespaces() != read_result_ok)
return last_error;
while (true) {
if (is_whitespace(get()))
return read_result_ok;
if (next() != read_result_ok)
return last_error;
}
}