File: pytype_memos.c

package info (click to toggle)
jppy 0.0.56-1.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 9,512 kB
  • ctags: 3,972
  • sloc: ansic: 45,883; sh: 10,974; python: 6,331; yacc: 1,014; makefile: 418
file content (413 lines) | stat: -rw-r--r-- 11,090 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include <Python.h>

#include "pytype_memos.h"
#include "pytype_basics.h"
#include "structmember.h"
#include "python_compatibility.h"

/*******************************************************
 * Create and delloc methods for objects
 ******************************************************/

extern PyObject* PyPiMemo_New(PyTypeObject *type, PyObject *args, PyObject *kwds) {
  int i;
  PyPiMemo* self;

  MemoType.ob_type = &PyType_Type;
  self = (PyPiMemo *)type->tp_alloc(type, 0);
  new_Memo(&(self->a));
  SetBasicRecordObjectAttributeDefaults((PyObject*) self, pack_Memo);
  self->filters = NULL;

  return (PyObject*)self;
}

extern int PyPiMemo_Init(PyObject *self, PyObject *args, PyObject *kwds) {
  PyPiMemo* frommemo = NULL;
  PyPiMemo* memo = NULL;
  PyObject* filters = NULL;
  int i;

  static char *kwlist[] = {"memo","record_field_filters", NULL};
  if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist,
				   &frommemo, &filters)) {
    return -1;
  }

  memo = (PyPiMemo*)self;
  /* we have to support calling __init__ more than once */
  if (memo->filters != NULL) {
    Py_DECREF(memo->filters);
    memo->filters = NULL;
  }
  if (filters != NULL) {
    memo->filters = filters;
    Py_INCREF(filters);
  }
  free_Memo(&(memo->a));
  if (memo->saved_br.size > 0 && memo->saved_br.buf) {
    free(memo->saved_br.buf);
  }
  if ((frommemo == NULL) || ((PyObject *)frommemo == Py_None)) {
    /* Initialise attributes custom to this type of object */
    new_Memo(&(memo->a));
    SetBasicRecordObjectAttributeDefaults((PyObject*) memo, pack_Memo);
  } else {
    if (!PyPiMemo_Check(frommemo)) {
      PyErr_SetString(PyExc_TypeError,"Must provide a Memo object to share");
      return -1;
    }

    /* copy all the database agnostic record attributes */
    memo->saved_br.size = frommemo->saved_br.size;
    memo->saved_br.attrib = frommemo->saved_br.attrib;
    memo->saved_br.rt = frommemo->saved_br.rt;
    memo->saved_br.unique_id = frommemo->saved_br.unique_id;

    memo->rt = frommemo->rt;
    memo->unique_id = frommemo->unique_id;

    memo->saved_br.buf = malloc(frommemo->saved_br.size);
    memcpy(memo->saved_br.buf,
	   frommemo->saved_br.buf,
	   frommemo->saved_br.size);

    memo->category = frommemo->category;
    memo->unsaved_changes = frommemo->unsaved_changes;
    memo->deleted = frommemo->deleted;
    memo->modified = frommemo->modified;
    memo->busy = frommemo->busy;
    memo->secret = frommemo->secret;

    /* Now do the pointers */
    pyint_strcpy(memo->a.text, frommemo->a.text);

  }
  return 0;
}


static PyObject * PyPiMemo_Allocate(PyTypeObject *type, int nitems) {
  PyPiMemo *memo;
  if (type == &MemoType) {
    memo = PyObject_New(PyPiMemo, &MemoType);
    return (PyObject *) memo;
  } else {
    /* Is this for subclasses ? */
    memo = (PyPiMemo *)PyType_GenericAlloc(type, nitems);
    return (PyObject*)memo;
  }
}

/**
 * Wrap an existing memo in a jppy memo. This makes
 * a copy of a into a newly allocated struct inside the python object.
 *
 * @param a the palm object
 * @param rt
 * @param unique_id
 * @param attrib
 * @param size
 * @param buf
 * @return a newly created python event object that represents the data in a
 */
