PureDB.xs

filter: Statementstmt 73.4% 36 / 49 Branchbran 60.0% 12 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.5% 48 / 69 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Tie/PureDB.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5  
6 #include <errno.h>
7  
8 #include "PureDB.h"
9 #include "puredb_read.h"
10 #include "puredb_write.h"
11  
12 #undef PERL_UNUSED_VAR
13  
14 /*
15 //perlmonks.org/?node_id=248697#panic: leave_scope inconsistency. (need help creating Tie::PureDB)
16 //http://www.manning.com/jenness/
17 //http://www.manning.com/getpage.html?project=jenness&filename=forum.html
18 //http://www.manning.com/ao/eep/eepao.html
19 //http://www.manning.com/ao/readforum.html?forum=eepao
20 // = malloc(sizeof(PureDB));
21 //// = malloc(sizeof(PureDB));
22 //puredb_read_free(founddata);Safefree(founddata);
23 //E:\new\Win32API-Registry-0.23
24  
25 sv_setpvf(
26 get_sv("!", TRUE),
27 "Error Reading '%s', '%s', '%s' : %s",
28 file_index,
29 file_data,
30 file_final,
31 strerror(errno)
32 );
33  
34 perl E:\dev\PureDB\4.Tie-PureDB-0.02\leak.t
35 E:\dev\PureDB\4.Tie-PureDB-0.02\leak.t
36 ## sv_setsv(get_sv("!", TRUE),newSVpvf("Error reading '%s'",dbfile));
37 ## sv_setpvf(get_sv("!", TRUE),"Error reading '%s'",dbfile);
38 ## sv_setsv(get_sv("@", TRUE),newSVpvf("Error reading '%s'",dbfile));
39  
40 */
41  
42 #ifdef PureDEBUG
43 #include "perlio.h"
44  
45 void ReadDebug2(char * what, off_t off, size_t len) {
46 PerlIO_printf(PerlIO_stderr(),"\n\
47 #/////////////////////////////////\n\
48 #What '%s' \n\
49 # off_t offset'%d' \n\
50 # size_t length'%d' \n\n",
51 what, off, len
52 );
53 }
54 void ReadDebug(char * from, IV myIv, PureDB* foy) {
55 PerlIO_printf(PerlIO_stderr(),"\n\
56 #/////////////////////////////////\n\
57 #From '%s' \n\
58 # '%d' \n\
59 # PTR2IV(foy) '%d' \n\
60 #unsigned char *map '%s' \n\
61 # int fd '%d' \n\
62 # puredb_u32_t size'%d' \n\n",
63 from,
64 myIv,
65 PTR2IV(foy),
66 (char*) foy->map ,
67 (int) foy->fd,
68 (puredb_u32_t) foy->size
69 );
70  
71 }
72 #endif
73  
74  
75  
76  
77 MODULE = Tie::PureDB PACKAGE = Tie::PureDB::Read
78  
79 PROTOTYPES: ENABLE
80  
81 BOOT:
82 8 sv_setpv(get_sv("Tie::PureDB::version", TRUE), the_puredb_PACKAGE_STRING );
83  
84  
85 void
86 xs_new(dbfile)
87 INPUT:
88 char * dbfile
89 PREINIT:
90 PureDB * db;
91 int ret;
92  
93 PPCODE:
94 {
95 14 New(0,db,1,PureDB);
96 14 ret = puredb_open(db, dbfile);
97  
98 14 ▶ if( ret != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( ret != 0 ) { 10 4 14
99 10 puredb_close(db);
100 10 Safefree(db);
101 } else {
102 4 ▶ XPUSHs(sv_2mortal(newSViv(PTR2IV(db))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(PTR2IV(db)))); 0 4 4
103 }
104 }
105  
106  
107 IV
108 xs_puredb_getsize(db)
109 CASE:
110 IV db;
111 PREINIT:
112 PureDB* foy;
113 CODE:
114 4 foy = INT2PTR(PureDB*,db);
115 4 ▶ RETVAL = foy->size;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = foy->size; 2 2 4
116 OUTPUT:
117 RETVAL
118  
119  
120 void
121 xs_puredb_find(db, tofind )
122 CASE:
123 IV db;
124 SV* tofind;
125  
126 PREINIT:
127 size_t retlen;
128 off_t retpos;
129 int ret;
130 PureDB* foy;
131  
132 PPCODE:
133 {
134 57 foy = INT2PTR(PureDB*,db);
135  
136 57 ret = puredb_find(foy, SvPVX((SV*)tofind), sv_len((SV*)tofind), &retpos, &retlen );
137  
138 57 switch(ret){
139 57 case 0:
140 57 ▶ EXTEND(SP,2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP,2); 0 57 57
141 57 PUSHs(sv_2mortal(newSViv((IV) retpos)));
142 57 PUSHs(sv_2mortal(newSViv((IV) retlen)));
143  
144 57 break;
145  
146 0 case -1:
147 0 sv_setpvf(get_sv("!", TRUE),"Key '%s' was not found", tofind);
148 0 break;
149  
150 0 case -2:
151 0 sv_setpvf(get_sv("!", TRUE),"the database is corrupted", tofind);
152 0 break;
153  
154 0 case -3:
155 0 sv_setpvf(get_sv("!", TRUE),"a system error occured: %s", strerror(errno));
156 0 break;
157 }
158 }
159  
160  
161 void
162 xs_puredb_read(db, offset, len)
163 CASE:
164 IV db;
165 off_t offset
166 size_t len
167 PREINIT:
168 char* founddata;
169 PureDB* foy;
170 PPCODE:
171 {
172 51 foy = (PureDB*) db;
173 51 ▶ if( ( founddata = puredb_read(foy, offset, len) ) != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if( ( founddata = puredb_read(foy, offset, len) ) != NULL ) { 51 0 51
174 #ifdef PureDEBUG
175 ReadDebug( "xs_puredb_read" , db, foy );
176 ReadDebug2( founddata, offset, len );
177 #endif
178 51 ▶ XPUSHs(sv_2mortal( newSVpvn(founddata,len) ));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal( newSVpvn(founddata,len) )); 0 51 51
179 } else {
180 0 sv_setpvf(get_sv("!", TRUE),"Unknown error reading offset=%d, lenth=%d ",offset,len);
181 }
182  
183 }
184  
185  
186 void
187 xs_free(db)
188 CASE:
189 IV db;
190 PREINIT:
191 PureDB* foy;
192 CODE:
193 {
194 4 foy = INT2PTR(PureDB*,db);
195 4 puredb_close(foy);
196 4 Safefree(foy);
197 }
198  
199  
200  
201 MODULE = Tie::PureDB PACKAGE = Tie::PureDB::Write
202  
203 PROTOTYPES: ENABLE
204  
205 void
206 xs_new(file_index, file_data, file_final)
207 INPUT:
208 char * file_index
209 char * file_data
210 char * file_final
211  
212 PREINIT:
213 PureDBW * dbw;
214 int ret;
215  
216 PPCODE:
217 {
218 14 New(0,dbw,1,PureDBW);
219 14 ret = puredbw_open( dbw, file_index, file_data, file_final );
220  
221 14 ▶ if( ret != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( ret != 0 ) { 0 14 14
222 0 puredbw_close(dbw);
223 0 Safefree(dbw);
224 } else {
225 14 ▶ EXTEND(SP,1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP,1); 0 14 14
226 14 PUSHs(sv_2mortal(newSViv(PTR2IV(dbw))));
227 }
228 }
229  
230  
231  
232  
233  
234  
235 void
236 xs_puredbw_add(dbw, key, content)
237 CASE:
238 IV dbw;
239 SV* key;
240 SV* content;
241 PREINIT:
242 PureDBW* foy;
243 int ret;
244 PPCODE:
245 25 foy = INT2PTR(PureDBW*,dbw);
246 25 ret = puredbw_add(foy, SvPVX(key), sv_len(key), SvPVX(content), sv_len(content));
247  
248 25 ▶ if( ret != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( ret != 0 ) { 0 25 25
249 0 sv_setpvf(
250 get_sv("!", TRUE),
251 "Error adding '%s' => '%s' (ret=%d)(%s)",
252 SvPVX(key),
253 SvPVX(content),
254 ret,
255 strerror(errno)
256 );
257 } else {
258 25 ▶ XPUSHs(&PL_sv_yes);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_yes); 0 25 25
259 }
260  
261 void
262 xs_free(dbw)
263 CASE:
264 IV dbw;
265 PREINIT:
266 PureDBW* foy;
267 int ret;
268 CODE:
269 14 foy = INT2PTR(PureDBW*,dbw);
270 14 puredbw_close(foy);
271 14 puredbw_free(foy);
272 14 Safefree(foy);
273  
« blib/lib/Tie/PureDB.pm ↑ Summary