Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path-read-text and path-write-text in (liii path) #234

Merged
merged 7 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
266 changes: 257 additions & 9 deletions GoldfishStandardLibrary.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@

\ \ \ \ \ \ #f

\ \ \ \ \ \ (if (g_access path 4) ; R_OK
\ \ \ \ \ \ (if (g_access path 1) ; R_OK

\ \ \ \ \ \ \ \ \ \ #t

Expand Down Expand Up @@ -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"))))

\;
</goldfish-chunk>

<\scm-chunk|tests/goldfish/liii/os-test.scm|true|true>
(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))

\ \ (check-true (access "bin/goldfish" 'X_OK)))

\;
</scm-chunk>

<\cpp-chunk|src/goldfish.hpp|true|true>
static s7_pointer

Expand All @@ -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);

Expand Down Expand Up @@ -2692,7 +2712,9 @@

\ \ path-\<gtr\>string

\ \ path-dir? path-file? path-exists? path-getsize
\ \ path-dir? path-file? path-exists?

\ \ path-getsize path-read-text path-write-text

)

Expand Down Expand Up @@ -3109,6 +3131,228 @@
\;
</cpp-chunk>

<paragraph|path-read-text>

<\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))))

\;
</goldfish-chunk>

<\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 (string (os-sep)) file-name))

\ \ (path-write-text file-path file-content)

\ \ (check (path-read-text file-path) =\<gtr\> file-content)

\ \ (delete-file file-path))

\;
</scm-chunk>

<\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\<less\>char*\<gtr\>(buffer), real_size);

\ \ delete[] buffer;

\;

\ \ return s7_make_string(sc, content.c_str());

}

\;
</cpp-chunk>

<\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) =\<gtr\> 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>

<paragraph|path-write-text>

<\goldfish-chunk|goldfish/liii/path.scm|true|true>
(define path-write-text

\ \ (typed-lambda ((path string?) (content string?))

\ \ \ \ (g_path-write-text path content)))

\;
</goldfish-chunk>

<\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\<less\>const tb_byte_t*\<gtr\>(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);

\ \ }

}

\;
</cpp-chunk>

<\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) =\<gtr\> 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>

<paragraph|glue_path>

<\cpp-chunk|src/goldfish.hpp|true|true>
Expand All @@ -3122,6 +3366,10 @@

\ \ glue_path_getsize (sc);

\ \ glue_path_read_text (sc);

\ \ glue_path_write_text (sc);

}

\;
Expand Down
4 changes: 2 additions & 2 deletions goldfish/liii/os.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion goldfish/liii/path.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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 path-write-text
)
(import (liii error) (liii vector) (liii string) (liii list))
(begin
Expand Down Expand Up @@ -90,6 +91,17 @@
(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))))

(define path-write-text
(typed-lambda ((path string?) (content string?))
(g_path-write-text path content)))

) ; end of begin
) ; end of define-library

2 changes: 1 addition & 1 deletion goldfish/scheme/boot.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand Down
12 changes: 3 additions & 9 deletions http/tests/chat_demo.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down
Loading
Loading