Files
Viper_VRTE/os/__init__.vp
T
TermiNexus e71daeea0e docs(includes): R11 注释英文化——75 个文件的中文注释改写为英文短注释
按规范「注释只讲解作用,不掺杂历史原因,尽量英文」:把 includes 下 75 个 .vp 的中文 # 注释与三引号 docstring 改写为英文,每个注释块压到 2 行以内。

保留要点:作用、约束、危险警告(不得/必须/注意)、ABI 与类型约定(如 BYTEPTR | t.CPtr = i8** 的跨模块坍缩坑)、位宽/长度约定、fail-fast 要求、内存与句柄所有权。

未动:文件头注释(规范豁免,仍是中文)、运行时中文字符串常量(printf / fatal_error / MlirFail 等用户可见消息,属代码字面量)。

验证:独立脚本比对 HEAD 与工作区的代码骨架(剥离 # 注释与三引号 docstring 后的非空行)——75 个文件逐字节一致;行尾 CRLF/LF 未被改动;全文剩余中文仅 547 行头注释、0 条行尾注释。bootstrap 三重重编 SUCCESS;全量 77/77 顺序回归通过。
2026-10-05 16:18:44 +08:00

131 lines
4.3 KiB
Plaintext

import t, c
from stdint import *
import platmacro
import memhub
import os.path
import os._win32
import os._posix
# os package entry point
# Dispatches to _win32 or _posix backend based on platmacro.IS_WINDOWS
# os.path is for cross-platform pure string operations, can be used directly
# Module-level global MBuddy pointer (initialized by the user in main)
_mbuddy: memhub.MemBuddy*
# =============================================================================
# File/Directory Attribute Query
# =============================================================================
def exists(path: str) -> bool:
"""Check if the file/directory exists"""
if platmacro.IS_WINDOWS:
return os._win32.exists(path)
return os._posix.exists(path)
def isdir(path: str) -> bool:
"""Check if the path is a directory"""
if platmacro.IS_WINDOWS:
return os._win32.isdir(path)
return os._posix.isdir(path)
def isfile(path: str) -> bool:
"""Check if the path is a regular file"""
if platmacro.IS_WINDOWS:
return os._win32.isfile(path)
return os._posix.isfile(path)
def getsize(path: str) -> int64:
"""Returns the file size (in bytes). Returns -1 on failure."""
if platmacro.IS_WINDOWS:
return os._win32.getsize(path)
return os._posix.getsize(path)
# =============================================================================
# Working Directory
# =============================================================================
def getcwd() -> str:
"""Return the current working directory (memory allocated via _mbuddy)"""
if platmacro.IS_WINDOWS:
return os._win32.getcwd()
return os._posix.getcwd_impl()
def chdir(path: str) -> int:
"""Switch the current working directory. Returns 0 if successful, -1 if it fails."""
if platmacro.IS_WINDOWS:
return os._win32.chdir(path)
return os._posix.chdir_impl(path)
# =============================================================================
# File/Directory Operations
# =============================================================================
def mkdir(path: str) -> int:
"""Create a directory. Returns 0 on success, -1 on failure."""
if platmacro.IS_WINDOWS:
return os._win32.mkdir(path)
return os._posix.mkdir_impl(path)
def remove(path: str) -> int:
"""Delete the file. Returns 0 if successful, -1 if it fails."""
if platmacro.IS_WINDOWS:
return os._win32.remove(path)
return os._posix.remove(path)
def rmdir(path: str) -> int:
"""Delete empty directories. Returns 0 if successful, -1 if it fails."""
if platmacro.IS_WINDOWS:
return os._win32.rmdir(path)
return os._posix.rmdir_impl(path)
def rename(old_path: str, new_path: str) -> int:
"""Rename/move a file or directory. Returns 0 on success, -1 on failure."""
if platmacro.IS_WINDOWS:
return os._win32.rename(old_path, new_path)
return os._posix.rename_impl(old_path, new_path)
def chmod(path: str, mode: int) -> int:
"""Modify file permissions. Returns 0 on success, -1 on failure.
On Windows, only the read-only bit (S_IWUSR) is handled;
On POSIX, full permission bits are used."""
if platmacro.IS_WINDOWS:
return os._win32.chmod(path, mode)
return os._posix.chmod_impl(path, mode)
# =============================================================================
# Table of Contents Listing
# =============================================================================
def listdir(path: str, out_names: str*, max_count: ULONG) -> ULONG:
"""List file/subdirectory names under a directory.
out_names is a caller-provided char** array (each entry points to a filename allocated via _mbuddy); max_count is its capacity. Return the count, or 0 on failure."""
if platmacro.IS_WINDOWS:
return os._win32.listdir(path, out_names, max_count)
return os._posix.listdir(path, out_names, max_count)
# =============================================================================
# Command Execution
# =============================================================================
def system(cmd: str) -> int:
"""Execute a command line command (via the C runtime system()).
Output goes to the terminal (inherits parent stdout/stderr). Return the exit code (0 = success)."""
if platmacro.IS_WINDOWS:
return os._win32.system(cmd)
return os._posix.system_impl(cmd)