mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Make winutil classes re-useable
This commit is contained in:
parent
5f4c2ce051
commit
ebb6f89b5b
@ -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"
|
||||
|
80
src/calibre/utils/windows/common.h
Normal file
80
src/calibre/utils/windows/common.h
Normal file
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Kovid Goyal <kovid at kovidgoyal.net>
|
||||
*
|
||||
* Distributed under terms of the GPL3 license.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
#define UNICODE
|
||||
#include <Windows.h>
|
||||
#include <Python.h>
|
||||
|
||||
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;
|
||||
}
|
@ -5,9 +5,7 @@
|
||||
* Distributed under terms of the GPL3 license.
|
||||
*/
|
||||
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
#define UNICODE
|
||||
#include <Windows.h>
|
||||
#include "common.h"
|
||||
#include <processthreadsapi.h>
|
||||
#include <wininet.h>
|
||||
#include <Lmcons.h>
|
||||
@ -21,7 +19,6 @@
|
||||
#include <comip.h>
|
||||
#include <comdef.h>
|
||||
#include <atlbase.h> // for CComPtr
|
||||
#include <Python.h>
|
||||
#include <versionhelpers.h>
|
||||
|
||||
// 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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user