| 1 | /* --- Input Values --- */ |
| 2 | |
| 3 | %typemap(in) char * "$1 = StringValuePtr($input);"; |
| 4 | %typemap(in) char [ANY] "$1 = StringValuePtr($input);"; |
| 5 | |
| 6 | /* ------------------------------------------------------------ |
| 7 | * String & length |
| 8 | * ------------------------------------------------------------ */ |
| 9 | |
| 10 | %typemap(in) (int LENGTH, char *STRING) { |
| 11 | $1 = ($1_ltype) StringValueLen($input); |
| 12 | $2 = ($2_ltype) StringValuePtr($input); |
| 13 | } |
| 14 | |
| 15 | %typemap(in) (char *STRING, int LENGTH) { |
| 16 | $1 = ($1_ltype) StringValuePtr($input); |
| 17 | $2 = ($2_ltype) StringValueLen($input); |
| 18 | } |
| 19 | |
| 20 | /* --- Output typemaps --- */ |
| 21 | |
| 22 | /* Single char */ |
| 23 | %typemap(out) char "$result = rb_str_new(&$1,1);"; |
| 24 | %typemap(out) const char & "$result = rb_str_new($1, 1);"; |
| 25 | |
| 26 | /* C string */ |
| 27 | %typemap(out) char * "$result = rb_str_new2($1);"; |
| 28 | |
| 29 | /* Special typemap for character array return values */ |
| 30 | %typemap(out) char [ANY], const char [ANY] "$result = rb_str_new2($1);"; |
| 31 | |
| 32 | /* --- Variable Input --- */ |
| 33 | |
| 34 | /* A string */ |
| 35 | #ifdef __cplusplus |
| 36 | |
| 37 | %typemap(varin) char * { |
| 38 | char *temp = (char *) StringValuePtr($input); |
| 39 | if ($1) delete [] $1; |
| 40 | $1 = ($type) new char[strlen(temp)+1]; |
| 41 | strcpy((char*)$1,temp); |
| 42 | } |
| 43 | |
| 44 | %typemap(varin,warning="451:Setting const char * variable may leak memory") const char * { |
| 45 | char *temp = (char *) StringValuePtr($input); |
| 46 | $1 = ($type) new char[strlen(temp)+1]; |
| 47 | strcpy((char*)$1,temp); |
| 48 | } |
| 49 | |
| 50 | #else |
| 51 | |
| 52 | %typemap(varin) char * { |
| 53 | char *temp = (char *) StringValuePtr($input); |
| 54 | if ($1) free((char*) $1); |
| 55 | $1 = ($type) malloc(strlen(temp)+1); |
| 56 | strcpy((char*)$1,temp); |
| 57 | } |
| 58 | |
| 59 | %typemap(varin,warning="451:Setting const char * variable may leak memory") const char * { |
| 60 | char *temp = (char *) StringValuePtr($input); |
| 61 | $1 = ($type) malloc(strlen(temp)+1); |
| 62 | strcpy((char*)$1,temp); |
| 63 | } |
| 64 | |
| 65 | #endif /* __cplusplus */ |
| 66 | |
| 67 | /* Special case for string array variables */ |
| 68 | %typemap(varin, |
| 69 | warning="462:Unable to set variable of type char []") char[] |
| 70 | { |
| 71 | rb_raise(rb_eTypeError, "C/C++ variable '$name' is read-only."); |
| 72 | } |
| 73 | |
| 74 | %typemap(varin) char[ANY] "strncpy($1,StringValuePtr($input),$1_dim0);"; |
| 75 | |
| 76 | /* --- Variable Output --- */ |
| 77 | |
| 78 | /* Character */ |
| 79 | %typemap(varout) char "$result = rb_str_new(&$1,1);"; |
| 80 | |
| 81 | /* C string */ |
| 82 | %typemap(varout) char * "$result = rb_str_new2($1);"; |
| 83 | |
| 84 | /* Special typemap for character array return values */ |
| 85 | %typemap(varout) char [ANY], const char [ANY] "$result = rb_str_new2($1);"; |
| 86 | |
| 87 | /* --- Constants --- */ |
| 88 | |
| 89 | %typemap(constant) char { |
| 90 | char temp = $1; |
| 91 | rb_define_const($module,"$symname", rb_str_new(&temp,1)); |
| 92 | } |
| 93 | |
| 94 | %typemap(constant) char * |
| 95 | "rb_define_const($module,\"$symname\", rb_str_new2($1));"; |
| 96 | |
| 97 | /* directorin typemaps */ |
| 98 | |
| 99 | %typemap(directorin) char * "$input = rb_str_new2($1);"; |
| 100 | |
| 101 | /* directorout typemaps */ |
| 102 | |
| 103 | %typemap(directorout) char * "$result = STR2CSTR($1);"; |
| 104 | %typemap(directorout) const char * "$result = STR2CSTR($1);"; |
| 105 | |
| 106 | /* ------------------------------------------------------------ |
| 107 | * Typechecking rules |
| 108 | * ------------------------------------------------------------ */ |
| 109 | |
| 110 | %typecheck(SWIG_TYPECHECK_CHAR) char { |
| 111 | $1 = (TYPE($input) == T_STRING && (RSTRING($input)->len == 1)) ? 1 : 0; |
| 112 | } |
| 113 | |
| 114 | %typecheck(SWIG_TYPECHECK_STRING) char * { |
| 115 | $1 = (TYPE($input) == T_STRING) ? 1 : 0; |
| 116 | } |
| 117 | |
| 118 | /* ------------------------------------------------------------ |
| 119 | * Exception handling |
| 120 | * ------------------------------------------------------------ */ |
| 121 | |
| 122 | %typemap(throws) char * { |
| 123 | rb_raise(rb_eRuntimeError, $1); |
| 124 | } |
| 125 | |