gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:41:08 25 4
gpt4 key购买 nike

我想要一个像这样的词

read-site ( add n buff max -- n flag )

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

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

最佳答案

只是方法列表

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

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

下一个方法是使用系统调用并生成子进程(在资源使用方面不是那么有效的方法)。 Gforth有system这个词。使用示例:

S" curl http://example.com/" system

网页 HTML 代码将打印到 stdout。不幸的是,重定向为 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
;

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

关于networking - 如何从 Gforth 网站读取原始代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48587263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com