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