extern PyObject* PyPiMemo_Wrap(struct Memo* a, PCRecType rt,
				 unsigned int unique_id, unsigned char attrib,
				 int size, void* buf, PyObject *filters) {
  PyPiMemo* memo;
  int i;

  PyObject *python_mod, *python_mdict, *memo_class, *python_args, *python_kw;
  python_mod = PyImport_Import(PyString_FromString("jppy.jpilot.legacy"));
  if (python_mod == NULL) {
    PyErr_Print();
    return NULL;
  }
  python_mdict = PyModule_GetDict(python_mod);
  if (python_mdict == NULL) {
    PyErr_Print();
    Py_DECREF(python_mod);
    return NULL;
  }
  Py_INCREF(python_mdict);
  Py_DECREF(python_mod);
  memo_class = PyDict_GetItemString(python_mdict, "Memo"); /* borrowed reference */
  if (memo_class == NULL) {
    PyErr_Print();
    Py_DECREF(python_mdict);
    return NULL;
  }
  Py_INCREF(memo_class);
  python_args = Py_BuildValue("()");
  python_kw = Py_BuildValue("{s:O}","record_field_filters",filters);
  memo = (PyPiMemo*) PyObject_Call(memo_class, python_args, python_kw);
  Py_DECREF(memo_class);
  Py_DECREF(python_args);
  Py_DECREF(python_kw);
  if (memo == NULL) {
    PyErr_Print();
    return NULL;
  }
  Py_INCREF(memo);


  /* set saved_br stuff, and rt and unique_id, and attrib derived details for the current memo */
  SetSavedBrAndRTandUniqueIDandAttribs(rt, unique_id, attrib, size, buf, (PyObject *)memo);

  pyp_strcpy(memo->a.text, a->text);

  return (PyObject*)memo;
}

static void PyPiMemo_Dealloc(PyPiMemo* self) {
  free_Memo(&(self->a));
  if (self->filters != NULL) {
    Py_DECREF(self->filters);
    self->filters = NULL;
  }
  if (self->saved_br.size > 0 && self->saved_br.buf) {
    free(self->saved_br.buf);
  }
  self->ob_type->tp_free((PyObject*)self);
}


static int PyPiMemo_Compare(PyPiMemo* self,PyPiMemo *other) {
  int res;

  if ((self->a.text) &&
      (other->a.text)) {

    res = strcasecmp(self->a.text,
		 other->a.text);
    if (res > 0) {
      res = 1;
    } else if (res < 0) {
      res = -1;
    } else {
      res = 0;
    }
  } else if (self->a.text) {
    res = -1;
  } else if (other->a.text) {
    res = 1;
  } else {
    res = 0;
  }

  return res;

}

static char *PyPiMemo_key_list[] = {
  "text",
  NULL};

static PyObject* PyPiMemo_keys(PyObject* self) {
  PyObject *list = Py_BuildValue("[s]", PyPiMemo_key_list[0]);
  PyPi_extend_keys_from_filters((PyPiBase*)self, list);
  return list;
}

/* forward declaration */
PyObject *PyPiMemo_GetItem(PyPiMemo* self,  PyObject* key);

static PyObject* PyPiMemo_values(PyObject* self) {
  PyObject *list;
  PyObject *key;
  PyObject *value;
  key = PyString_FromString(PyPiMemo_key_list[0]);
  value =  PyPiMemo_GetItem((PyPiMemo *)self, key);
  list = Py_BuildValue("[O]", value);
  Py_DECREF(value);
  Py_DECREF(key);
  return list;
}

static PyObject* PyPiMemo_items(PyObject* self) {
  PyObject *list;
  PyObject *key;
  PyObject *value;
  key = PyString_FromString(PyPiMemo_key_list[0]);
  value =  PyPiMemo_GetItem((PyPiMemo *)self, key);
  list = Py_BuildValue("[(OO)]", key, value);
  Py_DECREF(value);
  Py_DECREF(key);
  return list;
}

/*******************************************************
 *
 ******************************************************/

static PyMethodDef PyPiMemo_Methods[] = {
  { "keys", (PyCFunction)PyPiMemo_keys, METH_NOARGS, "Return a list of available keys"},
  { "items",(PyCFunction)PyPiMemo_items, METH_NOARGS, "Return a list of available items"},
  { "values",(PyCFunction)PyPiMemo_values, METH_NOARGS, "Return a list of available items"},
  {NULL,NULL,0,NULL} /* Sentinel */
};

static PyMemberDef PyPiMemo_Members[] = {
  PYPI_MEMBERS_HEAD,
  {NULL}  /* Sentinel */
};

static PyGetSetDef PyPiMemo_Getseters[] = {
  PYPI_GETSETERS_HEAD,
  {NULL}  /* Sentinel */
};

/**** mapping interface ****/
int PyPiMemo_Len(PyObject* self) {
  int len=0;
  // quite expensive way to do it, but we need to get filters too
  PyObject *keys = PyPiMemo_keys(self);
  len = PySequence_Size(keys);
  Py_DECREF(keys);
  return len;
}

