improve istream and format code

This commit is contained in:
lixianjing 2019-09-13 18:05:00 +08:00
parent 813de2bd4f
commit 299497b4da
19 changed files with 65 additions and 49 deletions

View File

@ -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,

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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);

View File

@ -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]);

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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))

View File

@ -163,9 +163,7 @@ ret_t path_normalize(const char* path, char* result, int32_t size) {
}
break;
}
default: {
*d++ = *s++;
}
default: { *d++ = *s++; }
}
}

View File

@ -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

View File

@ -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;

View File

@ -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);

View File

@ -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);