Repository navigation
Expand file tree
/
Copy path_py_unicode.cpp
More file actions
202 lines (190 loc) · 5.85 KB
/
Copy path_py_unicode.cpp
File metadata and controls
202 lines (190 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* libTML: A BEEP based Messaging Suite
* Copyright (C) 2015 wobe-systems GmbH
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
* You may find a copy of the license under this software is released
* at COPYING file. This is LGPL software: you are welcome to develop
* proprietary applications using this library without any royalty or
* fee but returning back any change, improvement or addition in the
* form of source code, project image, documentation patches, etc.
*
* Homepage:
* http://www.libtml.org
*
* For professional support contact us:
*
* wobe-systems GmbH
*/
#ifndef LINUX
#endif
#include <wchar.h>
#include <sidex.h>
#include <Python.h>
/*
*------------------------------------------------------
* In case of Python 2.7 there is a special behaviour for
* strings on LINUX & OSX platforms
*------------------------------------------------------
*/
#ifdef LINUX
#if PY_MAJOR_VERSION < 3
#if PY_MINOR_VERSION == 7
#define LINUX_X64
#endif // PY_MINOR_VERSION
#endif // PY_MAJOR_VERSION
#endif //LINUX
/*
*------------------------------------------------------
* Helper method
*------------------------------------------------------
*/
PyObject* utf32_utf16_decode(SIDEX_TCHAR* seq, int sLength) {
PyObject* result;
#ifdef LINUX_X64
if (0 > sLength){
sLength = (SIDEX_INT32)wcslen(seq);
}
result = PyUnicode_FromWideChar(seq, sLength);
#else //LINUX_X64
result = Py_BuildValue("u", seq);
#endif //LINUX_X64
return result;
}
/*
*------------------------------------------------------
* Helper method
*------------------------------------------------------
*/
void unicodeFree(PyObject* unicodeObj, SIDEX_TCHAR* sUnicode){
if (NULL!= unicodeObj){
//printf ("unicodeFree / NEED PY_DECREF \n");
Py_XDECREF(unicodeObj);
}
#ifdef LINUX_X64
if (NULL != sUnicode)
//printf ("unicodeFree / Delete UTF32\n");
delete (sUnicode);
#endif //LINUX_X64
}
/*
*------------------------------------------------------
* Helper method
*------------------------------------------------------
*/
bool utf16_utf32_decode(PyUnicodeObject* py_UObj, Py_ssize_t sLength, wchar_t** sRetVal) {
bool bFail = false;
wchar_t* sRet = NULL;
#ifdef LINUX_X64
sRet = new wchar_t[sLength+1];
#if PY_MAJOR_VERSION < 3
Py_ssize_t lRet = PyUnicode_AsWideChar(py_UObj, sRet, sLength+1);
#else // PY_MAJOR_VERSION
#if PY_MINOR_VERSION < 2
Py_ssize_t lRet = PyUnicode_AsWideChar(py_UObj, sRet, sLength+1);
#else // PY_MINOR_VERSION
Py_ssize_t lRet = PyUnicode_AsWideChar((PyObject*)py_UObj, sRet, sLength+1);
#endif // PY_MINOR_VERSION
#endif // PY_MAJOR_VERSION
if (lRet != sLength){
bFail = true;
delete sRet;
sRet = NULL;
}
#else //LINUX_X64
int iLength;
PyObject* tuple = Py_BuildValue("(O)", py_UObj);
if (!PyArg_ParseTuple(tuple, "u#", &sRet, &iLength)){
bFail = true;
sRet = NULL;
}
Py_XDECREF(tuple);
#endif //LINUX_X64
if (!bFail)
*sRetVal = sRet;
return bFail;
}
/*
*------------------------------------------------------
* Helper method
*------------------------------------------------------
*/
bool _unicodeConversion(PyObject* py_Obj, SIDEX_TCHAR** u_ret, PyObject** py_ObjRes, PyObject* errOutputObj = NULL)
{
bool bFail = false;
PyObject* py_Obj16 = NULL;
int length;
*py_ObjRes = NULL;
bool bUnicode = true;
bUnicode = (true == PyUnicode_Check(py_Obj));
#if PY_MAJOR_VERSION < 3 // On Python 3 we only have Unicode:
if (!bUnicode){
if (false == PyString_Check(py_Obj)){
// Not unicode and not a string:
if (NULL != errOutputObj){
PyErr_Format(errOutputObj, "Unicode / String parameter expected");
}
return true;
}
}
#else
if (!bUnicode){
// Not unicode:
if (NULL != errOutputObj){
PyErr_Format(errOutputObj, "Unicode parameter expected");
}
return true;
}
#endif
if (!bUnicode){
///////////////////////////////////////
// The char sting case:
char* s_ret;
PyObject* tuple = Py_BuildValue("(O)", py_Obj);
if (!PyArg_ParseTuple(tuple, "s#", &s_ret, &length))
bFail = true;
Py_XDECREF(tuple);
////////////////////////////////////////////
// First generate 16- Bit Unicode:
if (!bFail){
#if PY_MAJOR_VERSION < 3 // On Python 3 we only have Unicode:
#if PY_MINOR_VERSION < 6
PyObject* py_strObj = PyString_FromStringAndSize((const char*) s_ret, length);
py_Obj16 = PyUnicode_FromObject(py_strObj);
Py_XDECREF(py_strObj);
#else
py_Obj16 = PyUnicode_FromStringAndSize((const char*) s_ret, length);
#endif
#else
py_Obj16 = PyUnicode_FromStringAndSize((const char*) s_ret, length);
#endif
*py_ObjRes = py_Obj16;
}
}
else{
py_Obj16 = py_Obj;
length = (int)PyUnicode_GET_SIZE(py_Obj16);
}
////////////////////////////////////////////
// Maybe we run on a LINUX- x64 /generate 16- Bit Unicode:
if (!bFail){
bFail = utf16_utf32_decode((PyUnicodeObject*)py_Obj16, (Py_ssize_t)length, u_ret);
//fwprintf(stderr, L"--- %ls ---\n", *u_ret);
}
return bFail;
}