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
|
/*
* python-gammu - Phone communication libary
* Copyright © 2003 - 2008 Michal Čihař
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License.
*
* 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 General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* vim: expandtab sw=4 ts=4 sts=4:
*/
/* Unicode strings conversion between Gammu and Python */
#include "pyg-config.h"
#include "convertors.h"
unsigned char *StringPythonToGammu(PyObject *o) {
PyObject *u;
Py_UNICODE *ps;
unsigned char *gs;
u = PyObject_Unicode(o);
if (u == NULL) {
PyErr_Format(PyExc_ValueError, "Value can not be converted to unicode object");
return NULL;
}
ps = PyUnicode_AsUnicode(u);
if (ps == NULL) {
PyErr_Format(PyExc_ValueError, "Can not get unicode value");
return NULL;
}
gs = strPythonToGammu(ps, PyUnicode_GetSize(u));
Py_DECREF(u);
return gs;
}
unsigned char *strPythonToGammu(const Py_UNICODE *src, const size_t len) {
unsigned char *dest;
size_t i;
/* Allocate memory */
dest = malloc((len + 1) * 2 * sizeof(char));
if (dest == NULL) {
PyErr_SetString(PyExc_MemoryError, "Not enough memory to allocate string");
return NULL;
}
/* Convert and copy string. */
for (i = 0; i < len; i++) {
dest[(i * 2)] = (src[i] >> 8) & 0xff;
dest[(i * 2) + 1] = src[i] & 0xff;
}
/* Zero terminate string. */
dest[(len * 2)] = 0;
dest[(len * 2) + 1] = 0;
return dest;
}
Py_UNICODE *strGammuToPython(const unsigned char *src) {
int len = 0;
/* Get string length */
while (src[len*2] != 0 || src[(len*2)+1] != 0 ) len++;
return strGammuToPythonL(src, len);
}
Py_UNICODE *strGammuToPythonL(const unsigned char *src, const int len) {
Py_UNICODE *dest;
int i;
/* Allocate memory */
dest = malloc((len + 1) * sizeof(Py_UNICODE));
if (dest == NULL) {
PyErr_SetString(PyExc_MemoryError, "Not enough memory to allocate string");
return NULL;
}
/* Convert string including zero at the end. */
for (i = 0; i <= len; i++) {
dest[i] = (src[2*i] << 8) + src[(2*i) + 1];
}
return dest;
}
PyObject *UnicodeStringToPython(const unsigned char *src) {
int len;
len = UnicodeLength(src);
return UnicodeStringToPythonL(src, len);
}
PyObject *UnicodeStringToPythonL(const unsigned char *src, const int len) {
Py_UNICODE *val;
PyObject *res;
val = strGammuToPythonL(src, len);
if (val == NULL) return NULL;
res = PyUnicode_FromUnicode(val, len);
free(val);
return res;
}
PyObject *LocaleStringToPython(const char *src) {
unsigned char *w;
size_t len;
PyObject *ret;
/* Length of input */
len = strlen(src);
/* Allocate it */
w = malloc(2 * (len + 5));
if (w == NULL) {
PyErr_SetString(PyExc_MemoryError, "Not enough memory to allocate string");
return NULL;
}
EncodeUnicode(w, src, len);
ret = UnicodeStringToPython(w);
free(w);
return ret;
}
|