PageRenderTime 35ms CodeModel.GetById 28ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Lib/python/argcargv.i

#
Swig | 92 lines | 78 code | 6 blank | 8 comment | 0 complexity | e4f1c2db55cbcb09c5723ec052247a91 MD5 | raw file
 1/* ------------------------------------------------------------
 2 * --- Argc & Argv ---
 3 * ------------------------------------------------------------ */
 4
 5%fragment("SWIG_AsArgcArgv","header",fragment="SWIG_AsCharPtrAndSize") {
 6SWIGINTERN int
 7SWIG_AsArgcArgv(PyObject *input,
 8		swig_type_info *ppchar_info,
 9		size_t *argc, char ***argv, int *owner)
10{  
11  void *vptr;
12  int res = SWIG_ConvertPtr(input, &vptr, ppchar_info, 0);
13  if (!SWIG_IsOK(res)) {
14    int list = 0;
15    PyErr_Clear();
16    list = PyList_Check(input);
17    if (list || PyTuple_Check(input)) {
18      size_t i = 0;
19      size_t size = list ? PyList_Size(input) : PyTuple_Size(input);
20      if (argc) *argc = size;
21      if (argv) {
22	*argv = %new_array(size + 1, char*);
23	for (; i < size; ++i) {
24	  PyObject *obj = list ? PyList_GetItem(input,i) : PyTuple_GetItem(input,i);
25	  char *cptr = 0; size_t sz = 0; int alloc = 0;
26	  res = SWIG_AsCharPtrAndSize(obj, &cptr, &sz, &alloc);
27	  if (SWIG_IsOK(res)) {
28	    if (cptr && sz) {
29	      (*argv)[i] = (alloc == SWIG_NEWOBJ) ? cptr : %new_copy_array(cptr, sz, char);
30	    } else {
31	      (*argv)[i] = 0;
32	    }
33	  } else {
34	    return SWIG_TypeError;
35	  }
36	}
37	(*argv)[i] = 0;
38	if (owner) *owner = 1;
39      } else {
40	for (; i < size; ++i) {
41	  PyObject *obj = list ? PyList_GetItem(input,i) : PyTuple_GetItem(input,i);
42	  res = SWIG_AsCharPtrAndSize(obj, 0, 0, 0);
43	  if (!SWIG_IsOK(res)) return SWIG_TypeError;
44	}
45	if (owner) *owner = 0;
46      }
47      return SWIG_OK;
48    } else {
49      return SWIG_TypeError;
50    }
51  } else {
52    /* seems dangerous, but the user asked for it... */
53    size_t i = 0;
54    if (argv) { while (*argv[i] != 0) ++i;}    
55    if (argc) *argc = i;
56    if (owner) *owner = 0;
57    return SWIG_OK;
58  }
59}
60}
61
62/*
63  This typemap works with either a char **, a python list or a python
64  tuple
65 */
66
67%typemap(in,noblock=0,fragment="SWIG_AsArgcArgv") (int ARGC, char **ARGV) (int res,char **argv = 0, size_t argc = 0, int owner= 0) {
68  res = SWIG_AsArgcArgv($input, $descriptor(char**), &argc, &argv, &owner);
69  if (!SWIG_IsOK(res)) { 
70    $1 = 0; $2 = 0;
71    %argument_fail(SWIG_TypeError, "int ARGC, char **ARGV", $symname, $argnum);
72  } else {  
73    $1 = %static_cast(argc,$1_ltype);
74    $2 = %static_cast(argv, $2_ltype);
75  }
76}
77
78%typemap(typecheck, precedence=SWIG_TYPECHECK_STRING_ARRAY) (int ARGC, char **ARGV) {
79  int res = SWIG_AsArgcArgv($input, $descriptor(char**), 0, 0, 0);
80  $1 = SWIG_IsOK(res);
81}
82
83%typemap(freearg,noblock=1) (int ARGC, char **ARGV)  {
84  if (owner$argnum) {
85    size_t i = argc$argnum;
86    while (i) {
87      %delete_array(argv$argnum[--i]);
88    }
89    %delete_array(argv$argnum);
90  }
91}
92