| 1 |
#include "EXTERN.h" |
| 2 |
#include "perl.h" |
| 3 |
#include "XSUB.h" |
| 4 |
#include "ppport.h" |
| 5 |
#include "ffi_platypus.h" |
| 6 |
#include "ffi_platypus_guts.h" |
| 7 |
|
| 8 |
XS(ffi_pl_record_accessor_string_ro) |
| 9 |
{ |
| 10 |
ffi_pl_record_member *member; |
| 11 |
SV *self; |
| 12 |
char *ptr1; |
| 13 |
char **ptr2; |
| 14 |
|
| 15 |
dVAR; dXSARGS; |
| 16 |
|
| 17 |
if(items == 0) |
| 18 |
croak("This is a method, you must provide at least the object"); |
| 19 |
|
| 20 |
member = (ffi_pl_record_member*) CvXSUBANY(cv).any_ptr; |
| 21 |
|
| 22 |
self = ST(0); |
| 23 |
if(SvROK(self)) |
| 24 |
self = SvRV(self); |
| 25 |
|
| 26 |
if(!SvOK(self)) |
| 27 |
croak("Null record error"); |
| 28 |
|
| 29 |
ptr1 = (char*) SvPV_nolen(self); |
| 30 |
ptr2 = (char**) &ptr1[member->offset]; |
| 31 |
|
| 32 |
if(items > 1) |
| 33 |
{ |
| 34 |
croak("member is read only"); |
| 35 |
} |
| 36 |
|
| 37 |
if(GIMME_V == G_VOID) |
| 38 |
XSRETURN_EMPTY; |
| 39 |
|
| 40 |
if(*ptr2 != NULL) |
| 41 |
XSRETURN_PV(*ptr2); |
| 42 |
else |
| 43 |
XSRETURN_EMPTY; |
| 44 |
} |
| 45 |
|
| 46 |
XS(ffi_pl_record_accessor_string_rw) |
| 47 |
{ |
| 48 |
ffi_pl_record_member *member; |
| 49 |
SV *self; |
| 50 |
SV *arg; |
| 51 |
char *ptr1; |
| 52 |
char **ptr2; |
| 53 |
char *arg_ptr; |
| 54 |
STRLEN len; |
| 55 |
|
| 56 |
dVAR; dXSARGS; |
| 57 |
|
| 58 |
if(items == 0) |
| 59 |
croak("This is a method, you must provide at least the object"); |
| 60 |
|
| 61 |
member = (ffi_pl_record_member*) CvXSUBANY(cv).any_ptr; |
| 62 |
|
| 63 |
self = ST(0); |
| 64 |
if(SvROK(self)) |
| 65 |
self = SvRV(self); |
| 66 |
|
| 67 |
if(!SvOK(self)) |
| 68 |
croak("Null record error"); |
| 69 |
|
| 70 |
ptr1 = (char*) SvPV_nolen(self); |
| 71 |
ptr2 = (char**) &ptr1[member->offset]; |
| 72 |
|
| 73 |
if(items > 1) |
| 74 |
{ |
| 75 |
if(SvREADONLY(self)) |
| 76 |
{ |
| 77 |
croak("record is read-only"); |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
arg = ST(1); |
| 82 |
if(SvOK(arg)) |
| 83 |
{ |
| 84 |
arg_ptr = SvPV(arg, len); |
| 85 |
*ptr2 = realloc(*ptr2, len+1); |
| 86 |
(*ptr2)[len] = 0; |
| 87 |
memcpy(*ptr2, arg_ptr, len); |
| 88 |
} |
| 89 |
else if(*ptr2 != NULL) |
| 90 |
{ |
| 91 |
free(*ptr2); |
| 92 |
*ptr2 = NULL; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if(GIMME_V == G_VOID) |
| 98 |
XSRETURN_EMPTY; |
| 99 |
|
| 100 |
if(*ptr2 != NULL) |
| 101 |
XSRETURN_PV(*ptr2); |
| 102 |
else |
| 103 |
XSRETURN_EMPTY; |
| 104 |
} |
| 105 |
|
| 106 |
XS(ffi_pl_record_accessor_string_fixed) |
| 107 |
{ |
| 108 |
ffi_pl_record_member *member; |
| 109 |
SV *self; |
| 110 |
SV *arg; |
| 111 |
SV *value; |
| 112 |
char *ptr1; |
| 113 |
char *ptr2; |
| 114 |
char *arg_ptr; |
| 115 |
STRLEN len; |
| 116 |
|
| 117 |
dVAR; dXSARGS; |
| 118 |
|
| 119 |
if(items == 0) |
| 120 |
croak("This is a method, you must provide at least the object"); |
| 121 |
|
| 122 |
member = (ffi_pl_record_member*) CvXSUBANY(cv).any_ptr; |
| 123 |
|
| 124 |
self = ST(0); |
| 125 |
if(SvROK(self)) |
| 126 |
self = SvRV(self); |
| 127 |
|
| 128 |
if(!SvOK(self)) |
| 129 |
croak("Null record error"); |
| 130 |
|
| 131 |
ptr1 = (char*) SvPV_nolen(self); |
| 132 |
ptr2 = (char*) &ptr1[member->offset]; |
| 133 |
|
| 134 |
if(items > 1) |
| 135 |
{ |
| 136 |
if(SvREADONLY(self)) |
| 137 |
{ |
| 138 |
croak("record is read-only"); |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
arg = ST(1); |
| 143 |
if(SvOK(arg)) |
| 144 |
{ |
| 145 |
arg_ptr = SvPV(ST(1), len); |
| 146 |
if(len > member->count) |
| 147 |
len = member->count; |
| 148 |
memcpy(ptr2, arg_ptr, len); |
| 149 |
} |
| 150 |
else |
| 151 |
{ |
| 152 |
croak("Cannot assign undef to a fixed string field"); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if(GIMME_V == G_VOID) |
| 158 |
XSRETURN_EMPTY; |
| 159 |
|
| 160 |
value = sv_newmortal(); |
| 161 |
sv_setpvn(value, ptr2, member->count); |
| 162 |
ST(0) = value; |
| 163 |
XSRETURN(1); |
| 164 |
} |