From fe89f859151b5453ed72a7e66ac3d9becf2c5a08 Mon Sep 17 00:00:00 2001 From: Dave Beazley Date: Tue, 11 Apr 2000 21:23:07 +0000 Subject: [PATCH] *** empty log message *** git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@405 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Source/Swig/wrapfunc.c | 233 +++++++++++++++--------------------- 1 file changed, 94 insertions(+), 139 deletions(-) diff --git a/SWIG/Source/Swig/wrapfunc.c b/SWIG/Source/Swig/wrapfunc.c index 78590b313..bbb0da247 100644 --- a/SWIG/Source/Swig/wrapfunc.c +++ b/SWIG/Source/Swig/wrapfunc.c @@ -18,24 +18,6 @@ static char cvsroot[] = "$Header$"; #include "swig.h" -/* ----------------------------------------------------------------------------- - * isolate the type name. This is a hack (sorry). - * ----------------------------------------------------------------------------- */ - -static char type_ext[512]; -static char *isolate_type_name(char *tname) { - static char s[512]; - char *c; - c = s; - while ((*tname) && (isalnum(*tname) || (*tname == '_') || (*tname == '$') || (*tname == ' ') || (*tname == ':'))) { - *c = *tname; - tname++; - c++; - } - strcpy(type_exit,tname); - return s; -} - /* ----------------------------------------------------------------------------- * NewSwigWrapper() * @@ -46,11 +28,11 @@ SwigWrapper * NewSwigWrapper() { SwigWrapper *w; w = (SwigWrapper *) malloc(sizeof(SwigWrapper)); - w->h = NewHash(); w->localh = NewHash(); - w->locals = NewString(); - w->code = NewString(); - w->def = NewString(); + w->locals = NewString(""); + w->code = NewString(""); + w->def = NewString(""); + return w; } /* ----------------------------------------------------------------------------- @@ -61,7 +43,6 @@ NewSwigWrapper() { void DelSwigWrapper(SwigWrapper *w) { - Delete(w->h); Delete(w->localh); Delete(w->locals); Delete(w->code); @@ -72,140 +53,114 @@ DelSwigWrapper(SwigWrapper *w) { /* ----------------------------------------------------------------------------- * SwigWrapper_print() * - * Print out a wrapper function. + * Print out a wrapper function. Does pretty printing as well. * ----------------------------------------------------------------------------- */ void SwigWrapper_print(SwigWrapper *w, DOHFile *f) { - DOHString *s, *key, *c; + DOHString *str, *ts; + int level = 0; + int c, i; + int empty = 1; - key = Firstkey(w->localh); - while (key) { - s = Getattr(w->localh,key); - c = Copy(s); - Delitem(c,DOH_END); - Append(c,";\n"); - Printf(w->locals," %s;\n", c); - Delete(c); - key = Nextkey(w->localh); + str = NewString(""); + ts = NewString(""); + Printf(str,"%s\n", w->def); + Printf(str,"%s\n", w->locals); + Printf(str,"%s\n", w->code); + + Seek(str,0, SEEK_SET); + Clear(ts); + while ((c = Getc(str)) != EOF) { + + if (c == '{') { + Putc(c,ts); + Putc('\n',ts); + for (i = 0; i < level; i++) + Putc(' ',f); + Printf(f,"%s", ts); + Clear(ts); + level+=4; + while ((c = Getc(str)) != EOF) { + if (!isspace(c)) { + Ungetc(c,str); + break; + } + } + } else if (c == '}') { + if (!empty) { + Putc('\n',ts); + for (i = 0; i < level; i++) + Putc(' ',f); + Printf(f,"%s",ts); + Clear(ts); + } + level-=4; + Putc(c,ts); + } else if (c == '\n') { + Putc(c,ts); + for (i = 0; i < level; i++) + Putc(' ',f); + Printf(f,"%s",ts); + Clear(ts); + empty = 1; + } else { + Putc(c,ts); + empty = 0; + } } - Printf(f,"%s\n", w->def); - Printf(f,"%s", w->locals); - Printf(f,"%s\n", w->code); + Delete(ts); + Delete(str); } /* ----------------------------------------------------------------------------- * SwigWrapper_add_local() * - * Add a new local variable (accounts for duplicates) + * Adds a new local variable declaration to a function. Returns -1 if already + * present (which may or may not be okay to the caller). * ----------------------------------------------------------------------------- */ -void -SwigWrapper_add_local(SwigWrapper *w, char *type, char *name, char *defarg) { - DOHString *stored_type, *lstr; - char *new_type; - char temp[256],*c,*t; - char *tname; - - new_type = copy_string(type); - - /* Figure out what the name of this variable is */ - - c = name; - t = temp; - while ((isalnum(*c) || (*c == '_') || (*c == '$')) && (*c)) { - *(t++) = *c; - c++; - } - *t = 0; - if (Getattr(w->h,temp)) { - /* Check to see if a type mismatch has occurred */ - stored_type = Getattr(w->h,temp); - if (Cmp(type,stored_type) != 0) { - fprintf(stderr,"Error. Type %s conflicts with previously declared type of %s\n", - type, stored_type); - return; - } - } else { - Setattr(w->h,temp,new_type); +int +SwigWrapper_add_local(SwigWrapper *w, DOHString_or_char *decl, DOHString_or_char *name) { + /* See if the local has already been declared */ + if (Getattr(w->localh,name)) { + return -1; } + Setattr(w->localh,name,decl); + Printf(w->locals,"%s;\n", decl); +} - /* See if any wrappers have been generated with this type */ +/* ----------------------------------------------------------------------------- + * SwigWrapper_check_local() + * + * Check to see if a local name has already been declared + * ----------------------------------------------------------------------------- */ - tname = isolate_type_name(type); - lstr = Getattr(w->localh, tname); - if (!lstr) { - lstr = NewStringf("%s ",tname); - Setattr(w->localh, tname, lstr); +int +SwigWrapper_check_local(SwigWrapper *w, DOHString_or_char *name) { + if (Getattr(w->localh,name)) { + return 1; } + return 0; +} + +#ifdef TEST +int main() { + SwigWrapper *w; + w = NewSwigWrapper(); + Printf(w->def,"int foo_wrap(ClientData clientdata, Tcl_Interp *interp, int argc, char *argv[]) {"); + SwigWrapper_add_local(w,"int a", "a"); + SwigWrapper_add_local(w,"int a", "a"); + SwigWrapper_add_local(w,"int b", "b"); + SwigWrapper_add_local(w,"char temp[256]","temp"); - /* Successful, write some wrapper code */ - if (!defarg) { - Printf(lstr,"%s%s,", type_ext,name); - } else { - Printf(lstr,"%s%s = %s,", type_ext,name,defarg); - } - Delete(lstr); + Printf(w->code,"for (i = 0; i < 10; i++) { printf(\"%%d\", i); }\n"); + + Printf(w->code,"if (1) { foo;\n} else { bar; \n}\n"); + + Printf(w->code,"}\n"); + SwigWrapper_print(w,stdout); + DelSwigWrapper(w); } - -#ifdef OLD - -// ------------------------------------------------------------------- -// char *WrapperFunction::new_local(char *type, char *name, char *defarg) { -// -// A safe way to add a new local variable. type and name are used as -// a starting point, but a new local variable will be created if these -// are already in use. -// ------------------------------------------------------------------- - -char *WrapperFunction::new_local(char *type, char *name, char *defarg) { - char *new_type; - static String new_name; - char *c; - new_type = new char[strlen(type)+1]; - - strcpy(new_type,type); - new_name = ""; - c = name; - for (c = name; ((isalnum(*c) || (*c == '_') || (*c == '$')) && (*c)); c++) - new_name << *c; - - // Try to add a new local variable - if (h.add(new_name,new_type,WrapperFunction::del_type) == -1) { - // Local variable already exists, try to generate a new name - int i = 0; - new_name = ""; - // This is a little funky. We copy characters until we reach a nonvalid - // identifier symbol, add a number, then append the rest. This is - // needed to properly handle arrays. - c = name; - for (c = name; ((isalnum(*c) || (*c == '_') || (*c == '$')) && (*c)); c++) - new_name << *c; - new_name << i; - while (h.add(new_name,new_type,WrapperFunction::del_type) == -1) { - i++; - c = name; - new_name = ""; - for (c = name; ((isalnum(*c) || (*c == '_') || (*c == '$')) && (*c)); c++) - new_name << *c; - new_name << i; - } - } - new_name << c; - // Successful, write some wrapper code - if (!defarg) - locals << tab4 << type << " " << new_name << ";\n"; - else - locals << tab4 << type << " " << new_name << " = " << defarg << ";\n"; - - // Need to strip off the array symbols now - - c = new_name.get(); - while ((isalnum(*c) || (*c == '_') || (*c == '$')) && (*c)) - c++; - *c = 0; - return new_name; -} - #endif