5
Quiero convertir Zval en char *. ¿Cómo hago eso en mi extensión de php?Convertir Zval en char *
Quiero convertir Zval en char *. ¿Cómo hago eso en mi extensión de php?Convertir Zval en char *
Es el zval representa una cadena, puede utilizar Z_STRVAL
(o Z_STRVAL_P
/Z_STRVAL_PP
si tiene un zval*
/zval**
).
De lo contrario, puede que tenga que convertir el zval antes:
zval *var;
char *cstr;
int cstrlen;
/* ... */
if (Z_TYPE_P(var) != IS_STRING) {
convert_to_string(var);
}
cstr = Z_STRVAL_P(var);
cstrlen = Z_STRLEN_P(var);
Si no desea cambiar el zval original y desea cambiar la cadena C resultante, que puede hacer:
zval *var, *varcopy;
char *cstr;
int cstrlen;
if (Z_TYPE_P(var) != IS_STRING) {
ALLOC_INIT_ZVAL(varcopy);
*varcopy = *var;
INIT_PZVAL(varcopy); /* reset refcount and clear is_ref */
zval_copy_ctor(varcopy);
convert_to_string(varcopy);
} else {
varcopy = var;
}
cstrlen = Z_STRLEN_P(varcopy);
cstr = estrndup(Z_STRVAL_P(varcopy), cstrlen);
if (varcopy != var) {
zval_ptr_dtor(&varcopy);
}