diff options
author | Rechi <Rechi@users.noreply.github.com> | 2018-03-23 19:00:00 +0100 |
---|---|---|
committer | Rechi <Rechi@users.noreply.github.com> | 2018-03-23 19:00:00 +0100 |
commit | 14ffbe39d6a3b0cfbf334653d0f03d88f690acbe (patch) | |
tree | 1f9948c3393a7e2c875a89ec1ce1542f53f5c052 /lib | |
parent | cf7dd38abb6901f536b6a8d937a0a56248e06652 (diff) |
[cpluff] add windows changes
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cpluff/CMakeLists.txt | 55 | ||||
-rw-r--r-- | lib/cpluff/libcpluff/internal.h | 4 | ||||
-rw-r--r-- | lib/cpluff/libcpluff/ploader.c | 14 | ||||
-rw-r--r-- | lib/cpluff/libcpluff/win32/cpluffdef.h | 4 | ||||
-rw-r--r-- | lib/cpluff/libcpluff/win32/dirent.c | 66 | ||||
-rw-r--r-- | lib/cpluff/libcpluff/win32/dirent.h | 4 | ||||
-rw-r--r-- | lib/cpluff/libcpluff/win32/win32_utils.c | 73 | ||||
-rw-r--r-- | lib/cpluff/libcpluff/win32/win32_utils.h | 30 |
8 files changed, 212 insertions, 38 deletions
diff --git a/lib/cpluff/CMakeLists.txt b/lib/cpluff/CMakeLists.txt new file mode 100644 index 0000000000..931c0aee1c --- /dev/null +++ b/lib/cpluff/CMakeLists.txt @@ -0,0 +1,55 @@ +cmake_minimum_required(VERSION 3.0) + +project(libcpluff VERSION 0.1.4 LANGUAGES C) + +find_package(expat 2.2.0 REQUIRED) + +add_library(${PROJECT_NAME} + ${CMAKE_CURRENT_SOURCE_DIR}/kazlib/hash.h + ${CMAKE_CURRENT_SOURCE_DIR}/kazlib/hash.c + ${CMAKE_CURRENT_SOURCE_DIR}/kazlib/list.h + ${CMAKE_CURRENT_SOURCE_DIR}/kazlib/list.c + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/context.c + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/cpluff.h + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/cpluff.c + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/defines.h + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/internal.h + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/logging.c + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/pcontrol.c + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/pinfo.c + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/ploader.c + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/pscan.c + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/psymbol.c + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/serial.c + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/thread.h + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/util.h + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/util.c +) + +if(WIN32) + target_sources(${PROJECT_NAME} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/thread_windows.c + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/win32/cpluffdef.h + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/win32/dirent.h + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/win32/dirent.c + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/win32/win32_utils.h + ${CMAKE_CURRENT_SOURCE_DIR}/libcpluff/win32/win32_utils.c + ) +endif() + +target_link_libraries(${PROJECT_NAME} PRIVATE ${EXPAT_LIBRARIES}) + +target_include_directories(${PROJECT_NAME} + PRIVATE + $<BUILD_INTERFACE:kazlib;libcpluff;libcpluff/win32;${EXPAT_INCLUDE_DIR}> + INTERFACE + $<INSTALL_INTERFACE:include> +) + +target_compile_definitions(${PROJECT_NAME} + PRIVATE + XML_STATIC + CP_C_API=CP_EXPORT + _CRT_SECURE_NO_WARNINGS + _CRT_NONSTDC_NO_DEPRECATE +) diff --git a/lib/cpluff/libcpluff/internal.h b/lib/cpluff/libcpluff/internal.h index 161ba6de07..5f5761750b 100644 --- a/lib/cpluff/libcpluff/internal.h +++ b/lib/cpluff/libcpluff/internal.h @@ -85,7 +85,11 @@ extern "C" { #if defined(_WIN32) #define DLHANDLE void * +#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP) +#define DLOPEN(name) LoadPackagedLibrary(name, 0) +#else #define DLOPEN(name) LoadLibraryA(name) +#endif #define DLSYM(handle, symbol) GetProcAddress(handle, symbol) #define DLCLOSE(handle) CloseHandle(handle) #define DLERROR() "WIN32 - TODO" diff --git a/lib/cpluff/libcpluff/ploader.c b/lib/cpluff/libcpluff/ploader.c index 5c01addd43..66249381ab 100644 --- a/lib/cpluff/libcpluff/ploader.c +++ b/lib/cpluff/libcpluff/ploader.c @@ -41,6 +41,9 @@ #include "defines.h" #include "util.h" #include "internal.h" +#if defined(_WIN32) +#include "win32_utils.h" +#endif // Use XMLCALL if available #ifdef XMLCALL @@ -1159,11 +1162,22 @@ CP_C_API cp_plugin_info_t * cp_load_plugin_descriptor(cp_context_t *context, con file[path_len] = CP_FNAMESEP_CHAR; strcpy(file + path_len + 1, CP_PLUGIN_DESCRIPTOR); +#if defined(_WIN32) + wchar_t* fileW = to_utf16(file, 0); + fh = _wfopen(fileW, L"rb"); + free(fileW); + if (!fh) + { + status = CP_ERR_IO; + break; + } +#else // Open the file if ((fh = fopen(file, "rb")) == NULL) { status = CP_ERR_IO; break; } +#endif // Initialize descriptor parsing status = init_descriptor_parsing(context, &plcontext, &parser, file); diff --git a/lib/cpluff/libcpluff/win32/cpluffdef.h b/lib/cpluff/libcpluff/win32/cpluffdef.h index 891e95a0f6..7e3d55f269 100644 --- a/lib/cpluff/libcpluff/win32/cpluffdef.h +++ b/lib/cpluff/libcpluff/win32/cpluffdef.h @@ -136,7 +136,11 @@ #if defined(_WIN32) # define CP_EXPORT __declspec(dllexport) +#if defined(_WINDLL) # define CP_IMPORT extern __declspec(dllimport) +#else +# define CP_IMPORT +#endif # define CP_HIDDEN #elif defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) # define CP_EXPORT diff --git a/lib/cpluff/libcpluff/win32/dirent.c b/lib/cpluff/libcpluff/win32/dirent.c index 3254b22729..ead41848f9 100644 --- a/lib/cpluff/libcpluff/win32/dirent.c +++ b/lib/cpluff/libcpluff/win32/dirent.c @@ -9,6 +9,7 @@ */ #include "dirent.h" +#include "win32_utils.h" #include <errno.h> #include <io.h> /* _findfirst and _findnext set errno iff they return -1 */ #include <stdlib.h> @@ -21,10 +22,10 @@ extern "C" struct DIR { - long handle; /* -1 for failed rewind */ - struct _finddata_t info; + HANDLE handle; /* -1 for failed rewind */ struct dirent result; /* d_name null iff first time */ - char *name; /* null-terminated char string */ + wchar_t* name; + WIN32_FIND_DATAW info; }; DIR *opendir(const char *name) @@ -33,16 +34,29 @@ DIR *opendir(const char *name) if(name && name[0]) { - size_t base_length = strlen(name); - const char *all = /* search pattern must end with suitable wildcard */ - strchr("/\\", name[base_length - 1]) ? "*" : "/*"; - - if((dir = (DIR *) malloc(sizeof *dir)) != 0 && - (dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0) + if ((dir = (DIR *)malloc(sizeof *dir)) != 0) { - strcat(strcpy(dir->name, name), all); - - if((dir->handle = (long) _findfirst(dir->name, &dir->info)) != -1) + dir->handle = INVALID_HANDLE_VALUE; + int len = strlen(name); + size_t newLength = len + 2; //add an extra for null and another for a * at the end + if (!(name[0] == '\\' && name[1] == '\\' && name[2] == '?' && name[3] == '\\')) + newLength += 4; + if (name[len - 1] != '\\') + newLength += 1; + + char* newDir = (char*)malloc(newLength); + strcpy_s(newDir, newLength, "\\\\?\\"); + strcat_s(newDir, newLength, name); + if (name[len - 1] != '\\') + strcat_s(newDir, newLength, "\\"); + strcat_s(newDir, newLength, "*"); + newDir[newLength - 1] = '\0'; + + dir->name = to_utf16(newDir, newLength); + free(newDir); + + dir->handle = FindFirstFileW(dir->name, &dir->info); + if (dir->handle != INVALID_HANDLE_VALUE) { dir->result.d_name = 0; } @@ -55,6 +69,7 @@ DIR *opendir(const char *name) } else /* rollback */ { + free(dir->name); free(dir); dir = 0; errno = ENOMEM; @@ -72,12 +87,9 @@ int closedir(DIR *dir) { int result = -1; - if(dir) + if(dir && dir->handle != INVALID_HANDLE_VALUE) { - if(dir->handle != -1) - { - result = _findclose(dir->handle); - } + FindClose(dir->handle); free(dir->name); free(dir); @@ -95,12 +107,12 @@ struct dirent *readdir(DIR *dir) { struct dirent *result = 0; - if(dir && dir->handle != -1) + if(dir && dir->handle != INVALID_HANDLE_VALUE) { - if(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1) + if (FindNextFileW(dir->handle, &dir->info)) { result = &dir->result; - result->d_name = dir->info.name; + result->d_name = to_utf8(dir->info.cFileName, 0); } } else @@ -111,20 +123,6 @@ struct dirent *readdir(DIR *dir) return result; } -void rewinddir(DIR *dir) -{ - if(dir && dir->handle != -1) - { - _findclose(dir->handle); - dir->handle = (long) _findfirst(dir->name, &dir->info); - dir->result.d_name = 0; - } - else - { - errno = EBADF; - } -} - // helper for scandir below static void scandir_free_dir_entries(struct dirent*** namelist, int entries) { int i; diff --git a/lib/cpluff/libcpluff/win32/dirent.h b/lib/cpluff/libcpluff/win32/dirent.h index b41998c6eb..c250bfdb28 100644 --- a/lib/cpluff/libcpluff/win32/dirent.h +++ b/lib/cpluff/libcpluff/win32/dirent.h @@ -19,10 +19,6 @@ extern "C" typedef struct DIR DIR; -#ifndef WIN32 -static int errno; -#endif - struct dirent { char *d_name; diff --git a/lib/cpluff/libcpluff/win32/win32_utils.c b/lib/cpluff/libcpluff/win32/win32_utils.c new file mode 100644 index 0000000000..52cf353c13 --- /dev/null +++ b/lib/cpluff/libcpluff/win32/win32_utils.c @@ -0,0 +1,73 @@ +/*------------------------------------------------------------------------- + * C-Pluff, a plug-in framework for C + * Copyright 2016 Team Kodi + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *-----------------------------------------------------------------------*/ + +#include <stdlib.h> + +#include "win32_utils.h" + +wchar_t* to_utf16(const char* str, size_t length) +{ + if (length == 0) + length = strlen(str); + int result = MultiByteToWideChar(CP_UTF8, 0, str, length, NULL, 0); + if (result == 0) + { + return NULL; + } + + int newLen = result + 1; + wchar_t* dirPath = malloc(newLen * 2); + result = MultiByteToWideChar(CP_UTF8, 0, str, length, dirPath, newLen); + + if (result == 0) + { + free(dirPath); + return NULL; + } + + dirPath[newLen - 1] = L'\0'; + return dirPath; +} + +char* to_utf8(const wchar_t* str, size_t length) +{ + if (length == 0) + length = wcslen(str); + + int result = WideCharToMultiByte(CP_UTF8, 0, str, length, NULL, 0, NULL, NULL); + if (result == 0) + return NULL; + + int newLen = result + 1; + char *newStr = malloc(newLen); + result = WideCharToMultiByte(CP_UTF8, 0, str, length, newStr, result, NULL, NULL); + if (result == 0) + { + free(newStr); + return NULL; + } + + newStr[newLen - 1] = '\0'; + + return newStr; +} diff --git a/lib/cpluff/libcpluff/win32/win32_utils.h b/lib/cpluff/libcpluff/win32/win32_utils.h new file mode 100644 index 0000000000..f4074f3745 --- /dev/null +++ b/lib/cpluff/libcpluff/win32/win32_utils.h @@ -0,0 +1,30 @@ +/*------------------------------------------------------------------------- + * C-Pluff, a plug-in framework for C + * Copyright 2016 Team Kodi + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *-----------------------------------------------------------------------*/ + +#if !defined(WIN32_LEAN_AND_MEAN) + #define WIN32_LEAN_AND_MEAN +#endif +#include <windows.h> + +wchar_t* to_utf16(const char* str, size_t length); +char* to_utf8(const wchar_t* str, size_t length); |