| 1 |
|
| #ifdef __cplusplus |
| 2 |
|
| extern "C" { |
| 3 |
|
| #endif |
| 4 |
|
| |
| 5 |
|
| #define PERL_NO_GET_CONTEXT |
| 6 |
|
| #include "EXTERN.h" |
| 7 |
|
| #include "perl.h" |
| 8 |
|
| #include "XSUB.h" |
| 9 |
|
| |
| 10 |
|
| #ifdef __cplusplus |
| 11 |
|
| } |
| 12 |
|
| #endif |
| 13 |
|
| |
| 14 |
|
| static unsigned int STATUS_DONE ; |
| 15 |
|
| |
| 16 |
|
| // ------------------------------------------------------------------------ |
| 17 |
|
| // get_next() |
| 18 |
|
| // |
| 19 |
|
| // Called repeatedly to access successive elements in the data set. |
| 20 |
|
| // Should only be called after calling get_first() or a warning will |
| 21 |
|
| // be raised and (undef, STATUS_DONE) returned. |
| 22 |
|
| // #------------------------------------------------------------------------ |
| 23 |
|
| // |
| 24 |
|
| // sub get_next { |
| 25 |
|
| // my $self = shift; |
| 26 |
|
| // my ($max, $index) = @$self{ qw( MAX INDEX ) }; |
| 27 |
|
| // my $data = $self->{ _DATASET }; |
| 28 |
|
| // |
| 29 |
|
| // # warn about incorrect usage |
| 30 |
|
| // unless (defined $index) { |
| 31 |
|
| // my ($pack, $file, $line) = caller(); |
| 32 |
|
| // warn("iterator get_next() called before get_first() at $file line $line\n"); |
| 33 |
|
| // return (undef, Template::Constants::STATUS_DONE); ## RETURN ## |
| 34 |
|
| // } |
| 35 |
|
| // |
| 36 |
|
| // # if there's still some data to go... |
| 37 |
|
| // if ($index < $max) { |
| 38 |
|
| // # update counters and flags |
| 39 |
|
| // $index++; |
| 40 |
|
| // @$self{ qw( INDEX COUNT FIRST LAST ) } |
| 41 |
|
| // = ( $index, $index + 1, 0, $index == $max ? 1 : 0 ); |
| 42 |
|
| // @$self{ qw( PREV NEXT ) } = @$data[ $index - 1, $index + 1 ]; |
| 43 |
|
| // return $data->[ $index ]; ## RETURN ## |
| 44 |
|
| // } |
| 45 |
|
| // else { |
| 46 |
|
| // return (undef, Template::Constants::STATUS_DONE); ## RETURN ## |
| 47 |
|
| // } |
| 48 |
|
| // } |
| 49 |
|
| |
| 50 |
|
| |
| 51 |
|
| MODULE = Template::Iterator::XS PACKAGE = Template::Iterator::XS |
| 52 |
|
| |
| 53 |
|
| void |
| 54 |
|
| _init(status_done) |
| 55 |
|
| int status_done |
| 56 |
|
| PPCODE: |
| 57 |
2 |
| STATUS_DONE = status_done; |
| 58 |
|
| |
| 59 |
|
| void |
| 60 |
|
| get_next(self) |
| 61 |
|
| SV *self |
| 62 |
|
| PPCODE: |
| 63 |
|
| { |
| 64 |
|
| HV * obj; |
| 65 |
|
| AV * dataset; |
| 66 |
|
| SV ** ref; |
| 67 |
|
| int max, index; |
| 68 |
0 |
▶ | if ( !SvROK(self)) croak("panic: bad object"); |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !SvROK(self)) croak("panic: bad object"); |
0 |
0 |
0 |
|
| 69 |
0 |
| obj = (HV*)SvRV(self); |
| 70 |
0 |
▶ | if ( SvTYPE((SV*)obj) != SVt_PVHV) croak("panic: object is not a hash"); |
Branch: 1 branch
| Branch | True | False | Total |
| if ( SvTYPE((SV*)obj) != SVt_PVHV) croak("panic: object is not a hash"); |
0 |
0 |
0 |
|
| 71 |
0 |
| ref = hv_fetch( obj, "MAX", strlen("MAX"), 0); |
| 72 |
0 |
▶ | if (ref == NULL) croak("panic: no item %s", "MAX"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ref == NULL) croak("panic: no item %s", "MAX"); |
0 |
0 |
0 |
|
| 73 |
0 |
| max = SvIV(*ref); |
| 74 |
0 |
| ref = hv_fetch( obj, "INDEX", strlen("INDEX"), 0); |
| 75 |
0 |
▶ | if (ref == NULL) croak("panic: no item %s", "INDEX"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ref == NULL) croak("panic: no item %s", "INDEX"); |
0 |
0 |
0 |
|
| 76 |
|
| |
| 77 |
0 |
▶ | if (!SvOK(*ref)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(*ref)) { |
0 |
0 |
0 |
|
| 78 |
0 |
| warn("iterator get_next() called before get_first()"); |
| 79 |
0 |
▶ | EXTEND(sp,2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(sp,2); |
0 |
0 |
0 |
|
| 80 |
0 |
| PUSHs(&PL_sv_undef); |
| 81 |
0 |
| PUSHs(sv_2mortal(newSViv(STATUS_DONE))); |
| 82 |
0 |
| PUTBACK; |
| 83 |
0 |
| return; |
| 84 |
|
| } |
| 85 |
0 |
| index = SvIV(*ref); |
| 86 |
|
| |
| 87 |
0 |
▶ | if ( index >= max ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( index >= max ) { |
0 |
0 |
0 |
|
| 88 |
0 |
▶ | EXTEND(sp,2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(sp,2); |
0 |
0 |
0 |
|
| 89 |
0 |
| PUSHs(&PL_sv_undef); |
| 90 |
0 |
| PUSHs(sv_2mortal(newSViv(STATUS_DONE))); |
| 91 |
0 |
| PUTBACK; |
| 92 |
0 |
| return; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
0 |
| ref = hv_fetch( obj, "_DATASET", strlen("_DATASET"), 0); |
| 96 |
0 |
▶ | if (ref == NULL) croak("panic: no item %s", "_DATASET"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ref == NULL) croak("panic: no item %s", "_DATASET"); |
0 |
0 |
0 |
|
| 97 |
0 |
▶ | if ( SvTYPE(SvRV(*ref)) != SVt_PVAV) croak("panic: _DATASET is not an array %d %d", SvTYPE(*ref)); |
Branch: 1 branch
| Branch | True | False | Total |
| if ( SvTYPE(SvRV(*ref)) != SVt_PVAV) croak("panic: _DATASET is not an array %d %d", SvTYPE(*ref)); |
0 |
0 |
0 |
|
| 98 |
0 |
| dataset = (AV*)(SvRV(*ref)); |
| 99 |
|
| |
| 100 |
0 |
| index++; |
| 101 |
0 |
| hv_store( obj, "INDEX", strlen("INDEX"), newSViv(index), 0); |
| 102 |
0 |
| hv_store( obj, "COUNT", strlen("COUNT"), newSViv(index+1), 0); |
| 103 |
0 |
| hv_store( obj, "FIRST", strlen("FIRST"), newSViv(0), 0); |
| 104 |
0 |
| hv_store( obj, "LAST", strlen("LAST"), newSViv((index == max) ? 1 : 0), 0); |
| 105 |
|
| |
| 106 |
0 |
| ref = av_fetch( dataset, index - 1, 0); |
| 107 |
0 |
▶ | hv_store( obj, "PREV", strlen("PREV"), newSVsv(ref ? *ref : &PL_sv_undef), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| hv_store( obj, "PREV", strlen("PREV"), newSVsv(ref ? *ref : &PL_sv_undef), 0); |
0 |
0 |
0 |
|
| 108 |
0 |
| ref = av_fetch( dataset, index + 1, 0); |
| 109 |
0 |
▶ | hv_store( obj, "NEXT", strlen("NEXT"), newSVsv(ref ? *ref : &PL_sv_undef), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| hv_store( obj, "NEXT", strlen("NEXT"), newSVsv(ref ? *ref : &PL_sv_undef), 0); |
0 |
0 |
0 |
|
| 110 |
0 |
| ref = av_fetch( dataset, index, 0); |
| 111 |
0 |
▶ | XPUSHs(sv_2mortal(newSVsv(ref ? *ref : &PL_sv_undef))); |
Branch: 2 branches
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVsv(ref ? *ref : &PL_sv_undef))); |
0 |
0 |
0 |
| XPUSHs(sv_2mortal(newSVsv(ref ? *ref : &PL_sv_undef))); |
0 |
0 |
0 |
|
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
|
| |