Skip to content

Commit

Permalink
merge chinese filename bug on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli authored and jason-mao committed Oct 30, 2019
1 parent ce27844 commit 1b5b8cb
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 33 deletions.
67 changes: 56 additions & 11 deletions demos/preview_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@
#include "tkc/utils.h"
#include "base/timer.h"
#include "assets.h"
#include "tkc/utf8.h"

#include "ui_loader/ui_loader_xml.h"
#include "ui_loader/ui_loader_default.h"
#include "ui_loader/ui_builder_default.h"

#ifdef WITH_SDL
#include <SDL.h>
#endif

static ret_t refresh_in_timer(const timer_info_t* info) {
widget_t* widget = WIDGET(info->ctx);

Expand Down Expand Up @@ -90,15 +95,23 @@ widget_t* preview_ui(const char* filename) {
#if defined(WIN32)
#include <windows.h>

#define MAX_ARGV 5
#define MAX_ARGV 7
void command_line_to_argv(char* lpcmdline, const char* argv[MAX_ARGV], int32_t* argc) {
int32_t i = 1;
char* p = lpcmdline;

argv[0] = "preview.exe";
for (i = 1; i < MAX_ARGV; i++) {
argv[i] = p;
p = strchr(p, ' ');
if (*p == '\"') {
argv[i] = p + 1;
p = strchr(p + 1, '\"');
if (p == NULL) break;
*p++ = '\0';
if (*p == 0) break;
} else {
p = strchr(p, ' ');
}
if (p == NULL) {
break;
}
Expand All @@ -112,19 +125,31 @@ void command_line_to_argv(char* lpcmdline, const char* argv[MAX_ARGV], int32_t*
return;
}

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) {
int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPWSTR lpcmdline, int ncmdshow) {
str_t str;
int argc = 1;
int32_t len = wcslen(lpcmdline) * 6;
char* utf8_line = (char*)TKMEM_ALLOC(len);
utf8_from_utf16(lpcmdline, utf8_line, len);
char* argv[MAX_ARGV];
str_init(&str, 1024);
str_set(&str, lpcmdline);
str_set(&str, utf8_line);
command_line_to_argv(str.str, argv, &argc);
#if defined(WIN32)
#if !defined(NDEBUG)
{
AllocConsole();
FILE* fp = NULL;
freopen_s(&fp, "CONOUT$", "w+t", stdout);
bool_t log = FALSE;
int8_t last_arg = MAX_ARGV - 1;
if (argc > last_arg) {
if (atoi(argv[last_arg]) == 1) {
log = TRUE;
}
}
if (log) {
AllocConsole();
FILE* fp = NULL;
freopen_s(&fp, "CONOUT$", "w+t", stdout);
}
}
#endif /*NDEBUG*/
#endif /*WIN32*/
Expand All @@ -134,8 +159,11 @@ int main(int argc, char* argv[]) {
#endif
int32_t w = 320;
int32_t h = 480;
bool_t have_lang = FALSE;
const char* filename = DEFAULT_UI;
char res_root[MAX_PATH + 1];
char country[3];
char language[3];
memset(res_root, 0x00, sizeof(res_root));

if (argc > 1) {
Expand All @@ -162,7 +190,7 @@ int main(int argc, char* argv[]) {
}
}

if (argc > 5) {
if (argc > 4) {
memcpy(res_root, argv[4], strlen(argv[4]));
} else {
char* p = NULL;
Expand All @@ -172,18 +200,35 @@ int main(int argc, char* argv[]) {
*p = '\0';
}
}

log_debug("%s %s %d %d %s\n", argv[0], argv[1], w, h, res_root);
tk_init(w, h, APP_SIMULATOR, NULL, res_root);

if (argc > 5) {
char str[MAX_PATH + 1];
memset(str, 0x00, sizeof(str));
memcpy(str, argv[5], strlen(argv[5]));
tk_strncpy(language, str, 2);
tk_strncpy(country, str + 3, 2);
have_lang = TRUE;
}

tk_init(w, h, APP_DESKTOP, NULL, res_root);
assets_manager_set_res_root(assets_manager(), res_root);
#ifdef WITH_FS_RES
system_info_set_default_font(system_info(), "default_full");
system_info_set_default_font(system_info(), "default");
#endif /*WITH_FS_RES*/

assets_init();
tk_ext_widgets_init();

preview_ui(filename);
if (have_lang) {
locale_info_change(locale_info(), language, country);
}
tk_run();

#ifdef WIN32
TKMEM_FREE(utf8_line);
#endif

return 0;
}
1 change: 1 addition & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* 更新awtk config sample。
* 修改软键盘弹出时出现屏保的问题。
* 完善new.cpp/new.hpp (感谢陈谭提供补丁)。
* 修改windows中文件名的问题(感谢大恒提供补丁)

* 2019/10/23
* 修改clone出来的edit的显示问题。
Expand Down
32 changes: 16 additions & 16 deletions src/platforms/pc/dirent.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@ struct dirent {
uint8_t d_type;
unsigned short d_reclen;
unsigned short d_namlen;
char d_name[MAX_PATH];
wchar_t d_name[MAX_PATH];
};

typedef struct DIR DIR;

DIR* opendir(const char*);
DIR* opendir(const wchar_t*);
struct dirent* readdir(DIR*);
int closedir(DIR*);

struct DIR {
char* dirname;
wchar_t* dirname;
struct dirent ret; /* Used to return to caller */
HANDLE handle;
};

DIR* opendir(const char* dirname) {
DIR* opendir(const wchar_t* dirname) {
DWORD attr;
DIR* d;

/* Make sure it is a directory */
attr = GetFileAttributes(dirname);
attr = GetFileAttributesW(dirname);
if (attr == INVALID_FILE_ATTRIBUTES) {
errno = ENOENT;
return NULL;
Expand All @@ -64,17 +64,17 @@ DIR* opendir(const char* dirname) {
errno = ENOMEM;
return NULL;
}
d->dirname = malloc(strlen(dirname) + 4);
d->dirname = malloc(wcslen(dirname) * sizeof(wchar_t) + 4);
if (!d->dirname) {
errno = ENOMEM;
free(d);
return NULL;
}
strcpy(d->dirname, dirname);
if (d->dirname[strlen(d->dirname) - 1] != '/' && d->dirname[strlen(d->dirname) - 1] != '\\')
strcat(d->dirname, "\\"); /* Append backslash if not already
* there */
strcat(d->dirname, "*"); /* Search for entries named anything */
wcscpy(d->dirname, dirname);
if (d->dirname[wcslen(d->dirname) - 1] != '/' && d->dirname[wcslen(d->dirname) - 1] != '\\')
wcscat(d->dirname, L"\\"); /* Append backslash if not already
* there */
wcscat(d->dirname, L"*"); /* Search for entries named anything */
d->handle = INVALID_HANDLE_VALUE;
d->ret.d_ino = 0; /* no inodes on win32 */
d->ret.d_reclen = 0; /* not used on win32 */
Expand All @@ -86,16 +86,16 @@ void rewinddir(DIR* d) {
}

struct dirent* readdir(DIR* d) {
WIN32_FIND_DATA fd;
WIN32_FIND_DATAW fd;

if (d->handle == INVALID_HANDLE_VALUE) {
d->handle = FindFirstFile(d->dirname, &fd);
d->handle = FindFirstFileW(d->dirname, &fd);
if (d->handle == INVALID_HANDLE_VALUE) {
errno = ENOENT;
return NULL;
}
} else {
if (!FindNextFile(d->handle, &fd)) {
if (!FindNextFileW(d->handle, &fd)) {
if (GetLastError() == ERROR_NO_MORE_FILES) {
/* No more files, force errno=0 (unlike mingw) */
errno = 0;
Expand All @@ -104,9 +104,9 @@ struct dirent* readdir(DIR* d) {
return NULL;
}
}
strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH
wcscpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH
* long */
d->ret.d_namlen = strlen(d->ret.d_name);
d->ret.d_namlen = wcslen(d->ret.d_name);

d->ret.d_type = 0;
if (fd.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) {
Expand Down
Loading

0 comments on commit 1b5b8cb

Please sign in to comment.