Home | History | Annotate | Download | only in ruby
      1 /*
      2 int PROG_ARGC
      3 char **PROG_ARGV
      4 
      5     Some C function receive argc and argv from C main function.
      6     This typemap provides ignore typemap which pass Ruby ARGV contents
      7     as argc and argv to C function.
      8 */
      9 
     10 
     11 
     12 // argc and argv
     13 %typemap(in,numinputs=0) int PROG_ARGC {
     14     $1 = RARRAY_LEN(rb_argv) + 1;
     15 }
     16 
     17 %typemap(in,numinputs=0) char **PROG_ARGV {
     18     int i, n;
     19     VALUE ary = rb_eval_string("[$0] + ARGV");
     20     n = RARRAY_LEN(ary);
     21     $1 = (char **)malloc(n + 1);
     22     for (i = 0; i < n; i++) {
     23 	VALUE v = rb_obj_as_string(RARRAY_PTR(ary)[i]);
     24 	$1[i] = (char *)malloc(RSTRING_LEN(v) + 1);
     25 	strcpy($1[i], RSTRING_PTR(v));
     26     }
     27 }
     28 
     29 %typemap(freearg) char **PROG_ARGV {
     30     int i, n = RARRAY_LEN(rb_argv) + 1;
     31     for (i = 0; i < n; i++) free($1[i]);
     32     free($1);
     33 }
     34 
     35