如何从Gforth网站读取原始代码? [英] How do I read raw code from a website in Gforth?

查看:96
本文介绍了如何从Gforth网站读取原始代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个词

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

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

where 'add n' is the site name buffer, 'buff max' is the buffer to which the ASCII text should be read to, 'n' is the number of bytes that was read and flag is true if operation succeeded.

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

Is this possible in Gforth in Linux, Android or Windows?

推荐答案

只是方法列表

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

Just a list of approaches

The most easy proper way should be to use an HTTP-client library (or binding) in Forth (if any). It seems that some kind of such library exists in the Gforth repository — see netlib/httpclient.fs. Obviously it doesn't work with HTTPS.

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

The next way is to use an appropriate external shared library, for example libcurl. It is well-known tool that supports a lot of protocols (the binding and some usage examples can be also found in SP-Forth).

下一个方法是使用系统调用并生成一个子进程(就资源使用而言,效率不是很高). Gforth具有 这个词.用法示例:

The next way is to use a system call and spawn a child process (not so efficient approach in terms of resources usage). Gforth has system word for that. Usage example:

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

网页HTML代码将被打印到标准输出.不幸的是,使用 outfile-execute 进行重定向在这种情况下是行不通的(system单词的实现似乎不完整或执行不力).

The web-page HTML code will be printed to stdout. Unfortunately, redirection with outfile-execute doesn't work in this case (it looks like incomplete or weak implementation of the system word).

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

So, a temporary file should be used:

S" curl --silent http://example.com/ > tmp" system

之后,可以将文件内容读取到给定的缓冲区中.

After that the file content can be read into a given buffer.

以下是概念性实现:

: 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 )展开给定模板.

这篇关于如何从Gforth网站读取原始代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