*** empty log message ***
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@405 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
9b0c8dc542
commit
fe89f85915
1 changed files with 94 additions and 139 deletions
|
|
@ -18,24 +18,6 @@ static char cvsroot[] = "$Header$";
|
||||||
|
|
||||||
#include "swig.h"
|
#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()
|
* NewSwigWrapper()
|
||||||
*
|
*
|
||||||
|
|
@ -46,11 +28,11 @@ SwigWrapper *
|
||||||
NewSwigWrapper() {
|
NewSwigWrapper() {
|
||||||
SwigWrapper *w;
|
SwigWrapper *w;
|
||||||
w = (SwigWrapper *) malloc(sizeof(SwigWrapper));
|
w = (SwigWrapper *) malloc(sizeof(SwigWrapper));
|
||||||
w->h = NewHash();
|
|
||||||
w->localh = NewHash();
|
w->localh = NewHash();
|
||||||
w->locals = NewString();
|
w->locals = NewString("");
|
||||||
w->code = NewString();
|
w->code = NewString("");
|
||||||
w->def = NewString();
|
w->def = NewString("");
|
||||||
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
/* -----------------------------------------------------------------------------
|
||||||
|
|
@ -61,7 +43,6 @@ NewSwigWrapper() {
|
||||||
|
|
||||||
void
|
void
|
||||||
DelSwigWrapper(SwigWrapper *w) {
|
DelSwigWrapper(SwigWrapper *w) {
|
||||||
Delete(w->h);
|
|
||||||
Delete(w->localh);
|
Delete(w->localh);
|
||||||
Delete(w->locals);
|
Delete(w->locals);
|
||||||
Delete(w->code);
|
Delete(w->code);
|
||||||
|
|
@ -72,140 +53,114 @@ DelSwigWrapper(SwigWrapper *w) {
|
||||||
/* -----------------------------------------------------------------------------
|
/* -----------------------------------------------------------------------------
|
||||||
* SwigWrapper_print()
|
* SwigWrapper_print()
|
||||||
*
|
*
|
||||||
* Print out a wrapper function.
|
* Print out a wrapper function. Does pretty printing as well.
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
void
|
void
|
||||||
SwigWrapper_print(SwigWrapper *w, DOHFile *f) {
|
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);
|
str = NewString("");
|
||||||
while (key) {
|
ts = NewString("");
|
||||||
s = Getattr(w->localh,key);
|
Printf(str,"%s\n", w->def);
|
||||||
c = Copy(s);
|
Printf(str,"%s\n", w->locals);
|
||||||
Delitem(c,DOH_END);
|
Printf(str,"%s\n", w->code);
|
||||||
Append(c,";\n");
|
|
||||||
Printf(w->locals," %s;\n", c);
|
Seek(str,0, SEEK_SET);
|
||||||
Delete(c);
|
Clear(ts);
|
||||||
key = Nextkey(w->localh);
|
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);
|
Delete(ts);
|
||||||
Printf(f,"%s", w->locals);
|
Delete(str);
|
||||||
Printf(f,"%s\n", w->code);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
/* -----------------------------------------------------------------------------
|
||||||
* SwigWrapper_add_local()
|
* 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
|
int
|
||||||
SwigWrapper_add_local(SwigWrapper *w, char *type, char *name, char *defarg) {
|
SwigWrapper_add_local(SwigWrapper *w, DOHString_or_char *decl, DOHString_or_char *name) {
|
||||||
DOHString *stored_type, *lstr;
|
/* See if the local has already been declared */
|
||||||
char *new_type;
|
if (Getattr(w->localh,name)) {
|
||||||
char temp[256],*c,*t;
|
return -1;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
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);
|
int
|
||||||
lstr = Getattr(w->localh, tname);
|
SwigWrapper_check_local(SwigWrapper *w, DOHString_or_char *name) {
|
||||||
if (!lstr) {
|
if (Getattr(w->localh,name)) {
|
||||||
lstr = NewStringf("%s ",tname);
|
return 1;
|
||||||
Setattr(w->localh, tname, lstr);
|
|
||||||
}
|
}
|
||||||
|
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 */
|
Printf(w->code,"for (i = 0; i < 10; i++) { printf(\"%%d\", i); }\n");
|
||||||
if (!defarg) {
|
|
||||||
Printf(lstr,"%s%s,", type_ext,name);
|
Printf(w->code,"if (1) { foo;\n} else { bar; \n}\n");
|
||||||
} else {
|
|
||||||
Printf(lstr,"%s%s = %s,", type_ext,name,defarg);
|
Printf(w->code,"}\n");
|
||||||
}
|
SwigWrapper_print(w,stdout);
|
||||||
Delete(lstr);
|
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
|
#endif
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue