Skip to content

Commit

Permalink
mz_os.h - fix indentation; mz_os_win32.c - adjust for utf8 strings to…
Browse files Browse the repository at this point in the history
… be represented by char*; test_encoding.cc - remove useless cast
  • Loading branch information
ptc-tgamper authored and nmoinvaz committed Jun 8, 2023
1 parent 78b8721 commit 83301fc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion mz_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ wchar_t *mz_os_unicode_string_create(const char *string, int32_t encoding);
void mz_os_unicode_string_delete(wchar_t **string);
/* Delete a unicode string that was created */

char *mz_os_utf8_string_create(const char *string, int32_t encoding);
char *mz_os_utf8_string_create(const char *string, int32_t encoding);
/* Create a utf8 string from a string with another encoding */

void mz_os_utf8_string_delete(char **string);
Expand Down
24 changes: 12 additions & 12 deletions mz_os_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,41 +58,41 @@ void mz_os_unicode_string_delete(wchar_t **string) {
}
}

uint8_t *mz_os_utf8_string_create(const char *string, int32_t encoding) {
char *mz_os_utf8_string_create(const char *string, int32_t encoding) {
wchar_t *string_wide = NULL;
uint8_t *string_utf8 = NULL;
char *string_utf8 = NULL;
uint32_t string_utf8_size = 0;

string_wide = mz_os_unicode_string_create(string, encoding);
if (string_wide) {
string_utf8_size = WideCharToMultiByte(CP_UTF8, 0, string_wide, -1, NULL, 0, NULL, NULL);
string_utf8 = (uint8_t *)calloc(string_utf8_size + 1, sizeof(wchar_t));
string_utf8 = (char *)calloc(string_utf8_size + 1, sizeof(char));

if (string_utf8)
WideCharToMultiByte(CP_UTF8, 0, string_wide, -1, (char *)string_utf8, string_utf8_size, NULL, NULL);
WideCharToMultiByte(CP_UTF8, 0, string_wide, -1, string_utf8, string_utf8_size, NULL, NULL);

mz_os_unicode_string_delete(&string_wide);
}

return string_utf8;
}

uint8_t *mz_os_utf8_string_create_from_unicode(const wchar_t *string, int32_t encoding) {
uint8_t *string_utf8 = NULL;
char *mz_os_utf8_string_create_from_unicode(const wchar_t *string, int32_t encoding) {
char *string_utf8 = NULL;
uint32_t string_utf8_size = 0;

MZ_UNUSED(encoding);

string_utf8_size = WideCharToMultiByte(CP_UTF8, 0, string, -1, NULL, 0, NULL, NULL);
string_utf8 = (uint8_t *)calloc(string_utf8_size + 1, sizeof(wchar_t));
string_utf8 = (char *)calloc(string_utf8_size + 1, sizeof(char));

if (string_utf8)
WideCharToMultiByte(CP_UTF8, 0, string, -1, (char *)string_utf8, string_utf8_size, NULL, NULL);
WideCharToMultiByte(CP_UTF8, 0, string, -1, string_utf8, string_utf8_size, NULL, NULL);

return string_utf8;
}

void mz_os_utf8_string_delete(uint8_t **string) {
void mz_os_utf8_string_delete(char **string) {
if (string) {
free(*string);
*string = NULL;
Expand Down Expand Up @@ -563,7 +563,7 @@ int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_targ
int32_t target_path_len = 0;
int32_t target_path_idx = 0;
int32_t err = MZ_OK;
uint8_t *target_path_utf8 = NULL;
char *target_path_utf8 = NULL;

if (!path)
return MZ_PARAM_ERROR;
Expand Down Expand Up @@ -609,10 +609,10 @@ int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_targ
target_path_utf8 = mz_os_utf8_string_create_from_unicode(target_path_wide, MZ_ENCODING_UTF8);

if (target_path_utf8) {
strncpy(target_path, (const char *)target_path_utf8, max_target_path - 1);
strncpy(target_path, target_path_utf8, max_target_path - 1);
target_path[max_target_path - 1] = 0;
/* Ensure directories have slash at the end so we can recreate them later */
if (mz_os_is_dir((const char *)target_path_utf8) == MZ_OK)
if (mz_os_is_dir(target_path_utf8) == MZ_OK)
mz_path_append_slash(target_path, max_target_path, MZ_PATH_SLASH_PLATFORM);
mz_os_utf8_string_delete(&target_path_utf8);
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/test_encoding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ TEST(os, utf8_unicode_string) {
char *utf8_string = mz_os_utf8_string_create(test_string, MZ_ENCODING_CODEPAGE_950);
ASSERT_NE(utf8_string, nullptr);
#if defined(_WIN32)
wchar_t *unicode_string = mz_os_unicode_string_create((const char *)utf8_string, MZ_ENCODING_UTF8);
wchar_t *unicode_string = mz_os_unicode_string_create(utf8_string, MZ_ENCODING_UTF8);
ASSERT_NE(unicode_string, nullptr);
mz_os_unicode_string_delete(&unicode_string);
#endif
Expand Down

0 comments on commit 83301fc

Please sign in to comment.