~ubuntu-branches/ubuntu/hoary/swig1.3/hoary

« back to all changes in this revision

Viewing changes to Examples/ruby/hashargs/example.i

  • Committer: Bazaar Package Importer
  • Author(s): Thom May
  • Date: 2004-08-02 15:57:10 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040802155710-bm292q1d6x6tw7gc
Tags: 1.3.21-5ubuntu1
Fix linking for ruby, python, perl and tcl bindings

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 %{
 
29
void 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
%}