使用 Perl XS 和 PerlIO 制作旧库 [英] Making an old library work with Perl XS and PerlIO
问题描述
我是一个 XS 初学者,我正在考虑更改一个现有的 XS 模块,该模块大量使用 15 年以上的底层 C 库(实际上该模块基本上只是粘到这个库上).问题是我希望能够使用 PerlIO 字符串技巧,例如:
I am rather an XS beginner and I am looking into changing an existing XS module which uses a 15+ year old underlying C library heavily (in fact the module is basically just glue to this library). The problem is that I would like to be able to use PerlIO string trickery like:
open($fh, '<', \$string);
然后将 $fh
传递给库需要 FILE
的 XS 粘合.问题是 XS 有:
and then pass $fh
to the XS glue where the library is expecting FILE
. The problem is that the XS has:
int
_parse (entry_ref, filename, file, preserve=FALSE)
SV * entry_ref;
char * filename;
FILE * file;
boolean preserve;
我认为它需要:
PerlIO * file;
这当然行不通,因为肯定不止于此.当我查看库中的 _parse
代码时,结果是:
This doesn't work of course as there must be more to it than that. When I look at the _parse
code in the library, it ends up in:
AST * bt_parse_entry (FILE * infile,
char * filename,
btshort options,
boolean * status)
{
AST * entry_ast = NULL;
static int * err_counts = NULL;
static FILE * prev_file = NULL;
用 FILE
再次输入.现在我必须开始的基本问题是 - 这甚至可能在不更改库的情况下进行吗?也就是说,我可以通过更改 XS 来从字符串 PerlIO 行为中获得伪文件句柄吗?
with FILE
types again. Now the basic question I have have to start with is - is this even possible without changing the library; that is, can I get pseudo-filehandle from strings PerlIO behaviour just by changing the XS?
推荐答案
Perl API 提供 PerlIO_exportFILE()
(实现),它可以将带有文件描述符的 PerlIO 句柄转换为 stdio FILE
指针.由于 PerlIO::Scalar 是没有文件描述符的内存中"文件句柄转换无法成功.传递 PerlIO::Scalar
句柄的唯一可移植方式是将其刷新到临时文件.不太便携的方法是使用支持回调的 stdio,如 BSD 实现,funopen(3).
The Perl API provides PerlIO_exportFILE()
(Implementation) which can convert a PerlIO handle with a file descriptor to a stdio FILE
pointer. Since PerlIO::Scalar is an "in-memory" file handle without a file descriptor the conversion cannot succeed. The only portable way to pass a PerlIO::Scalar
handle would be to flush it to a temporary file. The less portable way would be to use a stdio that supports callbacks, like the BSD implementation, funopen(3).
这篇关于使用 Perl XS 和 PerlIO 制作旧库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!