summaryrefslogtreecommitdiff
path: root/efl/utils/conversions.pyx
blob: 400801dcb205267cb8ce043beb6e5f2aca9ebe7a (plain)
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
# Copyright (C) 2007-2014 various contributors (see AUTHORS)
#
# 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
# version 3 of the License, or (at your option) any later version.
#
# 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/>.

from libc.stdlib cimport malloc
from libc.string cimport strdup
from cpython cimport PyUnicode_AsUTF8String

from efl.c_eo cimport Eo as cEo
from efl.eo cimport Eo, object_from_instance
from efl.eina cimport eina_list_append, eina_stringshare_add

cdef unicode _touni(char* s):
    """

    Converts a char * to a python string object

    """
    return s.decode('UTF-8', 'strict') if s else None


cdef unicode _ctouni(const char *s):
    """

    Converts a const char * to a python string object

    """
    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


cdef const char ** python_list_strings_to_array_of_strings(list strings) except NULL:
    """

    Converts a python list to an array of strings.

    Note: Remember to free the array when it's no longer needed.

    """
    cdef:
        const char **array = NULL
        const char *string
        unsigned int str_len, i
        unsigned int arr_len = len(strings)

    # TODO: Should we just return NULL in this case?
    if arr_len == 0:
        array = <const char **>malloc(sizeof(const char*))
        if not array:
            raise MemoryError()
        array[0] = NULL
        return array

    array = <const char **>malloc(arr_len * sizeof(const char*))
    if not array:
        raise MemoryError()

    for i in range(arr_len):
        s = strings[i]
        if isinstance(s, unicode): s = PyUnicode_AsUTF8String(s)
        array[i] = <const char *>strdup(s)

    return array

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

cdef list eina_list_strings_to_python_list(const Eina_List *lst):
    cdef:
        const char *s
        list ret = []
        Eina_List *itr = <Eina_List *>lst
    while itr:
        s = <const char *>itr.data
        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:
        if isinstance(s, unicode): s = PyUnicode_AsUTF8String(s)
        lst = eina_list_append(lst, eina_stringshare_add(s))
    return lst


cdef list eina_list_objects_to_python_list(const Eina_List *lst):
    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:
        Eina_List *lst = NULL
        Eo o

    if objects is None:
        return NULL

    for o in objects:
        lst = eina_list_append(lst, o.obj)

    return lst