summaryrefslogtreecommitdiff
path: root/gi/pygi-util.c
blob: 3a297f40ea5704e92053c5c9b214607b71c62743 (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
/* -*- Mode: C; c-basic-offset: 4 -*-
 * pygtk- Python bindings for the GTK toolkit.
 * Copyright (C) 1998-2003  James Henstridge
 *
 * This library 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 library 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 library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "pygi-util.h"

gboolean
pygi_guint_from_pyssize (Py_ssize_t pyval, guint *result)
{
    if (pyval < 0) {
        PyErr_SetString (PyExc_ValueError, "< 0");
        return FALSE;
    } else if (G_MAXUINT < PY_SSIZE_T_MAX && pyval > (Py_ssize_t)G_MAXUINT) {
        PyErr_SetString (PyExc_ValueError, "too large");
        return FALSE;
    }
    *result = (guint)pyval;
    return TRUE;
}

PyObject *
pyg_integer_richcompare(PyObject *v, PyObject *w, int op)
{
    PyObject *result;
    gboolean t;

    switch (op) {
    case Py_EQ: t = PyLong_AS_LONG (v) == PyLong_AS_LONG (w); break;
    case Py_NE: t = PyLong_AS_LONG (v) != PyLong_AS_LONG (w); break;
    case Py_LE: t = PyLong_AS_LONG (v) <= PyLong_AS_LONG (w); break;
    case Py_GE: t = PyLong_AS_LONG (v) >= PyLong_AS_LONG (w); break;
    case Py_LT: t = PyLong_AS_LONG (v) <  PyLong_AS_LONG (w); break;
    case Py_GT: t = PyLong_AS_LONG (v) >  PyLong_AS_LONG (w); break;
    default: g_assert_not_reached();
    }

    result = t ? Py_True : Py_False;
    Py_INCREF(result);
    return result;
}

PyObject*
pyg_ptr_richcompare(void* a, void *b, int op)
{
    PyObject *res;

    switch (op) {
      case Py_EQ:
        res = (a == b) ? Py_True : Py_False;
        break;
      case Py_NE:
        res = (a != b) ? Py_True : Py_False;
        break;
      case Py_LT:
        res = (a < b) ? Py_True : Py_False;
        break;
      case Py_LE:
        res = (a <= b) ? Py_True : Py_False;
        break;
      case Py_GT:
        res = (a > b) ? Py_True : Py_False;
        break;
      case Py_GE:
        res = (a >= b) ? Py_True : Py_False;
        break;
      default:
        res = Py_NotImplemented;
        break;
    }

    Py_INCREF(res);
    return res;
}

/**
 * pyg_constant_strip_prefix:
 * @name: the constant name.
 * @strip_prefix: the prefix to strip.
 *
 * Advances the pointer @name by strlen(@strip_prefix) characters.  If
 * the resulting name does not start with a letter or underscore, the
 * @name pointer will be rewound.  This is to ensure that the
 * resulting name is a valid identifier.  Hence the returned string is
 * a pointer into the string @name.
 *
 * Returns: the stripped constant name.
 */
const gchar *
pyg_constant_strip_prefix(const gchar *name, const gchar *strip_prefix)
{
    size_t prefix_len, i;

    prefix_len = strlen(strip_prefix);

    /* Check so name starts with strip_prefix, if it doesn't:
     * return the rest of the part which doesn't match
     */
    for (i = 0; i < prefix_len; i++) {
	if (name[i] != strip_prefix[i] && name[i] != '_') {
	    return &name[i];
	}
    }

    /* strip off prefix from value name, while keeping it a valid
     * identifier */
    for (i = prefix_len + 1; i > 0; i--) {
	if (g_ascii_isalpha(name[i - 1]) || name[i - 1] == '_') {
	    return &name[i - 1];
	}
    }
    return name;
}