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 202 203 204 205 206
|
/*
* 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:
*/
/* Basic getters from Python objects */
#include "pyg-config.h"
#include "convertors.h"
#include "misc.h"
bool GetBoolFromDict(PyObject *dict, char *key) {
PyObject *o;
char *s;
int i;
o = PyDict_GetItemString(dict, key);
if (o == NULL) {
PyErr_Format(PyExc_ValueError, "Missing key in dictionary: %s", key);
return BOOL_INVALID;
}
if (!PyBool_Check(o)) {
if (PyInt_Check(o)) {
i = PyInt_AsLong(o);
if (i == 0) return false;
else return true;
}
if (PyString_Check(o)) {
s = PyString_AsString(o);
if (isdigit(s[0])) {
i = atoi(s);
if (i == 0) return false;
else return true;
} else {
PyErr_Format(PyExc_ValueError, "Value of '%s' doesn't seem to be bool", key);
return BOOL_INVALID;
}
}
PyErr_Format(PyExc_ValueError, "Value of '%s' doesn't seem to be bool", key);
return BOOL_INVALID;
}
if (Py_False == o) return false;
else if (Py_True == o) return true;
PyErr_Format(PyExc_ValueError, "Value of '%s' doesn't seem to be bool", key);
return BOOL_INVALID;
}
int GetIntFromDict(PyObject *dict, char *key) {
PyObject *o;
char *s;
int i;
o = PyDict_GetItemString(dict, key);
if (o == NULL) {
PyErr_Format(PyExc_ValueError, "Missing key in dictionary: %s", key);
return INT_INVALID;
}
if (PyLong_Check(o)) {
/* Well we loose here something, but it is intentional :-) */
return PyLong_AsLongLong(o);
}
if (PyInt_Check(o)) {
return PyInt_AsLong(o);
}
if (PyString_Check(o)) {
s = PyString_AsString(o);
if (isdigit(s[0])) {
i = atoi(s);
return i;
} else {
PyErr_Format(PyExc_ValueError, "Value of '%s' doesn't seem to be integer", key);
}
}
PyErr_Format(PyExc_ValueError, "Value of '%s' doesn't seem to be integer", key);
return INT_INVALID;
}
char *GetCStringFromDict(PyObject *dict, char *key) {
PyObject *o;
o = PyDict_GetItemString(dict, key);
if (o == NULL) {
PyErr_Format(PyExc_ValueError, "Missing key in dictionary: %s", key);
return NULL;
}
return PyString_AsString(o);
}
unsigned char *GetStringFromDict(PyObject *dict, char *key) {
PyObject *o;
o = PyDict_GetItemString(dict, key);
if (o == NULL) {
PyErr_Format(PyExc_ValueError, "Missing key in dictionary: %s", key);
return NULL;
}
return StringPythonToGammu(o);
}
int CopyStringFromDict(PyObject *dict, char *key, int len, unsigned char *dest) {
unsigned char *s;
s = GetStringFromDict(dict, key);
if (s == NULL) return 0;
if (UnicodeLength(s) > len) {
pyg_warning("Truncating text %s to %d chars!\n", key, len);
s[2*len] = 0;
s[(2*len) + 1] = 0;
}
CopyUnicodeString(dest, s);
free(s);
return 1;
}
GSM_DateTime GetDateTimeFromDict(PyObject *dict, char *key) {
PyObject *o;
GSM_DateTime dt;
o = PyDict_GetItemString(dict, key);
if (o == NULL) {
PyErr_Format(PyExc_ValueError, "Missing key in dictionary: %s", key);
dt.Year = -1;
return dt;
}
if (!BuildGSMDateTime(o, &dt)) {
dt.Year = -1;
}
return dt;
}
GSM_DateTime GetDateFromDict(PyObject *dict, char *key) {
PyObject *o;
GSM_DateTime dt;
o = PyDict_GetItemString(dict, key);
if (o == NULL) {
PyErr_Format(PyExc_ValueError, "Missing key in dictionary: %s", key);
dt.Year = -1;
return dt;
}
if (!BuildGSMDate(o, &dt)) {
dt.Year = -1;
}
return dt;
}
char *GetCharFromDict(PyObject *dict, char *key) {
PyObject *o;
char *ps;
o = PyDict_GetItemString(dict, key);
if (o == NULL) {
PyErr_Format(PyExc_ValueError, "Missing key in dictionary: %s", key);
return NULL;
}
ps = PyString_AsString(o);
if (ps == NULL) {
PyErr_Format(PyExc_ValueError, "Can not get string value for key %s", key);
return NULL;
}
return ps;
}
char *GetDataFromDict(PyObject *dict, char *key, Py_ssize_t *len) {
PyObject *o;
char *ps;
o = PyDict_GetItemString(dict, key);
if (o == NULL) {
PyErr_Format(PyExc_ValueError, "Missing key in dictionary: %s", key);
return NULL;
}
if (PyString_AsStringAndSize(o, &ps, len) != 0) {
PyErr_Format(PyExc_ValueError, "Can not get string value for key %s", key);
return NULL;
}
return ps;
}
|