PageRenderTime 44ms CodeModel.GetById 16ms 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
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. %module example
  2. %typemap(in) (int nattributes, const char **names, const int *values) (VALUE keys_ary, int i, VALUE key, VALUE val) {
  3. Check_Type($input, T_HASH);
  4. $1 = NUM2INT(rb_funcall($input, rb_intern("size"), 0, NULL));
  5. $2 = NULL;
  6. $3 = NULL;
  7. if ($1 > 0) {
  8. $2 = (char **) malloc($1*sizeof(char *));
  9. $3 = (int *) malloc($1*sizeof(int));
  10. keys_ary = rb_funcall($input, rb_intern("keys"), 0, NULL);
  11. for (i = 0; i < $1; i++) {
  12. key = rb_ary_entry(keys_ary, i);
  13. val = rb_hash_aref($input, key);
  14. Check_Type(key, T_STRING);
  15. Check_Type(val, T_FIXNUM);
  16. $2[i] = STR2CSTR(key);
  17. $3[i] = NUM2INT(val);
  18. }
  19. }
  20. }
  21. %typemap(freearg) (int nattributes, const char **names, const int *values) {
  22. free((void *) $2);
  23. free((void *) $3);
  24. }
  25. %inline %{
  26. void setVitalStats(const char *person, int nattributes, const char **names, const int *values) {
  27. int i;
  28. printf("Name: %s\n", person);
  29. for (i = 0; i < nattributes; i++) {
  30. printf(" %s => %d\n", names[i], values[i]);
  31. }
  32. }
  33. %}