PageRenderTime 37ms CodeModel.GetById 18ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/rel-1.3.35/Lib/ruby/argcargv.i

#
Swig | 48 lines | 22 code | 4 blank | 22 comment | 0 complexity | c53598d53736458d041f15338f2f62ad MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
 1/* ------------------------------------------------------------
 2 * --- Argc & Argv ---
 3 * ------------------------------------------------------------ */
 4 
 5/* ------------------------------------------------------------
 6
 7   Use it as follow:
 8
 9     %apply (int ARGC, char **ARGV) { (size_t argc, const char **argv) }
10
11     %inline %{
12
13     int mainApp(size_t argc, const char **argv) 
14     {
15       return argc;
16     }
17
18   then in the ruby side:
19
20     args = ["asdf", "asdf2"]
21     mainApp(args);
22
23 * ------------------------------------------------------------ */
24
25%typemap(in) (int ARGC, char **ARGV) {
26  if (rb_obj_is_kind_of($input,rb_cArray)) {
27    int i;
28    int size = RARRAY_LEN($input);
29    $1 = ($1_ltype) size;
30    $2 = (char **) malloc((size+1)*sizeof(char *));
31    VALUE *ptr = RARRAY_PTR($input);
32    for (i=0; i < size; i++, ptr++) {
33      $2[i]= STR2CSTR(*ptr);
34    }    
35    $2[i]=NULL;
36  } else {
37    $1 = 0; $2 = 0;
38    %argument_fail(SWIG_TypeError, "int ARGC, char **ARGV", $symname, $argnum);
39  }
40}
41
42%typemap(typecheck, precedence=SWIG_TYPECHECK_STRING_ARRAY) (int ARGC, char **ARGV) {
43  $1 = rb_obj_is_kind_of($input,rb_cArray);
44}
45
46%typemap(freearg) (int ARGC, char **ARGV) {
47  free((char *) $2);
48}