Skip to content

Commit

Permalink
[cpluff] add windows changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rechi committed Mar 23, 2018
1 parent cf7dd38 commit 14ffbe3
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 38 deletions.
55 changes: 55 additions & 0 deletions lib/cpluff/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
4 changes: 4 additions & 0 deletions lib/cpluff/libcpluff/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions lib/cpluff/libcpluff/ploader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions lib/cpluff/libcpluff/win32/cpluffdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 32 additions & 34 deletions lib/cpluff/libcpluff/win32/dirent.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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)
Expand All @@ -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;
}
Expand All @@ -55,6 +69,7 @@ DIR *opendir(const char *name)
}
else /* rollback */
{
free(dir->name);
free(dir);
dir = 0;
errno = ENOMEM;
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions lib/cpluff/libcpluff/win32/dirent.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ extern "C"

typedef struct DIR DIR;

#ifndef WIN32
static int errno;
#endif

struct dirent
{
char *d_name;
Expand Down
73 changes: 73 additions & 0 deletions lib/cpluff/libcpluff/win32/win32_utils.c
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 30 additions & 0 deletions lib/cpluff/libcpluff/win32/win32_utils.h
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 14ffbe3

Please sign in to comment.