1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2010-2017 Brazil
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License version 2.1 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "grn.h"
20#include "grn_windows.h"
21
22#ifdef WIN32
23static char *windows_base_dir = NULL;
24const char *
25grn_windows_base_dir(void)
26{
27 if (!windows_base_dir) {
28 HMODULE dll;
29 const wchar_t *dll_filename = GRN_DLL_FILENAME;
30 wchar_t absolute_dll_filename[MAX_PATH];
31 DWORD absolute_dll_filename_size;
32 dll = GetModuleHandleW(dll_filename);
33 absolute_dll_filename_size = GetModuleFileNameW(dll,
34 absolute_dll_filename,
35 MAX_PATH);
36 if (absolute_dll_filename_size == 0) {
37 windows_base_dir = grn_strdup_raw(".");
38 } else {
39 DWORD ansi_dll_filename_size;
40 ansi_dll_filename_size =
41 WideCharToMultiByte(CP_ACP, 0,
42 absolute_dll_filename, absolute_dll_filename_size,
43 NULL, 0, NULL, NULL);
44 if (ansi_dll_filename_size == 0) {
45 windows_base_dir = grn_strdup_raw(".");
46 } else {
47 char *path;
48 windows_base_dir = malloc(ansi_dll_filename_size + 1);
49 WideCharToMultiByte(CP_ACP, 0,
50 absolute_dll_filename, absolute_dll_filename_size,
51 windows_base_dir, ansi_dll_filename_size,
52 NULL, NULL);
53 windows_base_dir[ansi_dll_filename_size] = '\0';
54 if ((path = strrchr(windows_base_dir, '\\'))) {
55 *path = '\0';
56 }
57 path = strrchr(windows_base_dir, '\\');
58 if (path && (grn_strcasecmp(path + 1, "bin") == 0 ||
59 grn_strcasecmp(path + 1, "lib") == 0)) {
60 *path = '\0';
61 } else {
62 path = windows_base_dir + strlen(windows_base_dir);
63 *path = '\0';
64 }
65 for (path = windows_base_dir; *path; path++) {
66 if (*path == '\\') {
67 *path = '/';
68 }
69 }
70 }
71 }
72 }
73 return windows_base_dir;
74}
75
76UINT
77grn_windows_encoding_to_code_page(grn_encoding encoding)
78{
79 UINT code_page;
80
81 switch (encoding) {
82 case GRN_ENC_EUC_JP :
83 code_page = 20932;
84 break;
85 case GRN_ENC_UTF8 :
86 code_page = CP_UTF8;
87 break;
88 case GRN_ENC_SJIS :
89 code_page = 932;
90 break;
91 case GRN_ENC_LATIN1 :
92 code_page = 1252;
93 break;
94 case GRN_ENC_KOI8R :
95 code_page = 20866;
96 break;
97 default :
98 code_page = CP_ACP;
99 break;
100 }
101
102 return code_page;
103}
104#endif /* WIN32 */
105