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

/trunk/Examples/ruby/hashargs/example.i

#
Swig | 36 lines | 33 code | 3 blank | 0 comment | 0 complexity | d68b9fdfc2203458cbf6ca6f02c1a360 MD5 | raw file
 1%module example
 2
 3%typemap(in) (int nattributes, const char **names, const int *values) (VALUE keys_ary, int i, VALUE key, VALUE val) {
 4  Check_Type($input, T_HASH);
 5  $1 = NUM2INT(rb_funcall($input, rb_intern("size"), 0, NULL));
 6  $2 = NULL;
 7  $3 = NULL;
 8  if ($1 > 0) {
 9    $2 = (char **) malloc($1*sizeof(char *));
10    $3 = (int *) malloc($1*sizeof(int));
11    keys_ary = rb_funcall($input, rb_intern("keys"), 0, NULL);
12    for (i = 0; i < $1; i++) {
13      key = rb_ary_entry(keys_ary, i);
14      val = rb_hash_aref($input, key);
15      Check_Type(key, T_STRING);
16      Check_Type(val, T_FIXNUM);
17      $2[i] = STR2CSTR(key);
18      $3[i] = NUM2INT(val);
19    }
20  }
21}
22
23%typemap(freearg) (int nattributes, const char **names, const int *values) {
24  free((void *) $2);
25  free((void *) $3);
26}
27
28%inline %{
29void setVitalStats(const char *person, int nattributes, const char **names, const int *values) {
30  int i;
31  printf("Name: %s\n", person);
32  for (i = 0; i < nattributes; i++) {
33    printf("  %s => %d\n", names[i], values[i]);
34  }
35}
36%}