argcargv.i 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* ------------------------------------------------------------
  2. * --- Argc & Argv ---
  3. * ------------------------------------------------------------ */
  4. /* ------------------------------------------------------------
  5. Use it as follow:
  6. %apply (int ARGC, char **ARGV) { (size_t argc, const char **argv) }
  7. %inline %{
  8. int mainApp(size_t argc, const char **argv)
  9. {
  10. return argc;
  11. }
  12. then in the ruby side:
  13. args = ["asdf", "asdf2"]
  14. mainApp(args);
  15. * ------------------------------------------------------------ */
  16. %typemap(in) (int ARGC, char **ARGV) {
  17. if (rb_obj_is_kind_of($input,rb_cArray)) {
  18. int i;
  19. int size = RARRAY_LEN($input);
  20. $1 = ($1_ltype) size;
  21. $2 = (char **) malloc((size+1)*sizeof(char *));
  22. VALUE *ptr = RARRAY_PTR($input);
  23. for (i=0; i < size; i++, ptr++) {
  24. $2[i]= StringValuePtr(*ptr);
  25. }
  26. $2[i]=NULL;
  27. } else {
  28. $1 = 0; $2 = 0;
  29. %argument_fail(SWIG_TypeError, "int ARGC, char **ARGV", $symname, $argnum);
  30. }
  31. }
  32. %typemap(typecheck, precedence=SWIG_TYPECHECK_STRING_ARRAY) (int ARGC, char **ARGV) {
  33. $1 = rb_obj_is_kind_of($input,rb_cArray);
  34. }
  35. %typemap(freearg) (int ARGC, char **ARGV) {
  36. free((char *) $2);
  37. }