From 299497b4da1856365fdad38ebd89b87fb9383819 Mon Sep 17 00:00:00 2001 From: lixianjing Date: Fri, 13 Sep 2019 18:05:00 +0800 Subject: [PATCH] improve istream and format code --- src/base/assets_manager.c | 8 ++------ src/base/line_break.c | 4 +--- src/base/text_edit.c | 4 +--- src/streams/istream_serial.c | 9 ++++++++- src/streams/ostream_serial.c | 2 +- src/streams/serial_helper.c | 13 +++++++++---- src/streams/serial_helper.h | 1 + src/streams/tools/serial_at.c | 2 +- src/streams/tools/serial_echo.c | 2 +- src/svg/bsvg_draw.c | 4 +--- src/svg/bsvg_to_svg.c | 4 +--- src/svg/svg_path_parser.c | 4 +--- src/tkc/istream.c | 13 ++++++++++--- src/tkc/istream.h | 17 ++++++++++++++++- src/tkc/path.c | 4 +--- src/tkc/types_def.h | 7 ++++++- src/tkc/value.c | 8 ++------ src/vgcanvas/vgcanvas_cairo.c | 4 +--- src/vgcanvas/vgcanvas_nanovg_soft.inc | 4 +--- 19 files changed, 65 insertions(+), 49 deletions(-) diff --git a/src/base/assets_manager.c b/src/base/assets_manager.c index 919c92317..fe088bc47 100644 --- a/src/base/assets_manager.c +++ b/src/base/assets_manager.c @@ -180,9 +180,7 @@ static asset_info_t* try_load_image(assets_manager_t* am, const char* name, subpath = "assets/raw/images/svg"; break; } - default: { - return NULL; - } + default: { return NULL; } } return_value_if_fail(build_path(am, path, MAX_PATH, ratio, subpath, name, extname) == RET_OK, @@ -224,9 +222,7 @@ static asset_info_t* try_load_assets(assets_manager_t* am, const char* name, con subpath = "assets/raw/data"; break; } - default: { - return NULL; - } + default: { return NULL; } } return_value_if_fail(build_path(am, path, MAX_PATH, FALSE, subpath, name, extname) == RET_OK, diff --git a/src/base/line_break.c b/src/base/line_break.c index f773c284b..6a083cbab 100644 --- a/src/base/line_break.c +++ b/src/base/line_break.c @@ -50,9 +50,7 @@ break_type_t line_break_check(wchar_t c1, wchar_t c2) { case LINEBREAK_NOBREAK: { return LINE_BREAK_NO; } - default: { - return LINE_BREAK_ALLOW; - } + default: { return LINE_BREAK_ALLOW; } } } diff --git a/src/base/text_edit.c b/src/base/text_edit.c index a23aa5b85..de35629f1 100644 --- a/src/base/text_edit.c +++ b/src/base/text_edit.c @@ -464,9 +464,7 @@ static int32_t text_edit_calc_x(text_edit_t* text_edit, row_info_t* iter) { case ALIGN_H_RIGHT: { return (layout_info->w - row_width); } - default: { - break; - } + default: { break; } } } diff --git a/src/streams/istream_serial.c b/src/streams/istream_serial.c index 06ddf208d..793abc7fa 100644 --- a/src/streams/istream_serial.c +++ b/src/streams/istream_serial.c @@ -58,7 +58,7 @@ static ret_t tk_istream_serial_exec(object_t* obj, const char* name, const char* if (tk_str_eq(name, TK_STREAM_CMD_IFLUSH)) { return tk_istream_serial_flush(TK_ISTREAM(obj)); } - + return RET_NOT_IMPL; } @@ -68,6 +68,12 @@ static const object_vtable_t s_tk_istream_serial_vtable = {.type = "tk_istream_s .exec = tk_istream_serial_exec, .get_prop = tk_istream_serial_get_prop}; +ret_t tk_istream_wait_for_data(tk_istream_t* stream, uint32_t timeout_ms) { + tk_istream_serial_t* s = TK_ISTREAM_SERIAL(stream); + + return serial_wait_for_data(s->fd, timeout_ms); +} + tk_istream_t* tk_istream_serial_create(serial_handle_t fd) { object_t* obj = NULL; tk_istream_serial_t* istream_serial = NULL; @@ -79,6 +85,7 @@ tk_istream_t* tk_istream_serial_create(serial_handle_t fd) { istream_serial->fd = fd; TK_ISTREAM(obj)->read = tk_istream_serial_read; + TK_ISTREAM(obj)->flush = tk_istream_serial_flush; return TK_ISTREAM(obj); } diff --git a/src/streams/ostream_serial.c b/src/streams/ostream_serial.c index 32c55925e..de2a4e9f6 100644 --- a/src/streams/ostream_serial.c +++ b/src/streams/ostream_serial.c @@ -59,7 +59,7 @@ static ret_t tk_ostream_serial_exec(object_t* obj, const char* name, const char* if (tk_str_eq(name, TK_STREAM_CMD_OFLUSH)) { return tk_ostream_serial_flush(TK_OSTREAM(obj)); } - + return RET_NOT_IMPL; } diff --git a/src/streams/serial_helper.c b/src/streams/serial_helper.c index f6aaf83fa..5a2d0657e 100644 --- a/src/streams/serial_helper.c +++ b/src/streams/serial_helper.c @@ -44,8 +44,8 @@ serial_handle_t serial_open(const char* port) { wstr_insert(&str, 0, prefix, wcslen(prefix)); } - fd = CreateFileW(str.str, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, - 0); + fd = CreateFileW(str.str, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, 0); wstr_reset(&str); return fd; @@ -308,7 +308,7 @@ int32_t serial_read(serial_handle_t fd, uint8_t* buff, uint32_t max_size) { if (!ReadFile(fd, buff, (DWORD)(max_size), &bytes_read, NULL)) { ret = -1; } else { - ret = bytes_read; + ret = bytes_read; } return ret; @@ -321,12 +321,17 @@ int32_t serial_write(serial_handle_t fd, const uint8_t* buff, uint32_t max_size) if (!WriteFile(fd, buff, (DWORD)(max_size), &bytes_write, NULL)) { ret = -1; } else { - ret = bytes_write; + ret = bytes_write; } return ret; } +ret_t serial_wait_for_data(serial_handle_t fd, uint32_t timeout_ms) { + /*TODO*/ + return RET_NOT_IMPL; +} + int serial_close(serial_handle_t fd) { CloseHandle(fd); return 0; diff --git a/src/streams/serial_helper.h b/src/streams/serial_helper.h index 8645f4639..404ee5365 100644 --- a/src/streams/serial_helper.h +++ b/src/streams/serial_helper.h @@ -59,6 +59,7 @@ serial_handle_t serial_open(const char* port); ret_t serial_iflush(serial_handle_t fd); ret_t serial_oflush(serial_handle_t fd); +ret_t serial_wait_for_data(serial_handle_t fd, uint32_t timeout_ms); int32_t serial_read(serial_handle_t fd, uint8_t* buff, uint32_t max_size); int32_t serial_write(serial_handle_t fd, const uint8_t* buff, uint32_t max_size); diff --git a/src/streams/tools/serial_at.c b/src/streams/tools/serial_at.c index e407dab72..82a8d0ded 100644 --- a/src/streams/tools/serial_at.c +++ b/src/streams/tools/serial_at.c @@ -38,7 +38,7 @@ int main(int argc, char* argv[]) { printf("Usage: %s port msg\n", argv[0]); return 0; } - + TK_ENABLE_CONSOLE(); do_send(tk_iostream_serial_create(argv[1]), argv[2]); diff --git a/src/streams/tools/serial_echo.c b/src/streams/tools/serial_echo.c index dd2e5eb56..6e2a75a71 100644 --- a/src/streams/tools/serial_echo.c +++ b/src/streams/tools/serial_echo.c @@ -7,7 +7,7 @@ void do_echo(tk_iostream_t* iostream) { int32_t ret = 0; tk_istream_t* istream = tk_iostream_get_istream(iostream); tk_ostream_t* ostream = tk_iostream_get_ostream(iostream); - + while (ret == 0) { ret = tk_istream_read(istream, (uint8_t*)buff, sizeof(buff)); sleep_ms(1000); diff --git a/src/svg/bsvg_draw.c b/src/svg/bsvg_draw.c index 9cf76cf96..542c97e0f 100644 --- a/src/svg/bsvg_draw.c +++ b/src/svg/bsvg_draw.c @@ -229,9 +229,7 @@ ret_t bsvg_draw_path(draw_ctx_t* ctx, const svg_path_t* path) { break; } case SVG_PATH_NULL: - default: { - break; - } + default: { break; } } return RET_OK; diff --git a/src/svg/bsvg_to_svg.c b/src/svg/bsvg_to_svg.c index 5e90f5a1e..7a1593f9e 100644 --- a/src/svg/bsvg_to_svg.c +++ b/src/svg/bsvg_to_svg.c @@ -132,9 +132,7 @@ ret_t bsvg_to_svg_path(str_t* str, const svg_path_t* path) { break; } case SVG_PATH_NULL: - default: { - break; - } + default: { break; } } return str_append(str, buff); diff --git a/src/svg/svg_path_parser.c b/src/svg/svg_path_parser.c index 7cb7846d0..145404b69 100644 --- a/src/svg/svg_path_parser.c +++ b/src/svg/svg_path_parser.c @@ -248,9 +248,7 @@ static ret_t svg_path_parser_parse_cmd(svg_path_parser_t* parser, char c) { } break; } - default: { - assert(!"not supported path!"); - } break; + default: { assert(!"not supported path!"); } break; } return RET_OK; diff --git a/src/tkc/istream.c b/src/tkc/istream.c index a341dab00..91b7c8c20 100644 --- a/src/tkc/istream.c +++ b/src/tkc/istream.c @@ -36,6 +36,13 @@ ret_t tk_istream_seek(tk_istream_t* stream, uint32_t offset) { return stream->seek(stream, offset); } +ret_t tk_istream_wait_for_data(tk_istream_t* stream, uint32_t timeout_ms) { + return_value_if_fail(stream != NULL, RET_BAD_PARAMS); + return_value_if_fail(stream->wait_for_data != NULL, RET_NOT_IMPL); + + return stream->wait_for_data(stream, timeout_ms); +} + ret_t tk_istream_flush(tk_istream_t* stream) { return_value_if_fail(stream != NULL, RET_BAD_PARAMS); @@ -78,7 +85,7 @@ int32_t tk_istream_read_len(tk_istream_t* stream, uint8_t* buff, uint32_t max_si } int32_t tk_istream_read_line(tk_istream_t* stream, uint8_t* buff, uint32_t max_size, - uint32_t timeout_ms) { + uint32_t timeout_ms) { uint32_t start = 0; uint32_t end = 0; int32_t offset = 0; @@ -97,7 +104,7 @@ int32_t tk_istream_read_line(tk_istream_t* stream, uint8_t* buff, uint32_t max_s break; } - if(read_bytes == 1) { + if (read_bytes == 1) { log_debug("%c", buff[offset]); } offset += read_bytes; @@ -107,7 +114,7 @@ int32_t tk_istream_read_line(tk_istream_t* stream, uint8_t* buff, uint32_t max_s break; } - if(buff[offset] == '\n') { + if (buff[offset] == '\n') { break; } } while (remain_bytes > 0); diff --git a/src/tkc/istream.h b/src/tkc/istream.h index ca458f16a..08e795fe4 100644 --- a/src/tkc/istream.h +++ b/src/tkc/istream.h @@ -33,6 +33,7 @@ typedef struct _tk_istream_t tk_istream_t; typedef int32_t (*tk_istream_read_t)(tk_istream_t* stream, uint8_t* buff, uint32_t max_size); typedef ret_t (*tk_istream_flush_t)(tk_istream_t* stream); typedef ret_t (*tk_istream_seek_t)(tk_istream_t* stream, uint32_t offset); +typedef ret_t (*tk_istream_wait_for_data_t)(tk_istream_t* stream, uint32_t timeout_ms); /** * @class tk_istream_t @@ -47,6 +48,7 @@ struct _tk_istream_t { tk_istream_read_t read; tk_istream_seek_t seek; tk_istream_flush_t flush; + tk_istream_wait_for_data_t wait_for_data; }; /** @@ -76,6 +78,19 @@ int32_t tk_istream_read(tk_istream_t* stream, uint8_t* buff, uint32_t max_size); */ ret_t tk_istream_seek(tk_istream_t* stream, uint32_t offset); +/** + * @method tk_istream_wait_for_data + * + * 等待数据。 + * + * @param {tk_istream_t*} stream istream对象。 + * @param {uint32_t} timeout_ms 超时时间。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + * + */ +ret_t tk_istream_wait_for_data(tk_istream_t* stream, uint32_t timeout_ms); + /** * @method tk_istream_flush * @@ -118,7 +133,7 @@ int32_t tk_istream_read_len(tk_istream_t* stream, uint8_t* buff, uint32_t max_si * */ int32_t tk_istream_read_line(tk_istream_t* stream, uint8_t* buff, uint32_t max_size, - uint32_t timeout_ms); + uint32_t timeout_ms); #define TK_ISTREAM(obj) ((tk_istream_t*)(obj)) diff --git a/src/tkc/path.c b/src/tkc/path.c index 9547114ce..745fcde0d 100644 --- a/src/tkc/path.c +++ b/src/tkc/path.c @@ -163,9 +163,7 @@ ret_t path_normalize(const char* path, char* result, int32_t size) { } break; } - default: { - *d++ = *s++; - } + default: { *d++ = *s++; } } } diff --git a/src/tkc/types_def.h b/src/tkc/types_def.h index bc9e573b0..593276643 100644 --- a/src/tkc/types_def.h +++ b/src/tkc/types_def.h @@ -159,7 +159,12 @@ typedef enum _ret_t { * @const RET_BAD_PARAMS * 无效参数。 */ - RET_BAD_PARAMS + RET_BAD_PARAMS, + /** + * @const RET_TIMEOUT + * 超时。 + */ + RET_TIMEOUT } ret_t; #ifdef ANDROID diff --git a/src/tkc/value.c b/src/tkc/value.c index e1240ca4c..1965b5d74 100644 --- a/src/tkc/value.c +++ b/src/tkc/value.c @@ -37,9 +37,7 @@ bool_t value_bool(const value_t* v) { case VALUE_TYPE_WSTRING: { return tk_watob(v->value.wstr); } - default: { - return value_int(v) ? TRUE : FALSE; - } + default: { return value_int(v) ? TRUE : FALSE; } } } @@ -450,9 +448,7 @@ int value_int(const value_t* v) { case VALUE_TYPE_WSTRING: { return tk_watoi(v->value.wstr); } - default: { - assert(!"not supported type"); - } + default: { assert(!"not supported type"); } } return 0; diff --git a/src/vgcanvas/vgcanvas_cairo.c b/src/vgcanvas/vgcanvas_cairo.c index 27ef9d9e3..87732244b 100644 --- a/src/vgcanvas/vgcanvas_cairo.c +++ b/src/vgcanvas/vgcanvas_cairo.c @@ -423,9 +423,7 @@ static cairo_surface_t* create_surface(uint32_t w, uint32_t h, bitmap_format_t f cairo_format = CAIRO_FORMAT_RGB16_565; break; } - default: { - return NULL; - } + default: { return NULL; } } return cairo_image_surface_create_for_data(fbuff, cairo_format, w, h, w * bpp); diff --git a/src/vgcanvas/vgcanvas_nanovg_soft.inc b/src/vgcanvas/vgcanvas_nanovg_soft.inc index 1d83b5c0d..293ccf12f 100644 --- a/src/vgcanvas/vgcanvas_nanovg_soft.inc +++ b/src/vgcanvas/vgcanvas_nanovg_soft.inc @@ -111,9 +111,7 @@ static int vgcanvas_nanovg_ensure_image(vgcanvas_nanovg_t* canvas, bitmap_t* img f = NVG_TEXTURE_RGB; break; } - default: { - assert(!"not supported format"); - } + default: { assert(!"not supported format"); } } i = nvgCreateImageRaw(canvas->vg, img->w, img->h, f, NVG_IMAGE_NEAREST, img->data);