PyObject *PyPiMemo_GetItem(PyPiMemo* self,  PyObject* key) {
  char *keystring;
  PyObject *result;

  if (!PyString_Check(key)) {
    Py_INCREF(Py_None);
    return Py_None;
  }

  if ((result = PyPi_GetItem_from_filters((PyPiBase *)self, key)) != NULL)
    return result;
  else if (PyErr_Occurred() != NULL)
    return NULL;


  Py_INCREF(key);
  keystring = PyString_AsString(key);

  GET_STRING_ATTR(keystring,"text", a.text);

  PyErr_Format(PyExc_KeyError,"no such key '%s'", keystring);
  Py_DECREF(key);
  return NULL;
}

int PyPiMemo_SetItem(PyPiMemo* self, PyObject* key, PyObject* value) {
  char buf[255];
  char *keystring;

  if (!PyString_Check(key)) {
    PyErr_SetString(PyExc_TypeError,"key must be a String");
    return -1;
  }

  if (PyPi_SetItem_from_filters((PyPiBase *)self, key, value) > 0)
    return 0;
  else if (PyErr_Occurred() != NULL)
    return -1;

  Py_INCREF(key);
  keystring = PyString_AsString(key);

  if (value == NULL) {
    PyErr_Format(PyExc_ValueError,"Can't delete value %s", keystring);
    return -1;
  }

  SET_STRING_ATTR(keystring,"text",a.text,value, 4096);

  PyErr_SetString(PyExc_KeyError,"no such key");
  Py_DECREF(key);
  return -1;
}

static PyMappingMethods PyPiMemo_Mapping = {
  (lenfunc)PyPiMemo_Len,
  (binaryfunc)PyPiMemo_GetItem,
  (objobjargproc)PyPiMemo_SetItem,
};



/*******************************************************
 * Provide a repr method
 ******************************************************/

static PyObject *PyPiMemo_Repr(PyPiMemo* self) {
  static PyObject *format = NULL;
  PyObject *attrib, *args, *result;
  int len1;

  if (format == NULL) {
    format = PyString_FromString("<%s %r %s>");
    if (format == NULL)
      return NULL;
  }

  if (self->a.text) {
    len1 = strlen(self->a.text) > 25 ? 25 : strlen(self->a.text);
  } else {
    len1 = 0;
  }

  args = Py_BuildValue("ss#O",
		       (self->ob_type)->tp_name,
		       self->a.text,
		       len1,
		       Attribute_Repr((PyObject *)self));

  if (args == NULL)
    return NULL;

  result = PyString_Format(format, args);
  Py_DECREF(args);
  return result;
}


/*******************************************************
 * Declare the type
 ******************************************************/


PyTypeObject MemoType = {
  PyObject_HEAD_INIT(NULL)
  0, /* ob_size */
  "jppy._jpilot.__jpilot.Memo", /*tp_name*/
  sizeof(PyPiMemo),  0, /*tp_basicsize*/ /*tp_itemsize*/
  (destructor)PyPiMemo_Dealloc, /*tp_dealloc*/
  0,          /*tp_print*/
  0,
  0,
  (cmpfunc)PyPiMemo_Compare,          /*tp_compare*/
  (reprfunc)PyPiMemo_Repr,          /*tp_repr*/
  0,          /*tp_as_number*/
  0,          /*tp_as_sequence*/
  &PyPiMemo_Mapping,          /*tp_as_mapping*/
  0,          /*tp_hash */
  0,                         /*tp_call*/
  0,                         /*tp_str*/
  0,                         /*tp_getattro*/
  0,                         /*tp_setattro*/
  0,                         /*tp_as_buffer*/
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  "Memo objects",           /* tp_doc */
  0,		               /* tp_traverse */
  0,		               /* tp_clear */
  0,		               /* tp_richcompare */
  0,		               /* tp_weaklistoffset */
  0,		               /* tp_iter */
  0,		               /* tp_iternext */
  PyPiMemo_Methods,            /* tp_methods */
  PyPiMemo_Members,            /* tp_members */
  PyPiMemo_Getseters,          /* tp_getset */
  0,                           /* tp_base */
  0,                           /* tp_dict */
  0,                           /* tp_descr_get */
  0,                           /* tp_descr_set */
  0,                           /* tp_dictoffset */
  (initproc)PyPiMemo_Init,            /* tp_init */
  (allocfunc)PyPiMemo_Allocate,                 /* tp_alloc */
  (newfunc)PyPiMemo_New,                 /* tp_new */
  0, /* Low-level free-memory routine */
};