diff --git a/src/base/vgcanvas.c b/src/base/vgcanvas.c index ebe335727..4225f12f6 100644 --- a/src/base/vgcanvas.c +++ b/src/base/vgcanvas.c @@ -439,7 +439,7 @@ ret_t fbo_to_img(framebuffer_object_t* fbo, bitmap_t* img) { return_value_if_fail(fbo != NULL && img != NULL, RET_BAD_PARAMS); memset(img, 0x00, sizeof(bitmap_t)); - img->specific = (char*)NULL + fbo->id; + img->specific = tk_pointer_from_int(fbo->id); img->specific_ctx = NULL; img->specific_destroy = NULL; img->w = fbo->w * fbo->ratio; diff --git a/src/base/widget.c b/src/base/widget.c index 27513b068..36332ef25 100644 --- a/src/base/widget.c +++ b/src/base/widget.c @@ -1518,7 +1518,7 @@ uint32_t widget_add_timer(widget_t* widget, timer_func_t on_timer, uint32_t dura return_value_if_fail(widget != NULL && on_timer != NULL, TK_INVALID_ID); id = timer_add(on_timer, widget, duration_ms); - widget_on(widget, EVT_DESTROY, widget_remove_timer_on_destroy, ((char*)NULL) + id); + widget_on(widget, EVT_DESTROY, widget_remove_timer_on_destroy, tk_pointer_from_int(id)); return id; } diff --git a/src/tkc/str.c b/src/tkc/str.c index f676f0126..9e27991f3 100644 --- a/src/tkc/str.c +++ b/src/tkc/str.c @@ -296,90 +296,90 @@ ret_t str_reset(str_t* str) { return RET_OK; } -bool_t str_end_with(str_t* s, const char* str) { +bool_t str_end_with(str_t* str, const char* text) { size_t len = 0; - return_value_if_fail(s != NULL && s->str != NULL && str != NULL, FALSE); + return_value_if_fail(str != NULL && str->str != NULL && text != NULL, FALSE); - len = strlen(str); - if (len > s->size) { + len = strlen(text); + if (len > str->size) { return FALSE; } - return strncmp(s->str + s->size - len, str, len) == 0; + return strncmp(str->str + str->size - len, text, len) == 0; } -bool_t str_start_with(str_t* s, const char* str) { - return_value_if_fail(s != NULL && s->str != NULL && str != NULL, FALSE); +bool_t str_start_with(str_t* str, const char* text) { + return_value_if_fail(str != NULL && str->str != NULL && text != NULL, FALSE); - return strncmp(s->str, str, strlen(str)) == 0; + return strncmp(str->str, text, strlen(text)) == 0; } -ret_t str_trim_left(str_t* s, const char* str) { +ret_t str_trim_left(str_t* str, const char* text) { uint32_t i = 0; uint32_t k = 0; - char* p = s->str; - uint32_t n = s->size; - return_value_if_fail(s != NULL && s->str != NULL, RET_BAD_PARAMS); + char* p = str->str; + uint32_t n = str->size; + return_value_if_fail(str != NULL && str->str != NULL, RET_BAD_PARAMS); - if (!s->size) { + if (!str->size) { return RET_OK; } - if (str == NULL) { - str = " "; + if (text == NULL) { + text = " "; } for (i = 0; i < n; i++) { char c = p[i]; - if (strchr(str, c) == NULL) { + if (strchr(text, c) == NULL) { break; } } - s->size = n - i; + str->size = n - i; for (k = 0; i < n; i++, k++) { p[k] = p[i]; } - s->str[s->size] = '\0'; + str->str[str->size] = '\0'; return RET_OK; } -ret_t str_trim_right(str_t* s, const char* str) { +ret_t str_trim_right(str_t* str, const char* text) { int32_t i = 0; - char* p = s->str; - uint32_t n = s->size; - return_value_if_fail(s != NULL && s->str != NULL, RET_BAD_PARAMS); + char* p = str->str; + uint32_t n = str->size; + return_value_if_fail(str != NULL && str->str != NULL, RET_BAD_PARAMS); - if (!s->size) { + if (!str->size) { return RET_OK; } - if (str == NULL) { - str = " "; + if (text == NULL) { + text = " "; } for (i = n - 1; i >= 0; i--) { char c = p[i]; - if (strchr(str, c) == NULL) { + if (strchr(text, c) == NULL) { break; } } p[i + 1] = '\0'; - s->size = i + 1; + str->size = i + 1; return RET_OK; } -ret_t str_trim(str_t* s, const char* str) { - if (!s->size) { +ret_t str_trim(str_t* str, const char* text) { + if (!str->size) { return RET_OK; } - str_trim_left(s, str); + str_trim_left(str, text); - return str_trim_right(s, str); + return str_trim_right(str, text); } static uint32_t str_count_sub_str(str_t* s, const char* str) { @@ -398,27 +398,27 @@ static uint32_t str_count_sub_str(str_t* s, const char* str) { return count; } -ret_t str_replace(str_t* s, const char* str, const char* new_str) { +ret_t str_replace(str_t* str, const char* text, const char* new_text) { uint32_t count = 0; - return_value_if_fail(s != NULL && s->str != NULL && str != NULL && new_str != NULL, + return_value_if_fail(str != NULL && str->str != NULL && text != NULL && new_text != NULL, RET_BAD_PARAMS); - count = str_count_sub_str(s, str); + count = str_count_sub_str(str, text); if (count > 0) { - char* p = s->str; - char* src = s->str; - uint32_t str_len = strlen(str); - uint32_t new_str_len = strlen(new_str); - uint32_t capacity = s->size + count * (strlen(new_str) - strlen(str)) + 1; + char* p = str->str; + char* src = str->str; + uint32_t str_len = strlen(text); + uint32_t new_text_len = strlen(new_text); + uint32_t capacity = str->size + count * (strlen(new_text) - strlen(text)) + 1; char* temp_str = (char*)TKMEM_ALLOC(capacity); char* dst = temp_str; return_value_if_fail(temp_str != NULL, RET_OOM); do { uint32_t size = 0; - p = strstr(src, str); + p = strstr(src, text); if (p != NULL) { size = (uint32_t)(p - src); } else { @@ -428,19 +428,19 @@ ret_t str_replace(str_t* s, const char* str, const char* new_str) { src += size; dst += size; if (p != NULL) { - if (new_str_len > 0) { - memcpy(dst, new_str, new_str_len); - dst += new_str_len; + if (new_text_len > 0) { + memcpy(dst, new_text, new_text_len); + dst += new_text_len; } src += str_len; } *dst = '\0'; } while (p != NULL); - TKMEM_FREE(s->str); - s->str = temp_str; - s->size = strlen(s->str); - s->capacity = capacity; + TKMEM_FREE(str->str); + str->str = temp_str; + str->size = strlen(str->str); + str->capacity = capacity; } return RET_OK; diff --git a/src/tkc/utils.c b/src/tkc/utils.c index 02c5a130f..0e81cf374 100644 --- a/src/tkc/utils.c +++ b/src/tkc/utils.c @@ -717,3 +717,11 @@ const char* tk_under_score_to_camel(const char* name, char* out, uint32_t max_ou return out; } + +int32_t tk_pointer_to_int(void* p) { + return (char*)p - (char*)(NULL); +} + +void* tk_pointer_from_int(int32_t v) { + return ((char*)NULL) + v; +} diff --git a/src/tkc/utils.h b/src/tkc/utils.h index 8ac40e6c0..673af2916 100644 --- a/src/tkc/utils.h +++ b/src/tkc/utils.h @@ -78,6 +78,9 @@ ret_t tk_replace_locale(const char* name, char out[TK_NAME_LEN + 1], const char* bool_t tk_str_start_with(const char* str, const char* prefix); const char* tk_under_score_to_camel(const char* name, char* out, uint32_t max_out_size); +int32_t tk_pointer_to_int(void* p); +void* tk_pointer_from_int(int32_t v); + END_C_DECLS #endif /*TK_UTILS_H*/ diff --git a/src/vgcanvas/texture.inc b/src/vgcanvas/texture.inc index 290f99cc7..79eda32d7 100644 --- a/src/vgcanvas/texture.inc +++ b/src/vgcanvas/texture.inc @@ -1,6 +1,6 @@ static ret_t nanovg_on_bitmap_destroy(bitmap_t* img) { - int32_t id = (char*)(img->specific) - (char*)NULL; + int32_t id = tk_pointer_to_int(img->specific); NVGcontext* vg = (NVGcontext*)(img->specific_ctx); if (vg != NULL && id >= 0) { @@ -16,7 +16,7 @@ static ret_t nanovg_on_bitmap_destroy(bitmap_t* img) { static int vgcanvas_nanovg_ensure_image(vgcanvas_nanovg_t* canvas, bitmap_t* img) { int32_t i = 0; if (img->flags & BITMAP_FLAG_TEXTURE) { - i = (char*)(img->specific) - (char*)NULL; + i = tk_pointer_to_int(img->specific); if (img->flags & BITMAP_FLAG_CHANGED) { img->flags &= (~(BITMAP_FLAG_CHANGED)); @@ -31,7 +31,7 @@ static int vgcanvas_nanovg_ensure_image(vgcanvas_nanovg_t* canvas, bitmap_t* img if (i >= 0) { img->flags |= BITMAP_FLAG_TEXTURE; - img->specific = (char*)NULL + i; + img->specific = tk_pointer_from_int(i); img->specific_ctx = canvas->vg; img->specific_destroy = nanovg_on_bitmap_destroy; image_manager_update_specific(image_manager(), img); diff --git a/src/vgcanvas/vgcanvas_nanovg_soft.inc b/src/vgcanvas/vgcanvas_nanovg_soft.inc index a1b8fd555..f1374821d 100644 --- a/src/vgcanvas/vgcanvas_nanovg_soft.inc +++ b/src/vgcanvas/vgcanvas_nanovg_soft.inc @@ -1,3 +1,4 @@ +#include "tkc/utils.h" static enum NVGtexture bitmap_format_to_nanovg(bitmap_format_t format) { enum NVGtexture f = NVG_TEXTURE_BGRA; @@ -87,7 +88,7 @@ static int vgcanvas_nanovg_ensure_image(vgcanvas_nanovg_t* canvas, bitmap_t* img int32_t i = 0; int32_t f = 0; if (img->flags & BITMAP_FLAG_TEXTURE) { - i = (char*)(img->specific) - (char*)NULL; + i = tk_pointer_to_int(img->specific); return i; } @@ -116,7 +117,7 @@ static int vgcanvas_nanovg_ensure_image(vgcanvas_nanovg_t* canvas, bitmap_t* img if (i >= 0) { img->flags |= BITMAP_FLAG_TEXTURE; - img->specific = (char*)NULL + i; + img->specific = tk_pointer_from_int(i); image_manager_update_specific(image_manager(), img); } diff --git a/src/xml/xml_parser.c b/src/xml/xml_parser.c index d67c9c79f..b09a17352 100644 --- a/src/xml/xml_parser.c +++ b/src/xml/xml_parser.c @@ -20,6 +20,7 @@ */ #include "tkc/fs.h" #include "tkc/mem.h" +#include "tkc/utils.h" #include "xml/xml_parser.h" #ifndef isspace #define isspace(c) (c == ' ' || c == '\t' || c == '\r' || c == '\n') @@ -207,7 +208,6 @@ static void xml_parser_parse_attrs(XmlParser* thiz, char end_char) { STAT_END, } state = STAT_PRE_KEY; - char* nullptr = NULL; char value_end = '\"'; const char* start = thiz->read_ptr; @@ -227,7 +227,7 @@ static void xml_parser_parse_attrs(XmlParser* thiz, char end_char) { case STAT_KEY: { if (c == '=') { thiz->attrs[thiz->attrs_nr++] = - nullptr + xml_parser_strdup(thiz, start, thiz->read_ptr - start); + tk_pointer_from_int(xml_parser_strdup(thiz, start, thiz->read_ptr - start)); state = STAT_PRE_VALUE; } @@ -244,7 +244,7 @@ static void xml_parser_parse_attrs(XmlParser* thiz, char end_char) { case STAT_VALUE: { if (c == value_end) { thiz->attrs[thiz->attrs_nr++] = - nullptr + xml_parser_strdup(thiz, start, thiz->read_ptr - start); + tk_pointer_from_int(xml_parser_strdup(thiz, start, thiz->read_ptr - start)); state = STAT_PRE_KEY; } } @@ -258,7 +258,7 @@ static void xml_parser_parse_attrs(XmlParser* thiz, char end_char) { } for (i = 0; i < thiz->attrs_nr; i++) { - thiz->attrs[i] = thiz->buffer + (int)(thiz->attrs[i] - (char*)NULL); + thiz->attrs[i] = thiz->buffer + tk_pointer_to_int(thiz->attrs[i]); } thiz->attrs[thiz->attrs_nr] = NULL; @@ -272,7 +272,6 @@ static void xml_parser_parse_start_tag(XmlParser* thiz) { STAT_END, } state = STAT_NAME; - char* nullptr = NULL; char* tag_name = NULL; const char* start = thiz->read_ptr - 1; @@ -282,7 +281,7 @@ static void xml_parser_parse_start_tag(XmlParser* thiz) { switch (state) { case STAT_NAME: { if (isspace(c) || c == '>' || c == '/') { - tag_name = nullptr + xml_parser_strdup(thiz, start, thiz->read_ptr - start); + tag_name = tk_pointer_from_int(xml_parser_strdup(thiz, start, thiz->read_ptr - start)); state = (c != '>' && c != '/') ? STAT_ATTR : STAT_END; } break; @@ -302,7 +301,7 @@ static void xml_parser_parse_start_tag(XmlParser* thiz) { } } - tag_name = thiz->buffer + (tag_name - (char*)NULL); + tag_name = thiz->buffer + tk_pointer_to_int(tag_name); xml_builder_on_start(thiz->builder, tag_name, (const char**)thiz->attrs); if (thiz->read_ptr[0] == '/') { @@ -384,7 +383,6 @@ static void xml_parser_parse_comment(XmlParser* thiz) { static void xml_parser_parse_pi(XmlParser* thiz) { enum _State { STAT_NAME, STAT_ATTR, STAT_END } state = STAT_NAME; - char* nullptr = NULL; char* tag_name = NULL; const char* start = thiz->read_ptr; @@ -394,7 +392,7 @@ static void xml_parser_parse_pi(XmlParser* thiz) { switch (state) { case STAT_NAME: { if (isspace(c) || c == '>') { - tag_name = nullptr + xml_parser_strdup(thiz, start, thiz->read_ptr - start); + tag_name = tk_pointer_from_int(xml_parser_strdup(thiz, start, thiz->read_ptr - start)); state = c != '>' ? STAT_ATTR : STAT_END; } @@ -414,7 +412,7 @@ static void xml_parser_parse_pi(XmlParser* thiz) { } } - tag_name = thiz->buffer + (tag_name - (char*)NULL); + tag_name = thiz->buffer + tk_pointer_to_int(tag_name); xml_builder_on_pi(thiz->builder, tag_name, (const char**)thiz->attrs); for (; *thiz->read_ptr != '>' && *thiz->read_ptr != '\0'; thiz->read_ptr++)