1 /* ----------------------------------------------------------------------------- 2 * std_string.i 3 * 4 * SWIG typemaps for std::string 5 * ----------------------------------------------------------------------------- */ 6 7 // ------------------------------------------------------------------------ 8 // std::string is typemapped by value 9 // This can prevent exporting methods which return a string 10 // in order for the user to modify it. 11 // However, I think I'll wait until someone asks for it... 12 // ------------------------------------------------------------------------ 13 14 %include <exception.i> 15 16 %{ 17 #include <string> 18 %} 19 20 namespace std { 21 22 %naturalvar string; 23 24 class string; 25 26 %typemap(typecheck) string = char *; 27 %typemap(typecheck) const string & = char *; 28 29 %typemap(in) string (char * tempptr) { 30 if (scm_is_string($input)) { 31 tempptr = SWIG_scm2str($input); 32 $1.assign(tempptr); 33 if (tempptr) SWIG_free(tempptr); 34 } else { 35 SWIG_exception(SWIG_TypeError, "string expected"); 36 } 37 } 38 39 %typemap(in) const string & ($*1_ltype temp, char *tempptr) { 40 if (scm_is_string($input)) { 41 tempptr = SWIG_scm2str($input); 42 temp.assign(tempptr); 43 if (tempptr) SWIG_free(tempptr); 44 $1 = &temp; 45 } else { 46 SWIG_exception(SWIG_TypeError, "string expected"); 47 } 48 } 49 50 %typemap(in) string * (char *tempptr) { 51 if (scm_is_string($input)) { 52 tempptr = SWIG_scm2str($input); 53 $1 = new $*1_ltype(tempptr); 54 if (tempptr) SWIG_free(tempptr); 55 } else { 56 SWIG_exception(SWIG_TypeError, "string expected"); 57 } 58 } 59 60 %typemap(out) string { 61 $result = SWIG_str02scm($1.c_str()); 62 } 63 64 %typemap(out) const string & { 65 $result = SWIG_str02scm($1->c_str()); 66 } 67 68 %typemap(out) string * { 69 $result = SWIG_str02scm($1->c_str()); 70 } 71 72 %typemap(varin) string { 73 if (scm_is_string($input)) { 74 char *tempptr = SWIG_scm2str($input); 75 $1.assign(tempptr); 76 if (tempptr) SWIG_free(tempptr); 77 } else { 78 SWIG_exception(SWIG_TypeError, "string expected"); 79 } 80 } 81 82 %typemap(varout) string { 83 $result = SWIG_str02scm($1.c_str()); 84 } 85 86 } 87