Initial revision

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@2 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 1999-08-03 22:39:23 +00:00
commit f80f527736
9 changed files with 2808 additions and 0 deletions

56
Source/DOH/Doh/Makefile Normal file
View file

@ -0,0 +1,56 @@
#######################################################################
# $Header$
# Simplified Wrapper and Interface Generator (SWIG)
#
# Makefile for version 1.0 Final
# Dave Beazley
# August 1, 1996
#
#######################################################################
#.KEEP_STATE:
# Set your C++ compiler here. g++ works on most machines,
# but you might have to change it depending on your installation.
#
CC = cc
prefix = /usr/local
# Comment out the following line if you're on an SGI or don't have ranlib!
RANLIB = ranlib
AR = ar
########################################################################
# Normally, you shouldn't have to change anything below this point #
########################################################################
LIBOBJS = base.o file.o list.o hash.o string.o
LIBSRCS = base.c file.c list.c hash.c string.c
LIBHEADERS = ../Include/doh.h
LIB = ../libdoh.a
INCLUDE = -I../Include
CFLAGS =
SHELL = /bin/sh
#
#
#
# Rules for creation of a .o file from .cxx
.SUFFIXES: .c
.c.o:
$(CC) $(INCLUDE) $(CFLAGS) -c -o $*.o $<
all: $(LIB)
$(LIB): $(LIBOBJS)
@echo "Building library"
$(AR) cr $(LIB) $(LIBOBJS)
$(RANLIB) $(LIB)
clean::
rm -f *.o ../libdoh.a
nuke::
rm -f Makefile *~ #* core a.out

View file

@ -0,0 +1,95 @@
#######################################################################
# $Header$
# Simplified Wrapper and Interface Generator (SWIG)
#
# Makefile for version 1.0 Final
# Dave Beazley
# August 1, 1996
#
#######################################################################
#.KEEP_STATE:
srcdir = @srcdir@
VPATH = @srcdir@
# Set your C++ compiler here. g++ works on most machines,
# but you might have to change it depending on your installation.
#
CC = @CC@
#
# Set the prefix below to indicate where you want SWIG to install its
# files. Normally this is /usr/local
#
prefix = @prefix@
# Location of the SWIG library. Is normally put in /usr/local/lib/swig_lib
# The SWIG library contains configuration files and library modules
# so you should install it someplace where it can be easily accessed.
SWIG_LIB = $(prefix)/lib/@SWIGLIBDIR@
# YACC parser. Use bison by default. if this doesn't work, switch
# it over to yacc. If that still doesn't work, let me know...
YACC = @YACC@
# Comment out the following line if you're on an SGI or don't have ranlib!
RANLIB = @RANLIB@
AR = @AR@
########################################################################
# Normally, you shouldn't have to change anything below this point #
########################################################################
LIBOBJS = scanner.o parms.o types.o error.o module.o getopt.o file.o string.o list.o hash.o base.o main.o
LIBSRCS = scanner.c parms.c types.c error.c module.c getopt.c file.c string.c list.c hash.c base.c main.c
LIBHEADERS = ../Include/swig.h
LIB = ../libswig.a
PARSER = parser.y
INCLUDE = -I../Include
CFLAGS = @CFLAGS@ -DSWIG_LIB='"$(SWIG_LIB)"' -DSWIG_CC='"$(CC)"' @DEFS@
SHELL = /bin/sh
#
#
#
# Rules for creation of a .o file from .cxx
.SUFFIXES: .c
.c.o:
$(CC) $(INCLUDE) $(CFLAGS) -c -o $*.o $<
all:
@cd ..; $(MAKE)
build: $(LIB)
#$(CC) $(LIBOBJS) -o swig -ldl
py: $(LIB)
/usr/local/bin/swig -python -o swig_wrap.c ../Include/swig.h
$(CC) $(INCLUDE) -I/usr/local/include/python1.5 $(CFLAGS) -c swig_wrap.c
ld -G $(LIBOBJS) swig_wrap.o -o swigmodule.so
$(LIB): $(LIBOBJS)
@echo "Building library"
$(AR) cr $(LIB) $(LIBOBJS)
$(RANLIB) $(LIB)
Makefile: $(srcdir)/Makefile.in ../config.status
(cd ..; CONFIG_FILES=SWIG/Makefile $(SHELL) config.status)
.PRECIOUS: Makefile
clean::
rm -f *.o libswig.a y.tab.c y.tab.h
nuke::
rm -f Makefile *~ #* core a.out

623
Source/DOH/Doh/base.c Normal file
View file

