如何从 Gforth 网站读取原始代码?

Leh*_*ehs 2 networking forth gforth

我想要一个像这样的词

read-site ( add n buff max -- n flag )
Run Code Online (Sandbox Code Playgroud)

其中“add n”是站点名称缓冲区,“buff max”是应读取 ASCII 文本的缓冲区,“n”是读取的字节数,flag如果操作成功则为 true。

这在 Linux、Android 或 Windows 的 Gforth 中可能吗?

ruv*_*vim 5

只是方法列表

\n\n

最简单的正确方法应该是在 Forth 中使用 HTTP 客户端库(或绑定)(如果有)。Gforth 存储库 \xe2\x80\x94 中似乎存在某种此类库,请参阅netlib/httpclient.fs。显然它不适用于 HTTPS。

\n\n

下一种方法是使用适当的外部共享库,例如libcurl。它是众所周知的工具,支持很多协议(绑定和一些使用示例也可以在 SP-Forth 中找到)。

\n\n

下一种方法是使用系统调用并生成子进程(就资源使用而言不是那么有效的方法)。Gforthsystem对此有说法。使用示例:

\n\n
S" curl http://example.com/" system\n
Run Code Online (Sandbox Code Playgroud)\n\n

网页 HTML 代码将打印到 stdout。不幸的是,重定向outfile-execute在这种情况下不起作用(看起来这个system词的实现不完整或薄弱)。

\n\n

因此,应该使用临时文件:

\n\n
S" curl --silent http://example.com/ > tmp" system\n
Run Code Online (Sandbox Code Playgroud)\n\n

之后,可以将文件内容读入给定的缓冲区。

\n\n

一个概念性的实现如下:

\n\n
: read-url ( d-buffer d-txt-url -- d-txt-webpage )\n  s" curl --silent {} > tmp" interpolate system\n  over >r \\ keep buf addr\n  s" tmp" open-file throw dup >r read-file throw\n  r> close-file throw\n  r> swap\n;\n
Run Code Online (Sandbox Code Playgroud)\n\n

其中interpolate ( i*x d-txt1 -- d-txt2 )扩展给定的模板。

\n