feat(stdlib): add bounded std:fs/file read, write and copy
This commit is contained in:
1 parent
8dd3d5eb76
commit
cd90f059b4
9 files changed
+248
-11
No files matched your search
@@ -0,0 +1,111 @@
|
||||
---
|
||||
import:
|
||||
utf8: "../text/utf8.mq.md"
|
||||
---
|
||||
|
||||
## 读文本
|
||||
|
||||
`(路径)`
|
||||
|
||||
以 UTF-8 读取一个有界的项目文件。文件在打开时整文件载入,上限 8 MiB;
|
||||
结果使用可恢复 `Result` 形状。
|
||||
|
||||
**流 = resource.open("file", 路径, ["read"])**
|
||||
|
||||
**内存 = memory.new(128, 128)**
|
||||
|
||||
**读取 = resource.read(流, 内存, 0, 8388608)**
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
|
||||
1. `not 读取.ok`
|
||||
|
||||
*读取*
|
||||
|
||||
2. `not 关闭.ok`
|
||||
|
||||
*关闭*
|
||||
|
||||
3. `true`
|
||||
|
||||
**文本 = utf8.解码(内存, 0, 读取.value.count)**
|
||||
|
||||
1. `not 文本.ok`
|
||||
|
||||
*文本*
|
||||
|
||||
2. `true`
|
||||
|
||||
*{ok: true, value: 文本.value}*
|
||||
|
||||
## 写文本
|
||||
|
||||
`(路径, 文本)`
|
||||
|
||||
把文本按 UTF-8 写入项目文件。路径必须已经通过 `fs_write` 授权;
|
||||
文件只在 `resource.close` 成功时创建或覆盖,写入缓冲上限 8 MiB。
|
||||
|
||||
**内存 = memory.new(128, 128)**
|
||||
|
||||
**长度 = utf8.编码(文本, 内存, 0)**
|
||||
|
||||
**流 = resource.open("file", 路径, ["write"])**
|
||||
|
||||
**写入 = resource.write(流, 内存, 0, 长度)**
|
||||
|
||||
**关闭 = resource.close(流)**
|
||||
|
||||
1. `not 写入.ok`
|
||||
|
||||
*写入*
|
||||
|
||||
2. `not 关闭.ok`
|
||||
|
||||
*关闭*
|
||||
|
||||
3. `true`
|
||||
|
||||
*{ok: true, value: {count: 写入.value.count}}*
|
||||
|
||||
## 复制文件
|
||||
|
||||
`(源, 目标)`
|
||||
|
||||
按字节复制一个有界的项目文件。源读取和目标写入分别需要
|
||||
`fs_read`/`fs_write` 授权,结果值与 [`写文本`](#写文本) 相同。
|
||||
|
||||
**输入 = resource.open("file", 源, ["read"])**
|
||||
|
||||
**内存 = memory.new(128, 128)**
|
||||
|
||||
**读取 = resource.read(输入, 内存, 0, 8388608)**
|
||||
|
||||
**关闭输入 = resource.close(输入)**
|
||||
|
||||
1. `not 读取.ok`
|
||||
|
||||
*读取*
|
||||
|
||||
2. `not 关闭输入.ok`
|
||||
|
||||
*关闭输入*
|
||||
|
||||
3. `true`
|
||||
|
||||
**输出 = resource.open("file", 目标, ["write"])**
|
||||
|
||||
**写入 = resource.write(输出, 内存, 0, 读取.value.count)**
|
||||
|
||||
**关闭输出 = resource.close(输出)**
|
||||
|
||||
1. `not 写入.ok`
|
||||
|
||||
*写入*
|
||||
|
||||
2. `not 关闭输出.ok`
|
||||
|
||||
*关闭输出*
|
||||
|
||||
3. `true`
|
||||
|
||||
*{ok: true, value: {count: 写入.value.count}}*
|
||||
Reference in new issue
Block a user