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 中可能吗?
最简单的正确方法应该是在 Forth 中使用 HTTP 客户端库(或绑定)(如果有)。Gforth 存储库 \xe2\x80\x94 中似乎存在某种此类库,请参阅netlib/httpclient.fs。显然它不适用于 HTTPS。
\n\n下一种方法是使用适当的外部共享库,例如libcurl。它是众所周知的工具,支持很多协议(绑定和一些使用示例也可以在 SP-Forth 中找到)。
\n\n下一种方法是使用系统调用并生成子进程(就资源使用而言不是那么有效的方法)。Gforthsystem对此有说法。使用示例:
S" curl http://example.com/" system\nRun Code Online (Sandbox Code Playgroud)\n\n网页 HTML 代码将打印到 stdout。不幸的是,重定向outfile-execute在这种情况下不起作用(看起来这个system词的实现不完整或薄弱)。
因此,应该使用临时文件:
\n\nS" curl --silent http://example.com/ > tmp" system\nRun 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;\nRun Code Online (Sandbox Code Playgroud)\n\n其中interpolate ( i*x d-txt1 -- d-txt2 )扩展给定的模板。
| 归档时间: |
|
| 查看次数: |
396 次 |
| 最近记录: |