char cvsroot_wrapfunc_c[] = "$Id: wrapfunc.c 11080 2009-01-24 13:15:51Z bhy $";
#include "swig.h"
#include <ctype.h>
static int Compact_mode = 0;
static int Max_line_size = 128;
Wrapper *NewWrapper(void) {
Wrapper *w;
w = (Wrapper *) malloc(sizeof(Wrapper));
w->localh = NewHash();
w->locals = NewStringEmpty();
w->code = NewStringEmpty();
w->def = NewStringEmpty();
return w;
}
void DelWrapper(Wrapper *w) {
Delete(w->localh);
Delete(w->locals);
Delete(w->code);
Delete(w->def);
free(w);
}
void Wrapper_compact_print_mode_set(int flag) {
Compact_mode = flag;
}
void Wrapper_pretty_print(String *str, File *f) {
String *ts;
int level = 0;
int c, i;
int empty = 1;
int indent = 2;
int plevel = 0;
int label = 0;
ts = NewStringEmpty();
Seek(str, 0, SEEK_SET);
while ((c = Getc(str)) != EOF) {
if (c == '\"') {
Putc(c, ts);
while ((c = Getc(str)) != EOF) {
if (c == '\\') {
Putc(c, ts);
c = Getc(str);
}
Putc(c, ts);
if (c == '\"')
break;
}
empty = 0;
} else if (c == '\'') {
Putc(c, ts);
while ((c = Getc(str)) != EOF) {
if (c == '\\') {
Putc(c, ts);
c = Getc(str);
}
Putc(c, ts);
if (c == '\'')
break;
}
empty = 0;
} else if (c == ':') {
Putc(c, ts);
if ((c = Getc(str)) == '\n') {
if (!empty && !strchr(Char(ts), '?'))
label = 1;
}
Ungetc(c, str);
} else if (c == '(') {
Putc(c, ts);
plevel += indent;
empty = 0;
} else if (c == ')') {
Putc(c, ts);
plevel -= indent;
empty = 0;
} else if (c == '{') {
Putc(c, ts);
Putc('\n', ts);
for (i = 0; i < level; i++)
Putc(' ', f);
Printf(f, "%s", ts);
Clear(ts);
level += indent;
while ((c = Getc(str)) != EOF) {
if (!isspace(c)) {
Ungetc(c, str);
break;
}
}
empty = 0;
} else if (c == '}') {
if (!empty) {
Putc('\n', ts);
for (i = 0; i < level; i++)
Putc(' ', f);
Printf(f, "%s", ts);
Clear(ts);
}
level -= indent;
Putc(c, ts);
empty = 0;
} else if (c == '\n') {
Putc(c, ts);
empty = 0;
if (!empty) {
int slevel = level;
if (label && (slevel >= indent))
slevel -= indent;
if ((Char(ts))[0] != '#') {
for (i = 0; i < slevel; i++)
Putc(' ', f);
}
Printf(f, "%s", ts);
for (i = 0; i < plevel; i++)
Putc(' ', f);
}
Clear(ts);
label = 0;
empty = 1;
} else if (c == '/') {
empty = 0;
Putc(c, ts);
c = Getc(str);
if (c != EOF) {
Putc(c, ts);
if (c == '/') {
while ((c = Getc(str)) != EOF) {
if (c == '\n') {
Ungetc(c, str);
break;
}
Putc(c, ts);
}
} else if (c == '*') {
int endstar = 0;
while ((c = Getc(str)) != EOF) {
if (endstar && c == '/') {
Putc(c, ts);
break;
}
endstar = (c == '*');
Putc(c, ts);
if (c == '\n') {
for (i = 0; i < level; i++)
Putc(' ', ts);
}
}
}
}
} else {
if (!empty || !isspace(c)) {
Putc(c, ts);
empty = 0;
}
}
}
if (!empty)
Printf(f, "%s", ts);
Delete(ts);
Printf(f, "\n");
}
void Wrapper_compact_print(String *str, File *f) {
String *ts, *tf;
int level = 0;
int c, i;
int empty = 1;
int indent = 2;
ts = NewStringEmpty();
tf = NewStringEmpty();
Seek(str, 0, SEEK_SET);
while ((c = Getc(str)) != EOF) {
if (c == '\"') {
empty = 0;
Putc(c, ts);
while ((c = Getc(str)) != EOF) {
if (c == '\\') {
Putc(c, ts);
c = Getc(str);
}
Putc(c, ts);
if (c == '\"')
break;
}
} else if (c == '\'') {
empty = 0;
Putc(c, ts);
while ((c = Getc(str)) != EOF) {
if (c == '\\') {
Putc(c, ts);
c = Getc(str);
}
Putc(c, ts);
if (c == '\'')
break;
}
} else if (c == '{') {
empty = 0;
Putc(c, ts);
if (Len(tf) == 0) {
for (i = 0; i < level; i++)
Putc(' ', tf);
} else if ((Len(tf) + Len(ts)) < Max_line_size) {
Putc(' ', tf);
} else {
Putc('\n', tf);
Printf(f, "%s", tf);
Clear(tf);
for (i = 0; i < level; i++)
Putc(' ', tf);
}
Append(tf, ts);
Clear(ts);
level += indent;
while ((c = Getc(str)) != EOF) {
if (!isspace(c)) {
Ungetc(c, str);
break;
}
}
} else if (c == '}') {
empty = 0;
if (Len(tf) == 0) {
for (i = 0; i < level; i++)
Putc(' ', tf);
} else if ((Len(tf) + Len(ts)) < Max_line_size) {
Putc(' ', tf);
} else {
Putc('\n', tf);
Printf(f, "%s", tf);
Clear(tf);
for (i = 0; i < level; i++)
Putc(' ', tf);
}
Append(tf, ts);
Putc(c, tf);
Clear(ts);
level -= indent;
} else if (c == '\n') {
while ((c = Getc(str)) != EOF) {
if (!isspace(c))
break;
}
if (c == '#') {
Putc('\n', ts);
} else if (c == '}') {
Putc(' ', ts);
} else if ((c != EOF) || (Len(ts) != 0)) {
if (Len(tf) == 0) {
for (i = 0; i < level; i++)
Putc(' ', tf);
} else if ((Len(tf) + Len(ts)) < Max_line_size) {
Putc(' ', tf);
} else {
Putc('\n', tf);
Printf(f, "%s", tf);
Clear(tf);
for (i = 0; i < level; i++)
Putc(' ', tf);
}
Append(tf, ts);
Clear(ts);
}
Ungetc(c, str);
empty = 1;
} else if (c == '/') {
empty = 0;
c = Getc(str);
if (c != EOF) {
if (c == '/') {
while ((c = Getc(str)) != EOF) {
if (c == '\n') {
Ungetc(c, str);
break;
}
}
} else if (c == '*') {
int endstar = 0;
while ((c = Getc(str)) != EOF) {
if (endstar && c == '/') {
break;
}
endstar = (c == '*');
}
} else {
Putc('/', ts);
Putc(c, ts);
}
}
} else if (c == '#') {
Putc('#', ts);
while ((c = Getc(str)) != EOF) {
Putc(c, ts);
if (c == '\\') {
c = Getc(str);
if (c == '\n')
Putc(c, ts);
else
Ungetc(c, str);
} else if (c == '\n')
break;
}
if (!empty) {
Append(tf, "\n");
}
Append(tf, ts);
Printf(f, "%s", tf);
Clear(tf);
Clear(ts);
for (i = 0; i < level; i++)
Putc(' ', tf);
empty = 1;
} else {
if (!empty || !isspace(c)) {
Putc(c, ts);
empty = 0;
}
}
}
if (!empty) {
Append(tf, ts);
}
if (Len(tf) != 0)
Printf(f, "%s", tf);
Delete(ts);
Delete(tf);
Printf(f, "\n");
}
void Wrapper_print(Wrapper *w, File *f) {
String *str;
str = NewStringEmpty();
Printf(str, "%s\n", w->def);
Printf(str, "%s\n", w->locals);
Printf(str, "%s\n", w->code);
if (Compact_mode == 1)
Wrapper_compact_print(str, f);
else
Wrapper_pretty_print(str, f);
Delete(str);
}
int Wrapper_add_local(Wrapper *w, const_String_or_char_ptr name, const_String_or_char_ptr decl) {
if (Getattr(w->localh, name)) {
return -1;
}
Setattr(w->localh, name, decl);
Printf(w->locals, "%s;\n", decl);
return 0;
}
int Wrapper_add_localv(Wrapper *w, const_String_or_char_ptr name, ...) {
va_list ap;
int ret;
String *decl;
DOH *obj;
decl = NewStringEmpty();
va_start(ap, name);
obj = va_arg(ap, void *);
while (obj) {
Append(decl, obj);
Putc(' ', decl);
obj = va_arg(ap, void *);
}
va_end(ap);
ret = Wrapper_add_local(w, name, decl);
Delete(decl);
return ret;
}
int Wrapper_check_local(Wrapper *w, const_String_or_char_ptr name) {
if (Getattr(w->localh, name)) {
return 1;
}
return 0;
}
char *Wrapper_new_local(Wrapper *w, const_String_or_char_ptr name, const_String_or_char_ptr decl) {
int i;
String *nname = NewString(name);
String *ndecl = NewString(decl);
char *ret;
i = 0;
while (Wrapper_check_local(w, nname)) {
Clear(nname);
Printf(nname, "%s%d", name, i);
i++;
}
Replace(ndecl, name, nname, DOH_REPLACE_ID);
Setattr(w->localh, nname, ndecl);
Printf(w->locals, "%s;\n", ndecl);
ret = Char(nname);
Delete(nname);
Delete(ndecl);
return ret;
}
char *Wrapper_new_localv(Wrapper *w, const_String_or_char_ptr name, ...) {
va_list ap;
char *ret;
String *decl;
DOH *obj;
decl = NewStringEmpty();
va_start(ap, name);
obj = va_arg(ap, void *);
while (obj) {
Append(decl, obj);
Putc(' ', decl);
obj = va_arg(ap, void *);
}
va_end(ap);
ret = Wrapper_new_local(w, name, decl);
Delete(decl);
return ret;
}