-
Notifications
You must be signed in to change notification settings - Fork 13
/
bufio.h
56 lines (48 loc) · 1.21 KB
/
bufio.h
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
/* SPDX-License-Identifier: MIT */
/*
* Author: Jianhui Zhao <[email protected]>
*/
#ifndef __ECO_BUFIO_H
#define __ECO_BUFIO_H
#include "eco.h"
struct eco_bufio {
struct eco_context *eco;
struct ev_timer tmr;
struct ev_io io;
lua_State *L;
int fd;
size_t n; /* how many bytes to read currently */
size_t pattern_len;
const char *pattern; /* read pattern currently */
double timeout;
struct {
uint8_t eof:1;
uint8_t overtime:1;
} flags;
size_t size;
size_t r, w;
int (*fill)(struct eco_bufio *b, lua_State *L, lua_KContext ctx, lua_KFunction k);
const char *eof_error;
const char *error;
const void *ctx;
luaL_Buffer *b;
char data[0];
};
#define buffer_skip(b, n) \
do { \
b->r += n; \
if (b->r == b->w) \
b->r = b->w = 0; \
} while(0)
#define buffer_slide(b) \
do { \
if (b->r > 0) { \
memmove(b->data, b->data + b->r, b->w - b->r); \
b->w -= b->r; \
b->r = 0; \
} \
} while(0)
#define buffer_length(b) (b->w - b->r)
#define buffer_data(b) (b->data + b->r)
#define buffer_room(b) (b->size - b->w)
#endif