Add subscription support to perl lib
[blerg.git] / lib / perl / Blerg-Database / Database.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include "ppport.h"
6
7 #include "const-c.inc"
8
9 #include "database/database.h"
10 #include "database/blergref.h"
11 #include "database/tags.h"
12 #include "database/subscription.h"
13 #include "database/util.h"
14
15 HV * blergref_to_perl_hash(struct blergref *list) {
16         HV *tmp;
17         char buf[21];
18         int n;
19
20         tmp = newHV();
21         hv_store(tmp, "author", 6, newSVpv(list->author, 0), 0);
22         n = snprintf(buf, 21, "%llu", list->record);
23         hv_store(tmp, "record", 6, newSVpv(buf, n), 0);
24
25         return tmp;
26 }
27
28 MODULE = Blerg::Database                PACKAGE = Blerg::Database               
29
30 INCLUDE: const-xs.inc
31 PROTOTYPES: ENABLE
32
33 int exists(const char *name)
34     CODE:
35         RETVAL = blerg_exists(name);
36     OUTPUT:
37         RETVAL
38
39 struct blerg * _open(const char *name)
40     CODE:
41         RETVAL = blerg_open(name);
42     OUTPUT:
43         RETVAL
44
45 int _close(struct blerg *ptr)
46     CODE:
47         RETVAL = blerg_close(ptr);
48     OUTPUT:
49         RETVAL
50
51 int _store(struct blerg *ptr, const char *data, int length(data))
52     PROTOTYPE: $$
53     CODE:
54         RETVAL = blerg_store(ptr, data, XSauto_length_of_data);
55     OUTPUT:
56         RETVAL
57
58 const char * _fetch(struct blerg *ptr, int record)
59     INIT:
60         char *buf;
61         int buflen;
62         int n;
63     PPCODE:
64         n = blerg_fetch(ptr, record, &buf, &buflen);
65         if (!n) {
66             XSRETURN_UNDEF;
67         }
68         XPUSHs(sv_2mortal(newSVpvn(buf, buflen)));
69         free(buf);
70
71 const char * _get_record_count(struct blerg *ptr)
72     INIT:
73         uint64_t count;
74         char buf[21];
75         int n;
76     PPCODE:
77         count = blerg_get_record_count(ptr);
78         n = snprintf(buf, 21, "%llu", count);
79         XPUSHs(sv_2mortal(newSVpv(buf, n)));
80
81 time_t _get_timestamp(struct blerg *ptr, int record)
82     CODE:
83         RETVAL = blerg_get_timestamp(ptr, record);
84     OUTPUT:
85         RETVAL
86
87 int _set_subscription_mark(struct blerg *ptr)
88     CODE:
89         RETVAL = blerg_set_subscription_mark(ptr);
90     OUTPUT:
91         RETVAL
92
93 const char * _get_subscription_mark(struct blerg *ptr)
94     INIT:
95         uint64_t mark;
96         char buf[21];
97         int n;
98     PPCODE:
99         mark = blerg_get_subscription_mark(ptr);
100         n = snprintf(buf, 21, "%llu", mark);
101         XPUSHs(sv_2mortal(newSVpv(buf, n)));
102
103 int _set_mute(struct blerg *ptr, int v)
104     CODE:
105         RETVAL = blerg_set_mute(ptr, v);
106     OUTPUT:
107         RETVAL
108
109 int _get_mute(struct blerg *ptr)
110     CODE:
111         RETVAL = blerg_get_mute(ptr);
112     OUTPUT:
113         RETVAL
114
115 void _tag_list(const char *tag, const char *str_offset, int direction)
116     INIT:
117         HV * tmp;
118         struct blergref *list;
119         uint64_t offset;
120         int count, i;
121     PPCODE:
122         offset = strtoull(str_offset, NULL, 0);
123         list = tag_list(tag, offset, &count, direction);
124         if (list == NULL) {
125             XSRETURN_UNDEF;
126         }
127
128         i = count - 1;
129         while (i >= 0) {
130             tmp = blergref_to_perl_hash(&list[i]);
131             XPUSHs(sv_2mortal(newRV_noinc((SV*)tmp)));
132             i--;
133         }
134         free(list);
135
136 int subscription_add(const char *from, const char *to)
137     CODE:
138         RETVAL = subscription_add(from, to);
139     OUTPUT:
140         RETVAL
141
142 int subscription_remove(const char *from, const char *to)
143     CODE:
144         RETVAL = subscription_remove(from, to);
145     OUTPUT:
146         RETVAL
147
148 void subscription_list(const char *author, const char *str_offset, int direction)
149     INIT:
150         HV *tmp;
151         struct blergref *list;
152         uint64_t offset;
153         int count, i;
154     PPCODE:
155         offset = strtoull(str_offset, NULL, 0);
156         list = subscription_list(author, offset, &count, direction);
157         if (list == NULL) {
158             XSRETURN_UNDEF;
159         }
160
161         i = count - 1;
162         while (i >= 0) {
163             tmp = blergref_to_perl_hash(&list[i]);
164             XPUSHs(sv_2mortal(newRV_noinc((SV*)tmp)));
165             i--;
166         }
167         free(list);
168
169 int valid_tag_name(const char *name)
170
171 int valid_name(const char *name)