From da3055964aeb79523400ca3f7493117e61606151 Mon Sep 17 00:00:00 2001 From: JaDogg Date: Sat, 27 Apr 2024 07:58:09 +0100 Subject: [PATCH] refactor(libs.io,runtime): use sr instead of str, update runtime c code to use sr --- compiler/carpntr/build/program_code.c | 118 +++++++++++--------------- compiler/carpntr/main.yaka.c | 14 +-- compiler/libs/libs/io.yaka | 8 +- compiler/runtime/_include_io.c | 27 ++---- compiler/runtime/_include_io.h | 4 +- compiler/runtime/yk__system.c | 27 ++---- compiler/runtime/yk__system.h | 4 +- 7 files changed, 81 insertions(+), 121 deletions(-) diff --git a/compiler/carpntr/build/program_code.c b/compiler/carpntr/build/program_code.c index 1fe8dcd9..d3451875 100644 --- a/compiler/carpntr/build/program_code.c +++ b/compiler/carpntr/build/program_code.c @@ -145,8 +145,8 @@ void yy__console_red(struct yk__bstr); void yy__console_green(struct yk__bstr); void yy__console_yellow(struct yk__bstr); void yy__console_cyan(struct yk__bstr); -yk__sds yy__io_readfile(yk__sds); -bool yy__io_writefile(yk__sds, yk__sds); +yk__sds yy__io_readfile(struct yk__bstr); +bool yy__io_writefile(struct yk__bstr, struct yk__bstr); yy__toml_Table yy__toml_from_str(struct yk__bstr); yy__toml_Table yy__toml_get_table(yy__toml_Table, struct yk__bstr); yy__toml_TomlArray yy__toml_get_array(yy__toml_Table, struct yk__bstr); @@ -165,12 +165,12 @@ yk__sds yy__path_remove_extension(yk__sds); yk__sds yy__array_join(yk__sds*, yk__sds); yk__sds* yy__array_extend(yk__sds*, yk__sds*); int32_t yy__strings_get_cstr(yy__c_CStr, int32_t); -int32_t yy__strings_get(yk__sds, int32_t); -bool yy__strings_contains(yk__sds, yk__sds); -int32_t yy__strings_ord(yk__sds); -bool yy__strings_startswith(yk__sds, yk__sds); -yk__sds* yy__strings_split(yk__sds, yk__sds); -int32_t yy__strings_find_char(yk__sds, int32_t); +int32_t yy__strings_get(struct yk__bstr, int32_t); +bool yy__strings_contains(struct yk__bstr, struct yk__bstr); +int32_t yy__strings_ord(struct yk__bstr); +bool yy__strings_startswith(struct yk__bstr, struct yk__bstr); +yk__sds* yy__strings_split(struct yk__bstr, struct yk__bstr); +int32_t yy__strings_find_char(struct yk__bstr, int32_t); yk__sds yy__strings_from_cstr(yy__c_CStr); yk__sds yy__strings_from_cstrlen(yy__c_CStr, int32_t); yk__sds yy__strings_from_cstrlen_after(yy__c_CStr, int32_t, int32_t); @@ -178,9 +178,9 @@ yy__c_CStr yy__strings_null_cstr(); yy__c_CStr yy__strings_to_cstr(yk__sds); void yy__strings_del_cstr(yy__c_CStr); void yy__strings_del_str(yy__c_CStr); -yk__sds yy__strings_cut_from(yk__sds, int32_t); -yk__sds yy__strings_mid(yk__sds, int32_t, int32_t); -bool yy__strings_endswith(yk__sds, yk__sds); +yk__sds yy__strings_cut_from(struct yk__bstr, int32_t); +yk__sds yy__strings_mid(struct yk__bstr, int32_t, int32_t); +bool yy__strings_endswith(struct yk__bstr, struct yk__bstr); yk__sds yy__os_exe_path(); yk__sds yy__os_cwd(); yy__os_ProcessResult yy__os_run(yk__sds*); @@ -1066,7 +1066,7 @@ bool yy__building_is_target_macos(yk__sds yy__building_target_to_check) yk__sdsfree(yy__building_target_to_check); return t__12; } - bool t__13 = yy__strings_contains(yk__sdsdup(yy__building_target_to_check), yk__sdsnewlen("macos", 5)); + bool t__13 = yy__strings_contains(yk__bstr_h(yy__building_target_to_check), yk__bstr_s("macos", 5)); yk__sdsfree(yy__building_target_to_check); return t__13; } @@ -1079,7 +1079,7 @@ bool yy__building_is_target_windows(yk__sds yy__building_target_to_check) yk__sdsfree(yy__building_target_to_check); return t__14; } - bool t__15 = yy__strings_contains(yk__sdsdup(yy__building_target_to_check), yk__sdsnewlen("windows", 7)); + bool t__15 = yy__strings_contains(yk__bstr_h(yy__building_target_to_check), yk__bstr_s("windows", 7)); yk__sdsfree(yy__building_target_to_check); return t__15; } @@ -1241,7 +1241,7 @@ void yy__building_cleanup_buildables(struct yy__building_BObject** yy__building_ } bool yy__building_keep_ray_objects(yk__sds yy__building_element, bool yy__building_negate) { - bool yy__building_obj = yy__strings_endswith(yk__sdsdup(yy__building_element), yk__sdsnewlen(".o", 2)); + bool yy__building_obj = yy__strings_endswith(yk__bstr_h(yy__building_element), yk__bstr_s(".o", 2)); if (yy__building_negate) { bool t__24 = !yy__building_obj; @@ -1825,7 +1825,7 @@ int32_t yy__building_build(struct yy__configuration_Config* yy__building_c, yk__ yk__sds t__83 = yy__path_join(yk__bstr_h(yy__building_build_path), yk__bstr_s("program_code.c", 14)); yk__sds yy__building_code_path = yk__sdsdup(t__83); yk__sds t__84 = yy__building_prepare_code(yy__building_c, yk__sdsdup(yy__building_code)); - if (!(yy__io_writefile(yk__sdsdup(yy__building_code_path), yk__sdsdup(t__84)))) + if (!(yy__io_writefile(yk__bstr_h(yy__building_code_path), yk__bstr_h(t__84)))) { yy__console_red(yk__bstr_s("Failed to write file:", 21)); yk__printlnstr(yy__building_code_path); @@ -1924,7 +1924,7 @@ int32_t yy__building_build_simple(struct yy__configuration_Config* yy__building_ yy__building_target = yk__sdsnewlen("web", 3); } yk__sds t__96 = yy__building_prepare_code(yy__building_c, yk__sdsdup(yy__building_code)); - if (!(yy__io_writefile(yk__sdsdup(yy__building_code_path), yk__sdsdup(t__96)))) + if (!(yy__io_writefile(yk__bstr_h(yy__building_code_path), yk__bstr_h(t__96)))) { yy__console_red(yk__bstr_s("Failed to write file:", 21)); yk__printlnstr(yy__building_code_path); @@ -2466,7 +2466,7 @@ struct yy__configuration_CCode* yy__configuration_inject_c_code_defaults(struct struct yy__configuration_Config* yy__configuration_load_runtime_features(struct yy__configuration_Config* yy__configuration_c, yk__sds yy__configuration_code, bool yy__configuration_silent) { yk__sds yy__configuration_header = yk__sdsnewlen("// YK:" , 6); - bool yy__configuration_has_requirements = yy__strings_startswith(yk__sdsdup(yy__configuration_code), yk__sdsdup(yy__configuration_header)); + bool yy__configuration_has_requirements = yy__strings_startswith(yk__bstr_h(yy__configuration_code), yk__bstr_h(yy__configuration_header)); if (!yy__configuration_has_requirements) { if (!yy__configuration_silent) @@ -2481,7 +2481,7 @@ struct yy__configuration_Config* yy__configuration_load_runtime_features(struct yk__sdsfree(yy__configuration_code); return t__62; } - int32_t yy__configuration_until = yy__strings_find_char(yk__sdsdup(yy__configuration_code), yy__strings_ord(yk__sdsnewlen("#", 1))); + int32_t yy__configuration_until = yy__strings_find_char(yk__bstr_h(yy__configuration_code), yy__strings_ord(yk__bstr_s("#", 1))); if (yy__configuration_until == INT32_C(-1)) { yk__arrput(yy__configuration_c->yy__configuration_errors, yk__sdsnewlen("Failed to extract runtime features. Cannot find # in compiled code first line.", 78)); @@ -2490,7 +2490,7 @@ struct yy__configuration_Config* yy__configuration_load_runtime_features(struct yk__sdsfree(yy__configuration_code); return t__63; } - yk__sds t__64 = yy__strings_mid(yk__sdsdup(yy__configuration_code), yk__sdslen(yy__configuration_header), (yy__configuration_until - yk__sdslen(yy__configuration_header))); + yk__sds t__64 = yy__strings_mid(yk__bstr_h(yy__configuration_code), yk__sdslen(yy__configuration_header), (yy__configuration_until - yk__sdslen(yy__configuration_header))); yk__sds yy__configuration_temp_features = yk__sdsdup(t__64); if (!yy__configuration_silent) { @@ -2499,7 +2499,7 @@ struct yy__configuration_Config* yy__configuration_load_runtime_features(struct yy__console_green(yk__bstr_h(yy__configuration_temp_features)); yk__printstr("\n"); } - yk__sds* yy__configuration_features = yy__strings_split(yk__sdsdup(yy__configuration_temp_features), yk__sdsnewlen(",", 1)); + yk__sds* yy__configuration_features = yy__strings_split(yk__bstr_h(yy__configuration_temp_features), yk__bstr_s(",", 1)); int32_t yy__configuration_x = INT32_C(0); int32_t yy__configuration_length = yk__arrlen(yy__configuration_features); while (true) @@ -2679,7 +2679,7 @@ struct yy__configuration_Config* yy__configuration_load_config() yk__sds t__80 = yy__os_cwd(); yk__sds t__81 = yy__path_join(yk__bstr_h(t__80), yk__bstr_s("yaksha.toml", 11)); yk__sds yy__configuration_configf = yk__sdsdup(t__81); - yk__sds t__82 = yy__io_readfile(yk__sdsdup(yy__configuration_configf)); + yk__sds t__82 = yy__io_readfile(yk__bstr_h(yy__configuration_configf)); yk__sds yy__configuration_config_text = yk__sdsdup(t__82); yy__toml_Table yy__configuration_conf = yy__toml_from_str(yk__bstr_h(yy__configuration_config_text)); if (!(yy__toml_valid_table(yy__configuration_conf))) @@ -3026,8 +3026,8 @@ void yy__console_cyan(struct yk__bstr yy__console_x) yy__console_color_print(INT32_C(7), yy__console_x); return; } -yk__sds yy__io_readfile(yk__sds nn__fname) { return yk__io_readfile(nn__fname); } -bool yy__io_writefile(yk__sds nn__fname, yk__sds nn__data) { return yk__io_writefile(nn__fname, nn__data); } +yk__sds yy__io_readfile(struct yk__bstr nn__fname) { return yk__io_readfile(nn__fname); } +bool yy__io_writefile(struct yk__bstr nn__fname, struct yk__bstr nn__data) { return yk__io_writefile(nn__fname, nn__data); } yy__toml_Table yy__toml_from_str(struct yk__bstr nn__x) { char errbuf[200]; @@ -3310,39 +3310,31 @@ int32_t yy__strings_get_cstr(yy__c_CStr nn__s, int32_t nn__pos) { return (int32_t)nn__s[nn__pos]; } -int32_t yy__strings_get(yk__sds nn__s, int32_t nn__pos) +int32_t yy__strings_get(struct yk__bstr nn__s, int32_t nn__pos) { - int32_t x = (int32_t)nn__s[nn__pos]; - yk__sdsfree(nn__s); + int32_t x = (int32_t)(yk__bstr_get_reference(nn__s))[nn__pos]; return x; } -bool yy__strings_contains(yk__sds nn__haystack, yk__sds nn__needle) +bool yy__strings_contains(struct yk__bstr nn__haystack, struct yk__bstr nn__needle) { - bool val = strstr(nn__haystack, nn__needle) != NULL; - yk__sdsfree(nn__haystack); - yk__sdsfree(nn__needle); + bool val = strstr(yk__bstr_get_reference(nn__haystack), yk__bstr_get_reference(nn__needle)) != NULL; return val; } -int32_t yy__strings_ord(yk__sds yy__strings_s) +int32_t yy__strings_ord(struct yk__bstr yy__strings_s) { - int32_t t__0 = yy__strings_get(yk__sdsdup(yy__strings_s), INT32_C(0)); - yk__sdsfree(yy__strings_s); + int32_t t__0 = yy__strings_get(yy__strings_s, INT32_C(0)); return t__0; } -bool yy__strings_startswith(yk__sds nn__a, yk__sds nn__b) +bool yy__strings_startswith(struct yk__bstr nn__a, struct yk__bstr nn__b) { - bool x = (strstr(nn__a, nn__b) != NULL); - yk__sdsfree(nn__a); - yk__sdsfree(nn__b); + bool x = (strstr(yk__bstr_get_reference(nn__a), yk__bstr_get_reference(nn__b)) != NULL); return x; } -yk__sds* yy__strings_split(yk__sds nn__value, yk__sds nn__sep) +yk__sds* yy__strings_split(struct yk__bstr nn__value, struct yk__bstr nn__sep) { int count; - yk__sds* result = yk__sdssplitlen(nn__value, yk__sdslen(nn__value), - nn__sep, yk__sdslen(nn__sep), &count); - yk__sdsfree(nn__value); - yk__sdsfree(nn__sep); + yk__sds* result = yk__sdssplitlen(yk__bstr_get_reference(nn__value), yk__bstr_len(nn__value), + yk__bstr_get_reference(nn__sep), yk__bstr_len(nn__sep), &count); if (NULL == result) { return NULL; } @@ -3353,17 +3345,17 @@ yk__sds* yy__strings_split(yk__sds nn__value, yk__sds nn__sep) free(result); // free array itself. return newarr; } -int32_t yy__strings_find_char(yk__sds nn__value, int32_t nn__ch) +int32_t yy__strings_find_char(struct yk__bstr nn__value, int32_t nn__ch) { int position = -1; - int length = (int)yk__sdslen(nn__value); + int length = (int)yk__bstr_len(nn__value); + const char* reference = yk__bstr_get_reference(nn__value); for (int i = 0; i < length; i++) { - if (nn__value[i] == nn__ch) { + if (reference[i] == nn__ch) { position = i; break; } } - yk__sdsfree(nn__value); return position; } yk__sds yy__strings_from_cstr(yy__c_CStr nn__a) @@ -3395,43 +3387,35 @@ void yy__strings_del_str(yy__c_CStr nn__a) if (NULL == nn__a) return; yk__sdsfree(nn__a); } -yk__sds yy__strings_cut_from(yk__sds nn__a, int32_t nn__position) +yk__sds yy__strings_cut_from(struct yk__bstr nn__a, int32_t nn__position) { - if (nn__a == NULL || yk__sdslen(nn__a) < nn__position) { - yk__sdsfree(nn__a); + if (yk__bstr_len(nn__a) < nn__position) { return yk__sdsempty(); } - yk__sds s = yk__sdsnewlen(nn__a + nn__position, yk__sdslen(nn__a) - nn__position); - yk__sdsfree(nn__a); + yk__sds s = yk__sdsnewlen(yk__bstr_get_reference(nn__a) + nn__position, yk__bstr_len(nn__a) - nn__position); return s; } -yk__sds yy__strings_mid(yk__sds nn__a, int32_t nn__position, int32_t nn__number) +yk__sds yy__strings_mid(struct yk__bstr nn__a, int32_t nn__position, int32_t nn__number) { - if (nn__a == NULL || nn__number < 1 || yk__sdslen(nn__a) < (nn__position + nn__number)) { - yk__sdsfree(nn__a); + if (nn__number < 1 || yk__bstr_len(nn__a) < (nn__position + nn__number)) { return yk__sdsempty(); } - yk__sds s = yk__sdsnewlen(nn__a + nn__position, nn__number); - yk__sdsfree(nn__a); + yk__sds s = yk__sdsnewlen(yk__bstr_get_reference(nn__a) + nn__position, nn__number); return s; } -bool yy__strings_endswith(yk__sds yy__strings_a, yk__sds yy__strings_b) +bool yy__strings_endswith(struct yk__bstr yy__strings_a, struct yk__bstr yy__strings_b) { - if (yk__sdslen(yy__strings_b) > yk__sdslen(yy__strings_a)) + if (yk__bstr_len(yy__strings_b) > yk__bstr_len(yy__strings_a)) { - yk__sdsfree(yy__strings_b); - yk__sdsfree(yy__strings_a); return false; } - int32_t yy__strings_pos = (yk__sdslen(yy__strings_a) - yk__sdslen(yy__strings_b)); - yk__sds t__1 = yy__strings_cut_from(yk__sdsdup(yy__strings_a), yy__strings_pos); + int32_t yy__strings_pos = (yk__bstr_len(yy__strings_a) - yk__bstr_len(yy__strings_b)); + yk__sds t__1 = yy__strings_cut_from(yy__strings_a, yy__strings_pos); yk__sds yy__strings_cut_a = yk__sdsdup(t__1); - bool yy__strings_result = (yk__sdscmp(yy__strings_cut_a , yy__strings_b) == 0); + bool yy__strings_result = (yk__cmp_sds_bstr(yy__strings_cut_a, yy__strings_b) == 0); bool t__2 = yy__strings_result; yk__sdsfree(yy__strings_cut_a); yk__sdsfree(t__1); - yk__sdsfree(yy__strings_b); - yk__sdsfree(yy__strings_a); return t__2; } yk__sds yy__os_exe_path() @@ -3498,7 +3482,7 @@ yk__sds yy__os_which(yk__sds yy__os_binary) yk__sdsfree(t__2); yk__sdsfree(t__1); } - yk__sds* yy__os_paths = yy__strings_split(yk__sdsdup(yy__os_env), yk__sdsdup(yy__os_sep)); + yk__sds* yy__os_paths = yy__strings_split(yk__bstr_h(yy__os_env), yk__bstr_h(yy__os_sep)); int32_t yy__os_length = yk__arrlen(yy__os_paths); while (true) { @@ -3824,7 +3808,7 @@ int32_t yy__build_from_config(struct yy__configuration_Config* yy__config, bool } else { - if (yy__strings_startswith(yk__sdsdup(yy__result->output), yk__sdsnewlen("// YK", 5))) + if (yy__strings_startswith(yk__bstr_h(yy__result->output), yk__bstr_s("// YK", 5))) { if (yy__simple) { diff --git a/compiler/carpntr/main.yaka.c b/compiler/carpntr/main.yaka.c index af8af3f6..98fd4e6d 100644 --- a/compiler/carpntr/main.yaka.c +++ b/compiler/carpntr/main.yaka.c @@ -135,8 +135,8 @@ void yy__console_red(struct yk__bstr); void yy__console_green(struct yk__bstr); void yy__console_yellow(struct yk__bstr); void yy__console_cyan(struct yk__bstr); -yk__sds yy__io_readfile(yk__sds); -bool yy__io_writefile(yk__sds, yk__sds); +yk__sds yy__io_readfile(struct yk__bstr); +bool yy__io_writefile(struct yk__bstr, struct yk__bstr); yy__toml_Table yy__toml_from_str(struct yk__bstr); yy__toml_Table yy__toml_get_table(yy__toml_Table, struct yk__bstr); yy__toml_TomlArray yy__toml_get_array(yy__toml_Table, struct yk__bstr); @@ -1815,7 +1815,7 @@ int32_t yy__building_build(struct yy__configuration_Config* yy__building_c, yk__ yk__sds t__83 = yy__path_join(yk__bstr_h(yy__building_build_path), yk__bstr_s("program_code.c", 14)); yk__sds yy__building_code_path = yk__sdsdup(t__83); yk__sds t__84 = yy__building_prepare_code(yy__building_c, yk__sdsdup(yy__building_code)); - if (!(yy__io_writefile(yk__sdsdup(yy__building_code_path), yk__sdsdup(t__84)))) + if (!(yy__io_writefile(yk__bstr_h(yy__building_code_path), yk__bstr_h(t__84)))) { yy__console_red(yk__bstr_s("Failed to write file:", 21)); yk__printlnstr(yy__building_code_path); @@ -1914,7 +1914,7 @@ int32_t yy__building_build_simple(struct yy__configuration_Config* yy__building_ yy__building_target = yk__sdsnewlen("web", 3); } yk__sds t__96 = yy__building_prepare_code(yy__building_c, yk__sdsdup(yy__building_code)); - if (!(yy__io_writefile(yk__sdsdup(yy__building_code_path), yk__sdsdup(t__96)))) + if (!(yy__io_writefile(yk__bstr_h(yy__building_code_path), yk__bstr_h(t__96)))) { yy__console_red(yk__bstr_s("Failed to write file:", 21)); yk__printlnstr(yy__building_code_path); @@ -2669,7 +2669,7 @@ struct yy__configuration_Config* yy__configuration_load_config() yk__sds t__80 = yy__os_cwd(); yk__sds t__81 = yy__path_join(yk__bstr_h(t__80), yk__bstr_s("yaksha.toml", 11)); yk__sds yy__configuration_configf = yk__sdsdup(t__81); - yk__sds t__82 = yy__io_readfile(yk__sdsdup(yy__configuration_configf)); + yk__sds t__82 = yy__io_readfile(yk__bstr_h(yy__configuration_configf)); yk__sds yy__configuration_config_text = yk__sdsdup(t__82); yy__toml_Table yy__configuration_conf = yy__toml_from_str(yk__bstr_h(yy__configuration_config_text)); if (!(yy__toml_valid_table(yy__configuration_conf))) @@ -3016,8 +3016,8 @@ void yy__console_cyan(struct yk__bstr yy__console_x) yy__console_color_print(INT32_C(7), yy__console_x); return; } -yk__sds yy__io_readfile(yk__sds nn__fname) { return yk__io_readfile(nn__fname); } -bool yy__io_writefile(yk__sds nn__fname, yk__sds nn__data) { return yk__io_writefile(nn__fname, nn__data); } +yk__sds yy__io_readfile(struct yk__bstr nn__fname) { return yk__io_readfile(nn__fname); } +bool yy__io_writefile(struct yk__bstr nn__fname, struct yk__bstr nn__data) { return yk__io_writefile(nn__fname, nn__data); } yy__toml_Table yy__toml_from_str(struct yk__bstr nn__x) { char errbuf[200]; diff --git a/compiler/libs/libs/io.yaka b/compiler/libs/libs/io.yaka index 7bd2c19c..bdf0e435 100644 --- a/compiler/libs/libs/io.yaka +++ b/compiler/libs/libs/io.yaka @@ -56,7 +56,7 @@ def valid(stream: Ptr[File]) -> bool: pass @nativedefine("yk__io_fopen") -def fopen(fname: str, mode: str) -> Ptr[File]: +def fopen(fname: sr, mode: sr) -> Ptr[File]: # Open a file pass @@ -144,17 +144,17 @@ def munlock(address: AnyPtr, length: c.Size) -> int: pass @native("yk__io_readfile") -def readfile(fname: str) -> str: +def readfile(fname: sr) -> str: # Read a file to as string, on error returns empty pass @native("yk__io_writefile") -def writefile(fname: str, data: str) -> bool: +def writefile(fname: sr, data: sr) -> bool: # Write a file pass @nativedefine("yk__io_open") -def open(path: str, mode: int) -> c.CInt: +def open(path: sr, mode: int) -> c.CInt: pass @nativedefine("yk__io_close") diff --git a/compiler/runtime/_include_io.c b/compiler/runtime/_include_io.c index fbb465ad..1446687c 100644 --- a/compiler/runtime/_include_io.c +++ b/compiler/runtime/_include_io.c @@ -9,17 +9,15 @@ #define YK_IO_MSVC 1 #endif -FILE *yk__io_fopen(yk__sds fpath, yk__sds mode) { +FILE *yk__io_fopen(struct yk__bstr fpath, struct yk__bstr mode) { FILE *file; #if defined(YK_IO_WIN) // ---------- BEGIN WINDOWS ----------------- - wchar_t *wpath = yk__utf8_to_utf16_null_terminated(fpath); - wchar_t *wmode = yk__utf8_to_utf16_null_terminated(mode); + wchar_t *wpath = yk__utf8_to_utf16_null_terminated(yk__bstr_get_reference(fpath)); + wchar_t *wmode = yk__utf8_to_utf16_null_terminated(yk__bstr_get_reference(mode)); if (wpath == NULL || wmode == NULL) { if (wpath != NULL) { free(wpath); } if (wmode != NULL) { free(wmode); } - yk__sdsfree(fpath); - yk__sdsfree(mode); return NULL; } #if defined(YK_IO_MSVC)// WINDOWS + MSVC @@ -28,8 +26,6 @@ FILE *yk__io_fopen(yk__sds fpath, yk__sds mode) { if (NULL != file) { fclose(file); } free(wpath); free(wmode); - yk__sdsfree(fpath); - yk__sdsfree(mode); return NULL; } #else// WINDOWS + GCC/MingW @@ -37,18 +33,14 @@ FILE *yk__io_fopen(yk__sds fpath, yk__sds mode) { if (file == NULL) { free(wpath); free(wmode); - yk__sdsfree(fpath); - yk__sdsfree(mode); return NULL; } #endif // ---------- END WINDOWS ----------------- #else // ---------- BEGIN UNIX ----------------- - file = fopen(fpath, mode); + file = fopen(yk__bstr_get_reference(fpath), yk__bstr_get_reference(mode)); if (file == NULL) { - yk__sdsfree(fpath); - yk__sdsfree(mode); return NULL; } // ---------- END UNIX ----------------- @@ -58,8 +50,6 @@ FILE *yk__io_fopen(yk__sds fpath, yk__sds mode) { free(wpath); free(wmode); #endif - yk__sdsfree(fpath); - yk__sdsfree(mode); return file; } bool yk__io_fclose(FILE *stream) { @@ -105,13 +95,12 @@ void yk__io_rewind(FILE *stream) { if (stream == NULL) { return; } rewind(stream); } -int yk__io_open(yk__sds fpath, int mode) { +int yk__io_open(struct yk__bstr fpath, int mode) { int fd = -1; #if defined(YK_IO_WIN) // ---------- BEGIN WINDOWS ----------------- - wchar_t *wpath = yk__utf8_to_utf16_null_terminated(fpath); + wchar_t *wpath = yk__utf8_to_utf16_null_terminated(yk__bstr_get_reference(fpath)); if (wpath == NULL) { - yk__sdsfree(fpath); return fd; } #if defined(YK_IO_MSVC)// WINDOWS + MSVC @@ -120,7 +109,6 @@ int yk__io_open(yk__sds fpath, int mode) { if (0 != openerr) { if (-1 != fd) { _close(fd); } free(wpath); - yk__sdsfree(fpath); fd = -1; return fd; } @@ -130,14 +118,13 @@ int yk__io_open(yk__sds fpath, int mode) { // ---------- END WINDOWS ----------------- #else // ---------- BEGIN UNIX ----------------- - fd = open(fpath, mode); + fd = open(yk__bstr_get_reference(fpath), mode); // ---------- END UNIX ----------------- #endif // ----- RETURNING FILE* ------ #if defined(YK_IO_WIN) free(wpath); #endif - yk__sdsfree(fpath); return fd; } bool yk__io_close(int fd) { diff --git a/compiler/runtime/_include_io.h b/compiler/runtime/_include_io.h index ab18b16f..4637a174 100644 --- a/compiler/runtime/_include_io.h +++ b/compiler/runtime/_include_io.h @@ -1,4 +1,4 @@ -FILE *yk__io_fopen(yk__sds path, yk__sds mode); +FILE *yk__io_fopen(struct yk__bstr path, struct yk__bstr mode); bool yk__io_fclose(FILE *stream); bool yk__io_fflush(FILE *stream); size_t yk__io_fread(void *buffer, size_t size, size_t count, FILE *stream); @@ -11,7 +11,7 @@ bool yk__io_feof(FILE *stream); bool yk__io_ferror(FILE *stream); void yk__io_clearerr(FILE *stream); void yk__io_rewind(FILE *stream); -int yk__io_open(yk__sds path, int mode); +int yk__io_open(struct yk__bstr path, int mode); bool yk__io_close(int fd); #if defined(_WIN32) || defined(_WIN64) #define YK__IO_O_RDONLY _O_RDONLY diff --git a/compiler/runtime/yk__system.c b/compiler/runtime/yk__system.c index 2321a66a..20dccbe4 100644 --- a/compiler/runtime/yk__system.c +++ b/compiler/runtime/yk__system.c @@ -397,15 +397,14 @@ char *yk__windows_read_file(const char *path, size_t *length, int *error) { return buffer; } #endif -yk__sds yk__io_readfile(yk__sds name) { +yk__sds yk__io_readfile(struct yk__bstr name) { size_t ln; int err; #if defined(_WIN32) || defined(_WIN64) - char *out = yk__windows_read_file(name, &ln, &err); + char *out = yk__windows_read_file(yk__bstr_get_reference(name), &ln, &err); #else - char *out = yk__bhalib_read_file(name, &ln, &err); + char *out = yk__bhalib_read_file(yk__bstr_get_reference(name), &ln, &err); #endif - yk__sdsfree(name);// clean up filename as it will be copied if (err == 0) { yk__sds tmp = yk__sdsnewlen(out, ln); yk__bhalib_free(out); @@ -413,12 +412,10 @@ yk__sds yk__io_readfile(yk__sds name) { } return yk__sdsempty(); } -bool yk__io_writefile(yk__sds fpath, yk__sds data) { +bool yk__io_writefile(struct yk__bstr fpath, struct yk__bstr data) { #if defined(_WIN32) || defined(_WIN64) - wchar_t *wpath = yk__utf8_to_utf16_null_terminated(fpath); + wchar_t *wpath = yk__utf8_to_utf16_null_terminated(yk__bstr_get_reference(fpath)); if (wpath == NULL) { - yk__sdsfree(fpath); - yk__sdsfree(data); return false; } #if defined(_MSC_VER)// MSVC @@ -427,34 +424,26 @@ bool yk__io_writefile(yk__sds fpath, yk__sds data) { if (0 != openerr) { if (NULL != file) { fclose(file); } free(wpath); - yk__sdsfree(fpath); - yk__sdsfree(data); return false; } #else // GCC, MingW, etc FILE *file = _wfopen(wpath, L"wb+"); if (file == NULL) { free(wpath); - yk__sdsfree(fpath); - yk__sdsfree(data); return false; } #endif// msvc check #else // not windows - FILE *file = fopen(fpath, "wb+"); + FILE *file = fopen(yk__bstr_get_reference(fpath), "wb+"); if (file == NULL) { - yk__sdsfree(fpath); - yk__sdsfree(data); return false; } #endif// unix / windows check - size_t written = fwrite(data, sizeof(char), yk__sdslen(data), file); - bool fully_written = (written == yk__sdslen(data)); + size_t written = fwrite(yk__bstr_get_reference(data), sizeof(char), yk__bstr_len(data), file); + bool fully_written = (written == yk__bstr_len(data)); #if defined(_WIN32) || defined(_WIN64) free(wpath); #endif - yk__sdsfree(fpath); - yk__sdsfree(data); fclose(file); return fully_written; } diff --git a/compiler/runtime/yk__system.h b/compiler/runtime/yk__system.h index d0da23d9..25f4ad43 100644 --- a/compiler/runtime/yk__system.h +++ b/compiler/runtime/yk__system.h @@ -28,8 +28,8 @@ bool yk__writable(yk__sds path); bool yk__executable(yk__sds path); bool yk__mkdir(yk__sds path); yk__sds yk__getenv(yk__sds name); -yk__sds yk__io_readfile(yk__sds name); -bool yk__io_writefile(yk__sds name, yk__sds data); +yk__sds yk__io_readfile(struct yk__bstr name); +bool yk__io_writefile(struct yk__bstr name, struct yk__bstr data); #if defined(_WIN32) || defined(_WIN64) wchar_t *yk__utf8_to_utf16_null_terminated(const char *str); char *yk__utf16_to_utf8_null_terminated(const wchar_t *str);