From bc0927f407eecaa704d97c60a0e2bcc9b72fe5a7 Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 6 Jan 2025 15:07:09 +0800 Subject: [PATCH 1/7] wip --- GoldfishStandardLibrary.tmu | 104 +++++++++++++++++++++++++++++++++++- goldfish/liii/path.scm | 10 +++- src/goldfish.hpp | 39 ++++++++++++++ 3 files changed, 151 insertions(+), 2 deletions(-) diff --git a/GoldfishStandardLibrary.tmu b/GoldfishStandardLibrary.tmu index 8e62c57..266406e 100644 --- a/GoldfishStandardLibrary.tmu +++ b/GoldfishStandardLibrary.tmu @@ -2692,7 +2692,9 @@ \ \ path-\string - \ \ path-dir? path-file? path-exists? path-getsize + \ \ path-dir? path-file? path-exists? + + \ \ path-getsize path-read-text ) @@ -3109,6 +3111,104 @@ \; + + + <\goldfish-chunk|goldfish/liii/path.scm|true|true> + (define path-read-text + + \ \ (typed-lambda ((path string?)) + + \ \ \ \ (if (not (file-exists? path)) + + \ \ \ \ \ \ (file-not-found-error + + \ \ \ \ \ \ \ \ (string-append "No such file or directory: '" path "'")) + + \ \ \ \ \ \ (g_path-read-text path)))) + + \; + + + <\cpp-chunk|src/goldfish.hpp|true|true> + static s7_pointer f_path_read_text(s7_scheme* sc, s7_pointer args) { + + \ \ const char* path = s7_string (s7_car (args)); + + \ \ if (!path) { + + \ \ \ \ return s7_make_boolean(sc, false); // 路径无效 + + \ \ } + + \; + + \ \ tb_file_ref_t file = tb_file_init(path, TB_FILE_MODE_RO); + + \ \ if (file == tb_null) { + + \ \ \ \ // TODO: warning on the tb_file_init failure + + \ \ \ \ return s7_make_boolean(sc, false); + + \ \ } + + \; + + \ \ tb_file_sync (file); + + \; + + \ \ tb_size_t size = tb_file_size(file); + + \ \ if (size == 0) { + + \ \ \ \ tb_file_exit (file); + + \ \ \ \ return s7_make_string (sc, ""); + + \ \ } + + \; + + \ \ tb_byte_t* buffer = new tb_byte_t[size + 1]; + + \ \ tb_size_t real_size = tb_file_read (file, buffer, size); + + \ \ buffer[real_size] = '\\0'; + + \; + + \ \ tb_file_exit(file); + + \ \ std::string content (reinterpret_cast\char*\(buffer), real_size); + + \ \ delete[] buffer; + + \; + + \ \ return s7_make_string(sc, content.c_str()); + + } + + \; + + + <\cpp-chunk|src/goldfish.hpp|true|true> + inline void + + glue_path_read_text(s7_scheme* sc) { + + \ \ const char* name = "g_path-read-text"; + + \ \ const char* desc = "(g_path-read-text path) =\ string, read the content of the file at the given path"; + + \ \ s7_define_function(sc, name, f_path_read_text, 1, 0, false, desc); + + } + + \; + + <\cpp-chunk|src/goldfish.hpp|true|true> @@ -3122,6 +3222,8 @@ \ \ glue_path_getsize (sc); + \ \ glue_path_read_text (sc); + } \; diff --git a/goldfish/liii/path.scm b/goldfish/liii/path.scm index b1759dc..691690c 100644 --- a/goldfish/liii/path.scm +++ b/goldfish/liii/path.scm @@ -18,7 +18,8 @@ (export make-path path-parts path-absolute? path->string - path-dir? path-file? path-exists? path-getsize + path-dir? path-file? path-exists? + path-getsize path-read-text ) (import (liii error) (liii vector) (liii string) (liii list)) (begin @@ -90,6 +91,13 @@ (string-append "No such file or directory: '" path "'")) (g_path-getsize path)))) +(define path-read-text + (typed-lambda ((path string?)) + (if (not (file-exists? path)) + (file-not-found-error + (string-append "No such file or directory: '" path "'")) + (g_path-read-text path)))) + ) ; end of begin ) ; end of define-library diff --git a/src/goldfish.hpp b/src/goldfish.hpp index c56429d..7da5d22 100644 --- a/src/goldfish.hpp +++ b/src/goldfish.hpp @@ -566,11 +566,50 @@ glue_path_getsize (s7_scheme* sc) { glue_define (sc, name, desc, f_path_getsize, 1, 0); } +static s7_pointer f_path_read_text(s7_scheme* sc, s7_pointer args) { + const char* path = s7_string (s7_car (args)); + if (!path) { + return s7_make_boolean(sc, false); // 路径无效 + } + + tb_file_ref_t file = tb_file_init(path, TB_FILE_MODE_RO); + if (file == tb_null) { + // TODO: warning on the tb_file_init failure + return s7_make_boolean(sc, false); + } + + tb_file_sync (file); + + tb_size_t size = tb_file_size(file); + if (size == 0) { + tb_file_exit (file); + return s7_make_string (sc, ""); + } + + tb_byte_t* buffer = new tb_byte_t[size + 1]; + tb_size_t real_size = tb_file_read (file, buffer, size); + buffer[real_size] = '\0'; + + tb_file_exit(file); + std::string content (reinterpret_cast(buffer), real_size); + delete[] buffer; + + return s7_make_string(sc, content.c_str()); +} + +inline void +glue_path_read_text(s7_scheme* sc) { + const char* name = "g_path-read-text"; + const char* desc = "(g_path-read-text path) => string, read the content of the file at the given path"; + s7_define_function(sc, name, f_path_read_text, 1, 0, false, desc); +} + inline void glue_liii_path (s7_scheme* sc) { glue_isfile (sc); glue_isdir (sc); glue_path_getsize (sc); + glue_path_read_text (sc); } void From 46d8b784bb083d3b4bc2e453a914174d22ca1314 Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 6 Jan 2025 15:14:59 +0800 Subject: [PATCH 2/7] path-read-text in (liii path) --- GoldfishStandardLibrary.tmu | 28 ++++++++++++++++++++++++++++ tests/goldfish/liii/path-test.scm | 13 +++++++++++++ 2 files changed, 41 insertions(+) diff --git a/GoldfishStandardLibrary.tmu b/GoldfishStandardLibrary.tmu index 266406e..05b1954 100644 --- a/GoldfishStandardLibrary.tmu +++ b/GoldfishStandardLibrary.tmu @@ -3129,6 +3129,34 @@ \; + <\scm-chunk|tests/goldfish/liii/path-test.scm|true|false> + (let () + + \ \ (define file-name "中文文件名.txt") + + \ \ (define file-content "你好,世界!") + + \; + + \ \ (define temp-dir (os-temp-dir)) + + \ \ (define file-path (string-append temp-dir "/" file-name)) + + \; + + \ \ (with-output-to-file file-path + + \ \ \ \ (lambda () + + \ \ \ \ \ \ (display file-content))) + + \ \ + + \ \ (check (path-read-text file-path) =\ file-content)) + + \; + + <\cpp-chunk|src/goldfish.hpp|true|true> static s7_pointer f_path_read_text(s7_scheme* sc, s7_pointer args) { diff --git a/tests/goldfish/liii/path-test.scm b/tests/goldfish/liii/path-test.scm index 830f601..9572347 100644 --- a/tests/goldfish/liii/path-test.scm +++ b/tests/goldfish/liii/path-test.scm @@ -54,5 +54,18 @@ (check-true (> (path-getsize "C:/Windows") 0)) (check-true (> (path-getsize "C:\\Windows\\System32\\drivers\\etc\\hosts") 0))) +(let () + (define file-name "中文文件名.txt") + (define file-content "你好,世界!") + + (define temp-dir (os-temp-dir)) + (define file-path (string-append temp-dir "/" file-name)) + + (with-output-to-file file-path + (lambda () + (display file-content))) + + (check (path-read-text file-path) => file-content)) + (check-report) From 6d923c88dc7c82f510316108ab4bbe930af46ab4 Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 6 Jan 2025 15:33:18 +0800 Subject: [PATCH 3/7] wip --- GoldfishStandardLibrary.tmu | 136 ++++++++++++++++++++++++++---- goldfish/liii/path.scm | 9 +- src/goldfish.hpp | 46 ++++++++++ tests/goldfish/liii/path-test.scm | 12 +-- 4 files changed, 176 insertions(+), 27 deletions(-) diff --git a/GoldfishStandardLibrary.tmu b/GoldfishStandardLibrary.tmu index 05b1954..b29ef90 100644 --- a/GoldfishStandardLibrary.tmu +++ b/GoldfishStandardLibrary.tmu @@ -2694,7 +2694,7 @@ \ \ path-dir? path-file? path-exists? - \ \ path-getsize path-read-text + \ \ path-getsize path-read-text path-write-text ) @@ -3129,28 +3129,16 @@ \; - <\scm-chunk|tests/goldfish/liii/path-test.scm|true|false> - (let () - - \ \ (define file-name "中文文件名.txt") - - \ \ (define file-content "你好,世界!") + <\scm-chunk|tests/goldfish/liii/path-test.scm|true|true> + (let ((file-name "中文文件名.txt") - \; + \ \ \ \ \ \ (file-content "你好,世界!")) \ \ (define temp-dir (os-temp-dir)) \ \ (define file-path (string-append temp-dir "/" file-name)) - \; - - \ \ (with-output-to-file file-path - - \ \ \ \ (lambda () - - \ \ \ \ \ \ (display file-content))) - - \ \ + \ \ (path-write-text file-path file-content) \ \ (check (path-read-text file-path) =\ file-content)) @@ -3237,6 +3225,118 @@ \; + + + <\goldfish-chunk|goldfish/liii/path.scm|true|true> + (define path-write-text + + \ \ (typed-lambda ((path string?) (content string?)) + + \ \ \ \ (if (not (file-exists? path)) + + \ \ \ \ \ \ (file-not-found-error + + \ \ \ \ \ \ \ \ (string-append "No such file or directory: '" path "'")) + + \ \ \ \ \ \ (g_path-write-text path content)))) + + \; + + + <\cpp-chunk|src/goldfish.hpp|true|true> + static s7_pointer f_path_write_text(s7_scheme* sc, s7_pointer args) { + + \ \ const char* path = s7_string(s7_car(args)); + + \ \ if (!path) { + + \ \ \ \ return s7_make_integer(sc, -1); + + \ \ } + + \; + + \ \ const char* content = s7_string(s7_cadr (args)); + + \ \ if (!content) { + + \ \ \ \ return s7_make_integer(sc, -1); + + \ \ } + + \; + + \ \ tb_file_ref_t file = tb_file_init(path, TB_FILE_MODE_WO \| TB_FILE_MODE_CREAT \| TB_FILE_MODE_TRUNC); + + \ \ if (file == tb_null) { + + \ \ \ \ return s7_make_integer(sc, -1); + + \ \ } + + \; + + \ \ tb_filelock_ref_t lock = tb_filelock_init(file); + + \ \ if (tb_filelock_enter(lock, TB_FILELOCK_MODE_EX) == tb_false) { + + \ \ \ \ tb_filelock_exit(lock); + + \ \ \ \ tb_file_exit(file); + + \ \ \ \ return s7_make_integer(sc, -1); + + \ \ } + + \; + + \ \ tb_size_t content_size = strlen(content); + + \ \ tb_size_t written_size = tb_file_writ(file, reinterpret_cast\const tb_byte_t*\(content), content_size); + + \; + + \ \ bool release_success = tb_filelock_leave(lock); + + \ \ tb_filelock_exit(lock); + + \ \ bool exit_success = tb_file_exit(file); + + \; + + \ \ // 检查写入是否成功 + + \ \ if (written_size == content_size && release_success && exit_success) { + + \ \ \ \ return s7_make_integer(sc, written_size); // 返回实际写入的字节数 + + \ \ } else { + + \ \ \ \ return s7_make_integer(sc, -1); // 写入失败,返回 -1 + + \ \ } + + } + + \; + + + <\cpp-chunk|src/goldfish.hpp|true|true> + inline void glue_path_write_text(s7_scheme* sc) { + + \ \ const char* name = "g_path-write-text"; + + \ \ const char* desc = "(g_path-write-text path content) =\ integer,\\ + + write content to the file at the given path and return the number of bytes written, or -1 on failure"; + + \ \ s7_define_function(sc, name, f_path_write_text, 2, 0, false, desc); + + } + + \; + + <\cpp-chunk|src/goldfish.hpp|true|true> @@ -3252,6 +3352,8 @@ \ \ glue_path_read_text (sc); + \ \ glue_path_write_text(sc); + } \; diff --git a/goldfish/liii/path.scm b/goldfish/liii/path.scm index 691690c..a7dcaeb 100644 --- a/goldfish/liii/path.scm +++ b/goldfish/liii/path.scm @@ -19,7 +19,7 @@ make-path path-parts path-absolute? path->string path-dir? path-file? path-exists? - path-getsize path-read-text + path-getsize path-read-text path-write-text ) (import (liii error) (liii vector) (liii string) (liii list)) (begin @@ -98,6 +98,13 @@ (string-append "No such file or directory: '" path "'")) (g_path-read-text path)))) +(define path-write-text + (typed-lambda ((path string?) (content string?)) + (if (not (file-exists? path)) + (file-not-found-error + (string-append "No such file or directory: '" path "'")) + (g_path-write-text path content)))) + ) ; end of begin ) ; end of define-library diff --git a/src/goldfish.hpp b/src/goldfish.hpp index 7da5d22..f073101 100644 --- a/src/goldfish.hpp +++ b/src/goldfish.hpp @@ -604,12 +604,58 @@ glue_path_read_text(s7_scheme* sc) { s7_define_function(sc, name, f_path_read_text, 1, 0, false, desc); } +static s7_pointer f_path_write_text(s7_scheme* sc, s7_pointer args) { + const char* path = s7_string(s7_car(args)); + if (!path) { + return s7_make_integer(sc, -1); + } + + const char* content = s7_string(s7_cadr (args)); + if (!content) { + return s7_make_integer(sc, -1); + } + + tb_file_ref_t file = tb_file_init(path, TB_FILE_MODE_WO | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + if (file == tb_null) { + return s7_make_integer(sc, -1); + } + + tb_filelock_ref_t lock = tb_filelock_init(file); + if (tb_filelock_enter(lock, TB_FILELOCK_MODE_EX) == tb_false) { + tb_filelock_exit(lock); + tb_file_exit(file); + return s7_make_integer(sc, -1); + } + + tb_size_t content_size = strlen(content); + tb_size_t written_size = tb_file_writ(file, reinterpret_cast(content), content_size); + + bool release_success = tb_filelock_leave(lock); + tb_filelock_exit(lock); + bool exit_success = tb_file_exit(file); + + // 检查写入是否成功 + if (written_size == content_size && release_success && exit_success) { + return s7_make_integer(sc, written_size); // 返回实际写入的字节数 + } else { + return s7_make_integer(sc, -1); // 写入失败,返回 -1 + } +} + +inline void glue_path_write_text(s7_scheme* sc) { + const char* name = "g_path-write-text"; + const char* desc = "(g_path-write-text path content) => integer,\ +write content to the file at the given path and return the number of bytes written, or -1 on failure"; + s7_define_function(sc, name, f_path_write_text, 2, 0, false, desc); +} + inline void glue_liii_path (s7_scheme* sc) { glue_isfile (sc); glue_isdir (sc); glue_path_getsize (sc); glue_path_read_text (sc); + glue_path_write_text(sc); } void diff --git a/tests/goldfish/liii/path-test.scm b/tests/goldfish/liii/path-test.scm index 9572347..84dfe88 100644 --- a/tests/goldfish/liii/path-test.scm +++ b/tests/goldfish/liii/path-test.scm @@ -54,17 +54,11 @@ (check-true (> (path-getsize "C:/Windows") 0)) (check-true (> (path-getsize "C:\\Windows\\System32\\drivers\\etc\\hosts") 0))) -(let () - (define file-name "中文文件名.txt") - (define file-content "你好,世界!") - +(let ((file-name "中文文件名.txt") + (file-content "你好,世界!")) (define temp-dir (os-temp-dir)) (define file-path (string-append temp-dir "/" file-name)) - - (with-output-to-file file-path - (lambda () - (display file-content))) - + (path-write-text file-path file-content) (check (path-read-text file-path) => file-content)) (check-report) From 04970f8507234da04b1130aef1912f81bf46842f Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 6 Jan 2025 16:07:22 +0800 Subject: [PATCH 4/7] wip --- GoldfishStandardLibrary.tmu | 40 ++++++++++++++----------------- goldfish/liii/path.scm | 5 +--- src/goldfish.hpp | 26 ++++++++++---------- tests/goldfish/liii/path-test.scm | 3 ++- 4 files changed, 34 insertions(+), 40 deletions(-) diff --git a/GoldfishStandardLibrary.tmu b/GoldfishStandardLibrary.tmu index b29ef90..370e201 100644 --- a/GoldfishStandardLibrary.tmu +++ b/GoldfishStandardLibrary.tmu @@ -3140,7 +3140,9 @@ \ \ (path-write-text file-path file-content) - \ \ (check (path-read-text file-path) =\ file-content)) + \ \ (check (path-read-text file-path) =\ file-content) + + \ \ (delete-file file-path)) \; @@ -3152,7 +3154,7 @@ \ \ if (!path) { - \ \ \ \ return s7_make_boolean(sc, false); // 路径无效 + \ \ \ \ return s7_make_boolean(sc, false); \ \ } @@ -3232,21 +3234,17 @@ \ \ (typed-lambda ((path string?) (content string?)) - \ \ \ \ (if (not (file-exists? path)) - - \ \ \ \ \ \ (file-not-found-error - - \ \ \ \ \ \ \ \ (string-append "No such file or directory: '" path "'")) - - \ \ \ \ \ \ (g_path-write-text path content)))) + \ \ \ \ (g_path-write-text path content))) \; <\cpp-chunk|src/goldfish.hpp|true|true> - static s7_pointer f_path_write_text(s7_scheme* sc, s7_pointer args) { + static s7_pointer + + f_path_write_text (s7_scheme* sc, s7_pointer args) { - \ \ const char* path = s7_string(s7_car(args)); + \ \ const char* path = s7_string (s7_car (args)); \ \ if (!path) { @@ -3256,7 +3254,7 @@ \; - \ \ const char* content = s7_string(s7_cadr (args)); + \ \ const char* content= s7_string (s7_cadr (args)); \ \ if (!content) { @@ -3290,29 +3288,27 @@ \; - \ \ tb_size_t content_size = strlen(content); + \ \ tb_size_t content_size= strlen(content); - \ \ tb_size_t written_size = tb_file_writ(file, reinterpret_cast\const tb_byte_t*\(content), content_size); + \ \ tb_size_t written_size= tb_file_writ(file, reinterpret_cast\const tb_byte_t*\(content), content_size); \; - \ \ bool release_success = tb_filelock_leave(lock); + \ \ bool release_success= tb_filelock_leave (lock); - \ \ tb_filelock_exit(lock); + \ \ tb_filelock_exit (lock); - \ \ bool exit_success = tb_file_exit(file); + \ \ bool exit_success= tb_file_exit(file); \; - \ \ // 检查写入是否成功 - \ \ if (written_size == content_size && release_success && exit_success) { - \ \ \ \ return s7_make_integer(sc, written_size); // 返回实际写入的字节数 + \ \ \ \ return s7_make_integer(sc, written_size); \ \ } else { - \ \ \ \ return s7_make_integer(sc, -1); // 写入失败,返回 -1 + \ \ \ \ return s7_make_integer(sc, -1); \ \ } @@ -3352,7 +3348,7 @@ \ \ glue_path_read_text (sc); - \ \ glue_path_write_text(sc); + \ \ glue_path_write_text (sc); } diff --git a/goldfish/liii/path.scm b/goldfish/liii/path.scm index a7dcaeb..8d28ccc 100644 --- a/goldfish/liii/path.scm +++ b/goldfish/liii/path.scm @@ -100,10 +100,7 @@ (define path-write-text (typed-lambda ((path string?) (content string?)) - (if (not (file-exists? path)) - (file-not-found-error - (string-append "No such file or directory: '" path "'")) - (g_path-write-text path content)))) + (g_path-write-text path content))) ) ; end of begin ) ; end of define-library diff --git a/src/goldfish.hpp b/src/goldfish.hpp index f073101..29bcc27 100644 --- a/src/goldfish.hpp +++ b/src/goldfish.hpp @@ -569,7 +569,7 @@ glue_path_getsize (s7_scheme* sc) { static s7_pointer f_path_read_text(s7_scheme* sc, s7_pointer args) { const char* path = s7_string (s7_car (args)); if (!path) { - return s7_make_boolean(sc, false); // 路径无效 + return s7_make_boolean(sc, false); } tb_file_ref_t file = tb_file_init(path, TB_FILE_MODE_RO); @@ -604,13 +604,14 @@ glue_path_read_text(s7_scheme* sc) { s7_define_function(sc, name, f_path_read_text, 1, 0, false, desc); } -static s7_pointer f_path_write_text(s7_scheme* sc, s7_pointer args) { - const char* path = s7_string(s7_car(args)); +static s7_pointer +f_path_write_text (s7_scheme* sc, s7_pointer args) { + const char* path = s7_string (s7_car (args)); if (!path) { return s7_make_integer(sc, -1); } - const char* content = s7_string(s7_cadr (args)); + const char* content= s7_string (s7_cadr (args)); if (!content) { return s7_make_integer(sc, -1); } @@ -627,18 +628,17 @@ static s7_pointer f_path_write_text(s7_scheme* sc, s7_pointer args) { return s7_make_integer(sc, -1); } - tb_size_t content_size = strlen(content); - tb_size_t written_size = tb_file_writ(file, reinterpret_cast(content), content_size); + tb_size_t content_size= strlen(content); + tb_size_t written_size= tb_file_writ(file, reinterpret_cast(content), content_size); - bool release_success = tb_filelock_leave(lock); - tb_filelock_exit(lock); - bool exit_success = tb_file_exit(file); + bool release_success= tb_filelock_leave (lock); + tb_filelock_exit (lock); + bool exit_success= tb_file_exit(file); - // 检查写入是否成功 if (written_size == content_size && release_success && exit_success) { - return s7_make_integer(sc, written_size); // 返回实际写入的字节数 + return s7_make_integer(sc, written_size); } else { - return s7_make_integer(sc, -1); // 写入失败,返回 -1 + return s7_make_integer(sc, -1); } } @@ -655,7 +655,7 @@ glue_liii_path (s7_scheme* sc) { glue_isdir (sc); glue_path_getsize (sc); glue_path_read_text (sc); - glue_path_write_text(sc); + glue_path_write_text (sc); } void diff --git a/tests/goldfish/liii/path-test.scm b/tests/goldfish/liii/path-test.scm index 84dfe88..9256e82 100644 --- a/tests/goldfish/liii/path-test.scm +++ b/tests/goldfish/liii/path-test.scm @@ -59,7 +59,8 @@ (define temp-dir (os-temp-dir)) (define file-path (string-append temp-dir "/" file-name)) (path-write-text file-path file-content) - (check (path-read-text file-path) => file-content)) + (check (path-read-text file-path) => file-content) + (delete-file file-path)) (check-report) From 214c6aba0120b4049553112a14e381aef5311ba5 Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 6 Jan 2025 16:11:39 +0800 Subject: [PATCH 5/7] wip --- GoldfishStandardLibrary.tmu | 2 +- tests/goldfish/liii/path-test.scm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/GoldfishStandardLibrary.tmu b/GoldfishStandardLibrary.tmu index 370e201..b8771c4 100644 --- a/GoldfishStandardLibrary.tmu +++ b/GoldfishStandardLibrary.tmu @@ -3136,7 +3136,7 @@ \ \ (define temp-dir (os-temp-dir)) - \ \ (define file-path (string-append temp-dir "/" file-name)) + \ \ (define file-path (string-append temp-dir (string (os-sep)) file-name)) \ \ (path-write-text file-path file-content) diff --git a/tests/goldfish/liii/path-test.scm b/tests/goldfish/liii/path-test.scm index 9256e82..4959f6b 100644 --- a/tests/goldfish/liii/path-test.scm +++ b/tests/goldfish/liii/path-test.scm @@ -57,7 +57,7 @@ (let ((file-name "中文文件名.txt") (file-content "你好,世界!")) (define temp-dir (os-temp-dir)) - (define file-path (string-append temp-dir "/" file-name)) + (define file-path (string-append temp-dir (string (os-sep)) file-name)) (path-write-text file-path file-content) (check (path-read-text file-path) => file-content) (delete-file file-path)) From 17b7d224513e5eb7a2a243db4a5f0b736e39260b Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 6 Jan 2025 16:43:26 +0800 Subject: [PATCH 6/7] wip --- GoldfishStandardLibrary.tmu | 36 +++++++++++++++++++++++++-------- goldfish/liii/os.scm | 4 ++-- goldfish/scheme/boot.scm | 2 +- src/goldfish.hpp | 13 +++++++----- tests/goldfish/liii/os-test.scm | 6 ++++++ 5 files changed, 45 insertions(+), 16 deletions(-) diff --git a/GoldfishStandardLibrary.tmu b/GoldfishStandardLibrary.tmu index b8771c4..bcc1fd3 100644 --- a/GoldfishStandardLibrary.tmu +++ b/GoldfishStandardLibrary.tmu @@ -250,7 +250,7 @@ \ \ \ \ \ \ #f - \ \ \ \ \ \ (if (g_access path 4) ; R_OK + \ \ \ \ \ \ (if (g_access path 1) ; R_OK \ \ \ \ \ \ \ \ \ \ #t @@ -1856,17 +1856,31 @@ \ \ (cond ((eq? mode 'F_OK) (g_access path 0)) - \ \ \ \ \ \ \ \ ((eq? mode 'X_OK) (g_access path 1)) + \ \ \ \ \ \ \ \ ((eq? mode 'X_OK) (g_access path 128)) \ \ \ \ \ \ \ \ ((eq? mode 'W_OK) (g_access path 2)) - \ \ \ \ \ \ \ \ ((eq? mode 'R_OK) (g_access path 4)) + \ \ \ \ \ \ \ \ ((eq? mode 'R_OK) (g_access path 1)) \ \ \ \ \ \ \ \ (else (error 'value-error "Allowed mode 'F_OK, 'X_OK,'W_OK, 'R_OK")))) \; + <\scm-chunk|tests/goldfish/liii/os-test.scm|true|true> + (when (os-linux?) + + \ \ (check-true (access "/root" 'F_OK)) + + \ \ (check-false (access "/root" 'R_OK)) + + \ \ (check-false (access "/root" 'W_OK)) + + \ \ (check-true (access "bin/goldfish" 'X_OK))) + + \; + + <\cpp-chunk|src/goldfish.hpp|true|true> static s7_pointer @@ -1876,15 +1890,21 @@ \ \ int \ \ \ \ \ \ \ \ mode \ = s7_integer ((s7_cadr (args))); - #ifdef TB_CONFIG_OS_WINDOWS + \ \ bool ret= false; - \ \ bool ret= (_access (path_c, mode) == 0); + \ \ if (mode == 0) { - #else + \ \ \ \ tb_file_info_t info; - \ \ bool \ \ \ \ \ \ \ \ \ \ ret= (access (path_c, mode) == 0); + \ \ \ \ ret= tb_file_info (path_c, &info); - #endif + \ \ } else { + + \ \ \ \ ret= tb_file_access (path_c, mode); + + \ \ } + + \ \ \ \ return s7_make_boolean (sc, ret); diff --git a/goldfish/liii/os.scm b/goldfish/liii/os.scm index 1f3cd8c..c7525d1 100644 --- a/goldfish/liii/os.scm +++ b/goldfish/liii/os.scm @@ -71,9 +71,9 @@ (define (access path mode) (cond ((eq? mode 'F_OK) (g_access path 0)) - ((eq? mode 'X_OK) (g_access path 1)) + ((eq? mode 'X_OK) (g_access path 128)) ((eq? mode 'W_OK) (g_access path 2)) - ((eq? mode 'R_OK) (g_access path 4)) + ((eq? mode 'R_OK) (g_access path 1)) (else (error 'value-error "Allowed mode 'F_OK, 'X_OK,'W_OK, 'R_OK")))) (define (getenv key) diff --git a/goldfish/scheme/boot.scm b/goldfish/scheme/boot.scm index 887cbf5..0e2766a 100644 --- a/goldfish/scheme/boot.scm +++ b/goldfish/scheme/boot.scm @@ -2,7 +2,7 @@ (if (string? path) (if (not (g_access path 0)) ; F_OK #f - (if (g_access path 4) ; R_OK + (if (g_access path 1) ; R_OK #t (error 'permission-error (string-append "No permission: " path)))) (error 'type-error "(file-exists? path): path should be string"))) diff --git a/src/goldfish.hpp b/src/goldfish.hpp index 29bcc27..b52e385 100644 --- a/src/goldfish.hpp +++ b/src/goldfish.hpp @@ -315,11 +315,14 @@ static s7_pointer f_access (s7_scheme* sc, s7_pointer args) { const char* path_c= s7_string (s7_car (args)); int mode = s7_integer ((s7_cadr (args))); -#ifdef TB_CONFIG_OS_WINDOWS - bool ret= (_access (path_c, mode) == 0); -#else - bool ret= (access (path_c, mode) == 0); -#endif + bool ret= false; + if (mode == 0) { + tb_file_info_t info; + ret= tb_file_info (path_c, &info); + } else { + ret= tb_file_access (path_c, mode); + } + return s7_make_boolean (sc, ret); } diff --git a/tests/goldfish/liii/os-test.scm b/tests/goldfish/liii/os-test.scm index 5514987..a7ec7d1 100644 --- a/tests/goldfish/liii/os-test.scm +++ b/tests/goldfish/liii/os-test.scm @@ -37,6 +37,12 @@ (let ((t2 (current-second))) (check (>= (ceiling (- t2 t1)) 1) => #t)))) +(when (os-linux?) + (check-true (access "/root" 'F_OK)) + (check-false (access "/root" 'R_OK)) + (check-false (access "/root" 'W_OK)) + (check-true (access "bin/goldfish" 'X_OK))) + (check (string-null? (getenv "PATH")) => #f) (unsetenv "PATH") (check (getenv "PATH") => #f) From ae45258d58dbbfe3034c94c96621d086d3495561 Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 6 Jan 2025 16:52:25 +0800 Subject: [PATCH 7/7] wip --- GoldfishStandardLibrary.tmu | 2 +- http/tests/chat_demo.scm | 12 +++--------- liii_http.tmu | 20 ++++---------------- tests/goldfish/liii/os-test.scm | 2 +- 4 files changed, 9 insertions(+), 27 deletions(-) diff --git a/GoldfishStandardLibrary.tmu b/GoldfishStandardLibrary.tmu index bcc1fd3..be00cf4 100644 --- a/GoldfishStandardLibrary.tmu +++ b/GoldfishStandardLibrary.tmu @@ -1868,7 +1868,7 @@ <\scm-chunk|tests/goldfish/liii/os-test.scm|true|true> - (when (os-linux?) + (when (and (os-linux?) (not (string=? "root" (getlogin)))) \ \ (check-true (access "/root" 'F_OK)) diff --git a/http/tests/chat_demo.scm b/http/tests/chat_demo.scm index 658a177..e2f5eb4 100644 --- a/http/tests/chat_demo.scm +++ b/http/tests/chat_demo.scm @@ -5,17 +5,11 @@ (liii json) (liii list) (liii os) - (liii string)) + (liii string) + (liii path)) -;; 定义一个函数来读取文件内容并移除行末的回车符 (define (read-and-clean-file file-path) - (call-with-input-file file-path - (lambda (port) - (let loop ((line (read-line port)) - (content "")) - (if (eof-object? line) - (string-trim-right content #\newline) ; 移除行末的回车符 - (loop (read-line port) (string-append content line))))))) + (string-trim-right (path-read-text file-path) #\newline)) (define (load-silicon-cloud-api-key) (let* ((home (if (os-windows?) diff --git a/liii_http.tmu b/liii_http.tmu index d3e5c27..dfa6cc3 100644 --- a/liii_http.tmu +++ b/liii_http.tmu @@ -218,27 +218,15 @@ \ \ \ \ \ \ \ \ (liii os) - \ \ \ \ \ \ \ \ (liii string)) + \ \ \ \ \ \ \ \ (liii string) - \; + \ \ \ \ \ \ \ \ (liii path)) - ;; 定义一个函数来读取文件内容并移除行末的回车符 + \; (define (read-and-clean-file file-path) - \ \ (call-with-input-file file-path - - \ \ \ \ (lambda (port) - - \ \ \ \ \ \ (let loop ((line (read-line port)) - - \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (content "")) - - \ \ \ \ \ \ \ \ (if (eof-object? line) - - \ \ \ \ \ \ \ \ \ \ \ \ (string-trim-right content #\\newline) ; 移除行末的回车符 - - \ \ \ \ \ \ \ \ \ \ \ \ (loop (read-line port) (string-append content line))))))) + \ \ (string-trim-right (path-read-text file-path) #\\newline)) \; diff --git a/tests/goldfish/liii/os-test.scm b/tests/goldfish/liii/os-test.scm index a7ec7d1..bd193de 100644 --- a/tests/goldfish/liii/os-test.scm +++ b/tests/goldfish/liii/os-test.scm @@ -37,7 +37,7 @@ (let ((t2 (current-second))) (check (>= (ceiling (- t2 t1)) 1) => #t)))) -(when (os-linux?) +(when (and (os-linux?) (not (string=? "root" (getlogin)))) (check-true (access "/root" 'F_OK)) (check-false (access "/root" 'R_OK)) (check-false (access "/root" 'W_OK))