-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathParser.xs
121 lines (110 loc) · 2.79 KB
/
Parser.xs
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
/* vim: set expandtab sts=4: */
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>
#include "ppport.h"
#include "rdb_parser.h"
MODULE = RedisDB::Parser PACKAGE = RedisDB::Parser::XS PREFIX = rdb_parser_
PROTOTYPES: DISABLE
RDB_parser*
rdb_parser__new(master, error_class, utf8)
SV* master;
SV* error_class;
int utf8;
CODE:
RETVAL = rdb_parser__init(master, error_class, utf8);
RETVAL->thx = (IV)PERL_GET_THX;
OUTPUT:
RETVAL
void
rdb_parser_DESTROY(parser)
RDB_parser *parser;
CODE:
if (parser->thx == (IV)PERL_GET_THX) {
rdb_parser__free(parser);
}
SV*
rdb_parser_build_request(parser, ...)
RDB_parser *parser;
PREINIT:
int i;
STRLEN len;
char *pv;
SV *tmp;
CODE:
RETVAL = newSV(128);
sv_setpvf(RETVAL, "*%ld\r\n", items - 1L);
for (i = 1; i < items; i++) {
if (parser->utf8) {
tmp = sv_mortalcopy(ST(i));
pv = SvPVutf8(tmp, len);
}
else {
pv = SvPV(ST(i), len);
}
sv_catpvf(RETVAL, "$%ld\r\n", (long)len);
sv_catpvn(RETVAL, pv, len);
sv_catpvn(RETVAL, "\r\n", 2);
}
OUTPUT:
RETVAL
int
rdb_parser_push_callback(parser, cb)
RDB_parser *parser;
SV* cb;
CODE:
SvREFCNT_inc(cb);
av_push(parser->callbacks, cb);
RETVAL = 1 + av_len(parser->callbacks);
OUTPUT:
RETVAL
SV*
rdb_parser_set_default_callback(parser, cb)
RDB_parser *parser;
SV* cb;
CODE:
if (parser->default_cb != NULL)
SvSetSV(parser->default_cb, cb);
else
parser->default_cb = newSVsv(cb);
RETVAL = newSVsv(cb);
OUTPUT:
RETVAL
void
rdb_parser_callbacks(parser)
RDB_parser *parser;
PREINIT:
int i, len;
SV **ptr;
PPCODE:
len = 1 + av_len(parser->callbacks);
if (GIMME_V != G_ARRAY) {
XPUSHs(sv_2mortal(newSViv(len)));
}
else {
EXTEND(SP, len);
for (i=0; i < len; i++) {
ptr = av_fetch(parser->callbacks, i, 0);
if (ptr == NULL) {
croak("Callback doesn't exist");
}
PUSHs(*ptr);
}
}
void
rdb_parser_propagate_reply(parser, reply)
RDB_parser *parser;
SV *reply;
CODE:
rdb_parser__propagate_reply(parser, reply);
int
rdb_parser_parse(parser, data)
RDB_parser *parser;
SV* data;
CODE:
sv_catsv(parser->buffer, data);
RETVAL = 0;
while (sv_len(parser->buffer) && rdb_parser__parse_reply(parser))
RETVAL++;
OUTPUT:
RETVAL