Archivos e I/O
Todo acceso a archivos está gateado por la capacidad file (file.read / file.write para mínimo privilegio). Las escrituras son atómicas (archivo temporal + rename).
-- Doc example: files & I/O. Needs file capability (deny-by-default).
intent: "doc example: files and I/O"
require file("_doctest_io.txt")
write_file("_doctest_io.txt", "hi from the io demo")
print("read back → " + read_file("_doctest_io.txt"))
test "write then read round-trips (atomic write)"
write_file("_doctest_io.txt", "hello\nworld")
assert_eq(read_file("_doctest_io.txt"), "hello\nworld")
assert(file_exists("_doctest_io.txt"))
Leer
read_file(path) -- archivo completo como texto
read_file(path, 1, 100) -- rango de líneas: 1–100 (1-based, conserva EOLs)
read_file(path, 500) -- desde la línea 500 hasta el final
read_file_bytes(path) -- byte-exacto, para binarios
Escribir y editar
write_file(path, text) -- sobrescritura atómica
append_file(path, text) -- append
edit_file(path, old, new) -- reemplazo de string exacto (old debe ser único; replace_all? para todos)
Navegar y buscar
file_exists(path) -- bool
file_info(path) -- {name, is_dir, size, ...}
list_dir(path) -- lista de {name, is_dir, size}
grep(target, pattern, opts?) -- {matches: [{file, line, col, text}], truncated} — por línea, streaming
Los scopes de ruta son fieles — un escape con .. fuera del scope declarado se deniega. Ver Capacidades.