home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Modules / palmappsmodule.c < prev    next >
C/C++ Source or Header  |  2000-12-27  |  4KB  |  201 lines

  1. /* 
  2.    This module contains the interfaces to the four main Palm applications.
  3. */
  4.  
  5. #include "kludge.h"
  6.  
  7. #include "_palmutils.h"
  8. #include "AddressDB.h"
  9. #include "datebook.h"
  10. #include "MemoDB.h"
  11. #include "ToDoDB.h"
  12.  
  13. static PyObject *palmapps_error;
  14.  
  15. static char *_get_memptr(PyObject *args, PyObject **Oret) SEG_APPSMODULE_C;
  16. static PyObject *palmapps_memo(PyObject *self, PyObject *args) SEG_APPSMODULE_C;
  17. static PyObject *palmapps_datebook(PyObject *self, PyObject *args) SEG_APPSMODULE_C;
  18. static PyObject *palmapps_todo(PyObject *self, PyObject *args) SEG_APPSMODULE_C;
  19. static PyObject *palmapps_addrbook(PyObject *self, PyObject *args) SEG_APPSMODULE_C;
  20. void initpalmapps(void) SEG_APPSMODULE_C;
  21.  
  22.  
  23. static char *
  24. _get_memptr(PyObject *args, PyObject **Oret)
  25. {
  26.     char *rec;
  27.     PyObject *o;
  28.  
  29.     if (!PyArg_ParseTuple(args, "O", &o))
  30.         return NULL;
  31.     if (PyBuffer_Check(o)){
  32.         /* handle buffer - be sure to grab the base */
  33.         rec = (char*) ((PyBufferObject *)o)->b_ptr;
  34.     }
  35.     else if (PyString_Check(o)){
  36.         /* handle as string */
  37.         rec = (char *) ((PyStringObject *)o)->ob_sval;
  38.     }
  39.     else {
  40.         PyErr_SetString(PyExc_ValueError,
  41.                 "Argument must be a Buffer or String");
  42.         return NULL;
  43.     }
  44.     /* set the return object */
  45.     *Oret = o;
  46.  
  47.     return rec;
  48. }
  49.  
  50. static PyObject *
  51. palmapps_memo(PyObject *self, PyObject *args)
  52. {
  53.     PyObject *o;
  54.     char *rec;
  55.     
  56.     if (!(rec = _get_memptr(args, &o)))
  57.         return NULL;
  58.     
  59.     /* return result as a buffer */
  60.     return PyBuffer_FromObject(o, 0, strlen(rec));
  61. }
  62.  
  63. static PyObject *
  64. palmapps_datebook(PyObject *self, PyObject *args)
  65. {
  66.     PyObject *o;
  67.     char *rec;
  68.     PyObject *b1, *b2=NULL;
  69.     int off, off2, l;
  70.     PackedApptDBRecordType *ap;
  71.  
  72.     if (!(rec = _get_memptr(args, &o)))
  73.         return NULL;
  74.     
  75.     ap = (PackedApptDBRecordType *) rec;
  76.     off = sizeof(PackedApptDBRecordType);
  77.     if ( ap->flags.alarm )   /* skip alarm */
  78.         off += sizeof(AlarmInfoType);
  79.     if ( ap->flags.repeat )   /* skip repeat */
  80.         off += sizeof(RepeatInfoType);
  81.     if ( ap->flags.exceptions )   /* skip exceptions */
  82.         off += sizeof(ExceptionsListType);
  83.  
  84.     l = strlen(&rec[off]);
  85.     b1 = PyBuffer_FromObject(o, off, l);
  86.     
  87.     off2 = off + l + 1;
  88.     if (!b1)
  89.         return NULL;
  90.     if ( ap->flags.note ){   /* note is present */
  91.         b2 = PyBuffer_FromObject(o, off2, strlen(&rec[off2]));
  92.         if (!b2)
  93.             return NULL;
  94.     }
  95.       
  96.     if (b2)
  97.         return Py_BuildValue("(OO)", b1, b2);
  98.     else {
  99.         Py_INCREF(Py_None);
  100.         return Py_BuildValue("(OO)", b1, Py_None);
  101.     }
  102. }
  103.  
  104. static PyObject *
  105. palmapps_todo(PyObject *self, PyObject *args)
  106. {
  107.     PyObject *o;
  108.     char *rec;
  109.     char *desc, *note;
  110.     PyObject *b1, *b2, *priority;
  111.     int off1, off2;
  112.  
  113.     if (!(rec = _get_memptr(args, &o)))
  114.         return NULL;
  115.     
  116.     /* skip the first 3 bytes */
  117.     off1 = 3;
  118.     desc = &rec[off1];
  119.  
  120.     off2 = off1 + strlen(desc)+1;
  121.     note = &rec[off2];
  122.     
  123.     b1 = PyBuffer_FromObject(o, off1, strlen(desc));
  124.     if (!b1)
  125.         return NULL;
  126.     b2 = PyBuffer_FromObject(o, off2, strlen(note));
  127.     if (!b2)
  128.         return NULL;
  129.     priority = PyInt_FromLong((long)(rec[2] & 0xff));
  130.     if (!priority)
  131.         return NULL;
  132.     return Py_BuildValue("(OOO)", priority, b1, b2);
  133. }
  134.  
  135. static PyObject *
  136. palmapps_addrbook(PyObject *self, PyObject *args)
  137. {
  138.     char *rec;
  139.     PyObject *o;
  140.     struct header {
  141.         unsigned long p;
  142.         unsigned long flags;
  143.     } h;
  144.     struct header *hp = &h;
  145.     int item_off[19];
  146.     int off;
  147.     int i;
  148.     PyObject *t, *buf;
  149.  
  150.     if (!(rec = _get_memptr(args, &o)))
  151.         return NULL;
  152.     hp = (struct header *) rec;
  153.     off = 9;
  154.     for (i = 0; i < 19; i++) {
  155.         if (hp->flags & (1L << i)) {
  156.             item_off[i] = off;
  157.             off += strlen(&rec[item_off[i]]) + 1;
  158.         } else
  159.             item_off[i] = -1;
  160.     }
  161.  
  162.     if (!(t = PyTuple_New(19)))
  163.         return NULL;
  164.  
  165.     for (i=0; i < 19; i++){
  166.         off = item_off[i];
  167.         if (off != -1) {
  168.             buf = PyBuffer_FromObject(o, off, strlen(&rec[off]));
  169.             if (!buf)
  170.                 return NULL;
  171.             PyTuple_SET_ITEM(t, i, buf);
  172.         } 
  173.         else {
  174.             Py_INCREF(Py_None);
  175.             PyTuple_SET_ITEM(t, i, Py_None);
  176.         }
  177.     }
  178.     return t;
  179. }
  180.  
  181.  
  182. static PyMethodDef palmapps_methods[] = {
  183.     {"memo",  (PyCFunction)palmapps_memo, METH_VARARGS},
  184.     {"datebook",  (PyCFunction)palmapps_datebook, METH_VARARGS},
  185.     {"todo",  (PyCFunction)palmapps_todo, METH_VARARGS},
  186.     {"addrbook",  (PyCFunction)palmapps_addrbook, METH_VARARGS},
  187.     {NULL, NULL} };
  188.  
  189. void initpalmapps(void) {
  190.     PyObject *m, *d;
  191.  
  192.     m = Py_InitModule3("palmapps", palmapps_methods, NULL);
  193.     d = PyModule_GetDict(m);
  194.     palmapps_error = PyErr_NewException("palmapps.error", NULL, NULL);
  195.     if (palmapps_error == NULL)
  196.         return;
  197.  
  198.     PyDict_SetItemString(d, "error", palmapps_error);
  199.  
  200. }
  201.