diff options
Diffstat (limited to 'hangul/hangulkeyboard.c')
-rw-r--r-- | hangul/hangulkeyboard.c | 104 |
1 files changed, 99 insertions, 5 deletions
diff --git a/hangul/hangulkeyboard.c b/hangul/hangulkeyboard.c index 9e1e599..97be149 100644 --- a/hangul/hangulkeyboard.c +++ b/hangul/hangulkeyboard.c @@ -26,8 +26,9 @@ #if ENABLE_EXTERNAL_KEYBOARDS #include <locale.h> +#ifdef HAVE_GLOB_H #include <glob.h> -#include <libgen.h> +#endif /* HAVE_GLOB_H */ #include <expat.h> #endif /* ENABLE_EXTERNAL_KEYBOARDS */ @@ -36,8 +37,11 @@ #include "hangulinternals.h" #ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#include <fileapi.h> #define strdup _strdup -#endif +#endif /* _WIN32 */ /** * @file hangulkeyboard.c @@ -674,8 +678,11 @@ on_element_start(void* data, const XML_Char* element, const XML_Char** attr) strncpy(path, file, n); } else { char* orig_path = strdup(context->path_stack[top]); - char* dir = dirname(orig_path); - snprintf(path, n, "%s/%s", dir, file); + char* last_slash = strrchr(orig_path, '/'); + if (last_slash) + last_slash[0] = '\0'; + + snprintf(path, n, "%s/%s", orig_path, file); free(orig_path); } @@ -798,6 +805,7 @@ hangul_keyboard_list_load_dir(const char* path) snprintf(pattern, len, "%s%s", path, subpattern); +#ifdef HAVE_GLOB_H glob_t result; int res = glob(pattern, GLOB_ERR, NULL, &result); if (res != 0) { @@ -815,6 +823,54 @@ hangul_keyboard_list_load_dir(const char* path) globfree(&result); free(pattern); +#else /* _WIN32 */ + WIN32_FIND_DATAW findFileData; + HANDLE hFind; + int n = (strlen(pattern) + 1) * sizeof(WCHAR); + + LPWSTR wpattern = (LPWSTR)malloc(n); + if (wpattern == NULL) { + free(pattern); + return 0; + } + + MultiByteToWideChar(CP_ACP, 0, pattern, -1, wpattern, n); + + hFind = FindFirstFileW(wpattern, &findFileData); + if (hFind == INVALID_HANDLE_VALUE) { + free(wpattern); + free(pattern); + return 0; + } + + do { + n = WideCharToMultiByte(CP_ACP, 0, findFileData.cFileName, -1, NULL, 0, NULL, NULL); + if (n == 0) + continue; + + int path_len = strlen(path); + len = path_len + n + 2; + char* file = (char*)malloc(len); + if (file == NULL) + continue; + + memcpy(file, path, path_len); + file[path_len] = '/'; + char* pfile = &file[path_len + 1]; + WideCharToMultiByte(CP_ACP, 0, findFileData.cFileName, -1, pfile, n, NULL, NULL); + + HangulKeyboard* keyboard = hangul_keyboard_new_from_file(file); + free(file); + + if (keyboard == NULL) + continue; + hangul_keyboard_list_append(keyboard); + } while(FindNextFileW(hFind, &findFileData)); + + FindClose(hFind); + free(wpattern); + free(pattern); +#endif /* HAVE_GLOB_H */ return hangul_keyboards.n; } @@ -845,6 +901,38 @@ hangul_keyboard_get_default_keyboard_path() /* default LIBHANGUL_KEYBOARD_PATH is * SYSTEM_KEYBOARD_DIR:USER_KEYBOARD_DIR */ +#ifdef _WIN32 + /* system default dir */ + char* system_dir = NULL; + const char* data_dir = getenv("APPDATA"); + if (data_dir != NULL) { + const char* subdir = "/libhangul/keyboards"; + size_t system_dir_len = strlen(data_dir) + strlen(subdir) + 1; + system_dir = (char*)malloc(system_dir_len); + if (system_dir != NULL) { + snprintf(system_dir, system_dir_len, "%s%s", data_dir, subdir); + keyboard_path_len += strlen(system_dir); + } + } + /* user default dir */ + char* home_dir = getenv("USERPROFILE"); + if (home_dir == NULL) { + /* no user data dir */ + return system_dir; + } else { + const char* subdir = "/.libhangul/keyboards"; + keyboard_path_len += strlen(home_dir) + strlen(subdir); + keyboard_path = (char*)malloc(keyboard_path_len); + if (keyboard_path != NULL) { + if (system_dir != NULL) { + keyboard_path_len++; + snprintf(keyboard_path, keyboard_path_len, "%s;%s%s", system_dir, home_dir, subdir); + } else { + snprintf(keyboard_path, keyboard_path_len, "%s%s", home_dir, subdir); + } + } + } +#else /* system default dir */ const char* system_dir = LIBHANGUL_KEYBOARD_DIR; keyboard_path_len += strlen(system_dir); @@ -875,6 +963,7 @@ hangul_keyboard_get_default_keyboard_path() snprintf(keyboard_path, keyboard_path_len, "%s:%s%s", system_dir, xdg_data_home, subdir); } } +#endif /* _WIN32 */ return keyboard_path; } @@ -914,8 +1003,13 @@ hangul_keyboard_list_init() unsigned n = 0; char* dir = libhangul_keyboard_path; +#ifdef _WIN32 + char sep = ';'; +#else + char sep = ':'; +#endif /* _WIN32 */ while (dir != NULL && dir[0] != '\0') { - char* next = strchr(dir, ':'); + char* next = strchr(dir, sep); if (next != NULL) { next[0] = '\0'; ++next; |