From 5eaef2c9004b123c8022dda17ed863a50613feaf Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 14 Apr 2024 14:19:08 +0800 Subject: [PATCH] update `os` --- docs/modules/os.md | 12 ++++++++++++ src/io.cpp | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/docs/modules/os.md b/docs/modules/os.md index 01f94a5d..e40464f7 100644 --- a/docs/modules/os.md +++ b/docs/modules/os.md @@ -43,6 +43,18 @@ Check if the given path exists. Returns the basename of the given path. +### `os.path.isdir(path: str)` + +Check if the given path is a directory. + +### `os.path.isfile(path: str)` + +Check if the given path is a file. + +### `os.path.abspath(path: str)` + +Returns the absolute path of the given path. + ## Other functions diff --git a/src/io.cpp b/src/io.cpp index d22650ff..7637001e 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -226,6 +226,23 @@ void add_module_os(VM* vm){ std::filesystem::path path(CAST(Str&, args[0]).sv()); return VAR(path.filename().string()); }); + + vm->bind_func<1>(path_obj, "isdir", [](VM* vm, ArgsView args){ + std::filesystem::path path(CAST(Str&, args[0]).sv()); + bool isdir = std::filesystem::is_directory(path); + return VAR(isdir); + }); + + vm->bind_func<1>(path_obj, "isfile", [](VM* vm, ArgsView args){ + std::filesystem::path path(CAST(Str&, args[0]).sv()); + bool isfile = std::filesystem::is_regular_file(path); + return VAR(isfile); + }); + + vm->bind_func<1>(path_obj, "abspath", [](VM* vm, ArgsView args){ + std::filesystem::path path(CAST(Str&, args[0]).sv()); + return VAR(std::filesystem::absolute(path).string()); + }); } #else