我想要一个词
read-site ( add n buff max -- n flag )
其中“add n”是站点名称缓冲区,“buff max”是应该读取 ASCII 文本的缓冲区,“n”是读取的字节数,flag
如果操作成功则为真。
这在 Linux、Android 或 Windows 的 Gforth 中是否可行?
我想要一个词
read-site ( add n buff max -- n flag )
其中“add n”是站点名称缓冲区,“buff max”是应该读取 ASCII 文本的缓冲区,“n”是读取的字节数,flag
如果操作成功则为真。
这在 Linux、Android 或 Windows 的 Gforth 中是否可行?
最简单的正确方法应该是在 Forth(如果有的话)中使用 HTTP 客户端库(或绑定)。Gforth 存储库中似乎存在某种此类库 - 请参阅netlib/httpclient.fs。显然它不适用于 HTTPS。
下一种方法是使用适当的外部共享库,例如libcurl。它是支持很多协议的知名工具(绑定和一些使用示例也可以在 SP-Forth 中找到)。
下一种方法是使用系统调用并产生一个子进程(在资源使用方面不是那么有效的方法)。Gforth对此有话要说system
。使用示例:
S" curl http://example.com/" system
网页 HTML 代码将打印到标准输出。不幸的是,outfile-execute
在这种情况下,重定向不起作用(它看起来像system
单词的不完整或弱实现)。
因此,应使用临时文件:
S" curl --silent http://example.com/ > tmp" system
之后,可以将文件内容读入给定的缓冲区。
一个概念实现如下:
: read-url ( d-buffer d-txt-url -- d-txt-webpage )
s" curl --silent {} > tmp" interpolate system
over >r \ keep buf addr
s" tmp" open-file throw dup >r read-file throw
r> close-file throw
r> swap
;
whereinterpolate ( i*x d-txt1 -- d-txt2 )
扩展给定的模板。