|
| 1 | +// Copyright 2025 International Digital Economy Academy |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +///| |
| 16 | +priv type File_Handler |
| 17 | + |
| 18 | +///| |
| 19 | +fn fopen_ffi(path : Bytes, mode : Bytes) -> File_Handler = "$moonbit.fopen_ffi" |
| 20 | + |
| 21 | +///| |
| 22 | +fn is_null(ptr : File_Handler) -> Int = "$moonbit.is_null" |
| 23 | + |
| 24 | +///| |
| 25 | +fn fread_ffi(ptr : Bytes, size : Int, nitems : Int, stream : File_Handler) -> Int = "$moonbit.fread_ffi" |
| 26 | + |
| 27 | +///| |
| 28 | +fn fwrite_ffi(ptr : Bytes, size : Int, nitems : Int, stream : File_Handler) -> Int = "$moonbit.fwrite_ffi" |
| 29 | + |
| 30 | +///| |
| 31 | +fn get_error_message_ffi() -> Bytes = "$moonbit.get_error_message" |
| 32 | + |
| 33 | +///| |
| 34 | +fn fseek_ffi(file : File_Handler, offset : Int, whence : Int) -> Int = "$moonbit.fseek_ffi" |
| 35 | + |
| 36 | +///| |
| 37 | +fn ftell_ffi(file : File_Handler) -> Int = "$moonbit.ftell_ffi" |
| 38 | + |
| 39 | +///| |
| 40 | +fn fflush_ffi(stream : File_Handler) -> Int = "$moonbit.fflush_ffi" |
| 41 | + |
| 42 | +///| |
| 43 | +fn fclose_ffi(file : File_Handler) -> Int = "$moonbit.fclose_ffi" |
| 44 | + |
| 45 | +///| |
| 46 | +fn get_error_message() -> String { |
| 47 | + utf8_bytes_to_mbt_string(get_error_message_ffi()) |
| 48 | +} |
| 49 | + |
| 50 | +///| |
| 51 | +fn read_file_to_bytes_internal(path : String) -> Bytes! { |
| 52 | + let file = fopen_ffi(mbt_string_to_utf8_bytes(path, true), b"rb") |
| 53 | + guard is_null(file) == 0 else { raise IOError(get_error_message()) } |
| 54 | + guard fseek_ffi(file, 0, 2) == 0 else { raise IOError(get_error_message()) } |
| 55 | + let size = ftell_ffi(file) |
| 56 | + guard size != -1 else { raise IOError(get_error_message()) } |
| 57 | + guard fseek_ffi(file, 0, 0) == 0 else { raise IOError(get_error_message()) } |
| 58 | + let bytes = Bytes::make(size, 0) |
| 59 | + let bytes_read = fread_ffi(bytes, 1, size, file) |
| 60 | + guard bytes_read == size else { raise IOError(get_error_message()) } |
| 61 | + guard fclose_ffi(file) == 0 else { raise IOError(get_error_message()) } |
| 62 | + bytes |
| 63 | +} |
| 64 | + |
| 65 | +fn read_file_to_string_internal(path : String, encoding~ : String = "utf8") -> String! { |
| 66 | + guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") } |
| 67 | + utf8_bytes_to_mbt_string(read_file_to_bytes_internal!(path)) |
| 68 | +} |
| 69 | + |
| 70 | +///| |
| 71 | +fn write_bytes_to_file_internal(path : String, content : Bytes) -> Unit! { |
| 72 | + let file = fopen_ffi(mbt_string_to_utf8_bytes(path, true), b"wb") |
| 73 | + guard is_null(file) == 0 else { raise IOError(get_error_message()) } |
| 74 | + let bytes_written = fwrite_ffi(content, 1, content.length(), file) |
| 75 | + guard bytes_written == content.length() else { raise IOError(get_error_message()) } |
| 76 | + guard fflush_ffi(file) == 0 else { raise IOError(get_error_message()) } |
| 77 | + guard fclose_ffi(file) == 0 else { raise IOError(get_error_message()) } |
| 78 | +} |
| 79 | + |
| 80 | +fn write_string_to_file_internal(path : String, content : String, encoding~ : String = "utf8") -> Unit! { |
| 81 | + guard encoding == "utf8" else { raise IOError("Unsupported encoding: \{encoding}, only utf8 is supported for now") } |
| 82 | + let bytes = mbt_string_to_utf8_bytes(content, false) |
| 83 | + write_bytes_to_file_internal!(path, bytes) |
| 84 | +} |
0 commit comments