From d4d33f06d53497bf58ccf7f03faf9255334d3bae Mon Sep 17 00:00:00 2001 From: Dave Beazley Date: Sat, 30 Nov 2002 22:10:17 +0000 Subject: [PATCH] *** empty log message *** git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4142 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Examples/java/typemap/example.i | 87 ++++++++++++++++++ Lib/tcl/wish.i | 4 +- Source/Modules1.1/swigmain.cxx | 153 ++++++++++++++++++++++++++++++++ 3 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 Examples/java/typemap/example.i create mode 100644 Source/Modules1.1/swigmain.cxx diff --git a/Examples/java/typemap/example.i b/Examples/java/typemap/example.i new file mode 100644 index 000000000..d50ddc4cc --- /dev/null +++ b/Examples/java/typemap/example.i @@ -0,0 +1,87 @@ +/* File : example.i */ +%module example +%{ +/* + example of a function that returns a value in the char * argument + normally used like: + + char buf[bigenough]; + f1(buf); +*/ + +void f1(char *s) { + if(s != NULL) { + sprintf(s, "hello world"); + } +} + +void f2(char *s) { + f1(s); +} + +void f3(char *s) { + f1(s); +} + +%} + +/* default behaviour is that of input arg, Java cannot return a value in a + * string argument, so any changes made by f1(char*) will not be seen in the Java + * string passed to the f1 function. +*/ +void f1(char *s); + +%include various.i + +/* use the BYTE argout typemap to get around this. Changes in the string by + * f2 can be seen in Java. */ +void f2(char *BYTE); + +/* alternative approach uses a StringBuffer typemap for argout */ + +/* what is the corresponding jni type */ +%typemap(jni) char *SBUF "jobject" + +/* what types to use in java source code */ +%typemap(jtype) char *SBUF "StringBuffer" +%typemap(jstype) char *SBUF "StringBuffer" + +/* how to convert java type to requested c type */ +%typemap(in) char *SBUF { + jclass sbufClass; + jmethodID toStringID; + jmethodID setLengthID; + jstring js; + + $1 = NULL; + if($input != NULL) { + /* get the String from the StringBuffer */ + sbufClass = (*jenv)->GetObjectClass(jenv, $input); + toStringID = (*jenv)->GetMethodID(jenv, sbufClass, "toString", "()Ljava/lang/String;"); + js = (jstring) (*jenv)->CallObjectMethod(jenv, $input, toStringID); + /* convert the String to a char * */ + $1 = (char *)(*jenv)->GetStringUTFChars(jenv, js, 0); + /* zero the original StringBuffer, so we can replace it with the result */ + setLengthID = (*jenv)->GetMethodID(jenv, sbufClass, "setLength", "(I)V"); + (*jenv)->CallVoidMethod(jenv, $input, setLengthID, (jint) 0); + } +} + +/* how to convert the c type to the java type */ +%typemap(argout) char *SBUF { + jclass sbufClass; + jmethodID appendStringID; + + if($1 != NULL) { + /* append the result to the empty StringBuffer */ + sbufClass = (*jenv)->GetObjectClass(jenv, $input); + appendStringID = (*jenv)->GetMethodID(jenv, sbufClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"); + (*jenv)->CallObjectMethod(jenv, $input, appendStringID, (*jenv)->NewStringUTF(jenv, $1)); + if($input != NULL) (*jenv)->ReleaseStringUTFChars(jenv, $input, $1); + } +} +/* Prevent the default freearg typemap from being used */ +%typemap(freearg) char *SBUF "" + +/* apply the new typemap to our function */ +void f3(char *SBUF); diff --git a/Lib/tcl/wish.i b/Lib/tcl/wish.i index 183acb149..5d9e036b4 100644 --- a/Lib/tcl/wish.i +++ b/Lib/tcl/wish.i @@ -7,8 +7,8 @@ // /* Revision History * $Log$ - * Revision 1.2 2002/11/30 22:01:08 beazley - * The great merge + * Revision 1.3 2002/11/30 22:10:09 beazley + * *** empty log message *** * * Revision 1.1.2.1 2001/06/20 11:47:29 mkoeppe * Portability fixes diff --git a/Source/Modules1.1/swigmain.cxx b/Source/Modules1.1/swigmain.cxx new file mode 100644 index 000000000..2048bb82c --- /dev/null +++ b/Source/Modules1.1/swigmain.cxx @@ -0,0 +1,153 @@ +/******************************************************************************* + * Simplified Wrapper and Interface Generator (SWIG) + * + * swigmain.cxx + * + * This file is the main entry point to SWIG. It collects the command + * line options, registers built-in language modules, and instantiates + * a module for code generation. If adding new language modules + * to SWIG, you would modify this file. + * + * Author : David Beazley + * + * Department of Computer Science + * University of Chicago + * 1100 E 58th Street + * Chicago, IL 60637 + * beazley@cs.uchicago.edu + * + * Please read the file LICENSE for the copyright and terms by which SWIG + * can be used and distributed. + *******************************************************************************/ + +char cvsroot_swigmain_cxx[] = "$Header$"; + +#ifndef MACSWIG +#include "swigconfig.h" +#endif + +#include "swigmod.h" + +/* Module factories. These functions are used to instantiate + the built-in language modules. If adding a new language + module to SWIG, place a similar function here. Make sure + the function has "C" linkage. This is required so that modules + can be dynamically loaded in future versions. */ + +extern "C" { + Language *swig_tcl(void); + Language *swig_python(void); + Language *swig_perl5(void); + Language *swig_ruby(void); + Language *swig_guile(void); + Language *swig_mzscheme(void); + Language *swig_java(void); + Language *swig_php(void); + Language *swig_ocaml(void); + Language *swig_pike(void); + Language *swig_sexp(void); + Language *swig_xml(void); +} + +struct swig_module { + const char *name; + ModuleFactory fac; + const char *help; +}; + +/* Association of command line options to language modules. + Place an entry for new language modules here, keeping the + list sorted alphabetically. */ + +swig_module modules[] = { + {"-guile", swig_guile, "Guile"}, + {"-java", swig_java, "Java"}, + {"-mzscheme", swig_mzscheme, "Mzscheme"}, + {"-ocaml", swig_ocaml, "Ocaml"}, + {"-perl", swig_perl5, "Perl"}, + {"-perl5", swig_perl5, 0}, + {"-php", swig_php, "PHP"}, + {"-php4", swig_php, 0}, + {"-pike", swig_pike, "Pike"}, + {"-python", swig_python, "Python"}, + {"-ruby", swig_ruby, "Ruby"}, + {"-sexp", swig_sexp, "Lisp S-Expressions"}, + {"-tcl", swig_tcl, "Tcl"}, + {"-tcl8", swig_tcl, 0}, + {"-xml", swig_xml, "XML"}, + {NULL, NULL, NULL} +}; + +#ifdef MACSWIG +#include +#include +#endif + +#ifndef SWIG_LANG +#define SWIG_LANG "-python" +#endif + +//----------------------------------------------------------------- +// main() +// +// Main program. Initializes the files and starts the parser. +//----------------------------------------------------------------- + + +int main(int argc, char **argv) { + int i; + Language *dl = 0; + ModuleFactory fac = 0; + + extern int SWIG_main(int, char **, Language *); + +#ifdef MACSWIG + SIOUXSettings.asktosaveonclose = false; + argc = ccommand(&argv); +#endif + + /* Register built-in modules */ + for (i = 0; modules[i].name; i++) { + Swig_register_module(modules[i].name, modules[i].fac); + } + + Swig_init_args(argc,argv); + + /* Get options */ + for (i = 1; i < argc; i++) { + if (argv[i]) { + fac = Swig_find_module(argv[i]); + if (fac) { + dl = (fac)(); + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-nolang") == 0) { + dl = new Language; + Swig_mark_arg(i); + } else if ((strcmp(argv[i],"-dnone") == 0) || + (strcmp(argv[i],"-dhtml") == 0) || + (strcmp(argv[i],"-dlatex") == 0) || + (strcmp(argv[i],"-dascii") == 0) || + (strcmp(argv[i],"-stat") == 0)) + { + Printf(stderr,"swig: Warning. %s option deprecated.\n",argv[i]); + Swig_mark_arg(i); + } else if (strcmp(argv[i],"-help") == 0) { + Printf(stderr,"Target Language Options:\n"); + for (int j = 0; modules[j].name; j++) { + if (modules[j].help) { + Printf(stderr," %-15s - Generate %s wrappers.\n", modules[j].name, modules[j].help); + } + } + Swig_mark_arg(i); + } + } + } + if (!dl) { + fac = Swig_find_module(SWIG_LANG); + if (fac) { + dl = (fac)(); + } + } + return SWIG_main(argc,argv,dl); +} +