File: PyBZDB.cpp

package info (click to toggle)
bzflag 2.0.13.20080902-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 27,564 kB
  • ctags: 34,716
  • sloc: cpp: 139,842; ansic: 14,510; sh: 10,715; makefile: 2,454; perl: 477; php: 428; python: 345; objc: 243; xml: 24
file content (208 lines) | stat: -rw-r--r-- 5,139 bytes parent folder | download
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
207
208
/* bzflag
 * Copyright (c) 1993 - 2008 Tim Riker
 *
 * This package is free software;  you can redistribute it and/or
 * modify it under the terms of the license found in the file
 * named COPYING that should have accompanied this file.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include "PyBZDB.h"

namespace Python
{

static void      BZDB_dealloc       (BZDB *bzdb);
static PyObject *BZDB_getAttr       (BZDB *bzdb, char *name);
static PyObject *BZDB_repr	    (BZDB *bzdb);
static int       BZDB_length	    (BZDB *bzdb);
static PyObject *BZDB_subscript     (BZDB *bzdb, PyObject *key);
static int       BZDB_ass_subscript (BZDB *bzdb, PyObject *key, PyObject *value);
static PyObject *BZDB_get_bool      (BZDB *self, PyObject *args);
static PyObject *BZDB_get_double    (BZDB *self, PyObject *args);
static PyObject *BZDB_get_int       (BZDB *self, PyObject *args);
static PyObject *BZDB_set_bool      (BZDB *self, PyObject *args);
static PyObject *BZDB_set_double    (BZDB *self, PyObject *args);
static PyObject *BZDB_set_int       (BZDB *self, PyObject *args);

static PyMethodDef BZDB_methods[] = {
  {"GetBool",   (PyCFunction) BZDB_get_bool,   METH_VARARGS, NULL},
  {"GetDouble", (PyCFunction) BZDB_get_double, METH_VARARGS, NULL},
  {"GetInt",    (PyCFunction) BZDB_get_int,    METH_VARARGS, NULL},
  {"SetBool",   (PyCFunction) BZDB_set_bool,   METH_VARARGS, NULL},
  {"SetDouble", (PyCFunction) BZDB_set_double, METH_VARARGS, NULL},
  {"SetInt",    (PyCFunction) BZDB_set_int,    METH_VARARGS, NULL},
  {NULL,	NULL,			       0,	     NULL},
};

PyMappingMethods BZDB_mapping = {
  (inquiry) BZDB_length,		// mp_length
  (binaryfunc) BZDB_subscript,		// mp_subscript
  (objobjargproc) BZDB_ass_subscript,	// mp_ass_subscript
};

PyTypeObject BZDB_Type = {
  PyObject_HEAD_INIT(NULL)
  0,				// ob_size
  "BZDB",			// tp_name
  sizeof (BZDB),		// tp_basicsize
  0,				// tp_itemsize
  (destructor) BZDB_dealloc,	// tp_dealloc
  0,				// tp_print
  (getattrfunc) BZDB_getAttr,	// tp_getattr
  0,				// tp_setattr
  0,				// tp_compare
  0,				// tp_repr - FIXME?
  0,				// tp_as_number
  0,				// tp_as_sequence
  &BZDB_mapping,		// tp_as_mapping
  0,				// tp_as_hash
  0,				// tp_call
  0,				// tp_str
  0,				// tp_getattro
  0,				// tp_setattro
  0,				// tp_flags
  0,				// tp_doc
};

PyObject *
CreateBZDB ()
{
  BZDB *bzdb = (BZDB *) PyObject_NEW (BZDB, &BZDB_Type);
  return ((PyObject *) bzdb);
}

static void
BZDB_dealloc (BZDB *bzdb)
{
  PyObject_DEL (bzdb);
}

static PyObject *
BZDB_getAttr (BZDB *bzdb, char *name)
{
  return Py_FindMethod (BZDB_methods, (PyObject *) bzdb, name);
}

static PyObject *
BZDB_repr (BZDB *bzdb)
{
}

static int
BZDB_length (BZDB *bzdb)
{
  return 0;
}

static PyObject *
BZDB_subscript (BZDB *bzdb, PyObject *key)
{
  if (!PyString_Check (key)) {
    // FIXME - throw KeyError
    return NULL;
  }
  char *k = PyString_AsString (key);
  return PyString_FromString (bz_getBZDBString (k).c_str ());
}

static int
BZDB_ass_subscript (BZDB *bzdb, PyObject *key, PyObject *value)
{
  if (!PyString_Check (key)) {
    // FIXME - throw error
    return 0;
  }
  if (!PyString_Check (value)) {
    // FIXME - throw error
    return 0;
  }
  char *skey, *svalue;
  skey   = PyString_AsString (key);
  svalue = PyString_AsString (value);
  bz_setBZDBString (skey, svalue);
  return 1;
}

static PyObject *
BZDB_get_bool (BZDB *self, PyObject *args)
{
  char *key;
  if (!PyArg_ParseTuple (args, "s", &key)) {
    // FIXME - throw KeyError
    return NULL;
  }
  return (bz_getBZDBBool (key) ? Py_True : Py_False);
}

static PyObject *
BZDB_get_double (BZDB *self, PyObject *args)
{
  char *key;
  if (!PyArg_ParseTuple (args, "s", &key)) {
    // FIXME - throw KeyError
    return NULL;
  }
  return PyFloat_FromDouble (bz_getBZDBDouble (key));
}

static PyObject *
BZDB_get_int (BZDB *self, PyObject *args)
{
  char *key;
  if (!PyArg_ParseTuple (args, "s", &key)) {
    // FIXME - throw KeyError
    return NULL;
  }
  return PyInt_FromLong ((long) bz_getBZDBInt (key));
}

static PyObject *
BZDB_set_bool (BZDB *self, PyObject *args)
{
  char *key;
  PyObject *value;
  if (!PyArg_ParseTuple (args, "so", &key, &value)) {
    // FIXME - throw error
    return NULL;
  }
  return (bz_setBZDBBool (key, (value == Py_True)) ? Py_True : Py_False);
}

static PyObject *
BZDB_set_double (BZDB *self, PyObject *args)
{
  char *key;
  double value;
  if (!PyArg_ParseTuple (args, "sd", &key, &value)) {
    // FIXME - throw error
    return NULL;
  }
  return (bz_setBZDBDouble (key, value) ? Py_True : Py_False);
}

static PyObject *
BZDB_set_int (BZDB *self, PyObject *args)
{
  char *key;
  int value;
  if (!PyArg_ParseTuple (args, "si", &key, &value)) {
    // FIXME - throw error
    return NULL;
  }
  return (bz_setBZDBInt (key, value) ? Py_True : Py_False);
}

}

// Local Variables: ***
// mode:C++ ***
// tab-width: 8 ***
// c-basic-offset: 2 ***
// indent-tabs-mode: t ***
// End: ***
// ex: shiftwidth=2 tabstop=8