mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-12-02 03:58:33 +08:00
add file_read_as_unix_text
This commit is contained in:
parent
6a386566c5
commit
0716e7a3e5
@ -1,4 +1,6 @@
|
||||
# 最新动态
|
||||
2023/04/12
|
||||
* 增加函数file\_read\_as\_unix\_text。
|
||||
|
||||
2023/04/11
|
||||
* 增离线画布缓存功能(不修改图片时跳过bsvg解析步骤,使用bitmap绘制在线画布)(感谢高源提供补丁)
|
||||
|
@ -1577,3 +1577,22 @@ bool_t tk_is_ui_thread(void) {
|
||||
return s_ui_thread_id == tk_thread_self();
|
||||
}
|
||||
#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;
|
||||
}
|
||||
|
@ -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]);
|
||||
|
||||
/**
|
||||
* @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*/
|
||||
ret_t xml_file_expand(const char* filename, str_t* s, const char* data);
|
||||
|
||||
|
@ -783,3 +783,18 @@ TEST(Utils, wcsncmp) {
|
||||
ASSERT_EQ(wcsncmp(L"ABCD1234", L"ABCD1234", 7), 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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user