feat(stdlib): add bounded std:fs/file read, write and copy

This commit is contained in:
Marqdo Agent committed 2026-11-21 08:10:10 +08:00
1 parent 8dd3d5eb76
commit cd90f059b4
9 files changed
+248 -11

No files matched your search

+1
View File
@@ -8,6 +8,7 @@ module; a mismatch is a hard error.
- `text.mq.md` — compatibility UTF-8 encoding into `Memory`
- `text/utf8.mq.md` — `.mq.md` UTF-8 encoder and validating decoder
- `io.mq.md` — compatibility facade over `stream` and `console`
- `fs/file.mq.md` — bounded project-file read/write/copy over `resource.*`
- `io/stream.mq.md` — `写全部` / `写文本` with short-write and close checking
- `io/console.mq.md` — `读取行` / `打印` over `stdio/stdin` and `stdio/stdout`
- `demo.mq.md` — side-by-side comparison with builtin `打印`
+111
View File
@@ -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}}*
+1 -1
View File
@@ -1,5 +1,5 @@
{
"name": "marqdo-stdlib",
"version": "0.1.0",
"digest": "sha256:91f1f76daf694efac0c4d8c937d2b2f61d07efd8c105ce407e55690e0a3ad2f5"
"digest": "sha256:5e16dc37ef7fcecf8f6839e4065bf02aa552660c5e872b4c17e1eb71dda9d7e7"
}
+16
View File
@@ -0,0 +1,16 @@
---
import:
fs: "std:fs/file"
---
## main
**写入 = fs.写文本("out/fs-a.txt", "你好\n")**
**读取 = fs.读文本("out/fs-a.txt")**
**复制 = fs.复制文件("out/fs-a.txt", "out/fs-b.txt")**
**复制读取 = fs.读文本("out/fs-b.txt")**
*[写入.value.count, 读取.value, 复制.value.count, 复制读取.value]*