| 1 | /* $Id$ |
|---|
| 2 | * |
|---|
| 3 | * Copyright (c) 2005-2008 Daisuke Maki <daisuke@endeworks.jp> |
|---|
| 4 | * All rights reserved. |
|---|
| 5 | */ |
|---|
| 6 | #ifndef __SENNA_RECORDS_C__ |
|---|
| 7 | #define __SENNA_RECORDS_C__ |
|---|
| 8 | |
|---|
| 9 | #include "senna-perl.h" |
|---|
| 10 | |
|---|
| 11 | SV * |
|---|
| 12 | SennaPerl_Records_new(pkg, records) |
|---|
| 13 | char *pkg; |
|---|
| 14 | sen_records *records; |
|---|
| 15 | { |
|---|
| 16 | SennaPerl_Records *xs; |
|---|
| 17 | SV *sv; |
|---|
| 18 | |
|---|
| 19 | Newz(1234, xs, 1, SennaPerl_Records); |
|---|
| 20 | |
|---|
| 21 | xs->records = records; |
|---|
| 22 | |
|---|
| 23 | XS_STRUCT2OBJ(sv, pkg, xs); |
|---|
| 24 | SvREADONLY_on(sv); |
|---|
| 25 | return sv; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | SV * |
|---|
| 29 | SennaPerl_Records_next(obj) |
|---|
| 30 | SV *obj; |
|---|
| 31 | { |
|---|
| 32 | SV *sv; |
|---|
| 33 | char keybuf[SEN_MAX_KEY_SIZE]; |
|---|
| 34 | int score; |
|---|
| 35 | SennaPerl_Records *records = XS_STATE(SennaPerl_Records *, obj); |
|---|
| 36 | |
|---|
| 37 | if (sen_records_next(records->records, &keybuf, SEN_MAX_KEY_SIZE, &score)) { |
|---|
| 38 | dSP; |
|---|
| 39 | ENTER; |
|---|
| 40 | SAVETMPS; |
|---|
| 41 | PUSHMARK(SP); |
|---|
| 42 | |
|---|
| 43 | XPUSHs(sv_2mortal(newSVpv("Senna::Record", 13))); |
|---|
| 44 | XPUSHs(sv_2mortal(newSVpv("key", 3))); |
|---|
| 45 | XPUSHs(sv_2mortal(newSVpvf("%s", keybuf))); |
|---|
| 46 | XPUSHs(sv_2mortal(newSVpv("score", 5))); |
|---|
| 47 | XPUSHs(sv_2mortal(newSViv(score))); |
|---|
| 48 | PUTBACK; |
|---|
| 49 | if (call_method("Senna::Record::new", G_SCALAR) <= 0) { |
|---|
| 50 | croak ("Senna::Record::new did not return an object "); |
|---|
| 51 | } |
|---|
| 52 | SPAGAIN; |
|---|
| 53 | sv = POPs; |
|---|
| 54 | |
|---|
| 55 | if (! SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV ) { |
|---|
| 56 | croak ("Senna::Record::new did not return a proper object"); |
|---|
| 57 | } |
|---|
| 58 | SvREFCNT_inc(sv); |
|---|
| 59 | |
|---|
| 60 | FREETMPS; |
|---|
| 61 | LEAVE; |
|---|
| 62 | |
|---|
| 63 | return sv; |
|---|
| 64 | } |
|---|
| 65 | return &PL_sv_undef; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | SV * |
|---|
| 69 | SennaPerl_Records_nhits(obj) |
|---|
| 70 | SV *obj; |
|---|
| 71 | { |
|---|
| 72 | SennaPerl_Records *records = XS_STATE(SennaPerl_Records *, obj); |
|---|
| 73 | return newSViv( sen_records_nhits( records->records ) ); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void |
|---|
| 77 | SennaPerl_Records_DESTROY(obj) |
|---|
| 78 | SV *obj; |
|---|
| 79 | { |
|---|
| 80 | SennaPerl_Records *records = XS_STATE(SennaPerl_Records *, obj); |
|---|
| 81 | sen_records_close(records->records); |
|---|
| 82 | Safefree(records); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | SV * |
|---|
| 86 | SennaPerl_Records_curr_key(obj) |
|---|
| 87 | SV *obj; |
|---|
| 88 | { |
|---|
| 89 | char keybuf[SEN_MAX_KEY_SIZE]; |
|---|
| 90 | SennaPerl_Records *records = XS_STATE(SennaPerl_Records *, obj); |
|---|
| 91 | if (sen_records_curr_key(records->records, keybuf, SEN_MAX_KEY_SIZE)) { |
|---|
| 92 | return newSVpvf("%s", keybuf); |
|---|
| 93 | } |
|---|
| 94 | return &PL_sv_undef; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | SV * |
|---|
| 98 | SennaPerl_Records_sort(obj, limit, optarg) |
|---|
| 99 | SV *obj; |
|---|
| 100 | int limit; |
|---|
| 101 | HV *optarg; |
|---|
| 102 | { |
|---|
| 103 | SV *sv; |
|---|
| 104 | SennaPerl_Records *records = XS_STATE(SennaPerl_Records *, obj); |
|---|
| 105 | sen_sort_optarg *o = NULL; |
|---|
| 106 | if (optarg != NULL) { |
|---|
| 107 | Newz(1234, o, 1, sen_sort_optarg); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | sv = sen_rc2obj(sen_records_sort(records->records, limit, o)); |
|---|
| 111 | |
|---|
| 112 | if (o) { |
|---|
| 113 | Safefree(o); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | return sv; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | #endif /* __SENNA_RECORDS_C__ */ |
|---|