我想要一个像这样的词
read-site ( add n buff max -- n flag )
其中“add n”是站点名称缓冲区,“buff max”是应读取 ASCII 文本的缓冲区,“n”是读取的字节数,flag
如果操作成功则为 true。
这在 Linux、Android 或 Windows 中的 Gforth 中可能吗?
只是方法列表
最简单的正确方法应该是在 Forth 中使用 HTTP 客户端库(或绑定)(如果有)。 Gforth 存储库中似乎存在某种此类库 - 请参阅netlib/httpclient.fs https://github.com/earl/gforth-mirror/blob/master/netlib/httpclient.fs。显然它不适用于 HTTPS。
下一个方法是使用适当的外部共享库,例如libcurl https://curl.haxx.se/libcurl/。它是众所周知的工具,支持很多协议(绑定和一些使用示例也可以在 SP-Forth 中找到)。
下一种方法是使用系统调用并生成子进程(就资源使用而言不是那么有效的方法)。格福斯有system https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Passing-Commands-to-the-OS.html这个词。使用示例:
S" curl http://example.com/" system
网页 HTML 代码将打印到 stdout。不幸的是,重定向outfile-execute https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Redirection.html在这种情况下不起作用(看起来不完整或执行不力system
word).
因此,应该使用临时文件:
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
;
where interpolate ( i*x d-txt1 -- d-txt2 )
扩展给定的模板。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)