diff --git a/setup/extensions.json b/setup/extensions.json index f5e9067989..8140463c55 100644 --- a/setup/extensions.json +++ b/setup/extensions.json @@ -139,6 +139,7 @@ { "name": "winutil", "only": "windows", + "headers": "calibre/utils/windows/common.h", "sources": "calibre/utils/windows/winutil.cpp", "libraries": "shell32 wininet advapi32", "cflags": "/X" diff --git a/src/calibre/utils/windows/common.h b/src/calibre/utils/windows/common.h new file mode 100644 index 0000000000..6cb498b9f1 --- /dev/null +++ b/src/calibre/utils/windows/common.h @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2020 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +#pragma once +#define PY_SSIZE_T_CLEAN +#define UNICODE +#include +#include + +class wchar_raii { + private: + wchar_t *handle; + wchar_raii( const wchar_raii & ) ; + wchar_raii & operator=( const wchar_raii & ) ; + + public: + wchar_raii() : handle(NULL) {} + + ~wchar_raii() { + if (handle) { + PyMem_Free(handle); + handle = NULL; + } + } + + wchar_t *ptr() { return handle; } + void set_ptr(wchar_t *val) { handle = val; } +}; + +class handle_raii { + private: + HANDLE handle; + handle_raii( const handle_raii & ) ; + handle_raii & operator=( const handle_raii & ) ; + + public: + handle_raii() : handle(INVALID_HANDLE_VALUE) {} + handle_raii(HANDLE h) : handle(h) {} + + ~handle_raii() { + if (handle != INVALID_HANDLE_VALUE) { + CloseHandle(handle); + handle = INVALID_HANDLE_VALUE; + } + } + + HANDLE ptr() const { return handle; } + void set_ptr(HANDLE val) { handle = val; } + explicit operator bool() const { return handle != INVALID_HANDLE_VALUE; } + +}; + + +static int +py_to_wchar(PyObject *obj, wchar_raii *output) { + if (!PyUnicode_Check(obj)) { + if (obj == Py_None) { return 1; } + PyErr_SetString(PyExc_TypeError, "unicode object expected"); + return 0; + } + wchar_t *buf = PyUnicode_AsWideCharString(obj, NULL); + if (!buf) { PyErr_NoMemory(); return 0; } + output->set_ptr(buf); + return 1; +} + +static int +py_to_wchar_no_none(PyObject *obj, wchar_raii *output) { + if (!PyUnicode_Check(obj)) { + PyErr_SetString(PyExc_TypeError, "unicode object expected"); + return 0; + } + wchar_t *buf = PyUnicode_AsWideCharString(obj, NULL); + if (!buf) { PyErr_NoMemory(); return 0; } + output->set_ptr(buf); + return 1; +} diff --git a/src/calibre/utils/windows/winutil.cpp b/src/calibre/utils/windows/winutil.cpp index 5472bee295..125cdd5e85 100644 --- a/src/calibre/utils/windows/winutil.cpp +++ b/src/calibre/utils/windows/winutil.cpp @@ -5,9 +5,7 @@ * Distributed under terms of the GPL3 license. */ -#define PY_SSIZE_T_CLEAN -#define UNICODE -#include +#include "common.h" #include #include #include @@ -21,7 +19,6 @@ #include #include #include // for CComPtr -#include #include // Handle {{{ @@ -218,49 +215,6 @@ class DeleteFileProgressSink : public IFileOperationProgressSink { // {{{ ULONG m_cRef; }; // }}} -class wchar_raii { // {{{ - private: - wchar_t *handle; - wchar_raii( const wchar_raii & ) ; - wchar_raii & operator=( const wchar_raii & ) ; - - public: - wchar_raii() : handle(NULL) {} - - ~wchar_raii() { - if (handle) { - PyMem_Free(handle); - handle = NULL; - } - } - - wchar_t *ptr() { return handle; } - void set_ptr(wchar_t *val) { handle = val; } -}; // }}} - -class handle_raii { // {{{ - private: - HANDLE handle; - handle_raii( const handle_raii & ) ; - handle_raii & operator=( const handle_raii & ) ; - - public: - handle_raii() : handle(INVALID_HANDLE_VALUE) {} - handle_raii(HANDLE h) : handle(h) {} - - ~handle_raii() { - if (handle != INVALID_HANDLE_VALUE) { - CloseHandle(handle); - handle = INVALID_HANDLE_VALUE; - } - } - - HANDLE ptr() const { return handle; } - void set_ptr(HANDLE val) { handle = val; } - explicit operator bool() const { return handle != INVALID_HANDLE_VALUE; } - -}; // }}} - class scoped_com_initializer { // {{{ public: scoped_com_initializer() : m_succeded(false) { if (SUCCEEDED(CoInitialize(NULL))) m_succeded = true; } @@ -272,31 +226,6 @@ class scoped_com_initializer { // {{{ scoped_com_initializer & operator=( const scoped_com_initializer & ) ; }; // }}} -// py_to_wchar {{{ -static int -py_to_wchar(PyObject *obj, wchar_raii *output) { - if (!PyUnicode_Check(obj)) { - if (obj == Py_None) { return 1; } - PyErr_SetString(PyExc_TypeError, "unicode object expected"); - return 0; - } - wchar_t *buf = PyUnicode_AsWideCharString(obj, NULL); - if (!buf) { PyErr_NoMemory(); return 0; } - output->set_ptr(buf); - return 1; -} - -static int -py_to_wchar_no_none(PyObject *obj, wchar_raii *output) { - if (!PyUnicode_Check(obj)) { - PyErr_SetString(PyExc_TypeError, "unicode object expected"); - return 0; - } - wchar_t *buf = PyUnicode_AsWideCharString(obj, NULL); - if (!buf) { PyErr_NoMemory(); return 0; } - output->set_ptr(buf); - return 1; -} // }}} static PyObject * winutil_folder_path(PyObject *self, PyObject *args) {