add fs_file_read_line

This commit is contained in:
lixianjing 2020-12-10 09:18:38 +08:00
parent 2edb81ae48
commit f5db2f26d9
8 changed files with 271 additions and 1 deletions

View File

@ -1077,6 +1077,7 @@ EXPORTS
event_source_manager_remove_by_tag
event_source_manager_destroy
fs_file_read
fs_file_read_line
fs_file_write
fs_file_printf
fs_file_seek
@ -1926,6 +1927,7 @@ EXPORTS
progress_bar_set_max
progress_bar_set_vertical
progress_bar_set_show_text
progress_bar_set_reverse
progress_bar_get_percent
row_create
row_cast

View File

@ -268,6 +268,7 @@ EXPORTS
event_source_manager_remove_by_tag
event_source_manager_destroy
fs_file_read
fs_file_read_line
fs_file_write
fs_file_printf
fs_file_seek

View File

@ -1,5 +1,8 @@
# 最新动态
2020/12/08
* 增加函数fs\_file\_read\_line。
2020/12/07
* 增加宏说明文档(感谢智明提供补丁)
* 进度条增加 reverse 属性,用于反向显示进度。

View File

@ -383,3 +383,57 @@ ret_t fs_test(fs_t* fs) {
fs_test_file(fs);
return fs_test_dir(fs);
}
int32_t fs_file_read_line(fs_file_t* file, char* buffer, uint32_t size) {
char tbuff[128];
char* d = buffer;
char* s = tbuff;
int32_t i = 0;
int32_t ret = 0;
int64_t offset = 0;
bool_t done = FALSE;
return_value_if_fail(file != NULL && buffer != NULL && size > 1, 0);
while (((d - buffer) < size) && !done) {
offset = fs_file_tell(file);
ret = fs_file_read(file, tbuff, sizeof(tbuff) - 1);
if (ret <= 0) {
break;
}
tbuff[ret] = '\0';
for (i = 0; (i < ret) && ((d - buffer) < size); i++) {
offset++;
if (s[i] == '\r') {
i++;
if (s[i] == '\n') {
offset++;
} else if (s[i] == '\0') {
char c = 0;
fs_file_read(file, &c, 1);
if (c == '\n') {
offset++;
}
}
done = TRUE;
break;
} else if (s[i] == '\n') {
done = TRUE;
break;
} else {
*d++ = s[i];
}
}
if (ret < (sizeof(tbuff) - 1)) {
break;
}
}
ret = d - buffer;
if (ret > 0) {
*d = '\0';
fs_file_seek(file, offset);
}
return ret;
}

View File

