add file_read_as_unix_text

This commit is contained in:
lixianjing 2023-04-12 11:57:23 +08:00
parent 6a386566c5
commit 0716e7a3e5
4 changed files with 47 additions and 0 deletions

View File

@ -1,4 +1,6 @@
# 最新动态 # 最新动态
2023/04/12
* 增加函数file\_read\_as\_unix\_text。
2023/04/11 2023/04/11
* 增离线画布缓存功能(不修改图片时跳过bsvg解析步骤使用bitmap绘制在线画布)(感谢高源提供补丁) * 增离线画布缓存功能(不修改图片时跳过bsvg解析步骤使用bitmap绘制在线画布)(感谢高源提供补丁)

View File

@ -1577,3 +1577,22 @@ bool_t tk_is_ui_thread(void) {
return s_ui_thread_id == tk_thread_self(); return s_ui_thread_id == tk_thread_self();
} }
#endif/*WITH_WASM*/ #endif/*WITH_WASM*/
char* file_read_as_unix_text(const char* filename, uint32_t* size) {
str_t str;
uint32_t s = 0;
char* data = (char*)file_read(filename, &s);
return_value_if_fail(data != NULL, NULL);
str_init(&str, s);
str_set_with_len(&str, data, s);
str_replace(&str, "\r\n", "\n");
str_replace(&str, "\r", "\n");
*size = str.size;
memcpy(data, str.str, str.size);
data[str.size] = '\0';
str_reset(&str);
return data;
}

View File

@ -932,6 +932,17 @@ ret_t tk_replace_locale(const char* name, char out[TK_NAME_LEN + 1], const char*
*/ */
const char* tk_normalize_key_name(const char* name, char fixed_name[TK_NAME_LEN + 1]); const char* tk_normalize_key_name(const char* name, char fixed_name[TK_NAME_LEN + 1]);
/**
* @method file_read_as_unix_text
* windows换行(\r\n)macos换行(\r)uinux换行(\n)
*
* @param {const char*} name
* @param {uint32_t*} size
*
* @return {void*} TKMEM_FREE释放
*/
char* file_read_as_unix_text(const char* filename, uint32_t* size);
/*public for test*/ /*public for test*/
ret_t xml_file_expand(const char* filename, str_t* s, const char* data); ret_t xml_file_expand(const char* filename, str_t* s, const char* data);

View File

@ -783,3 +783,18 @@ TEST(Utils, wcsncmp) {
ASSERT_EQ(wcsncmp(L"ABCD1234", L"ABCD1234", 7), 0); ASSERT_EQ(wcsncmp(L"ABCD1234", L"ABCD1234", 7), 0);
ASSERT_EQ(wcsncmp(L"ABCD1234", L"ABCD1234", 8), 0); ASSERT_EQ(wcsncmp(L"ABCD1234", L"ABCD1234", 8), 0);
} }
TEST(Utils, file_read_as_unix_text) {
uint32_t size = 0;
char* data = file_read_as_unix_text("tests/testdata/test.txt", &size);
#define EXPECT_STR "\
abc\n\
123\n\
hello\n\
"
ASSERT_STREQ(data, EXPECT_STR);
TKMEM_FREE(data);
}