diff --git a/src/charset.cpp b/src/charset.cpp index 5d2d0ea..61443b9 100644 --- a/src/charset.cpp +++ b/src/charset.cpp @@ -131,78 +131,54 @@ static size_t _decode_utf16(const uint16_t *buffer, size_t max_len, uint32_t *co return 1; } -size_t utf8_to_utf16(const std::string &input, uint16_t *buffer, size_t buffer_size) { - assert(buffer_size > 0); +size_t utf8_to_utf16(const char *input, size_t input_len, uint16_t *buffer, size_t buffer_size) { size_t new_len = 0; - for (size_t i = 0; i < input.size(); ) { + for (size_t i = 0; i < input_len; ) { uint32_t codepoint = 0; - i += _decode_utf8(&input[i], input.size() - i, &codepoint); + i += _decode_utf8(&input[i], input_len - i, &codepoint); if ((codepoint >= 0x0000 && codepoint <= 0xD7FF) || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) { - if ((new_len + 1) > (buffer_size - 1)) + if ((new_len + 1) > buffer_size) break; buffer[new_len++] = codepoint; } else { - if ((new_len + 2) > (buffer_size - 1)) + if ((new_len + 2) > buffer_size) break; uint32_t double_codepoint = codepoint - 0x10000; buffer[new_len++] = ((double_codepoint >> 10) & 0x3FF) | 0xD800; buffer[new_len++] = (double_codepoint & 0x3FF) | 0xDC00; } } - buffer[new_len] = 0; return new_len; } -size_t utf16_to_utf8(const std::vector &input, char *buffer, size_t buffer_size) { - assert(buffer_size > 0); +size_t utf16_to_utf8(const uint16_t *input, size_t input_len, char *buffer, size_t buffer_size) { size_t new_len = 0; - for (size_t i = 0; i < input.size(); ) { + for (size_t i = 0; i < input_len; ) { uint32_t codepoint = 0; - i += _decode_utf16(&input[i], input.size() - i, &codepoint); + i += _decode_utf16(&input[i], input_len - i, &codepoint); size_t char_len = _codepoint_len(codepoint); - if (new_len + char_len > (buffer_size - 1)) + if (new_len + char_len > buffer_size) break; new_len += _encode_codepoint(&buffer[new_len], codepoint); } - buffer[new_len] = 0; return new_len; } -std::string cp1251_to_utf8(const std::string &input) { - char tmp[4]; - std::string out; - for (auto &c: input) - out.append(tmp, _encode_codepoint(tmp, cp1251_to_utf16_table[c])); - return out; -} - size_t cp1251_to_utf8(const std::string &input, char *buffer, size_t buffer_size) { - assert(buffer_size > 0); size_t new_len = 0; for (size_t i = 0; i < input.size(); i++) { uint16_t codepoint = cp1251_to_utf16_table[input[i]]; size_t char_len = _codepoint_len(codepoint); - if (new_len + char_len > (buffer_size - 1)) + if (new_len + char_len > buffer_size) break; new_len += _encode_codepoint(&buffer[new_len], codepoint); } - buffer[new_len] = 0; return new_len; } -std::vector cp1251_to_utf16(const std::string &input) { - std::vector out; - out.resize(input.size()); - for (auto &c: input) - out.push_back(cp1251_to_utf16_table[c]); - return out; -} - -size_t cp1251_to_utf16(const std::string &input, uint16_t *buffer, size_t buffer_size) { - assert(buffer_size > 0); - size_t new_len = std::min(buffer_size - 1, input.size()); +size_t cp1251_to_utf16(const char *input, size_t input_len, uint16_t *buffer, size_t buffer_size) { + size_t new_len = std::min(buffer_size, input_len); for (size_t i = 0; i < new_len; i++) buffer[i] = cp1251_to_utf16_table[input[i]]; - buffer[new_len] = 0; return new_len; } diff --git a/src/charset.h b/src/charset.h index 7783c7c..21b5fc0 100644 --- a/src/charset.h +++ b/src/charset.h @@ -5,13 +5,13 @@ #include // CP1251 -> UTF8 -std::string cp1251_to_utf8(const std::string &input); -size_t cp1251_to_utf8(const std::string &input, char *buffer, size_t buffer_size); +size_t cp1251_to_utf16(const char *input, size_t input_len, uint16_t *buffer, size_t buffer_size); // CP1251 -> UTF16 -std::vector cp1251_to_utf16(const std::string &input); size_t cp1251_to_utf16(const std::string &input, uint16_t *buffer, size_t buffer_size); // UTF16 -> UTF8 -size_t utf16_to_utf8(const std::vector &input, char *buffer, size_t buffer_size); -size_t utf8_to_utf16(const std::string &input, uint16_t *buffer, size_t buffer_size); +size_t utf16_to_utf8(const uint16_t *input, size_t input_len, char *buffer, size_t buffer_size); + +// UTF8 -> UTF16 +size_t utf8_to_utf16(const char *input, size_t input_len, uint16_t *buffer, size_t buffer_size); diff --git a/src/swi.h b/src/swi.h index bae6c0a..e0ee074 100644 --- a/src/swi.h +++ b/src/swi.h @@ -172,7 +172,15 @@ TDateTimeSettings *SWI_RamDateTimeSettings(void); * Wide String * */ typedef struct { - uint16_t *wsbody; + uint16_t len; + uint16_t data[]; +} WSBODY; + +typedef struct { + union { + WSBODY *body; + uint16_t *wsbody; + }; void *(*ws_malloc)(size_t); void (*ws_mfree)(void *); int isbody_allocated; @@ -181,23 +189,23 @@ typedef struct { } WSHDR; WSHDR *SWI_wstrcpy(WSHDR *wshdr_d, WSHDR *wshdr_s); -WSHDR *SWI_wstrncpy(WSHDR *param1, WSHDR * param2, int param3); +WSHDR *SWI_wstrncpy(WSHDR *param1, WSHDR * param2, uint16_t param3); WSHDR *SWI_wstrcat(WSHDR *wshdr_d, WSHDR *wshdr_s); -WSHDR *SWI_wstrncat(WSHDR *wshdr_d, WSHDR *wshdr_s, int n); +WSHDR *SWI_wstrncat(WSHDR *wshdr_d, WSHDR *wshdr_s, uint16_t n); int SWI_wstrlen(WSHDR *wshdr); int SWI_wsprintf(WSHDR* param1, const char *format,...); -WSHDR *SWI_AllocWS(int len); -void SWI_CutWSTR(WSHDR *wshdr, int len); -WSHDR *SWI_CreateLocalWS(WSHDR *wshdr, uint16_t *wsbody, int len); -WSHDR *SWI_CreateWS(void *(*ws_malloc)(size_t), void (*ws_mfree)(void *), int len); +WSHDR *SWI_AllocWS(uint16_t len); +void SWI_CutWSTR(WSHDR *wshdr, uint16_t len); +WSHDR *SWI_CreateLocalWS(WSHDR *wshdr, uint16_t *wsbody, uint16_t len); +WSHDR *SWI_CreateWS(void *(*ws_malloc)(size_t), void (*ws_mfree)(void *), uint16_t len); void SWI_FreeWS(WSHDR *wshdr); -int SWI_str_2ws(WSHDR *ws, const char *str, uint32_t size); +int SWI_str_2ws(WSHDR *ws, const char *str, uint16_t size); int SWI_wstrcmp(WSHDR *ws1, WSHDR *ws2); -void SWI_wstrcpybypos(WSHDR *dest, WSHDR *src, int from, int len); -void SWI_wsRemoveChars(WSHDR *ws, int from, int to); -int SWI_ws_2utf8( WSHDR *from, char *to, int *result_length, uint32_t max_len); -int SWI_utf8_2ws(WSHDR *ws, const char *utf8_str, uint32_t maxLen); -short SWI_wstrchr(WSHDR *ws, uint32_t start_pos, uint32_t wchar); -short SWI_wstrrchr(WSHDR *ws, uint32_t max_pos, uint32_t wchar); -void SWI_wsAppendChar(WSHDR *ws, int wchar); -int SWI_wsInsertChar(WSHDR *ws, int wchar, int pos); +void SWI_wstrcpybypos(WSHDR *dest, WSHDR *src, uint16_t from, uint16_t len); +void SWI_wsRemoveChars(WSHDR *ws, uint16_t from, uint16_t to); +int SWI_ws_2utf8( WSHDR *from, char *to, int *result_length, uint16_t max_len); +int SWI_utf8_2ws(WSHDR *ws, const char *utf8_str, uint16_t maxLen); +uint16_t SWI_wstrchr(WSHDR *ws, uint16_t start_pos, uint16_t wchar); +uint16_t SWI_wstrrchr(WSHDR *ws, uint16_t max_pos, uint16_t wchar); +void SWI_wsAppendChar(WSHDR *ws, uint16_t wchar); +int SWI_wsInsertChar(WSHDR *ws, uint16_t wchar, uint16_t pos); diff --git a/src/swi/time.cpp b/src/swi/time.cpp index 6a74438..dcae9c3 100644 --- a/src/swi/time.cpp +++ b/src/swi/time.cpp @@ -28,7 +28,6 @@ void SWI_GetDateTime(TDate *d, TTime *t) { t->sec = local_tm.tm_sec; t->millisec = 0; } - } char SWI_GetWeek(TDate *param1) { diff --git a/src/swi/ws.cpp b/src/swi/ws.cpp index 9d10fd4..219556d 100644 --- a/src/swi/ws.cpp +++ b/src/swi/ws.cpp @@ -10,123 +10,13 @@ #include #include -int SWI_wstrcmp(WSHDR *ws1 ,WSHDR *ws2) { - fprintf(stderr, "%s not implemented!\n", __func__); - abort(); - return 0; -} - -void SWI_wstrcpybypos(WSHDR *dest, WSHDR *src, int from, int len) { - fprintf(stderr, "%s not implemented!\n", __func__); - abort(); -} - -void SWI_wsRemoveChars(WSHDR *ws, int from, int to) { - fprintf(stderr, "%s not implemented!\n", __func__); - abort(); -} - -short SWI_wstrchr(WSHDR *ws, uint32_t start_pos, uint32_t wchar) { - fprintf(stderr, "%s not implemented!\n", __func__); - abort(); - return 0; -} - -short SWI_wstrrchr(WSHDR *ws, uint32_t max_pos, uint32_t wchar) { - fprintf(stderr, "%s not implemented!\n", __func__); - abort(); - return 0; -} - -void SWI_wsAppendChar(WSHDR *ws, int wchar) { - WSHDR wslocal = {}; - uint16_t wsbody[2]; - SWI_CreateLocalWS(&wslocal, wsbody, 2); - wsbody[0] = wchar; - SWI_wstrcat(ws, &wslocal); -} - -int SWI_wsInsertChar(WSHDR *ws,int wchar,int pos) { - fprintf(stderr, "%s not implemented!\n", __func__); - abort(); - return 0; -} - -WSHDR *SWI_wstrcpy(WSHDR *ws1, WSHDR *ws2) { - return SWI_wstrncpy(ws1, ws2, SWI_wstrlen(ws2)); -} - -WSHDR *SWI_wstrncpy(WSHDR *ws1, WSHDR *ws2, int n) { - uint16_t *s = ws1->wsbody; - uint16_t *s2 = ws2->wsbody; - - if (n >= ws2->maxlen) - n = ws2->maxlen - 1; - if (n >= ws1->maxlen) - n = ws1->maxlen - 1; - - while (n > 0 && *s2 != 0) { - *s++ = *s2++; - --n; - } - while (n > 0) { - *s++ = 0; - --n; - } - return ws1; -} - -WSHDR *SWI_wstrcat(WSHDR *s1, WSHDR *s2) { - WSHDR wslocal = {}; - size_t len1 = SWI_wstrlen(s1); - SWI_CreateLocalWS(&wslocal, &s1->wsbody[len1], s1->maxlen - len1); - SWI_wstrcpy(&wslocal, s2); - return s1; -} - -WSHDR *SWI_wstrncat(WSHDR *s1, WSHDR *s2, int n) { - size_t len1 = SWI_wstrlen(s1); - size_t len2 = SWI_wstrlen(s2); - if (len2 < n) { - WSHDR wslocal = {}; - SWI_CreateLocalWS(&wslocal, &s1->wsbody[len1], s1->maxlen - len1); - SWI_wstrcpy(&wslocal, s2); - } else { - WSHDR wslocal = {}; - SWI_CreateLocalWS(&wslocal, &s1->wsbody[len1], s1->maxlen - len1); - SWI_wstrncpy(&s1[len1], s2, n); - s1->wsbody[len1 + n] = 0; - } - return s1; -} - -int SWI_wstrlen(WSHDR *ws) { - uint16_t *ptr = ws->wsbody; - int len = 0; - while (*ptr) { - *ptr++; - len++; - } - return len; -} - -int SWI_wsprintf(WSHDR * param1,const char *format,...) { - fprintf(stderr, "%s not implemented!\n", __func__); - abort(); - return 0; -} - -WSHDR *SWI_AllocWS(int len) { +WSHDR *SWI_AllocWS(uint16_t len) { return SWI_CreateWS(SWI_malloc, SWI_free, len); } -void SWI_CutWSTR(WSHDR *ws, int len) { - if (len >= ws->maxlen) - len = ws->maxlen - 1; - ws->wsbody[len] = 0; -} - -WSHDR *SWI_CreateLocalWS(WSHDR *ws, uint16_t *wsbody, int maxlen) { +WSHDR *SWI_CreateLocalWS(WSHDR *ws, uint16_t *wsbody, uint16_t maxlen) { + assert(ws != nullptr && wsbody != nullptr && maxlen >= 1); + ws->ws_malloc = nullptr; ws->ws_mfree = nullptr; ws->isbody_allocated = 0; @@ -137,7 +27,9 @@ WSHDR *SWI_CreateLocalWS(WSHDR *ws, uint16_t *wsbody, int maxlen) { return ws; } -WSHDR *SWI_CreateWS(void *(*ws_malloc)(size_t), void (*ws_mfree)(void *), int len) { +WSHDR *SWI_CreateWS(void *(*ws_malloc)(size_t), void (*ws_mfree)(void *), uint16_t len) { + assert(ws_malloc != nullptr && ws_mfree != nullptr); + WSHDR *ws = reinterpret_cast(ws_malloc(sizeof(WSHDR))); ws->ws_malloc = ws_malloc; ws->ws_mfree = ws_mfree; @@ -150,31 +42,144 @@ WSHDR *SWI_CreateWS(void *(*ws_malloc)(size_t), void (*ws_mfree)(void *), int le } void SWI_FreeWS(WSHDR *ws) { - if (ws) { - auto ws_free = ws->ws_mfree; - if (ws->isbody_allocated) - ws_free(ws->wsbody); - ws_free(ws); + assert(ws != nullptr && ws->ws_mfree != nullptr); + + auto ws_free = ws->ws_mfree; + if (ws->isbody_allocated) + ws_free(ws->wsbody); + ws_free(ws); +} + +int SWI_wstrlen(WSHDR *ws) { + assert(ws != nullptr); + return ws->wsbody[0]; +} + +int SWI_wstrcmp(WSHDR *ws1, WSHDR *ws2) { + assert(ws1 != nullptr && ws2 != nullptr); + + size_t len = std::min(ws1->body->len, ws2->body->len); + for (uint32_t i = 0; i < len; i++) { + if (ws1->body->data[i] != ws2->body->data[i]) + return ws1->body->data[i] - ws2->body->data[i]; } + + if (ws1->body->len == ws2->body->len) + return 0; + return ws1->body->len > ws2->body->len ? -1 : 1; } -int SWI_str_2ws(WSHDR *ws, const char *buffer, uint32_t max_size) { - if (max_size > ws->maxlen) - max_size = ws->maxlen; - return cp1251_to_utf16(buffer, ws->wsbody, max_size); +void SWI_wsRemoveChars(WSHDR *ws, uint16_t from, uint16_t to) { + fprintf(stderr, "%s not implemented!\n", __func__); + abort(); } -int SWI_ws_2utf8(WSHDR *from, char *buffer, int *result_length, uint32_t max_size) { - std::vector tmp; - tmp.assign(from->wsbody, from->wsbody + SWI_wstrlen(from)); - size_t new_len = utf16_to_utf8(tmp, buffer, max_size); +uint16_t SWI_wstrchr(WSHDR *ws, uint16_t start_pos, uint16_t ch) { + assert(ws != nullptr); + + for (size_t i = start_pos; i < ws->body->len; i++) { + if (ws->body->data[i] == ch) + return i; + } + return 0xFFFF; +} + +uint16_t SWI_wstrrchr(WSHDR *ws, uint16_t max_pos, uint16_t ch) { + assert(ws != nullptr && max_pos <= ws->body->len - 1); + + for (int i = max_pos; i >= 0; i--) { + if (ws->body->data[i] == ch) + return i; + } + return 0xFFFF; +} + +void SWI_wsAppendChar(WSHDR *ws, uint16_t ch) { + assert(ws != nullptr); + assert(ws->body->len < ws->maxlen - 1); + + ws->body->data[ws->body->len++] = ch; +} + +int SWI_wsInsertChar(WSHDR *ws, uint16_t ch, uint16_t pos) { + fprintf(stderr, "%s not implemented!\n", __func__); + abort(); + return 0; +} + +WSHDR *SWI_wstrcpy(WSHDR *ws1, WSHDR *ws2) { + assert(ws1 != nullptr && ws2 != nullptr); + return SWI_wstrncpy(ws1, ws2, ws2->body->len); +} + +WSHDR *SWI_wstrncpy(WSHDR *ws1, WSHDR *ws2, uint16_t n) { + assert(ws1 != nullptr && ws2 != nullptr); + + size_t copy_n = std::min(n, ws2->body->len); + assert(ws1->maxlen - 1 >= copy_n); + + ws1->body->len = copy_n; + memcpy(ws1->body->data, ws2->body->data, copy_n * sizeof(uint16_t)); + return ws1; +} + +void SWI_wstrcpybypos(WSHDR *ws1, WSHDR *ws2, uint16_t from, uint16_t n) { + assert(ws1 != nullptr && ws2 != nullptr); + + size_t copy_n = std::min(n, ws2->body->len); + assert(from < ws1->body->len); + assert(ws1->maxlen - 1 - from >= copy_n); + + ws1->body->len = copy_n; + memcpy(&ws1->body->data[from], ws2->body->data, copy_n * sizeof(uint16_t)); +} + +WSHDR *SWI_wstrcat(WSHDR *ws1, WSHDR *ws2) { + assert(ws1 != nullptr && ws2 != nullptr); + return SWI_wstrncat(ws1, ws2, ws2->body->len); +} + +WSHDR *SWI_wstrncat(WSHDR *ws1, WSHDR *ws2, uint16_t n) { + size_t copy_n = std::min(n, ws2->body->len); + + assert(ws1 != nullptr && ws2 != nullptr); + assert(ws1->maxlen - ws1->body->len - 1 >= copy_n); + + for (size_t i = 0; i < copy_n; i++) + ws1->body->data[ws1->body->len++] = ws2->body->data[i]; + return ws1; +} + +void SWI_CutWSTR(WSHDR *ws, uint16_t len) { + assert(ws != nullptr && len <= ws->body->len); + ws->body->len = len; +} + +int SWI_wsprintf(WSHDR *param1, const char *format,...) { + fprintf(stderr, "%s not implemented!\n", __func__); + abort(); + return 0; +} + +int SWI_str_2ws(WSHDR *ws, const char *buffer, uint16_t max_size) { + assert(ws != nullptr && buffer != nullptr && ws->maxlen - 1 >= max_size); + ws->body->len = cp1251_to_utf16(buffer, strlen(buffer), ws->body->data, max_size); + return ws->body->len; +} + +int SWI_ws_2utf8(WSHDR *ws, char *buffer, int *result_length, uint16_t max_size) { + assert(ws != nullptr && buffer != nullptr && max_size > 0); + + size_t new_len = utf16_to_utf8(ws->body->data, ws->body->len, buffer, max_size - 1); + buffer[new_len] = 0; + if (result_length) *result_length = new_len; return new_len; } -int SWI_utf8_2ws(WSHDR *ws, const char *buffer, uint32_t max_size) { - if (max_size > ws->maxlen) - max_size = ws->maxlen; - return utf8_to_utf16(buffer, ws->wsbody, max_size); +int SWI_utf8_2ws(WSHDR *ws, const char *buffer, uint16_t max_size) { + assert(ws != nullptr && buffer != nullptr && ws->maxlen - 1 >= max_size); + ws->body->len = utf8_to_utf16(buffer, strlen(buffer), ws->body->data, max_size); + return ws->body->len; }