@ -146,6 +146,19 @@ struct _fs_file_t {
*/
int32_t fs_file_read(fs_file_t* file, void* buffer, uint32_t size);
/**
* @method fs_file_read_line
*
*
*
* @param {fs_file_t*} file
* @param {char*} buffer
* @param {uint32_t} size
*
* @return {int32_t}
*/
int32_t fs_file_read_line(fs_file_t* file, char* buffer, uint32_t size);
/**
* @method fs_file_write
*

View File

@ -37,3 +37,103 @@ TEST(Fs, eof) {
file_remove(filename);
}
TEST(Fs, read_line1) {
char buff[128];
const char* str = "1\r22\r\n333\n4444";
const char* filename = "test.bin";
file_write(filename, str, strlen(str));
fs_file_t* f = fs_open_file(os_fs(), filename, "r");
ASSERT_EQ(fs_file_read_line(f, buff, sizeof(buff)-1), 1);
ASSERT_STREQ(buff, "1");
ASSERT_EQ(fs_file_eof(f), FALSE);
ASSERT_EQ(fs_file_read_line(f, buff, sizeof(buff)-1), 2);
ASSERT_STREQ(buff, "22");
ASSERT_EQ(fs_file_eof(f), FALSE);
ASSERT_EQ(fs_file_read_line(f, buff, sizeof(buff)-1), 3);
ASSERT_STREQ(buff, "333");
ASSERT_EQ(fs_file_eof(f), FALSE);
ASSERT_EQ(fs_file_read_line(f, buff, 3), 3);
ASSERT_STREQ(buff, "444");
ASSERT_EQ(fs_file_eof(f), FALSE);
ASSERT_EQ(fs_file_read_line(f, buff, 3), 1);
ASSERT_STREQ(buff, "4");
ASSERT_EQ(fs_file_read_line(f, buff, 3), 0);
ASSERT_EQ(fs_file_eof(f), TRUE);
fs_file_close(f);
file_remove(filename);
}
TEST(Fs, read_line2) {
char buff[256];
const char* str = \
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
const char* filename = "test.bin";
file_write(filename, str, strlen(str));
fs_file_t* f = fs_open_file(os_fs(), filename, "r");
ASSERT_EQ(fs_file_read_line(f, buff, sizeof(buff)-1), 127);
ASSERT_EQ(fs_file_eof(f), FALSE);
ASSERT_EQ(fs_file_read_line(f, buff, 3), 0);
ASSERT_EQ(fs_file_eof(f), TRUE);
fs_file_close(f);
file_remove(filename);
}
TEST(Fs, read_line3) {
char buff[256];
const char* str = \
"11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n";
const char* filename = "test.bin";
file_write(filename, str, strlen(str));
fs_file_t* f = fs_open_file(os_fs(), filename, "r");
ASSERT_EQ(fs_file_read_line(f, buff, sizeof(buff)-1), 128);
ASSERT_EQ(fs_file_eof(f), FALSE);
ASSERT_EQ(fs_file_read_line(f, buff, 3), 0);
ASSERT_EQ(fs_file_eof(f), TRUE);
fs_file_close(f);
file_remove(filename);
}
TEST(Fs, read_line4) {
char buff[256];
const char* str = \
"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\nabc";
const char* filename = "test.bin";
file_write(filename, str, strlen(str));
fs_file_t* f = fs_open_file(os_fs(), filename, "r");
ASSERT_EQ(fs_file_read_line(f, buff, sizeof(buff)-1), 127);
ASSERT_EQ(fs_file_read_line(f, buff, sizeof(buff)-1), 3);
ASSERT_EQ(fs_file_eof(f), FALSE);
ASSERT_EQ(fs_file_read_line(f, buff, 3), 0);
ASSERT_EQ(fs_file_eof(f), TRUE);
fs_file_close(f);
file_remove(filename);
}

View File

@ -18513,6 +18513,10 @@
"desc": "值。",
"name": "WIDGET_PROP_VALUE"
},
{
"desc": "进度条反向显示。",
"name": "WIDGET_PROP_REVERSE"
},
{
"desc": "长度。",
"name": "WIDGET_PROP_LENGTH"
@ -30749,6 +30753,32 @@
"desc": "返回实际读取的字节数。"
}
},
{
"params": [
{
"type": "fs_file_t*",
"name": "file",
"desc": "文件对象。"
},
{
"type": "char*",
"name": "buffer",
"desc": "用于返回数据的缓冲区。"
},
{
"type": "uint32_t",
"name": "size",
"desc": "缓冲区大小。"
}
],
"annotation": {},
"desc": "读取一行文本。",
"name": "fs_file_read_line",
"return": {
"type": "int32_t",
"desc": "返回实际读取的字节数。"
}
},
{
"params": [
{
@ -57820,6 +57850,29 @@
"desc": "返回RET_OK表示成功否则表示失败。"
}
},
{
"params": [
{
"type": "widget_t*",
"name": "widget",
"desc": "控件对象。"
},
{
"type": "bool_t",
"name": "reverse",
"desc": "是否反向。"
}
],
"annotation": {
"scriptable": true
},
"desc": "设置进度条是否反向。",
"name": "progress_bar_set_reverse",
"return": {
"type": "ret_t",
"desc": "返回RET_OK表示成功否则表示失败。"
}
},
{
"params": [
{
@ -57903,6 +57956,19 @@
"design": true,
"scriptable": true
}
},
{
"name": "reverse",
"desc": "是否反向显示。如果为TRUE水平方向从右向左表示增加垂直方向从上到下表示增加。",
"type": "bool_t",
"annotation": {
"set_prop": true,
"get_prop": true,
"readable": true,
"persitent": true,
"design": true,
"scriptable": true
}
}
],
"header": "widgets/progress_bar.h",
@ -62702,6 +62768,11 @@
"type": "combo_box_custom_open_popup_t",
"name": "open_popup",
"desc": "回调函数。"
},
{
"type": "combo_box_custom_on_layout_combobox_popup_t",
"name": "on_layout_combobox_popup",
"desc": "layout 的回调函数。"
}
],
"annotation": {},

View File

@ -7021,6 +7021,32 @@
"desc": "返回实际读取的字节数。"
}
},
{
"params": [
{
"type": "fs_file_t*",
"name": "file",
"desc": "文件对象。"
},
{
"type": "char*",
"name": "buffer",
"desc": "用于返回数据的缓冲区。"
},
{
"type": "uint32_t",
"name": "size",
"desc": "缓冲区大小。"
}
],
"annotation": {},
"desc": "读取一行文本。",
"name": "fs_file_read_line",
"return": {
"type": "int32_t",
"desc": "返回实际读取的字节数。"
}
},
{
"params": [
{
@ -17711,7 +17737,7 @@
"events": [],
"properties": [],
"header": "conf_io/app_conf.h",
"desc": "应用程序的配置信息。\n\n底层实现可以是任何格式比如INIXMLJSON和UBJSON。\n\n对于树状的文档key可以是多级的用.分隔。如network.ip。",
"desc": "应用程序的配置信息。\n\n底层实现可以是任何格式比如INIXMLJSON和UBJSON。\n\n对于树状的文档key可以是多级的用.分隔。如network.ip。\n\nconf-io是可选组件需要自己包含头文件否则64位数据类型会被截断成32位的数据。\n\n```c\n#include \"conf_io/app_conf.h\"\n```",
"name": "app_conf_t",
"annotation": {
"fake": true,