Tizen 2.1 base
[external/enchant.git] / unittests / mock_provider / mock_provider.cpp
1 /* Copyright (c) 2007 Eric Scott Albright\r
2  * \r
3  * Permission is hereby granted, free of charge, to any person obtaining a copy\r
4  * of this software and associated documentation files (the "Software"), to deal\r
5  * in the Software without restriction, including without limitation the rights\r
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7  * copies of the Software, and to permit persons to whom the Software is\r
8  * furnished to do so, subject to the following conditions:\r
9  * \r
10  * The above copyright notice and this permission notice shall be included in\r
11  * all copies or substantial portions of the Software.\r
12  * \r
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
19  * THE SOFTWARE.\r
20  */\r
21 #include <glib.h>\r
22 #include <string>\r
23 #include "mock_provider.h"\r
24 \r
25 ENCHANT_PLUGIN_DECLARE("mock")\r
26 \r
27 static void\r
28 mock_provider_dispose(EnchantProvider *me)\r
29 {\r
30     g_free(me);\r
31 }\r
32 \r
33 static const char *\r
34 mock_provider_identify (EnchantProvider *)\r
35 {\r
36         return "mock";\r
37 }\r
38 \r
39 static const char *\r
40 mock_provider_describe (EnchantProvider *)\r
41 {\r
42         return "Mock Provider";\r
43 }\r
44 \r
45 static ConfigureHook _hook;\r
46 \r
47 \r
48 extern "C" {\r
49 \r
50 ENCHANT_MODULE_EXPORT(void)\r
51 set_configure(ConfigureHook hook){\r
52     _hook = hook;\r
53 }\r
54 \r
55 \r
56 ENCHANT_MODULE_EXPORT(EnchantProvider *) \r
57 init_enchant_provider(void)\r
58 {\r
59     bool hasIdentify = true;\r
60     bool hasDescribe = true;\r
61 #if defined(_WIN32)\r
62     std::wstring null_provider(L"null_provider.dll");\r
63     std::wstring null_identify(L"null_identify.dll");\r
64     std::wstring null_describe(L"null_describe.dll");\r
65     WCHAR szFilename[MAX_PATH];\r
66     DWORD cFilename = GetModuleFileName((HMODULE)s_hModule, (LPWSTR) &szFilename, sizeof(szFilename));\r
67     if(cFilename > null_provider.size()){\r
68         if(std::wstring(&szFilename[cFilename-null_provider.size()]) == null_provider){\r
69             return NULL;\r
70         }\r
71     }\r
72     if(cFilename > null_identify.size()){\r
73         if(std::wstring(&szFilename[cFilename-null_identify.size()]) == null_identify){\r
74             hasIdentify = false;\r
75         }\r
76     }\r
77     if(cFilename > null_describe.size()){\r
78         if(std::wstring(&szFilename[cFilename-null_describe.size()]) == null_describe){\r
79             hasDescribe = false;\r
80         }\r
81     }\r
82 \r
83 #endif\r
84 \r
85     EnchantProvider *provider;\r
86         \r
87     provider = g_new0(EnchantProvider, 1);\r
88     provider->dispose = mock_provider_dispose; //although this is technically optional, it will result in a memory leak \r
89     provider->request_dict = NULL;\r
90     provider->dispose_dict = NULL;\r
91     provider->identify = hasIdentify ? mock_provider_identify : NULL; // this is required or module won't load\r
92     provider->describe = hasDescribe ? mock_provider_describe : NULL; // this is required or module won't load\r
93     provider->list_dicts = NULL;\r
94     provider->dictionary_exists = NULL;\r
95     provider->free_string_list = NULL;\r
96 \r
97     return provider;\r
98 }\r
99 \r
100 ENCHANT_MODULE_EXPORT(void)\r
101 configure_enchant_provider(EnchantProvider * me, const char *dir_name)\r
102 {\r
103     if(_hook){\r
104         _hook(me, dir_name);\r
105     }\r
106 }\r
107 \r
108 }