@ -0,0 +1,623 @@
/****************************************************************************
* DOH (Dynamic Object Hack)
*
* 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.
****************************************************************************/
/*******************************************************************************
* $Header$
*
* File : base.c
*
* Methods for base objects
*******************************************************************************/
#include "doh.h"
static DohObjInfo DohBaseType = {
"Base", /* objname */
0, /* objsize */
0, /* doh_del */
0, /* doh_copy */
0, /* doh_clear */
0, /* doh_str */
0, /* doh_aschar */
0, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_mapping */
0, /* doh_sequence */
0, /* doh_file */
0, /* reserved2 */
0, /* reserved3 */
0, /* reserved4 */
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
int DohCheck(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (!b) return 0;
if (b->magic != DOH_MAGIC) return 0;
if (!b->objinfo) return 0;
return 1;
}
int DohIsMapping(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (!DohCheck(b)) return 0;
if (b->objinfo->doh_mapping) return 1;
else return 0;
}
int DohIsSequence(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (!DohCheck(b)) return 0;
if (b->objinfo->doh_sequence) return 1;
else return 0;
}
/* -----------------------------------------------------------------------------
String objects
The following structure maps raw char * entries to string objects.
----------------------------------------------------------------------------- */
typedef struct StringNode {
char *cstr;
DOH *sstr;
struct StringNode *left;
struct StringNode *right;
} StringNode;
static StringNode *root = 0;
static DOH *find_internal(DOH *co) {
StringNode *r, *s;
int d;
char *c;
if (DohCheck(co)) return co;
c = (char *) co;
r = root;
s = 0;
while (r) {
s = r;
/* printf("checking %s\n", r->cstr);*/
d = strcmp(r->cstr,c);
if (d == 0) return r->sstr;
if (d < 0) r = r->left;
else r = r->right;
}
r = (StringNode *) malloc(sizeof(StringNode));
r->cstr = (char *) malloc(strlen(c)+1);
strcpy(r->cstr,c);
r->sstr = NewString(c);
Incref(r->sstr);
r->left = 0;
r->right = 0;
if (!s) { root = r; }
else {
if (d < 0) s->left = r;
else s->right = r;
}
return r->sstr;
}
/* Destroy an object */
void DohDestroy(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (!DohCheck(b)) return;
b->refcount--;
if (b->refcount <= 0) {
if (b->objinfo->doh_del) {
(b->objinfo->doh_del)(obj);
return;
}
free(b);
}
}
/* Copy an object */
DOH *DohCopy(DOH *obj) {
DOH *result;
DohBase *b = (DohBase *) obj;
if (!DohCheck(b)) return 0;
if (b->objinfo->doh_copy) {
return (b->objinfo->doh_copy)(obj);
}
printf("No copy method defined for type '%s'\n", b->objinfo->objname);
return 0;
}
void DohClear(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (!DohCheck(b)) return;
if (b->objinfo->doh_clear) {
(b->objinfo->doh_clear)(obj);
return;
}
printf("No clear method defined for type '%s'\n", b->objinfo->objname);
}
/* Turn an object into a string */
DOH *DohStr(DOH *obj) {
DOH *s;
DohBase *b = (DohBase *) obj;
if (DohCheck(b)) {
if (b->objinfo->doh_str) {
return (b->objinfo->doh_str)(b);
}
s = NewString("<Object ");
Appendf(s,"'%s' at %x>", b->objinfo->objname, b);
return s;
} else {
return NewString(obj);
}
}
/* Get the length */
int DohLen(DOH *obj) {
int s;
DohBase *b = (DohBase *) obj;
if (DohCheck(b)) {
if (b->objinfo->doh_len) {
return (b->objinfo->doh_len)(obj);
}
}
printf("No len method defined for type '%s'\n", b->objinfo->objname);
return 0;
}
/* Get the hash value */
int DohHashval(DOH *obj) {
int s;
DohBase *b = (DohBase *) obj;
if (DohCheck(b)) {
if (b->objinfo->doh_hash) {
return (b->objinfo->doh_hash)(obj);
}
printf("No hash method defined for type '%s'\n", b->objinfo->objname);
}
return 0;
}
/* Get raw data */
void *DohData(DOH *obj) {
char *c;
char *s;
DohBase *b = (DohBase *) obj;
c = (char *) obj;
if (!c) return 0;
if (*c == DOH_MAGIC) {
if (b->objinfo) {
if (b->objinfo->doh_data) {
return (b->objinfo->doh_data)(obj);
}
}
printf("No data method defined for type '%s'\n", b->objinfo->objname);
return 0;
}
return c;
}
/* Get the line number */
int DohGetline(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (DohCheck(obj)) {
return b->line;
}
return 0;
}
/* Set the line number */
void DohSetline(DOH *obj, int line) {
DohBase *b = (DohBase *) obj;
if (DohCheck(obj)) {
b->line = line;
}
}
/* Get the file name */
DOH *DohGetfile(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (DohCheck(obj)) {
return b->file;
}
return 0;
}
/* Set the file */
void DohSetfile(DOH *obj, DOH *file) {
DOH *nf;
DohBase *b = (DohBase *) obj;
if (DohCheck(obj)) {
if (file) {
nf = find_internal(file);
Incref(nf);
if (b->file) Delete(b->file);
b->file = nf;
} else {
Delete(b->file);
b->file = 0;
}
}
}
/* Get an attribute from an object */
int DohCmp(DOH *obj1, DOH *obj2) {
int s;
DohBase *b1, *b2;
b1 = (DohBase *) obj1;
b2 = (DohBase *) obj2;
if (!DohCheck(b1)) {
b1 = find_internal(b1);
}
if (!DohCheck(b2)) {
b2 = find_internal(b2);
}
if (b1->objinfo == b2->objinfo) {
if (b1->objinfo->doh_cmp) {
return (b1->objinfo->doh_cmp)(b1,b2);
}
printf("No cmp method defined for type '%s'\n", b1->objinfo->objname);
}
printf("Can't compare type '%s' with type '%s'\n", b1->objinfo->objname, b2->objinfo->objname);
return 0;
}
/* ----------------------------------------------------------------------
* Mapping Interface
* ---------------------------------------------------------------------- */
/* Get an attribute from an object */
DOH *DohGetattr(DOH *obj, DOH *name) {
DOH *s;
DOH *name_obj;
DohBase *b = (DohBase *) obj;
if (DohIsMapping(b)) {
name_obj = find_internal(name);
if (b->objinfo->doh_mapping->doh_getattr) {
return (b->objinfo->doh_mapping->doh_getattr)(obj,name_obj);
}
return 0;
}
return 0;
}
/* Set an attribute in an object */
int DohSetattr(DOH *obj, DOH *name, DOH *value) {
int s;
DOH *name_obj, *value_obj;
DohBase *b = (DohBase *) obj;
if (DohIsMapping(b)) {
name_obj = find_internal(name);
value_obj = find_internal(value);
if (b->objinfo->doh_mapping->doh_setattr) {
return (b->objinfo->doh_mapping->doh_setattr)(obj,name_obj,value_obj);
}
printf("No setattr method defined for type '%s'\n", b->objinfo->objname);
}
return 0;
}
/* Delete an attribute from an object */
void DohDelattr(DOH *obj, DOH *name) {
DOH *name_obj;
DohBase *b = (DohBase *) obj;
if (DohIsMapping(obj)) {
name_obj = find_internal(name);
if (b->objinfo->doh_mapping->doh_delattr) {
(b->objinfo->doh_mapping->doh_delattr)(obj,name_obj);
return;
}
printf("No delattr method defined for type '%s'\n", b->objinfo->objname);
}
}
/* Get first item in an object */
DOH *DohFirst(DOH *obj) {
DOH *s;
DohBase *b = (DohBase *) obj;
if (DohIsMapping(obj)) {
if (b->objinfo->doh_mapping->doh_firstkey) {
return DohGetattr(obj,(b->objinfo->doh_mapping->doh_firstkey)(obj));
}
printf("No firstkey method defined for type '%s'\n", b->objinfo->objname);
return 0;
}
return 0;
}
/* Get next item in an object */
DOH *DohNext(DOH *obj) {
DOH *s;
DohBase *b = (DohBase *) obj;
if (DohIsMapping(obj)) {
if (b->objinfo->doh_mapping->doh_nextkey) {
return DohGetattr(obj,(b->objinfo->doh_mapping->doh_nextkey)(obj));
}
printf("No nextkey method defined for type '%s'\n", b->objinfo->objname);
return 0;
}
return 0;
}
/* Get first item in an object */
DOH *DohFirstkey(DOH *obj) {
DOH *s;
DohBase *b = (DohBase *) obj;
if (DohIsMapping(obj)) {
if (b->objinfo->doh_mapping->doh_firstkey) {
return (b->objinfo->doh_mapping->doh_firstkey)(obj);
}
printf("No firstkey method defined for type '%s'\n", b->objinfo->objname);
return 0;
}
return 0;
}
/* Get next item in an object */
DOH *DohNextkey(DOH *obj) {
DOH *s;
DohBase *b = (DohBase *) obj;
if (DohIsMapping(obj)) {
if (b->objinfo->doh_mapping->doh_nextkey) {
return (b->objinfo->doh_mapping->doh_nextkey)(obj);
}
printf("No nextkey method defined for type '%s'\n", b->objinfo->objname);
return 0;
}
return 0;
}
/* ----------------------------------------------------------------------
* Sequence Interface
* ----------------------------------------------------------------------
/* Get an item from an object */
DOH *DohGetitem(DOH *obj, int index) {
DOH *s;
DohBase *b = (DohBase *) obj;
if (DohIsSequence(obj)) {
if (b->objinfo->doh_sequence->doh_getitem) {
return (b->objinfo->doh_sequence->doh_getitem)(obj,index);
}
printf("No getitem method defined for type '%s'\n", b->objinfo->objname);
}
return 0;
}
/* Set an item in an object */
void DohSetitem(DOH *obj, int index, DOH *value) {
DOH *value_obj;
DohBase *b = (DohBase *) obj;
if (DohIsSequence(obj)) {
value_obj = find_internal(value);
if (b->objinfo->doh_sequence->doh_setitem) {
(b->objinfo->doh_sequence->doh_setitem)(obj,index,value_obj);
return;
}
printf("No setitem method defined for type '%s'\n", b->objinfo->objname);
}
}
/* Delete an item from an object */
void DohDelitem(DOH *obj, int index) {
DohBase *b = (DohBase *) obj;
if (DohIsSequence(obj)) {
if (b->objinfo->doh_sequence->doh_delitem) {
(b->objinfo->doh_sequence->doh_delitem)(obj,index);
return;
}
printf("No delitem method defined for type '%s'\n", b->objinfo->objname);
}
}
/* Set an item in an object */
void DohInsertitem(DOH *obj, int index, DOH *value) {
DOH *value_obj;
DohBase *b = (DohBase *) obj;
if (DohIsSequence(obj)) {
value_obj = find_internal(value);
if (b->objinfo->doh_sequence->doh_insitem) {
(b->objinfo->doh_sequence->doh_insitem)(obj,index,value_obj);
return;
}
printf("No insitem method defined for type '%s'\n", b->objinfo->objname);
}
}
/* Set an item in an object */
void DohInsertitemf(DOH *obj, int index, char *format, ...) {
va_list ap;
DohBase *b = (DohBase *) obj;
if (DohIsSequence(obj)) {
if (b->objinfo->doh_sequence->doh_insertf) {
va_start(ap,format);
(b->objinfo->doh_sequence->doh_insertf)(obj,index,format,ap);
va_end(ap);
return;
}
printf("No insertf method defined for type '%s'\n", b->objinfo->objname);
}
}
/* Set an item in an object */
void DohvInsertitemf(DOH *obj, int index, char *format, va_list ap) {
DohBase *b = (DohBase *) obj;
if (DohIsSequence(obj)) {
if (b->objinfo->doh_sequence->doh_insertf) {
(b->objinfo->doh_sequence->doh_insertf)(obj,index,format,ap);
return;
}
printf("No insertf method defined for type '%s'\n", b->objinfo->objname);
}
}
/* Append an item to an object */
void DohAppendf(DOH *obj, char *format, ...) {
va_list ap;
DohBase *b = (DohBase *) obj;
if (DohIsSequence(obj)) {
if (b->objinfo->doh_sequence->doh_insertf) {
va_start(ap,format);
(b->objinfo->doh_sequence->doh_insertf)(obj,DOH_END,format,ap);
va_end(ap);
return;
}
printf("No appendf method defined for type '%s'\n", b->objinfo->objname);
}
}
/* Append an item to an object */
void DohvAppendf(DOH *obj, char *format, va_list ap) {
DohBase *b = (DohBase *) obj;
if (DohIsSequence(obj)) {
if (b->objinfo->doh_sequence->doh_insertf) {
(b->objinfo->doh_sequence->doh_insertf)(obj,DOH_END,format,ap);
return;
}
printf("No appendf method defined for type '%s'\n", b->objinfo->objname);
}
}
/* -----------------------------------------------------------------------------
* File methods
* ----------------------------------------------------------------------------- */
int DohIsFile(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (!DohCheck(b)) return 0;
if (b->objinfo->doh_file) return 1;
else return 0;
}
/* Read */
int DohRead(DOH *obj, void *buffer, int length) {
DohBase *b = (DohBase *) obj;
if (DohIsFile(obj)) {
if (b->objinfo->doh_file->doh_read) {
return (b->objinfo->doh_file->doh_read)(obj,buffer,length);
}
printf("No read method defined for type '%s'\n", b->objinfo->objname);
}
return -1;
}
/* Write */
int DohWrite(DOH *obj, void *buffer, int length) {
DohBase *b = (DohBase *) obj;
if (DohIsFile(obj)) {
if (b->objinfo->doh_file->doh_write) {
return (b->objinfo->doh_file->doh_write)(obj,buffer,length);
}
printf("No write method defined for type '%s'\n", b->objinfo->objname);
}
return -1;
}
/* Seek */
int DohSeek(DOH *obj, long offset, int whence) {
DohBase *b = (DohBase *) obj;
if (DohIsFile(obj)) {
if (b->objinfo->doh_file->doh_seek) {
return (b->objinfo->doh_file->doh_seek)(obj,offset,whence);
}
printf("No seek method defined for type '%s'\n", b->objinfo->objname);
}
return -1;
}
/* Tell */
long DohTell(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (DohIsFile(obj)) {
if (b->objinfo->doh_file->doh_tell) {
return (b->objinfo->doh_file->doh_tell)(obj);
}
printf("No tell method defined for type '%s'\n", b->objinfo->objname);
}
return -1;
}
/* Printf */
int DohPrintf(DOH *obj, char *format, ...) {
va_list ap;
int ret;
DohBase *b = (DohBase *) obj;
if (DohIsFile(obj)) {
if (b->objinfo->doh_file->doh_printf) {
va_start(ap,format);
ret = (b->objinfo->doh_file->doh_printf)(obj,format,ap);
va_end(ap);
return ret;
}
printf("No printf method defined for type '%s'\n", b->objinfo->objname);
}
return -1;
}
/* vPrintf */
int DohvPrintf(DOH *obj, char *format, va_list ap) {
DohBase *b = (DohBase *) obj;
if (DohIsFile(obj)) {
if (b->objinfo->doh_file->doh_printf) {
return (b->objinfo->doh_file->doh_printf)(obj,format,ap);
}
printf("No printf method defined for type '%s'\n", b->objinfo->objname);
}
return -1;
}
/* Printf */
int DohScanf(DOH *obj, char *format, ...) {
va_list ap;
int ret;
DohBase *b = (DohBase *) obj;
if (DohIsFile(obj)) {
if (b->objinfo->doh_file->doh_scanf) {
va_start(ap,format);
ret = (b->objinfo->doh_file->doh_scanf)(obj,format,ap);
va_end(ap);
return ret;
}
printf("No scanf method defined for type '%s'\n", b->objinfo->objname);
}
return -1;
}
/* vPrintf */
int DohvScanf(DOH *obj, char *format, va_list ap) {
DohBase *b = (DohBase *) obj;
if (DohIsFile(obj)) {
if (b->objinfo->doh_file->doh_scanf) {
return (b->objinfo->doh_file->doh_scanf)(obj,format,ap);
}
printf("No scanf method defined for type '%s'\n", b->objinfo->objname);
}
return -1;
}
void DohInit(DOH *b) {
DohBase *bs = (DohBase *) b;
bs->refcount =0;
bs->objinfo = &DohBaseType;
bs->magic = DOH_MAGIC;
bs->moremagic[0] = 0;
bs->moremagic[1] = 0;
bs->moremagic[2] = 0;
bs->line = 0;
bs->file = 0;
}

194
Source/DOH/Doh/file.c Normal file
View file

@ -0,0 +1,194 @@
/****************************************************************************
* DOH (Dynamic Object Hack)
*
* 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.
****************************************************************************/
#include "doh.h"
/* ---------------------------------------------------------------------------
* $Header$
* string.c
*
* String support.
* --------------------------------------------------------------------------- */
typedef struct File {
DOHCOMMON;
FILE *filep;
} File;
/* Forward references */
void DelFile(DOH *s);
int File_read(DOH *s, void *buffer, int length);
int File_write(DOH *s, void *buffer, int length);
int File_seek(DOH *s, long offset, int whence);
long File_tell(DOH *s);
int File_printf(DOH *s, char *fmt, va_list ap);
static DohFileMethods FileFileMethods = {
File_read,
File_write,
File_seek,
File_tell,
File_printf,
};
static DohObjInfo FileType = {
"File", /* objname */
0,
DelFile, /* sm_del */
0, /* sm_copy */
0, /* sm_clear */
0, /* sm_str */
0, /* doh_data */
0, /* sm_len */
0, /* sm_hash */
0, /* doh_cmp */
0, /* doh_mapping */
0, /* doh_sequence */
&FileFileMethods,/* doh_file */
0,
0,
0,
0,
};
DohObjInfo *File_type() {
return &FileType;
}
/* -----------------------------------------------------------------------------
* NewFile(char *filename, char *mode)
* ----------------------------------------------------------------------------- */
DOH *
NewFile(char *filename, char *mode)
{
File *f;
FILE *file;
file = fopen(filename,mode);
if (!file) return 0;
f = (File *) malloc(sizeof(File));
DohInit(f);
f->objinfo = &FileType;
f->filep = file;
return (DOH *) f;
}
/* -----------------------------------------------------------------------------
* NewFileFromFile(FILE *f)
*
* ----------------------------------------------------------------------------- */
DOH *
NewFileFromFile(FILE *file)
{
File *f;
f = (File *) malloc(sizeof(File));
DohInit(f);
f->objinfo = &FileType;
f->filep = file;
return (DOH *) f;
}
/* -----------------------------------------------------------------------------
* DelFile(DOH *s) - Delete a file
* ----------------------------------------------------------------------------- */
void
DelFile(DOH *so) {
File *f = (File *) so;
assert(f->refcount <= 0);
fclose(f->filep);
free(f);
}
/* -----------------------------------------------------------------------------
* int File_check(DOH *f) - Check if f is a file
* ----------------------------------------------------------------------------- */
int
File_check(DOH *f)
{
return 0;
}
/* -----------------------------------------------------------------------------
* int File_read(DOH *so, void *buffer, int len)
*
* Read data from the File
* ----------------------------------------------------------------------------- */
int
File_read(DOH *so, void *buffer, int len) {
File *s = (File *) so;
return (size_t) fread(buffer,len,1,s->filep);
}
/* -----------------------------------------------------------------------------
* int File_write(DOH *so, void *buffer, int len)
*
* Write data to the File
* ----------------------------------------------------------------------------- */
int
File_write(DOH *so, void *buffer, int len) {
File *s = (File *) so;
return (size_t) fwrite(buffer,len,1,s->filep);
}
/* -----------------------------------------------------------------------------
* int File_seek(DOH *so, long offset, int whence)
*
* Seek to a new position
* ----------------------------------------------------------------------------- */
int
File_seek(DOH *so, long offset, int whence) {
File *s = (File *) so;
return fseek(s->filep,offset,whence);
}
/* -----------------------------------------------------------------------------
* long File_tell(DOH *so)
*
* Return current position
* ----------------------------------------------------------------------------- */
long
File_tell(DOH *so) {
File *s = (File *) so;
return ftell(s->filep);
}
/* -----------------------------------------------------------------------------
* int File_printf(DOH *so, char *format, ...)
*
* ----------------------------------------------------------------------------- */
int
File_printf(DOH *so, char *format, va_list ap)
{
int len, wlen;
DOH *nso;
File *s = (File *) so;
nso = NewString("");
vAppendf(nso,format,ap);
len = Len(nso);
wlen = File_write(so,Data(nso),len);
Delete(nso);
return wlen;
}

492
Source/DOH/Doh/hash.c Normal file
View file

@ -0,0 +1,492 @@
/****************************************************************************
* Simplified Wrapper and Interface Generator (SWIG)
*
* 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.
****************************************************************************/
#include "doh.h"
/***********************************************************************
* $Header$
*
* hash.c
*
* Hash table implementation.
***********************************************************************/
/* Hash node */
typedef struct HashNode {
DOH *key;
DOH *object;
struct HashNode *next;
} HashNode;
/* Hash object */
typedef struct Hash {
DOHCOMMON;
HashNode **hashtable;
int hashsize;
int currentindex;
int nitems;
HashNode *current;
} Hash;
/* Forward references */
DOH *CopyHash(DOH *h);
void DelHash(DOH *h);
void Hash_clear(DOH *);
int Hash_setattr(DOH *, DOH *k, DOH *obj);
DOH *Hash_getattr(DOH *h, DOH *k);
int Hash_delattr(DOH *h, DOH *k);
DOH *Hash_firstkey(DOH *h);
DOH *Hash_nextkey(DOH *h);
DOH *Hash_str(DOH *h);
int Hash_len(DOH *h);
#define HASH_INIT_SIZE 7
static HashNode *NewNode(DOH *k, void *obj)
{
HashNode *hn = (HashNode *) malloc(sizeof(HashNode));
hn->key = k;
Incref(hn->key);
hn->object = obj;
Incref(obj);
hn->next = 0;
return hn;
}
static void DelNode(HashNode *hn)
{
Delete(hn->key);
Delete(hn->object);
free(hn);
}
static DohMappingMethods HashMappingMethods = {
Hash_getattr,
Hash_setattr,
Hash_delattr,
Hash_firstkey,
Hash_nextkey,
};
static DohObjInfo HashType = {
"Hash", /* objname */
0,
DelHash, /* sm_del */
CopyHash, /* sm_copy */
Hash_clear, /* sm_clear */
Hash_str, /* sm_str */
0, /* doh_data */
Hash_len, /* sm_len */
0, /* sm_hash */
0, /* doh_cmp */
&HashMappingMethods, /* doh_mapping */
0, /* doh_sequence */
0,
0,
0,
0,
0,
};
DohObjInfo *Hash_type() {
return &HashType;
}
int Hash_check(DOH *so) {
Hash *h = (Hash *) so;
if (!h) return 0;
if (h->magic != DOH_MAGIC) return 0;
if (h->objinfo != &HashType) return 0;
return 1;
}
/* -----------------------------------------------------------------------------
* NewHash() - Create a new hash table
* ----------------------------------------------------------------------------- */
DOH *NewHash() {
Hash *h;
int i;
h = (Hash *) malloc(sizeof(Hash));
DohInit(h);
h->hashsize = HASH_INIT_SIZE;
h->hashtable = (HashNode **) malloc(h->hashsize*sizeof(HashNode *));
for (i = 0; i < h->hashsize; i++) {
h->hashtable[i] = 0;
}
h->currentindex = -1;
h->current = 0;
h->nitems = 0;
h->objinfo = &HashType;
return (DOH *) h;
}
/* -----------------------------------------------------------------------------
* DOH *CopyHash(DOH *ho) - Copy a hash table
* ----------------------------------------------------------------------------- */
DOH *CopyHash(DOH *ho) {
Hash *h, *nh;
HashNode *n;
int i;
DOH *key;
h = (Hash *) ho;
nh = (Hash *) malloc(sizeof(Hash));
DohInit(nh);
nh->hashsize = h->hashsize;
nh->hashtable = (HashNode **) malloc(nh->hashsize*sizeof(HashNode *));
for (i = 0; i < nh->hashsize; i++) {
nh->hashtable[i] = 0;
}
nh->currentindex = -1;
nh->current = 0;
nh->nitems = 0;
nh->objinfo = h->objinfo;
for (i = 0; i < h->hashsize; i++) {
if (n = h->hashtable[i]) {
while (n) {
Hash_setattr(nh, n->key, n->object);
n = n->next;
}
}
}
return (DOH *) nh;
}
/* -----------------------------------------------------------------------------
* DelHash() - Delete a hash table
* ----------------------------------------------------------------------------- */
void DelHash(DOH *ho)
{
Hash *h;
HashNode *n,*next;
int i;
h = (Hash *) ho;
for (i = 0; i < h->hashsize; i++) {
if (n = h->hashtable[i]) {
while (n) {
next = n->next;
DelNode(n);
n = next;
}
}
}
free(h->hashtable);
free(h);
}
/* -----------------------------------------------------------------------------
* Hash_clear(DOH *ho) - Clear all entries in a hash table
* ----------------------------------------------------------------------------- */
void Hash_clear(DOH *ho)
{
Hash *h;
HashNode *n,*next;
int i;
h = (Hash *) ho;
for (i = 0; i < h->hashsize; i++) {
if (n = h->hashtable[i]) {
while (n) {
next = n->next;
DelNode(n);
n = next;
}
}
h->hashtable[i] = 0;
}
h->nitems = 0;
}
/* -----------------------------------------------------------------------------
* static resize(Hash *h) - Resizes the hash table
* ----------------------------------------------------------------------------- */
static void resize(Hash *h) {
HashNode *n, *next, **table;
int oldsize, newsize;
int i, p, hv;
if (h->nitems < 2*h->hashsize) return;
/* Too big. We have to rescale everything now */
oldsize = h->hashsize;
/* Calculate a new size */
newsize = 2*oldsize+1;
p = 3;
while (p < (newsize >> 1)) {
if (((newsize/p)*p) == newsize) {
newsize+=2;
p = 3;
continue;
}
p = p + 2;
}
table = (HashNode **) malloc(newsize*sizeof(HashNode *));
for (i = 0; i < newsize; i++ ) {
table[i] = 0;
}
/* Walk down the old set of nodes */
h->hashsize = newsize;
for (i = 0; i < oldsize; i++) {
n = h->hashtable[i];
while (n) {
hv = Hashval(n->key) % newsize;
next = n->next;
n->next = table[hv];
table[hv] = n;
n = next;
}
}
free(h->hashtable);
h->hashtable = table;
}
/* -----------------------------------------------------------------------------
* int Hash_setattr(DOH *h, DOH *k, DOH *obj) - Adds an object to a hash
* ----------------------------------------------------------------------------- */
int
Hash_setattr(DOH *ho, DOH *k, DOH *obj) {
int hv;
HashNode *n, *prev;
Hash *h;
h = (Hash *) ho;
hv = (Hashval(k)) % h->hashsize;
n = h->hashtable[hv];
prev = 0;
while (n) {
if (Cmp(n->key,k) == 0) {
HashNode *nn;
if (prev) {
prev->next = n->next;
h->hashtable[hv] = n->next;
} else {
h->hashtable[hv] = n->next;
}
nn = n->next;
DelNode(n);
h->nitems--;
n = nn;
} else {
prev = n;
n = n->next;
}
}
/* Add this to the table */
n = NewNode(k,obj);
if (prev) prev->next = n;
else h->hashtable[hv] = n;
h->nitems++;
resize(h);
return 0;
}
/* -----------------------------------------------------------------------------
* DOH *Hash_getattr(DOH *ho, DOH *k) - Get an item from the hash table
* ----------------------------------------------------------------------------- */
DOH *
Hash_getattr(DOH *ho, DOH *k) {
int hv;
HashNode *n;
Hash *h;
h = (Hash *) ho;
hv = Hashval(k) % h->hashsize;
n = h->hashtable[hv];
while (n) {
if (Cmp(n->key, k) == 0) return n->object;
n = n->next;
}
return 0;
}
/* -----------------------------------------------------------------------------
* void Hash_delattr(DOH *ho, DOH *k) - Delete an element from the table
* ----------------------------------------------------------------------------- */
int
Hash_delattr(DOH *ho, DOH *k)
{
HashNode *n, *prev;
int hv;
Hash *h;
h = (Hash *) ho;
hv = Hashval(k) % h->hashsize;
n = h->hashtable[hv];
prev = 0;
while (n) {
if (Cmp(n->key, k) == 0) {
/* Found it, kill it */
if (prev) {
prev->next = n->next;
} else {
h->hashtable[hv] = n->next;
}
DelNode(n);
h->nitems--;
return 1;
}
prev = n;
n = n->next;
}
return 0;
}
/* -----------------------------------------------------------------------------
* Iterators
* ----------------------------------------------------------------------------- */
static HashNode *hash_first(DOH *ho) {
Hash *h = (Hash *) ho;
h->currentindex = 0;
h->current = 0;
while (!h->hashtable[h->currentindex] && (h->currentindex < h->hashsize))
h->currentindex++;
if (h->currentindex >= h->hashsize) return 0;
h->current = h->hashtable[h->currentindex];
return h->current;
}
static HashNode *hash_next(DOH *ho) {
Hash *h = (Hash *) ho;
if (h->currentindex < 0) return hash_first(h);
/* Try to move to the next entry */
h->current = h->current->next;
if (h->current) {
return h->current;
}
h->currentindex++;
while ((h->currentindex < h->hashsize) && !h->hashtable[h->currentindex])
h->currentindex++;
if (h->currentindex >= h->hashsize) return 0;
h->current = h->hashtable[h->currentindex];
return h->current;
}
DOH *
Hash_first(DOH *ho) {
HashNode *hn = hash_first(ho);
if (hn) return hn->object;
return 0;
}
DOH *
Hash_next(DOH *ho)
{
HashNode *hn = hash_next(ho);
if (hn) return hn->object;
return 0;
}
DOH *
Hash_firstkey(DOH *ho)
{
HashNode *hn = hash_first(ho);
if (hn) return hn->key;
return 0;
}
DOH *
Hash_nextkey(DOH *ho)
{
HashNode *hn = hash_next(ho);
if (hn) return hn->key;
return 0;
}
/* -----------------------------------------------------------------------------
* String *Hash_str(DOH *ho) - Create a string representation of a hash
* ----------------------------------------------------------------------------- */
DOH *
Hash_str(DOH *ho) {
int i;
HashNode *n;
DOH *s;
Hash *h;
h = (Hash *) ho;
#ifdef OLD
s = NewString("Hash {\n");
for (i = 0; i < h->hashsize; i++) {
n = h->hashtable[i];
while (n) {
Appendf(s," '%o' : %o, \n", n->key, n->object);
n = n->next;
}
}
Append(s,"}\n");
#else
s = NewString("Hash");
Appendf(s,"(%x) {",h);
for (i = 0; i < h->hashsize; i++) {
n = h->hashtable[i];
while (n) {
Appendf(s,"'%o',", n->key);
n = n->next;
}
}
Appendf(s,"}");
#endif
return s;
}
/* -----------------------------------------------------------------------------
* Hash_len(DOH *)
* ----------------------------------------------------------------------------- */
int Hash_len(DOH *ho) {
Hash *h = (Hash *) ho;
return h->nitems;
}
/* -----------------------------------------------------------------------------
* Hash_keys(DOH *)
*
* Return a list of keys
* ----------------------------------------------------------------------------- */
DOH *Hash_keys(DOH *so) {
DOH *keys;
DOH *k;
keys = NewList();
k = Firstkey(so);
while (k) {
Append(keys,k);
k = Nextkey(so);
}
/* List_sort(keys); */
return keys;
}

360
Source/DOH/Doh/list.c Normal file
View file

@ -0,0 +1,360 @@
/****************************************************************************
* Simplified Wrapper and Interface Generator (SWIG)
*
* 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.
****************************************************************************/
/*******************************************************************************
* $Header$
*
* File : list.c
*
* General purpose structure for keeping a list of reference counted Swig objects.
*******************************************************************************/
#include "doh.h"
typedef struct List {
DOHCOMMON;
int maxitems; /* Max size */
int nitems; /* Num items */
int iter; /* Iterator */
DOH **items;
} List;
/* Forward references */
DOH *CopyList(DOH *);
void DelList(DOH *);
void List_clear(DOH *);
DOH *List_get(DOH *, int pos);
int List_set(DOH *, int pos, DOH *obj);
int List_insert(DOH *, int pos, DOH *item);
int List_remove(DOH *, int pos);
int List_len(DOH *);
DOH *List_first(DOH *);
DOH *List_next(DOH *);
DOH *List_str(DOH *);
#define MAXLISTITEMS 8
static DohSequenceMethods ListSeqMethods = {
List_get,
List_set,
List_remove,
List_insert,
0,
List_first,
List_next,
};
static DohObjInfo ListType = {
"List", /* objname */
0,
DelList, /* sm_del */
CopyList, /* sm_copy */
List_clear, /* sm_clear */
List_str, /* sm_str */
0, /* doh_data */
List_len, /* sm_len */
0, /* sm_hash */
0, /* doh_cmp */
0, /* doh_mapping */
&ListSeqMethods, /* doh_sequence */
0,
0,
0,
0,
0,
};
DohObjInfo *List_type() {
return &ListType;
}
/* Internal function. Doubles amount of memory in a list */
static
void more(List *l) {
int i;
void **newitems;
newitems = (void **) malloc(l->maxitems*2*sizeof(void *));
for (i = 0; i < l->maxitems; i++) {
newitems[i] = l->items[i];
}
for (i = l->maxitems; i < 2*l->maxitems; i++) {
newitems[i] = (void *) 0;
}
l->maxitems *= 2;
free(l->items);
l->items = newitems;
}
/* -----------------------------------------------------------------------------
* List_check(DOH *lo) - Check to see if an object is a list
* ----------------------------------------------------------------------------- */
int
List_check(DOH *lo) {
List *l = (List *) lo;
if (!l) return 0;
if (l->magic != DOH_MAGIC) return 0;
if (l->objinfo != &ListType) return 0;
return 1;
}
/* -----------------------------------------------------------------------------
* NewList() - Create a new list
* ----------------------------------------------------------------------------- */
DOH *
NewList() {
List *l;
int i;
l = (List *) malloc(sizeof(List));
DohInit(l);
l->objinfo = &ListType;
l->nitems = 0;
l->maxitems = MAXLISTITEMS;
l->items = (void **) malloc(l->maxitems*sizeof(void *));
for (i = 0; i < MAXLISTITEMS; i++) {
l->items[i] = 0;
}
l->iter = 0;
return (DOH *) l;
}
/* -----------------------------------------------------------------------------
* DOH *CopyList(DOH *l) - Copy a list
* ----------------------------------------------------------------------------- */
DOH *
CopyList(DOH *lo) {
List *l,*nl;
int i;
l = (List *) lo;
nl = (List *) malloc(sizeof(List));
DohInit(nl);
nl->objinfo = l->objinfo;
nl->nitems = l->nitems;
nl->maxitems = l->maxitems;
nl->items = (void **) malloc(l->maxitems*sizeof(void *));
nl->iter = 0;
for (i = 0; i < l->maxitems; i++) {
nl->items[i] = l->items[i];
if (nl->items[i]) {
Incref(nl->items[i]);
}
}
return (DOH *) nl;
}
/* -----------------------------------------------------------------------------
* void DelList(DOH *l) - Delete a list
* ----------------------------------------------------------------------------- */
void
DelList(DOH *lo) {
List *l;
int i;
l = (List *) lo;
assert(l->refcount <= 0);
for (i = 0; i < l->nitems; i++) {
Delete(l->items[i]);
}
free(l->items);
free(l);
}
/* -----------------------------------------------------------------------------
* void List_clear(DOH *l) - Clear all elements in the list
* ----------------------------------------------------------------------------- */
void
List_clear(DOH *lo) {
List *l;
int i;
l = (List *) lo;
for (i = 0; i < l->nitems; i++) {
Delete(l->items[i]);
l->items[i] = 0;
}
l->nitems = 0;
}
/* -----------------------------------------------------------------------------
* void List_insert(DOH *lo, int pos, DOH *item) - Insert an element
* ----------------------------------------------------------------------------- */
int
List_insert(DOH *lo, int pos, DOH *item)
{
List *l;
DohBase *b;
int i;
if (!item) return;
b = (DohBase *) item;
l = (List *) lo;
if (pos == DOH_END) pos = l->nitems;
if (pos < 0) pos = 0;
if (pos > l->nitems) pos = l->nitems;
if (l->nitems == l->maxitems) more(l);
for (i = l->nitems; i > pos; i--) {
l->items[i] = l->items[i-1];
}
l->items[pos] = item;
b->refcount++;
l->nitems++;
return 0;
}
/* -----------------------------------------------------------------------------
* void List_remove(DOH *lo, int pos) - Remove an element
* ----------------------------------------------------------------------------- */
int
List_remove(DOH *lo, int pos)
{
List *l;
DOH *item;
int i;
l = (List *) lo;
if (pos == DOH_END) pos = l->nitems-1;
if (pos == DOH_BEGIN) pos = 0;
if ((pos < 0) || (pos >= l->nitems)) return;
item = l->items[pos];
for (i = pos; i < l->nitems-1; i++) {
l->items[i] = l->items[i+1];
}
l->nitems--;
Delete(item);
return 0;
}
/* -----------------------------------------------------------------------------
* int List_len(DOH *l) - List length
* ----------------------------------------------------------------------------- */
int
List_len(DOH *lo)
{
return ((List *) lo)->nitems;
}
/* -----------------------------------------------------------------------------
* DOH *List_get(DOH *lo, int n) - Get an item
* ----------------------------------------------------------------------------- */
DOH *
List_get(DOH *lo, int n)
{
List *l;
l = (List *) lo;
if (n == DOH_END) n = l->nitems-1;
if (n == DOH_BEGIN) n = 0;
if ((n < 0) || (n >= l->nitems)) {
printf("List_get : Invalid list index %d\n", n);
}
return l->items[n];
}
/* -----------------------------------------------------------------------------
* int List_set(DOH *lo, int n, DOH *val) - Set an item
* ----------------------------------------------------------------------------- */
int
List_set(DOH *lo, int n, DOH *val)
{
List *l;
l = (List *) lo;
if ((n < 0) || (n >= l->nitems)) {
printf("List_set : Invalid list index %d\n", n);
}
Delete(l->items[n]);
l->items[n] = val;
Incref(val);
return 0;
}
/* -----------------------------------------------------------------------------
* Iterators
* ----------------------------------------------------------------------------- */
DOH *
List_first(DOH *lo)
{
List *l;
l = (List *) lo;
l->iter = 0;
if (l->iter >= l->nitems) return 0;
return l->items[l->iter];
}
DOH *
List_next(DOH *lo)
{
List *l;
l = (List *) lo;
l->iter++;
if (l->iter >= l->nitems) return 0;
return l->items[l->iter];
}
/* -----------------------------------------------------------------------------
* String *List_str() - Create a string representation
* ----------------------------------------------------------------------------- */
DOH *
List_str(DOH *lo)
{
DOH *s;
int i;
List *l = (List *) lo;
s = NewString("");
for (i = 0; i < l->nitems; i++) {
Appendf(s, "%o", l->items[i]);
if ((i+1) < l->nitems)
Append(s,", ");
}
return s;
}
#ifdef SORT
/* -----------------------------------------------------------------------------
* void List_sort(DOH *DOH)
*
* Sorts a list
* ----------------------------------------------------------------------------- */
int objcmp(const void *s1, const void *s2) {
DOH **so1, **so2;
so1 = (DOH **) s1;
so2 = (DOH **) s2;
return Cmp(*so1,*so2);
}
void List_sort(DOH *so) {
List *l;
if (!List_check(so)) return;
l = (List *) so;
qsort(l->items,l->nitems,sizeof(DOH *), objcmp);
}
#endif

30
Source/DOH/Doh/main.c Normal file
View file

@ -0,0 +1,30 @@
#include "doh.h"
int main() {
DOH *d1, *d2, *d3;
DOH *f;
DOH *l;
DOH *h;
printf("starting...\n");
f = NewFile("foo","w");
printf("%x\n",f);
d1 = NewString("Hello");
d2 = NewString("World");
Append(d1,d2);
Printf(d1,"This is a test %d", 42);
Setattr(d1,"foo",d2);
Printf(f,"Hello World\n");
Printf(f,"%s\n",d1);
l = NewList();
Append(l,d1);
Append(l,d2);
Append(l,f);
h = NewHash();
Setattr(h,"foo",d1);
Setattr(h,"bar",l);
Printf(f,"%o\n",l);
Printf(f,"%o\n",h);
Printf(f,"%o\n", Getattr(h,"bar"));
}

697
Source/DOH/Doh/string.c Normal file
View file

@ -0,0 +1,697 @@
/****************************************************************************
* DOH (Dynamic Object Hack)
*
* 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.
****************************************************************************/
#include "doh.h"
/* ---------------------------------------------------------------------------
* $Header$
* string.c
*
* String support.
* --------------------------------------------------------------------------- */
typedef struct String {
DOHCOMMON;
int maxsize; /* Max size allocated */
int len; /* Current length */
int hashkey; /* Hash key value */
int sp; /* Current position */
int lsp; /* Last returned position */
char *str; /* String data */
} String;
/* Forward references */
void DelString(DOH *s);
DOH *CopyString(DOH *s);
void String_clear(DOH *s);
DOH *String_str(DOH *s);
int String_delitem(DOH *s, int where);
int String_len(DOH *s);
void String_appendfv(DOH *s, char *fmt, va_list ap);
int String_insertfv(DOH *s, int pos, char *fmt, va_list ap);
int String_insert(DOH *s, int pos, DOH *DOH);
void *String_data(DOH *s);
int String_cmp(DOH *, DOH *);
int String_hash(DOH *s);
int String_read(DOH *s, void *buffer, int length);
int String_write(DOH *s, void *buffer, int length);
int String_seek(DOH *s, long offset, int whence);
long String_tell(DOH *s);
int String_printf(DOH *s, char *fmt, va_list ap);
static DohSequenceMethods StringSeqMethods = {
0,
0,
String_delitem,
String_insert,
String_insertfv,
0,
0,
};
static DohFileMethods StringFileMethods = {
String_read,
String_write,
String_seek,
String_tell,
String_printf,
};
static DohObjInfo StringType = {
"String", /* objname */
0,
DelString, /* sm_del */
CopyString, /* sm_copy */
String_clear, /* sm_clear */
String_str, /* sm_str */
String_data, /* doh_data */
String_len, /* sm_len */
String_hash, /* sm_hash */
String_cmp, /* doh_cmp */
0, /* doh_mapping */
&StringSeqMethods, /* doh_sequence */
&StringFileMethods,/* doh_file */
0,
0,
0,
0,
};
#define INIT_MAXSIZE 16
DohObjInfo *String_type() {
return &StringType;
}
/* -----------------------------------------------------------------------------
* void *String_data(DOH *so) - Return as a 'void *'
* ----------------------------------------------------------------------------- */
void *String_data(DOH *so) {
return (void *) ((String *) so)->str;
}
/* -----------------------------------------------------------------------------
* NewString(const char *c) - Create a new string
* ----------------------------------------------------------------------------- */
DOH *
NewString(char *s)
{
int l, max;
String *str;
str = (String *) malloc(sizeof(String));
DohInit(str);
str->objinfo = &StringType;
str->hashkey = -1;
str->sp = 0;
str->lsp = 0;
str->line = 1;
max = INIT_MAXSIZE;
if (s) {
l = (int) strlen(s);
if ((l+1) > max) max = l+1;
}
str->str = (char *) malloc(max);
str->maxsize = max;
if (s) {
strcpy(str->str,s);
str->len = l;
} else {
str->str[0] = 0;
str->len = 0;
}
return (DOH *) str;
}
/* -----------------------------------------------------------------------------
* CopyString(DOH *s) - Copy a string
* ----------------------------------------------------------------------------- */
DOH *
CopyString(DOH *so) {
String *s;
int l, max;
String *str;
s = (String *) so;
str = (String *) malloc(sizeof(String));
DohInit(str);
str->objinfo = &StringType;
str->hashkey = -1;
str->sp = 0;
str->lsp = 0;
str->line = 1;
max = s->maxsize;
str->str = (char *) malloc(max);
memmove(str->str, s->str, max);
str->maxsize= max;
return (DOH *) str;
}
/* -----------------------------------------------------------------------------
* DelString(DOH *s) - Delete a string
* ----------------------------------------------------------------------------- */
void
DelString(DOH *so) {
String *s;
s = (String *) so;
assert(s->refcount <= 0);
free(s->str);
free(s);
}
/* -----------------------------------------------------------------------------
* int String_check(DOH *s) - Check if s is a string
* ----------------------------------------------------------------------------- */
int
String_check(DOH *s)
{
char *c = (char *) s;
if (!s) return 0;
if (*c == DOH_MAGIC) {
if (((String *) c)->objinfo == &StringType)
return 1;
return 0;
}
else return 0;
}
/* -----------------------------------------------------------------------------
* int String_len(DOH *s) - Length of a string
* ----------------------------------------------------------------------------- */
int
String_len(DOH *so) {
String *s = (String *)so;
return s->len;
}
/* -----------------------------------------------------------------------------
* int String_cmp(DOH *s1, DOH *s2) - Compare two strings
* ----------------------------------------------------------------------------- */
int
String_cmp(DOH *so1, DOH *so2)
{
String *s1, *s2;
s1 = (String *) so1;
s2 = (String *) so2;
return strcmp(s1->str,s2->str);
}
/* -----------------------------------------------------------------------------
* int String_hash(DOH *s) - Compute string hash value
* ----------------------------------------------------------------------------- */
int String_hash(DOH *so) {
String *s = (String *) so;
char *c;
int i, h = 0, len;
if (s->hashkey >= 0) return s->hashkey;
c = s->str;
len = s->len > 50 ? 50 : s->len;
for (i = 0; i < len; i++) {
h = (((h << 5) + *(c++)));
}
h = h & 0x7fffffff;
s->hashkey = h;
return h;
}
/* -----------------------------------------------------------------------------
* static add(String *s, const char *newstr) - Append to s
* ----------------------------------------------------------------------------- */
static void
add(String *s, const char *newstr) {
int newlen, newmaxsize, l;
if (!newstr) return;
s->hashkey = -1;
l = (int) strlen(newstr);
newlen = s->len+l + 1;
if (newlen >= s->maxsize-1) {
newmaxsize = 2*s->maxsize;
if (newlen >= newmaxsize -1) newmaxsize = newlen + 1;
assert(s->str = (char *) realloc(s->str,newmaxsize));
s->maxsize = newmaxsize;
}
strcpy(s->str+s->len,newstr);
s->len += l;
}
static void
addstr(String *s, String *s1) {
int newlen, newmaxsize, l;
s->hashkey = -1;
l = s1->len;
newlen = s->len+l + 1;
if (newlen >= s->maxsize-1) {
newmaxsize = 2*s->maxsize;
if (newlen >= newmaxsize -1) newmaxsize = newlen + 1;
assert(s->str = (char *) realloc(s->str,newmaxsize));
s->maxsize = newmaxsize;
}
memmove(s->str+s->len,s1->str,s1->len);
s->len += l;
}
/* Add a single character to s */
void
String_addchar(DOH *so, char c) {
String *s = (String *) so;
s->hashkey = -1;
if ((s->len+1) > (s->maxsize-1)) {
assert(s->str = (char *) realloc(s->str,2*s->maxsize));
s->maxsize *= 2;
}
s->str[s->len++] = c;
s->str[s->len] = 0;
}
/* -----------------------------------------------------------------------------
* void String_clear(DOH *s) - Clear a string
* ----------------------------------------------------------------------------- */
void
String_clear(DOH *so)
{
String *s;
s = (String *) so;
s->hashkey = -1;
s->len = 0;
s->str[0] = 0;
s->sp = 0;
s->lsp = 0;
s->line = 1;
}
/* -----------------------------------------------------------------------------
* void String_appendfv(DOH *so, char *format, ...) - Append to string
* -----------------------------------------------------------------------------*/
void
String_appendfv(DOH *so, char *format, va_list ap)
{
char *p, *sval;
String *Sval;
DohBase *b;
int ival;
double dval;
char cval;
char temp[64];
String *s;
s = (String *) so;
assert(s->refcount <= 1);
for (p = format; *p; p++) {
if (*p != '%') {
String_addchar(s,*p);
continue;
}
switch(*++p) {
case 'c':
cval = va_arg(ap,char);
String_addchar(s,cval);
break;
case 'd':
ival = va_arg(ap,int);
sprintf(temp,"%d",ival);
add(s,temp);
break;
case 'x':
ival = va_arg(ap,int);
sprintf(temp,"%x", ival);
add(s,temp);
break;
case 'f':
dval = va_arg(ap,double);
sprintf(temp,"%f",dval);
add(s,temp);
break;
case 's':
Sval = va_arg(ap, String *);
if (String_check(Sval)) {
addstr(s,Sval);
} else {
add(s,(char *) Sval);
}
break;
case 'o':
b = va_arg(ap, DohBase *);
Sval = Str(b);
addstr(s, Sval);
Delete(Sval);
break;
/* Automatically deletes whatever object was passed */
case 'O':
b = va_arg(ap, DohBase *);
Sval = Str(b);
addstr(s, Sval);
Delete(Sval);
Delete(b);
break;
default:
String_addchar(s,*p);
break;
}
}
}
void
raw_insert(String *s, int pos, char *data, int len)
{
char *nstr;
nstr = s->str;
s->hashkey = -1;
if (pos == DOH_END) {
add(s, data);
return;
}
if (pos < 0) pos = 0;
else if (pos > s->len) pos = s->len;
/* See if there is room to insert the new data */
while (s->maxsize <= s->len+len) {
assert(s->str = (char *) realloc(s->str,2*s->maxsize));
s->maxsize *= 2;
}
memmove(s->str+pos+len, s->str+pos, (s->len - pos));
memcpy(s->str+pos,data,len);
s->len += len;
s->str[s->len] = 0;
}
/* -----------------------------------------------------------------------------
* void String_insert(DOH *so, int pos, SO *str) - Insert a string
* ----------------------------------------------------------------------------- */
int
String_insert(DOH *so, int pos, DOH *str)
{
String *s, *s1;
char *c;
int len;
s = (String *) so;
assert(s->refcount <= 1);
s1 = (String *) str;
len = s1->len;
c = s1->str;
raw_insert(s,pos,c,len);
return 0;
}
/* -----------------------------------------------------------------------------
* void String_insertf(DOH *so, char *format, ...) - Insert a string
* -----------------------------------------------------------------------------*/
int
String_insertfv(DOH *so, int pos, char *format, va_list ap)
{
String *s;
String *temp;
s = (String *) so;
if (pos == DOH_END) {
String_appendfv(so,format,ap);
return 0;
}
temp = NewString("");
String_appendfv(temp,format,ap);
raw_insert(s,pos, temp->str,temp->len);
Delete(temp);
return 0;
}
/* -----------------------------------------------------------------------------
* int String_delitem(DOH *so, int pos)
*
* Delete an individual item
* ----------------------------------------------------------------------------- */
int String_delitem(DOH *so, int pos)
{
String *s = (String *) so;
if (pos == DOH_END) pos = s->len-1;
if (pos == DOH_BEGIN) pos = 0;
if (s->len == 0) return;
memmove(s->str+pos, s->str+pos+1, ((s->len-1) - pos));
s->len--;
s->str[s->len] = 0;
return 0;
}
/* -----------------------------------------------------------------------------
* DOH *String_str(DOH *so) - Returns a string (used by printing commands)
* ----------------------------------------------------------------------------- */
DOH *
String_str(DOH *so) {
DOH *nstr;
String *s = (String *) so;
nstr = NewString(s->str);
return nstr;
}
/* -----------------------------------------------------------------------------
* int String_read(DOH *so, void *buffer, int len)
*
* Read data from the string
* ----------------------------------------------------------------------------- */
int
String_read(DOH *so, void *buffer, int len) {
int reallen;
String *s = (String *) so;
if ((s->sp + len) > s->len) reallen = (s->len - s->sp);
memmove(buffer, s->str+s->sp, reallen);
s->sp += reallen;
return reallen;
}
/* -----------------------------------------------------------------------------
* int String_write(DOH *so, void *buffer, int len)
*
* Write data to the string
* ----------------------------------------------------------------------------- */
int
String_write(DOH *so, void *buffer, int len) {
int reallen, newlen, newmaxsize;
String *s = (String *) so;
newlen = s->sp + len + 1;
if (newlen > s->maxsize) {
assert(s->str = (char *) realloc(s->str,newlen));
s->maxsize = newlen;
s->len = s->sp + len;
}
memmove(s->str+s->sp,buffer,len);
s->sp += len;
return len;
}
/* -----------------------------------------------------------------------------
* int String_seek(DOH *so, long offset, int whence)
*
* Seek to a new position
* ----------------------------------------------------------------------------- */
int
String_seek(DOH *so, long offset, int whence) {
int pos;
String *s = (String *) so;
if (whence == SEEK_SET) pos = 0;
if (whence == SEEK_CUR) pos = s->sp;
if (whence == SEEK_END) {
pos = s->len;
offset = -offset;
}
s->sp = pos + offset;
if (s->sp < 0) s->sp = 0;
if (s->sp > s->len) s->sp = s->len;
return 0;
}
/* -----------------------------------------------------------------------------
* long String_seek(DOH *so)
*
* Return current position
* ----------------------------------------------------------------------------- */
long
String_tell(DOH *so) {
String *s = (String *) so;
return (long) s->sp;
}
/* -----------------------------------------------------------------------------
* int String_printf(DOH *so, char *format, ...)
*
* ----------------------------------------------------------------------------- */
int
String_printf(DOH *so, char *format, va_list ap)
{
int len;
DOH *nso;
String *s = (String *) so;
nso = NewString("");
String_appendfv(nso,format,ap);
len = ((String *) nso)->len;
String_write(so,Data(nso),len);
Delete(nso);
return len;
}
/* -----------------------------------------------------------------------------
* static void replace_internal(String *str, char *token, char *rep, int flags, char *start, int count)
*
* Replaces token with rep. flags is as follows:
*
* REPLACE_ANY - Replace all occurrences
* REPLACE_NOQUOTE - Don't replace in quotes
* REPLACE_ID - Only replace valid identifiers
* REPLACE_FIRST - Only replace first occurrence
*
* start is a starting position. count is a count.
* ----------------------------------------------------------------------------- */
void replace_internal(String *str, char *token, char *rep, int flags, char *start, int count)
{
char *s, *c, *t;
int tokenlen;
int state;
/* Copy the current string representation */
s = str->str;
str->hashkey = -1;
tokenlen = strlen(token);
/* If a starting position was given, dump those characters first */
if (start) {
c = start;
} else {
c = s;
}
/* Now walk down the old string and search for tokens */
t = strstr(c,token);
if (t) {
str->len = 0;
str->str = (char *) malloc(str->maxsize);
if (start) {
char temp = *start;
*start = 0;
add(str,s);
*start = temp;
}
/* Depending on flags. We compare in different ways */
state = 0;
t = c;
while ((*c) && (count)) {
switch(state) {
case 0:
if (*c == *token) {
if (!(flags & DOH_REPLACE_ID)) {
if (strncmp(c,token,tokenlen) == 0) {
char temp = *c;
*c = 0;
add(str,t);
*c = temp;
add(str,rep);
c += (tokenlen-1);
t = c+1;
count--;
}
} else if (isalpha(*c) || (*c == '_') || (*c == '$')) {
char temp = *c;
*c = 0;
add(str,t);
*c = temp;
t = c;
state = 10;
}
} else if (flags & DOH_REPLACE_NOQUOTE) {
if (*c == '\"') state = 20;
else if (*c == '\'') state = 30;
}
break;
case 10: /* The start of an identifier */
if (isalnum(*c) || (*c == '_') || (*c == '$')) state = 10;
else {
char temp = *c;
*c = 0;
if (strcmp(token,t) == 0) {
add(str,rep);
count--;
} else {
add(str,t);
}
*c = temp;
t = c;
state = 0;
}
break;
case 20: /* A string */
if (*c == '\"') state = 0;
else if (*c == '\\') c++;
break;
case 30: /* A Single quoted string */
if (*c == '\'') state = 0;
else if (*c == '\\') c++;
break;
}
c++;
}
if ((count) && (state == 10)) {
if (strcmp(token,t) == 0) {
add(str,rep);
} else {
add(str,t);
}
} else {
add(str,t);
}
free(s);
}
}
/* -----------------------------------------------------------------------------
* void String_replace(DOH *str, DOH *token, DOH *rep, int flags)
* ----------------------------------------------------------------------------- */
void
String_replace(DOH *stro, DOH *token, DOH *rep, int flags)
{
int count = -1;
String *str;
if (!String_check(stro)) return;
str = (String *) stro;
assert(!str->refcount);
if (flags & DOH_REPLACE_FIRST) count = 1;
replace_internal(str,Char(token),Char(rep),flags,str->str,count);
}

261
Source/DOH/Include/doh.h Normal file
View file

@ -0,0 +1,261 @@
/*******************************************************************************
* DOH (Dynamic Object Hack)
*
* 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.
*******************************************************************************/
/***********************************************************************
* $Header$
*
* doh.h
***********************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
typedef void DOH;
#define DOH_BEGIN -1
#define DOH_END -2
#define DOH_CUR -3
#define DOH_MAGIC 0x04
/* -----------------------------------------------------------------------------
* Object classes
* ----------------------------------------------------------------------------- */
/* Mapping Objects */
typedef struct {
DOH *(*doh_getattr)(DOH *obj, DOH *name); /* Get attribute */
int (*doh_setattr)(DOH *obj, DOH *name, DOH *value); /* Set attribute */
int (*doh_delattr)(DOH *obj, DOH *name); /* Del attribute */
DOH *(*doh_firstkey)(DOH *obj); /* First key */
DOH *(*doh_nextkey)(DOH *obj); /* Next key */
} DohMappingMethods;
/* Sequence methods */
typedef struct {
DOH *(*doh_getitem)(DOH *obj, int index);
int (*doh_setitem)(DOH *obj, int index, DOH *value);
int (*doh_delitem)(DOH *obj, int index);
int (*doh_insitem)(DOH *obj, int index, DOH *value);
int (*doh_insertf)(DOH *obj, int index, char *format, va_list ap);
DOH *(*doh_first)(DOH *obj);
DOH *(*doh_next)(DOH *obj);
} DohSequenceMethods;
/* File methods */
typedef struct {
int (*doh_read)(DOH *obj, void *buffer, int nbytes);
int (*doh_write)(DOH *obj, void *buffer, int nbytes);
int (*doh_seek)(DOH *obj, long offset, int whence);
long (*doh_tell)(DOH *obj);
int (*doh_printf)(DOH *obj, char *format, va_list ap);
int (*doh_scanf)(DOH *obj, char *format, va_list ap);
int (*doh_close)(DOH *obj);
} DohFileMethods;
/* -----------------------------------------------------------------------------
* DohObjInfo
*
* Included in all DOH types.
* ----------------------------------------------------------------------------- */
typedef struct DohObjInfo {
char *objname; /* Object name */
int objsize; /* Object size */
/* Basic object methods */
void (*doh_del)(DOH *obj); /* Delete object */
DOH *(*doh_copy)(DOH *obj); /* Copy and object */
void (*doh_clear)(DOH *obj); /* Clear an object */
/* Output methods */
DOH *(*doh_str)(DOH *obj); /* Make a full string */
void *(*doh_data)(DOH *obj); /* Return raw data */
/* Length and hash values */
int (*doh_len)(DOH *obj);
int (*doh_hash)(DOH *obj);
/* Compare */
int (*doh_cmp)(DOH *obj1, DOH *obj2);
DohMappingMethods *doh_mapping; /* Mapping methods */
DohSequenceMethods *doh_sequence; /* Sequence methods */
DohFileMethods *doh_file; /* File methods */
void *reserved2; /* Number methods */
void *reserved3;
void *reserved4;
void *user[16]; /* User extensions */
} DohObjInfo;
/* Low-level doh methods. Do not call directly (well, unless you want to). */
extern void DohDestroy(DOH *obj);
extern DOH *DohCopy(DOH *obj);
extern void DohClear(DOH *obj);
extern int DohCmp(DOH *obj1, DOH *obj2);
extern DOH *DohStr(DOH *obj);
extern DOH *DohGetattr(DOH *obj, DOH *name);
extern int DohSetattr(DOH *obj, DOH *name, DOH *value);
extern void DohDelattr(DOH *obj, DOH *name);
extern int DohHashval(DOH *obj);
extern DOH *DohGetitem(DOH *obj, int index);
extern void DohSetitem(DOH *obj, int index, DOH *value);
extern void DohDelitem(DOH *obj, int index);
extern void DohInsertitem(DOH *obj, int index, DOH *value);
extern void DohInsertf(DOH *obj, int index, char *format, ...);
extern void DohvInsertf(DOH *obj, int index, char *format, va_list ap);
extern void DohAppendf(DOH *obj, char *format, ...);
extern void DohvAppendf(DOH *obj, char *format, va_list ap);
extern int DohLen(DOH *obj);
extern DOH *DohFirst(DOH *obj);
extern DOH *DohNext(DOH *obj);
extern DOH *DohFirstkey(DOH *obj);
extern DOH *DohNextkey(DOH *obj);
extern void *DohData(DOH *obj);
extern int DohGetline(DOH *obj);
extern void DohSetline(DOH *obj, int line);
extern DOH *DohGetfile(DOH *obj);
extern void DohSetfile(DOH *obj, DOH *file);
extern int DohCheck(DOH *obj);
extern void DohInit(DOH *obj);
/* File methods */
extern int DohWrite(DOH *obj, void *buffer, int length);
extern int DohRead(DOH *obj, void *buffer, int length);
extern int DohSeek(DOH *obj, long offser, int whence);
extern long DohTell(DOH *obj);
extern int DohPrintf(DOH *obj, char *format, ...);
extern int DohvPrintf(DOH *obj, char *format, va_list ap);
extern int DohScanf(DOH *obj, char *format, ...);
extern int DohvScanf(DOH *obj, char *format, va_list ap);
/* Macros to invoke the above functions. Includes the location of
the caller to simplify debugging if something goes wrong */
#define Delete DohDestroy
#define Copy DohCopy
#define Clear DohClear
#define Str DohStr
#define Signature DohSignature
#define Getattr DohGetattr
#define Setattr DohSetattr
#define Delattr DohDelattr
#define Hashval DohHashval
#define Getitem DohGetitem
#define Setitem DohSetitem
#define Delitem DohDelitem
#define Insert DohInsertitem
#define Insertf DohInsertitemf
#define vInsertf DohvInsertitemf
#define Append(s,x) DohInsertitem(s,DOH_END,x)
#define Appendf DohAppendf
#define vAppendf DohvAppendf
#define Push(s,x) DohInsertitem(s,DOH_BEGIN,x)
#define Len DohLen
#define First DohFirst
#define Next DohNext
#define Firstkey DohFirstkey
#define Nextkey DohNextkey
#define Data DohData
#define Char (char *) Data
#define Cmp DohCmp
#define Setline DohSetline
#define Getline DohGetline
#define Setfile DohSetfile
#define Getfile DohGetfile
#define Write DohWrite
#define Read DohRead
#define Seek DohSeek
#define Tell DohTell
#define Printf DohPrintf
#define vPrintf DohvPrintf
#define Scanf DohScanf
#define vScanf DohvScanf
/* -----------------------------------------------------------------------------
* DohBase
*
* DohBase object type. Attributes common to all objects.
* ----------------------------------------------------------------------------- */
#define DOHCOMMON \
char magic; \
char moremagic[3]; \
DohObjInfo *objinfo; \
int refcount; \
int line; \
DOH *file
typedef struct {
DOHCOMMON;
} DohBase;
/* Macros for decrefing and increfing (safe for null objects). */
#define Decref(a) if (a) ((DohBase *) a)->refcount--;
#define Incref(a) if (a) ((DohBase *) a)->refcount++;
#define Objname(a) ((DohBase *) a)->objinfo->objname
/* -----------------------------------------------------------------------------
* Strings.
* ----------------------------------------------------------------------------- */
extern DOH *NewString(char *c);
extern int String_check(DOH *s);
extern void String_replace(DOH *s, DOH *token, DOH *rep, int flags);
extern DohObjInfo *String_type();
/* String replacement flags */
#define DOH_REPLACE_ANY 0x00
#define DOH_REPLACE_NOQUOTE 0x01
#define DOH_REPLACE_ID 0x02
#define DOH_REPLACE_FIRST 0x04
/* -----------------------------------------------------------------------------
* Files
* ----------------------------------------------------------------------------- */
extern DOH *NewFile(char *file, char *mode);
extern DOH *NewFileFromFile(FILE *f);
extern DohObjInfo *File_type();
/* -----------------------------------------------------------------------------
* List
* ----------------------------------------------------------------------------- */
extern DOH *NewList();
extern int List_check(DOH *);
extern void List_sort(DOH *);
extern DohObjInfo *List_type();
/* -----------------------------------------------------------------------------
* Hash
* ----------------------------------------------------------------------------- */
extern DOH *NewHash();
extern int Hash_check(DOH *h);
extern DOH *Hash_keys(DOH *);
extern DohObjInfo *Hash_type();