2014-04-14 13:21:03 -07:00
|
|
|
# Copyright (C) 2007-2014 various contributors (see AUTHORS)
|
2013-09-30 18:56:28 -07:00
|
|
|
#
|
|
|
|
# This file is part of Python-EFL.
|
|
|
|
#
|
|
|
|
# Python-EFL 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
|
2013-12-07 08:54:58 -08:00
|
|
|
# version 3 of the License, or (at your option) any later version.
|
2013-09-30 18:56:28 -07:00
|
|
|
#
|
|
|
|
# Python-EFL 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 Python-EFL. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-04-13 14:50:16 -07:00
|
|
|
from libc.stdlib cimport malloc
|
2013-09-30 18:56:28 -07:00
|
|
|
from libc.string cimport strdup
|
2014-04-06 13:48:16 -07:00
|
|
|
from cpython cimport PyUnicode_AsUTF8String
|
2013-09-30 18:56:28 -07:00
|
|
|
|
|
|
|
from efl.c_eo cimport Eo as cEo
|
|
|
|
from efl.eo cimport Eo, object_from_instance
|
2013-10-01 19:59:21 -07:00
|
|
|
from efl.eina cimport eina_list_append, eina_stringshare_add
|
2013-09-30 18:56:28 -07:00
|
|
|
|
|
|
|
cdef unicode _touni(char* s):
|
|
|
|
"""
|
|
|
|
|
|
|
|
Converts a char * to a python string object
|
|
|
|
|
|
|
|
"""
|
|
|
|
return s.decode('UTF-8', 'strict') if s else None
|
|
|
|
|
|
|
|
|
2014-04-04 17:13:15 -07:00
|
|
|
cdef unicode _ctouni(const char *s):
|
2013-09-30 18:56:28 -07:00
|
|
|
"""
|
|
|
|
|
2014-04-04 17:13:15 -07:00
|
|
|
Converts a const char * to a python string object
|
2013-09-30 18:56:28 -07:00
|
|
|
|
|
|
|
"""
|
|
|
|
return s.decode('UTF-8', 'strict') if s else None
|
|
|
|
|
|
|
|
|
|
|
|
cdef list array_of_strings_to_python_list(char **array, int array_length):
|
|
|
|
"""
|
|
|
|
|
|
|
|
Converts an array of strings to a python list.
|
|
|
|
|
|
|
|
"""
|
|
|
|
cdef:
|
|
|
|
char *string
|
|
|
|
list ret = list()
|
|
|
|
int i
|
|
|
|
|
|
|
|
for i in range(array_length):
|
|
|
|
string = array[i]
|
|
|
|
ret.append(_touni(string))
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2014-04-04 17:13:15 -07:00
|
|
|
cdef const char ** python_list_strings_to_array_of_strings(list strings) except NULL:
|
2013-09-30 18:56:28 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
Converts a python list to an array of strings.
|
|
|
|
|
|
|
|
Note: Remember to free the array when it's no longer needed.
|
|
|
|
|
|
|
|
"""
|
|
|
|
cdef:
|
2014-04-04 17:13:15 -07:00
|
|
|
const char **array = NULL
|
|
|
|
const char *string
|
2013-09-30 18:56:28 -07:00
|
|
|
unsigned int str_len, i
|
|
|
|
unsigned int arr_len = len(strings)
|
|
|
|
|
|
|
|
# TODO: Should we just return NULL in this case?
|
|
|
|
if arr_len == 0:
|
2014-04-04 17:13:15 -07:00
|
|
|
array = <const char **>malloc(sizeof(const char*))
|
2013-09-30 18:56:28 -07:00
|
|
|
if not array:
|
|
|
|
raise MemoryError()
|
|
|
|
array[0] = NULL
|
|
|
|
return array
|
|
|
|
|
2014-04-04 17:13:15 -07:00
|
|
|
array = <const char **>malloc(arr_len * sizeof(const char*))
|
2013-09-30 18:56:28 -07:00
|
|
|
if not array:
|
|
|
|
raise MemoryError()
|
|
|
|
|
|
|
|
for i in range(arr_len):
|
|
|
|
s = strings[i]
|
2014-04-06 13:48:16 -07:00
|
|
|
if isinstance(s, unicode): s = PyUnicode_AsUTF8String(s)
|
2014-04-04 17:13:15 -07:00
|
|
|
array[i] = <const char *>strdup(s)
|
2013-09-30 18:56:28 -07:00
|
|
|
|
|
|
|
return array
|
|
|
|
|
2014-03-01 10:35:40 -08:00
|
|
|
cdef list array_of_ints_to_python_list(int *array, int array_length):
|
|
|
|
"""
|
|
|
|
|
|
|
|
Converts an array of ints to a python list.
|
|
|
|
|
|
|
|
UNTESTED (used in Win.wm_rotation_available_rotations)
|
|
|
|
|
|
|
|
"""
|
|
|
|
cdef:
|
|
|
|
list ret = list()
|
|
|
|
int i
|
|
|
|
|
|
|
|
for i in range(array_length):
|
|
|
|
ret.append(array[i])
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
cdef int * python_list_ints_to_array_of_ints(list ints) except NULL:
|
|
|
|
"""
|
|
|
|
|
|
|
|
Converts a python list to an array of ints.
|
|
|
|
|
|
|
|
UNTESTED (used in Win.wm_rotation_available_rotations)
|
|
|
|
|
|
|
|
Note: Remember to free the array when it's no longer needed.
|
|
|
|
|
|
|
|
"""
|
|
|
|
cdef:
|
|
|
|
int *array = NULL
|
|
|
|
unsigned int i
|
|
|
|
unsigned int arr_len = len(ints)
|
|
|
|
|
|
|
|
if arr_len == 0:
|
|
|
|
return NULL
|
|
|
|
|
|
|
|
array = <int *>malloc(arr_len * sizeof(int))
|
|
|
|
if not array:
|
|
|
|
raise MemoryError()
|
|
|
|
|
|
|
|
for i in range(arr_len):
|
|
|
|
array[i] = ints[i]
|
|
|
|
|
|
|
|
return array
|
2013-09-30 18:56:28 -07:00
|
|
|
|
2014-04-04 17:13:15 -07:00
|
|
|
cdef list eina_list_strings_to_python_list(const Eina_List *lst):
|
2013-09-30 18:56:28 -07:00
|
|
|
cdef:
|
2014-04-04 17:13:15 -07:00
|
|
|
const char *s
|
2013-09-30 18:56:28 -07:00
|
|
|
list ret = []
|
|
|
|
Eina_List *itr = <Eina_List *>lst
|
|
|
|
while itr:
|
2014-04-04 17:13:15 -07:00
|
|
|
s = <const char *>itr.data
|
2013-09-30 18:56:28 -07:00
|
|
|
ret.append(_ctouni(s))
|
|
|
|
itr = itr.next
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
cdef Eina_List *python_list_strings_to_eina_list(list strings):
|
|
|
|
cdef Eina_List *lst = NULL
|
|
|
|
for s in strings:
|
2014-04-06 13:48:16 -07:00
|
|
|
if isinstance(s, unicode): s = PyUnicode_AsUTF8String(s)
|
2013-10-01 19:59:21 -07:00
|
|
|
lst = eina_list_append(lst, eina_stringshare_add(s))
|
2013-09-30 18:56:28 -07:00
|
|
|
return lst
|
|
|
|
|
|
|
|
|
2014-04-04 17:13:15 -07:00
|
|
|
cdef list eina_list_objects_to_python_list(const Eina_List *lst):
|
2013-09-30 18:56:28 -07:00
|
|
|
cdef list ret = list()
|
|
|
|
while lst:
|
|
|
|
ret.append(object_from_instance(<cEo *>lst.data))
|
|
|
|
lst = lst.next
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
cdef Eina_List *python_list_objects_to_eina_list(list objects):
|
|
|
|
cdef:
|
2013-10-09 07:04:16 -07:00
|
|
|
Eina_List *lst = NULL
|
2013-09-30 18:56:28 -07:00
|
|
|
Eo o
|
2013-11-15 23:01:31 -08:00
|
|
|
|
|
|
|
if objects is None:
|
|
|
|
return NULL
|
|
|
|
|
2013-09-30 18:56:28 -07:00
|
|
|
for o in objects:
|
|
|
|
lst = eina_list_append(lst, o.obj)
|
2013-11-15 23:01:31 -08:00
|
|
|
|
2013-09-30 18:56:28 -07:00
|
|
|
return lst
|