source: nikanabo/current/xdelta/diy/xdelta3-python.h@ 336

Last change on this file since 336 was 185, checked in by geyser, 18 years ago
File size: 2.3 KB
Line 
1/* xdelta 3 - delta compression tools and library
2 * Copyright (C) 2003, 2004, 2005, 2006, 2007. Joshua P. MacDonald
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19#include "Python.h"
20
21static PyObject *pyxd3_error;
22
23/* spam: xdelta3.main([string,list,...]) */
24PyObject *xdelta3_main_cmdline (PyObject *self, PyObject *args)
25{
26 int ret, i, nargs;
27 char **argv = NULL;
28 int argc = 0;
29 PyObject *result = NULL;
30 PyObject *o;
31
32 if (! PyArg_ParseTuple (args, "O", &o)
33 || ! PyList_Check (o))
34 {
35 goto cleanup;
36 }
37
38 argc = PyList_Size (o);
39 nargs = argc + 2;
40
41 if (! (argv = malloc (sizeof(argv[0]) * nargs)))
42 {
43 PyErr_NoMemory ();
44 goto cleanup;
45 }
46 memset (argv, 0, sizeof(argv[0]) * nargs);
47
48 for (i = 1; i < nargs-1; i += 1)
49 {
50 char *ps;
51 PyObject *s;
52 if ((s = PyList_GetItem (o, i-1)) == NULL) { goto cleanup; }
53 ps = PyString_AsString (s);
54 /* TODO: ps is NULL if s is not a string, crashes the interpreter */
55 argv[i] = ps;
56 }
57
58 ret = xd3_main_cmdline (argc+1, argv);
59
60 if (ret == 0)
61 {
62 result = Py_BuildValue ("i", ret);
63 }
64 else
65 {
66 PyErr_SetString (pyxd3_error, "failed :(");
67 }
68 cleanup:
69 if (argv)
70 {
71 free (argv);
72 }
73 return result;
74}
75
76static PyMethodDef xdelta3_methods[] = {
77 { "main", xdelta3_main_cmdline, METH_VARARGS, "xdelta3 main()" },
78 { NULL, NULL }
79};
80
81DL_EXPORT(void) initxdelta3main (void)
82{
83 PyObject *m, *d;
84 m = Py_InitModule ("xdelta3main", xdelta3_methods);
85 d = PyModule_GetDict (m);
86 pyxd3_error = PyErr_NewException ("xdelta3main.error", NULL, NULL);
87 PyDict_SetItemString (d, "error", pyxd3_error);
88}
Note: See TracBrowser for help on using the repository browser.