From 18d3744adb4f698bfcb2805ef87ba431d9e5e7e5 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 30 Nov 2008 00:17:21 +0000 Subject: [PATCH] added python converter from unicode to path --- bindings/python/src/filesystem.cpp | 25 +++++++++++++++++++++++-- include/libtorrent/utf8.hpp | 4 ++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/bindings/python/src/filesystem.cpp b/bindings/python/src/filesystem.cpp index 3c5badb8e..dc7f01a6b 100644 --- a/bindings/python/src/filesystem.cpp +++ b/bindings/python/src/filesystem.cpp @@ -4,6 +4,7 @@ #include #include +#include "libtorrent/utf8.hpp" using namespace boost::python; @@ -26,15 +27,35 @@ struct path_from_python static void* convertible(PyObject* x) { - return PyString_Check(x) ? x : 0; + return PyString_Check(x) ? x : PyUnicode_Check(x) ? x : 0; } static void construct(PyObject* x, converter::rvalue_from_python_stage1_data* data) { + using libtorrent::wchar_utf8; void* storage = ((converter::rvalue_from_python_storage< boost::filesystem::path >*)data)->storage.bytes; - new (storage) boost::filesystem::path(PyString_AsString(x)); + if (PyUnicode_Check(x)) + { + std::wstring str; + str.resize(PyUnicode_GetSize(x) + 1, 0); + int len = PyUnicode_AsWideChar((PyUnicodeObject*)x, &str[0], str.size()); + if (len > -1) + { + assert(len < str.size()); + str[len] = 0; + } + else str[str.size()-1] = 0; + + std::string utf8; + int ret = wchar_utf8(str, utf8); + new (storage) boost::filesystem::path(utf8); + } + else + { + new (storage) boost::filesystem::path(PyString_AsString(x)); + } data->convertible = storage; } }; diff --git a/include/libtorrent/utf8.hpp b/include/libtorrent/utf8.hpp index 6c63c2417..43805c40b 100644 --- a/include/libtorrent/utf8.hpp +++ b/include/libtorrent/utf8.hpp @@ -68,7 +68,7 @@ namespace libtorrent return sourceIllegal; } } -/* + inline int wchar_utf8(const std::wstring &wide, std::string &utf8) { // allocate space for worst-case @@ -97,7 +97,7 @@ namespace libtorrent return sourceIllegal; } } -*/ + } #endif