Substantial cleanup. Performance optimizations.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@460 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-06-14 22:00:46 +00:00
commit a8bf264f66
10 changed files with 944 additions and 1093 deletions

View file

@ -14,26 +14,16 @@ static char cvsroot[] = "$Header$";
#include "dohint.h"
static DohObjInfo DohBaseType = {
"Base", /* objname */
sizeof(DohBase), /* objsize */
0, /* doh_del */
0, /* doh_copy */
0, /* doh_clear */
0, /* doh_str */
0, /* doh_data */
0, /* doh_dump */
0, /* doh_load */
0, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_mapping */
0, /* doh_list */
0, /* doh_file */
0, /* doh_string */
0, /* reserved1 */
0, /* reserved2 */
};
static DohObjInfo *dohtypes[MAX_DOHTYPE];
/* -----------------------------------------------------------------------------
* DohRegisterType()
* ----------------------------------------------------------------------------- */
void
DohRegisterType(int type, DohObjInfo *objinfo) {
dohtypes[type] = objinfo;
}
/* -----------------------------------------------------------------------------
* DohDelete()
@ -42,24 +32,22 @@ static DohObjInfo DohBaseType = {
void
DohDelete(DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (!DohCheck(b)) return;
if (b->flags & DOH_FLAG_INTERN) return;
if (b->flag_intern) return;
b->refcount--;
if (b->refcount <= 0) {
if (b->objinfo->doh_del) (b->objinfo->doh_del)(obj);
objinfo = dohtypes[b->type];
if (objinfo->doh_del) {
(objinfo->doh_del)(b);
} else {
if (b->data) DohFree(b->data);
}
DohObjFree(b);
}
}
/* -----------------------------------------------------------------------------
* DohIntern()
* ----------------------------------------------------------------------------- */
void
DohIntern(DOH *obj) {
DohBase *b = (DohBase *) obj;
b->flags = b->flags | DOH_FLAG_INTERN;
}
/* -----------------------------------------------------------------------------
* DohCopy()
* ----------------------------------------------------------------------------- */
@ -67,8 +55,10 @@ DohIntern(DOH *obj) {
DOH *
DohCopy(const DOH *obj) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_copy)
return (b->objinfo->doh_copy)(b);
DohObjInfo *objinfo = dohtypes[b->type];
objinfo = dohtypes[b->type];
if (objinfo->doh_copy)
return (objinfo->doh_copy)(b);
}
/* -----------------------------------------------------------------------------
@ -78,8 +68,9 @@ DohCopy(const DOH *obj) {
void
DohClear(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_clear)
(b->objinfo->doh_clear)(obj);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo->doh_clear)
(objinfo->doh_clear)(b);
}
/* -----------------------------------------------------------------------------
@ -90,11 +81,13 @@ DOH *
DohStr(const DOH *obj) {
char buffer[512];
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (DohCheck(b)) {
if (b->objinfo->doh_str) {
return (b->objinfo->doh_str)(b);
objinfo = dohtypes[b->type];
if (objinfo->doh_str) {
return (objinfo->doh_str)(b);
}
sprintf(buffer,"<Object '%s' at %x>", b->objinfo->objname, b);
sprintf(buffer,"<Object '%s' at %x>", objinfo->objname, b);
return NewString(buffer);
} else {
return NewString(obj);
@ -108,22 +101,25 @@ DohStr(const DOH *obj) {
int
DohDump(const DOH *obj, DOH *out) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_dump) {
return (b->objinfo->doh_dump)(b,out);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo->doh_dump) {
return (objinfo->doh_dump)(b,out);
}
return 0;
}
/* -----------------------------------------------------------------------------
* DohLen()
* DohLen() - Defaults to strlen() if not a DOH object
* ----------------------------------------------------------------------------- */
int
DohLen(const DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (!b) return 0;
if (DohCheck(b)) {
if (b->objinfo->doh_len) {
return (b->objinfo->doh_len)(b);
objinfo = dohtypes[b->type];
if (objinfo->doh_len) {
return (objinfo->doh_len)(b);
}
return 0;
} else {
@ -138,9 +134,11 @@ DohLen(const DOH *obj) {
int
DohHashval(const DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (DohCheck(b)) {
if (b->objinfo->doh_hash) {
return (b->objinfo->doh_hashval)(b);
objinfo = dohtypes[b->type];
if (objinfo->doh_hashval) {
return (objinfo->doh_hashval)(b);
}
}
return 0;
@ -153,11 +151,11 @@ DohHashval(const DOH *obj) {
void *
DohData(const DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (DohCheck(obj)) {
if (b->objinfo) {
if (b->objinfo->doh_data) {
return (b->objinfo->doh_data)(b);
}
objinfo = dohtypes[b->type];
if (objinfo->doh_data) {
return (objinfo->doh_data)(b);
}
return 0;
}
@ -171,26 +169,29 @@ DohData(const DOH *obj) {
int
DohCmp(const DOH *obj1, const DOH *obj2) {
DohBase *b1, *b2;
DohObjInfo *b1info, *b2info;
b1 = (DohBase *) obj1;
b2 = (DohBase *) obj2;
if ((!DohCheck(b1)) || (!DohCheck(b2))) {
return strcmp((char *) DohData(b1),(char *) DohData(b2));
}
if (b1->objinfo->doh_cmp) {
return (b1->objinfo->doh_cmp)(b1,b2);
}
b1info = dohtypes[b1->type];
b2info = dohtypes[b2->type];
if ((b1info == b2info) && (b1info->doh_cmp))
return (b1info->doh_cmp)(b1,b2);
return 1;
}
/* -----------------------------------------------------------------------------
* DohIsMapping()
* ----------------------------------------------------------------------------- */
int
DohIsMapping(const DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (!DohCheck(b)) return 0;
if (b->objinfo->doh_hash) return 1;
objinfo = dohtypes[b->type];
if (objinfo->doh_hash) return 1;
else return 0;
}
@ -201,8 +202,9 @@ DohIsMapping(const DOH *obj) {
DOH *
DohGetattr(DOH *obj, const DOH *name) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_hash->doh_getattr) {
return (b->objinfo->doh_hash->doh_getattr)(b,(DOH *) name);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo->doh_hash && objinfo->doh_hash->doh_getattr) {
return (objinfo->doh_hash->doh_getattr)(b,(DOH *) name);
}
return 0;
}
@ -214,8 +216,9 @@ DohGetattr(DOH *obj, const DOH *name) {
int
DohSetattr(DOH *obj, const DOH *name, const DOH *value) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_hash->doh_setattr) {
return (b->objinfo->doh_hash->doh_setattr)(b,(DOH *) name,(DOH *) value);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo->doh_hash && objinfo->doh_hash->doh_setattr) {
return (objinfo->doh_hash->doh_setattr)(b,(DOH *) name,(DOH *) value);
}
return 0;
}
@ -227,8 +230,9 @@ DohSetattr(DOH *obj, const DOH *name, const DOH *value) {
void
DohDelattr(DOH *obj, const DOH *name) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_hash->doh_delattr) {
(b->objinfo->doh_hash->doh_delattr)(b,(DOH *) name);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo->doh_hash && objinfo->doh_hash->doh_delattr) {
(objinfo->doh_hash->doh_delattr)(b,(DOH *) name);
}
}
@ -239,8 +243,9 @@ DohDelattr(DOH *obj, const DOH *name) {
DOH *
DohFirstkey(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_hash->doh_firstkey) {
return (b->objinfo->doh_hash->doh_firstkey)(obj);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo->doh_hash && objinfo->doh_hash->doh_firstkey) {
return (objinfo->doh_hash->doh_firstkey)(b);
}
return 0;
}
@ -252,8 +257,9 @@ DohFirstkey(DOH *obj) {
DOH *
DohNextkey(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_hash->doh_nextkey) {
return (b->objinfo->doh_hash->doh_nextkey)(obj);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo && objinfo->doh_hash->doh_nextkey) {
return (objinfo->doh_hash->doh_nextkey)(b);
}
return 0;
}
@ -345,9 +351,7 @@ DohSetDouble(DOH *obj, const DOH *name, double value) {
void
DohSetChar(DOH *obj, const DOH *name, char *value) {
DOH *temp;
temp = NewString(value);
Setattr(obj,(DOH *) name,temp);
Setattr(obj,(DOH *) name,NewString(value));
}
/* -----------------------------------------------------------------------------
@ -356,9 +360,7 @@ DohSetChar(DOH *obj, const DOH *name, char *value) {
void
DohSetVoid(DOH *obj, const DOH *name, void *value) {
DOH *temp;
temp = NewVoid(value,0);
Setattr(obj,(DOH *) name,temp);
Setattr(obj,(DOH *) name,NewVoid(value,0));
}
/* -----------------------------------------------------------------------------
@ -368,8 +370,10 @@ DohSetVoid(DOH *obj, const DOH *name, void *value) {
int
DohIsSequence(const DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (!DohCheck(b)) return 0;
if (b->objinfo->doh_list) return 1;
objinfo = dohtypes[b->type];
if (objinfo->doh_list) return 1;
else return 0;
}
@ -380,8 +384,9 @@ DohIsSequence(const DOH *obj) {
DOH *
DohGetitem(DOH *obj, int index) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_list->doh_getitem) {
return (b->objinfo->doh_list->doh_getitem)(obj,index);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo->doh_list && objinfo->doh_list->doh_getitem) {
return (objinfo->doh_list->doh_getitem)(b,index);
}
return 0;
}
@ -393,10 +398,11 @@ DohGetitem(DOH *obj, int index) {
int
DohSetitem(DOH *obj, int index, const DOH *value) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_list->doh_setitem) {
return (b->objinfo->doh_list->doh_setitem)(obj,index,(DOH *) value);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo->doh_list && objinfo->doh_list->doh_setitem) {
return (objinfo->doh_list->doh_setitem)(b,index,(DOH *) value);
}
return 0;
return -1;
}
/* -----------------------------------------------------------------------------
@ -406,9 +412,11 @@ DohSetitem(DOH *obj, int index, const DOH *value) {
int
DohDelitem(DOH *obj, int index) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_list->doh_delitem) {
return (b->objinfo->doh_list->doh_delitem)(obj,index);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo->doh_list && objinfo->doh_list->doh_delitem) {
return (objinfo->doh_list->doh_delitem)(b,index);
}
return -1;
}
/* -----------------------------------------------------------------------------
@ -417,11 +425,12 @@ DohDelitem(DOH *obj, int index) {
int
DohInsertitem(DOH *obj, int index, const DOH *value) {
int no = 0;
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_list->doh_insitem) {
return (b->objinfo->doh_list->doh_insitem)(obj,index,(DOH *) value);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo->doh_list && objinfo->doh_list->doh_insitem) {
return (objinfo->doh_list->doh_insitem)(b,index,(DOH *) value);
}
return -1;
}
/* -----------------------------------------------------------------------------
@ -431,8 +440,9 @@ DohInsertitem(DOH *obj, int index, const DOH *value) {
DOH *
DohFirstitem(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_list->doh_firstitem) {
return (b->objinfo->doh_list->doh_firstitem)(obj);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo->doh_list && objinfo->doh_list->doh_firstitem) {
return (objinfo->doh_list->doh_firstitem)(b);
}
return 0;
}
@ -444,8 +454,9 @@ DohFirstitem(DOH *obj) {
DOH *
DohNextitem(DOH *obj) {
DohBase *b = (DohBase *) obj;
if (b->objinfo->doh_list->doh_nextitem) {
return (b->objinfo->doh_list->doh_nextitem)(obj);
DohObjInfo *objinfo = dohtypes[b->type];
if (objinfo->doh_list && objinfo->doh_list->doh_nextitem) {
return (objinfo->doh_list->doh_nextitem)(b);
}
return 0;
}
@ -457,8 +468,10 @@ DohNextitem(DOH *obj) {
int
DohIsFile(const DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (!DohCheck(b)) return 0;
if (b->objinfo->doh_file) return 1;
objinfo = dohtypes[b->type];
if (objinfo->doh_file) return 1;
else return 0;
}
@ -469,9 +482,11 @@ DohIsFile(const DOH *obj) {
int
DohRead(DOH *obj, void *buffer, int length) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (DohCheck(obj)) {
if ((b->objinfo->doh_file) && (b->objinfo->doh_file->doh_read)) {
return (b->objinfo->doh_file->doh_read)(obj,buffer,length);
objinfo = dohtypes[b->type];
if ((objinfo->doh_file) && (objinfo->doh_file->doh_read)) {
return (objinfo->doh_file->doh_read)(b,buffer,length);
}
return -1;
}
@ -486,9 +501,11 @@ DohRead(DOH *obj, void *buffer, int length) {
int
DohWrite(DOH *obj, void *buffer, int length) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (DohCheck(obj)) {
if ((b->objinfo->doh_file) && (b->objinfo->doh_file->doh_write)) {
return (b->objinfo->doh_file->doh_write)(obj,buffer,length);
objinfo = dohtypes[b->type];
if ((objinfo->doh_file) && (objinfo->doh_file->doh_write)) {
return (objinfo->doh_file->doh_write)(b,buffer,length);
}
return -1;
}
@ -503,9 +520,11 @@ DohWrite(DOH *obj, void *buffer, int length) {
int
DohSeek(DOH *obj, long offset, int whence) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (DohCheck(obj)) {
if ((b->objinfo->doh_file) && (b->objinfo->doh_file->doh_seek)) {
return (b->objinfo->doh_file->doh_seek)(obj,offset,whence);
objinfo = dohtypes[b->type];
if ((objinfo->doh_file) && (objinfo->doh_file->doh_seek)) {
return (objinfo->doh_file->doh_seek)(b,offset,whence);
}
return -1;
}
@ -519,9 +538,11 @@ DohSeek(DOH *obj, long offset, int whence) {
long
DohTell(DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (DohCheck(obj)) {
if ((b->objinfo->doh_file) && (b->objinfo->doh_file->doh_tell)) {
return (b->objinfo->doh_file->doh_tell)(obj);
objinfo = dohtypes[b->type];
if ((objinfo->doh_file) && (objinfo->doh_file->doh_tell)) {
return (objinfo->doh_file->doh_tell)(b);
}
return -1;
}
@ -535,9 +556,11 @@ DohTell(DOH *obj) {
int
DohGetc(DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (DohCheck(obj)) {
if (b->objinfo->doh_file->doh_getc) {
return (b->objinfo->doh_file->doh_getc)(obj);
objinfo = dohtypes[b->type];
if (objinfo->doh_file->doh_getc) {
return (objinfo->doh_file->doh_getc)(b);
}
return EOF;
}
@ -551,9 +574,11 @@ DohGetc(DOH *obj) {
int
DohPutc(int ch, DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (DohCheck(obj)) {
if (b->objinfo->doh_file->doh_putc) {
return (b->objinfo->doh_file->doh_putc)(obj,ch);
objinfo = dohtypes[b->type];
if (objinfo->doh_file->doh_putc) {
return (objinfo->doh_file->doh_putc)(b,ch);
}
return EOF;
}
@ -567,9 +592,11 @@ DohPutc(int ch, DOH *obj) {
int
DohUngetc(int ch, DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (DohCheck(obj)) {
if (b->objinfo->doh_file->doh_ungetc) {
return (b->objinfo->doh_file->doh_ungetc)(obj,ch);
objinfo = dohtypes[b->type];
if (objinfo->doh_file->doh_ungetc) {
return (objinfo->doh_file->doh_ungetc)(b,ch);
}
return EOF;
}
@ -583,9 +610,11 @@ DohUngetc(int ch, DOH *obj) {
int
DohClose(DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (DohCheck(obj)) {
if (b->objinfo->doh_file->doh_close) {
return (b->objinfo->doh_file->doh_close)(obj);
objinfo = dohtypes[b->type];
if (objinfo->doh_file->doh_close) {
return (objinfo->doh_file->doh_close)(b);
}
return 0;
}
@ -599,8 +628,10 @@ DohClose(DOH *obj) {
int
DohIsString(const DOH *obj) {
DohBase *b = (DohBase *) obj;
DohObjInfo *objinfo;
if (!DohCheck(b)) return 0;
if (b->objinfo->doh_string) return 1;
objinfo = dohtypes[b->type];
if (objinfo->doh_string) return 1;
else return 0;
}
@ -611,9 +642,11 @@ DohIsString(const DOH *obj) {
int
DohReplace(DOH *src, const DOH *token, const DOH *rep, int flags) {
DohBase *b = (DohBase *) src;
DohObjInfo *objinfo;
if (DohIsString(src)) {
if (b->objinfo->doh_string->doh_replace) {
return (b->objinfo->doh_string->doh_replace)(src,(DOH *) token, (DOH *) rep,flags);
objinfo = dohtypes[b->type];
if (objinfo->doh_string->doh_replace) {
return (objinfo->doh_string->doh_replace)(b,(DOH *) token, (DOH *) rep,flags);
}
}
return 0;
@ -626,37 +659,26 @@ DohReplace(DOH *src, const DOH *token, const DOH *rep, int flags) {
void
DohChop(DOH *src) {
DohBase *b = (DohBase *) src;
DohObjInfo *objinfo;
if (DohIsString(src)) {
if (b->objinfo->doh_string->doh_chop) {
(b->objinfo->doh_string->doh_chop)(src);
objinfo = dohtypes[b->type];
if (objinfo->doh_string->doh_chop) {
(objinfo->doh_string->doh_chop)(b);
}
}
}
/* -----------------------------------------------------------------------------
* DohInit()
* ----------------------------------------------------------------------------- */
void
DohInit(DOH *b) {
DohBase *bs = (DohBase *) b;
bs->refcount = 1;
bs->objinfo = &DohBaseType;
bs->flags = 0;
bs->file = 0;
bs->line = 0;
}
/* -----------------------------------------------------------------------------
* DohSetFile()
* ----------------------------------------------------------------------------- */
void
DohSetfile(DOH *ho, DOH *file) {
DohBase *h = (DohBase *) ho;
DohObjInfo *objinfo;
if (!h) return;
if (!DohCheck(file)) file = NewString(file);
h->file = file;
Incref(h->file);
objinfo = dohtypes[h->type];
if (objinfo->doh_setfile)
(objinfo->doh_setfile)(h,file);
}
/* -----------------------------------------------------------------------------
@ -664,8 +686,13 @@ DohSetfile(DOH *ho, DOH *file) {
* ----------------------------------------------------------------------------- */
DOH *
DohGetfile(DOH *ho) {
if (!ho) return 0;
return ((DohBase *)ho)->file;
DohBase *h = (DohBase *) ho;
DohObjInfo *objinfo;
if (!h) return;
objinfo = dohtypes[h->type];
if (objinfo->doh_getfile)
return (objinfo->doh_getfile)(h);
return 0;
}
/* -----------------------------------------------------------------------------
@ -673,8 +700,12 @@ DohGetfile(DOH *ho) {
* ----------------------------------------------------------------------------- */
void
DohSetline(DOH *ho, int l) {
if (!ho) return;
((DohBase *) ho)->line = l;
DohBase *h = (DohBase *) ho;
DohObjInfo *objinfo;
if (!h) return;
objinfo = dohtypes[h->type];
if (objinfo->doh_setline)
(objinfo->doh_setline)(h,l);
}
/* -----------------------------------------------------------------------------
@ -682,8 +713,13 @@ DohSetline(DOH *ho, int l) {
* ----------------------------------------------------------------------------- */
int
DohGetline(DOH *ho) {
if (!ho) return 0;
return ((DohBase *)ho)->line;
DohBase *h = (DohBase *) ho;
DohObjInfo *objinfo;
if (!h) return;
objinfo = dohtypes[h->type];
if (objinfo->doh_getline)
return (objinfo->doh_getline)(h);
return 0;
}

View file

@ -13,116 +13,81 @@
static char cvsroot[] = "$Header$";
#include "dohint.h"
#include <unistd.h>
typedef struct {
DOHCOMMON;
FILE *filep;
int fd;
int closeondel;
int closeondel;
} DohFile;
/* -----------------------------------------------------------------------------
* DelFile()
*
* Delete a file. Closes the file if the closeondel flag is set.
* ----------------------------------------------------------------------------- */
static void
DelFile(DOH *so) {
DohFile *f = (DohFile *) so;
assert(f->refcount <= 0);
DelFile(DOH *fo) {
DohFile *f = (DohFile *) ObjData(fo);
if (f->closeondel)
fclose(f->filep);
DohObjFree(f);
}
/* -----------------------------------------------------------------------------
* File_read()
*
* Reads data from a file into a buffer.
* ----------------------------------------------------------------------------- */
static int
File_read(DOH *so, void *buffer, int len) {
DohFile *s = (DohFile *) so;
if (s->filep)
return fread(buffer,1,len,s->filep);
else
return read(s->fd,buffer,len);
File_read(DOH *fo, void *buffer, int len) {
DohFile *f = (DohFile *) ObjData(fo);
return fread(buffer,1,len,f->filep);
}
/* -----------------------------------------------------------------------------
* File_write()
*
* Write data from a buffer to a file.
* ----------------------------------------------------------------------------- */
static int
File_write(DOH *so, void *buffer, int len) {
DohFile *s = (DohFile *) so;
if (s->filep)
return fwrite(buffer,1,len,s->filep);
else
return write(s->fd, buffer, len);
File_write(DOH *fo, void *buffer, int len) {
DohFile *f = (DohFile *) ObjData(fo);
return fwrite(buffer,1,len,f->filep);
}
/* -----------------------------------------------------------------------------
* File_seek()
*
* Seek to a new file position.
* ----------------------------------------------------------------------------- */
static int
File_seek(DOH *so, long offset, int whence) {
DohFile *s = (DohFile *) so;
if (s->filep)
return fseek(s->filep,offset,whence);
else
return lseek(s->fd,offset,whence);
File_seek(DOH *fo, long offset, int whence) {
DohFile *f = (DohFile *) ObjData(fo);
return fseek(f->filep,offset,whence);
}
/* -----------------------------------------------------------------------------
* File_tell()
*
* Return current file pointer.
* ----------------------------------------------------------------------------- */
static long
File_tell(DOH *so) {
DohFile *s = (DohFile *) so;
if (s->filep)
return ftell(s->filep);
else
return lseek(s->fd,0,SEEK_CUR);
File_tell(DOH *fo) {
DohFile *f = (DohFile *) ObjData(fo);
return ftell(f->filep);
}
/* -----------------------------------------------------------------------------
* File_putc()
*
* Put a character on the output
* ----------------------------------------------------------------------------- */
static int
File_putc(DOH *obj, int ch) {
DohFile *s = (DohFile *) obj;
if (s->filep)
return fputc(ch,s->filep);
return EOF;
File_putc(DOH *fo, int ch) {
DohFile *f = (DohFile *) ObjData(fo);
return fputc(ch,f->filep);
}
/* -----------------------------------------------------------------------------
* File_getc()
*
* Get a character
* ----------------------------------------------------------------------------- */
static int
File_getc(DOH *obj) {
DohFile *s = (DohFile *) obj;
if (s->filep)
return fgetc(s->filep);
return EOF;
File_getc(DOH *fo) {
DohFile *f = (DohFile *) ObjData(fo);
return fgetc(f->filep);
}
/* -----------------------------------------------------------------------------
@ -132,11 +97,9 @@ File_getc(DOH *obj) {
* ----------------------------------------------------------------------------- */
static int
File_ungetc(DOH *obj, int ch) {
DohFile *s = (DohFile *) obj;
if (s->filep)
return ungetc(ch, s->filep);
return EOF;
File_ungetc(DOH *fo, int ch) {
DohFile *f = (DohFile *) ObjData(fo);
return ungetc(ch, f->filep);
}
static DohFileMethods FileFileMethods = {
@ -152,17 +115,19 @@ static DohFileMethods FileFileMethods = {
static DohObjInfo DohFileType = {
"DohFile", /* objname */
sizeof(DohFile), /* objsize */
DelFile, /* doh_del */
0, /* doh_copy */
0, /* doh_clear */
0, /* doh_str */
0, /* doh_data */
0, /* doh_dump */
0, /* doh_load */
0, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_setfile */
0, /* doh_getfile */
0, /* doh_setline */
0, /* doh_getline */
0, /* doh_mapping */
0, /* doh_sequence */
&FileFileMethods,/* doh_file */
@ -183,21 +148,25 @@ NewFile(DOH *fn, char *mode)
DohFile *f;
FILE *file;
char *filename;
static int init = 0;
if (!init) {
DohRegisterType(DOHTYPE_FILE, &DohFileType);
init = 1;
}
filename = Char(fn);
file = fopen(filename,mode);
if (!file) return 0;
f = (DohFile *) DohObjMalloc(sizeof(DohFile));
f = (DohFile *) DohMalloc(sizeof(DohFile));
if (!f) {
fclose(file);
return 0;
}
f->objinfo = &DohFileType;
f->filep = file;
f->fd = fileno(file);
f->closeondel = 1;
return (DOH *) f;
return DohObjMalloc(DOHTYPE_FILE,f);
}
/* -----------------------------------------------------------------------------
@ -210,52 +179,14 @@ DOH *
NewFileFromFile(FILE *file)
{
DohFile *f;
f = (DohFile *) DohObjMalloc(sizeof(DohFile));
static int init = 0;
if (!init) {
DohRegisterType(DOHTYPE_FILE, &DohFileType);
init = 1;
}
f = (DohFile *) DohMalloc(sizeof(DohFile));
if (!f) return 0;
f->objinfo = &DohFileType;
f->filep = file;
f->fd = fileno(file);
f->closeondel = 0;
return (DOH *) f;
return DohObjMalloc(DOHTYPE_FILE,f);
}
/* -----------------------------------------------------------------------------
* NewFileFromFd()
*
* Create a file object from an already existing integer file descriptor.
* ----------------------------------------------------------------------------- */
DOH *
NewFileFromFd(int fd)
{
DohFile *f;
f = (DohFile *) DohObjMalloc(sizeof(DohFile));
if (!f) return 0;
f->objinfo = &DohFileType;
f->filep = 0;
f->fd = fd;
f->closeondel = 0;
return (DOH *) f;
}
/* -----------------------------------------------------------------------------
* File_check()
*
* Check if an object is a file.
* ----------------------------------------------------------------------------- */
int
File_check(DOH *f)
{
DohFile *df;
if (!DohCheck(f)) return 0;
df = (DohFile *) f;
if (df->objinfo == &DohFileType) return 1;
return 0;
}

View file

@ -265,7 +265,7 @@ DohvPrintf(DOH *so, const char *format, va_list ap)
doh = va_arg(ap, DOH *);
if (DohCheck(doh)) {
/* Is a DOH object. */
if (String_check(doh)) {
if (DohIsString(doh)) {
Sval = doh;
} else {
Sval = Str(doh);
@ -435,9 +435,11 @@ DohSplit(DOH *in, char *chs, int nsplits) {
int c;
list = NewList();
if (String_check(in)) {
/* if (String_check(in)) {
Seek(in,0,SEEK_SET);
}
*/
while (1) {
str = NewString("");
do {

View file

@ -22,12 +22,13 @@ typedef struct HashNode {
/* Hash object */
typedef struct Hash {
DOHCOMMON;
HashNode **hashtable;
int hashsize;
int currentindex;
int nitems;
HashNode *current;
DOH *file;
int line;
HashNode **hashtable;
int hashsize;
int currentindex;
int nitems;
HashNode *current;
} Hash;
/* Key interning structure */
@ -98,11 +99,10 @@ static void DelNode(HashNode *hn) {
static void
DelHash(DOH *ho) {
Hash *h;
Hash *h = (Hash *) ObjData(ho);
HashNode *n,*next;
int i;
h = (Hash *) ho;
for (i = 0; i < h->hashsize; i++) {
if ((n = h->hashtable[i])) {
while (n) {
@ -115,7 +115,7 @@ DelHash(DOH *ho) {
DohFree(h->hashtable);
h->hashtable = 0;
h->hashsize = 0;
DohObjFree(h);
DohFree(h);
}
/* -----------------------------------------------------------------------------
@ -126,11 +126,10 @@ DelHash(DOH *ho) {
static void
Hash_clear(DOH *ho) {
Hash *h;
Hash *h = (Hash *) ObjData(ho);
HashNode *n,*next;
int i;
h = (Hash *) ho;
for (i = 0; i < h->hashsize; i++) {
if ((n = h->hashtable[i])) {
while (n) {
@ -199,7 +198,7 @@ static int
Hash_setattr(DOH *ho, DOH *k, DOH *obj) {
int hv;
HashNode *n, *prev;
Hash *h;
Hash *h = (Hash *) ObjData(ho);
if (!obj) return 0;
if (!DohCheck(k)) k = find_key(k);
@ -207,7 +206,6 @@ Hash_setattr(DOH *ho, DOH *k, DOH *obj) {
obj = NewString((char *) obj);
Decref(obj);
}
h = (Hash *) ho;
hv = (Hashval(k)) % h->hashsize;
n = h->hashtable[hv];
prev = 0;
@ -247,10 +245,9 @@ static DOH *
Hash_getattr(DOH *ho, DOH *k) {
int hv;
HashNode *n;
Hash *h;
Hash *h = (Hash *) ObjData(ho);
if (!DohCheck(k)) k = find_key(k);
h = (Hash *) ho;
hv = Hashval(k) % h->hashsize;
n = h->hashtable[hv];
while (n) {
@ -270,10 +267,9 @@ static int
Hash_delattr(DOH *ho, DOH *k) {
HashNode *n, *prev;
int hv;
Hash *h;
Hash *h = (Hash *) ObjData(ho);
if (!DohCheck(k)) k = find_key(k);
h = (Hash *) ho;
hv = Hashval(k) % h->hashsize;
n = h->hashtable[hv];
prev = 0;
@ -298,7 +294,7 @@ Hash_delattr(DOH *ho, DOH *k) {
/* General purpose iterators */
static HashNode *
hash_first(DOH *ho) {
Hash *h = (Hash *) ho;
Hash *h = (Hash *) ObjData(ho);
h->currentindex = 0;
h->current = 0;
while (!h->hashtable[h->currentindex] && (h->currentindex < h->hashsize))
@ -310,7 +306,7 @@ hash_first(DOH *ho) {
static HashNode *
hash_next(DOH *ho) {
Hash *h = (Hash *) ho;
Hash *h = (Hash *) ObjData(ho);
if (h->currentindex < 0) return hash_first(h);
/* Try to move to the next entry */
@ -363,15 +359,14 @@ Hash_str(DOH *ho) {
int i;
HashNode *n;
DOH *s;
Hash *h;
Hash *h = (Hash *) ObjData(ho);
h = (Hash *) ho;
s = NewString("");
if (h->flags & DOH_FLAG_PRINT) {
Printf(s,"Hash(0x%x)",h);
if (ObjGetMark(ho)) {
Printf(s,"Hash(0x%x)",ho);
return s;
}
h->flags = h->flags | DOH_FLAG_PRINT;
ObjSetMark(ho,1);
Printf(s,"Hash {\n");
for (i = 0; i < h->hashsize; i++) {
n = h->hashtable[i];
@ -381,7 +376,7 @@ Hash_str(DOH *ho) {
}
}
Printf(s,"}\n");
h->flags = h->flags & ~DOH_FLAG_PRINT;
ObjSetMark(ho,0);
return s;
}
@ -393,7 +388,7 @@ Hash_str(DOH *ho) {
static int
Hash_len(DOH *ho) {
Hash *h = (Hash *) ho;
Hash *h = (Hash *) ObjData(ho);
return h->nitems;
}
@ -428,9 +423,8 @@ CopyHash(DOH *ho) {
Hash *h, *nh;
HashNode *n;
int i;
h = (Hash *) ho;
nh = (Hash *) DohObjMalloc(sizeof(Hash));
nh->objinfo = h->objinfo;
h = (Hash *) ObjData(ho);
nh = (Hash *) DohMalloc(sizeof(Hash));
nh->hashsize = h->hashsize;
nh->hashtable = (HashNode **) DohMalloc(nh->hashsize*sizeof(HashNode *));
for (i = 0; i < nh->hashsize; i++) {
@ -451,7 +445,7 @@ CopyHash(DOH *ho) {
}
}
}
return (DOH *) nh;
return DohObjMalloc(DOHTYPE_HASH, nh);
}
/* -----------------------------------------------------------------------------
@ -468,40 +462,27 @@ static DohHashMethods HashHashMethods = {
static DohObjInfo HashType = {
"Hash", /* objname */
sizeof(Hash), /* size */
DelHash, /* doh_del */
CopyHash, /* doh_copy */
Hash_clear, /* doh_clear */
Hash_str, /* doh_str */
0, /* doh_data */
0, /* doh_dump */
0, /* doh_load */
Hash_len, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_setfile */
0, /* doh_getfile */
0, /* doh_setline */
0, /* doh_getline */
&HashHashMethods, /* doh_mapping */
0, /* doh_sequence */
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
0, /* doh_positional */
0,
};
/* -----------------------------------------------------------------------------
* Hash_check()
*
* Return 1 if an object is a hash table object.
* ----------------------------------------------------------------------------- */
int
Hash_check(const DOH *so) {
Hash *h = (Hash *) so;
if (!h) return 0;
if (!DohCheck(so)) return 0;
if (h->objinfo != &HashType) return 0;
return 1;
}
/* -----------------------------------------------------------------------------
* NewHash()
*
@ -512,8 +493,12 @@ DOH *
NewHash() {
Hash *h;
int i;
h = (Hash *) DohObjMalloc(sizeof(Hash));
h->objinfo = &HashType;
static int init = 0;
if (!init) {
DohRegisterType(DOHTYPE_HASH, &HashType);
init = 1;
}
h = (Hash *) DohMalloc(sizeof(Hash));
h->hashsize = HASH_INIT_SIZE;
h->hashtable = (HashNode **) DohMalloc(h->hashsize*sizeof(HashNode *));
for (i = 0; i < h->hashsize; i++) {
@ -522,5 +507,7 @@ NewHash() {
h->currentindex = -1;
h->current = 0;
h->nitems = 0;
return (DOH *) h;
h->file = 0;
h->line = 0;
return DohObjMalloc(DOHTYPE_HASH,h);
}

View file

@ -14,10 +14,11 @@ static char cvsroot[] = "$Header$";
#include "dohint.h"
typedef struct List {
DOHCOMMON;
int maxitems; /* Max size */
int nitems; /* Num items */
int iter; /* Iterator */
DOH *file;
int line;
DOH **items;
} List;
@ -38,9 +39,8 @@ static DOH *
CopyList(DOH *lo) {
List *l,*nl;
int i;
l = (List *) lo;
nl = (List *) DohObjMalloc(sizeof(List));
nl->objinfo = l->objinfo;
l = (List *) ObjData(lo);
nl = (List *) DohMalloc(sizeof(List));
nl->nitems = l->nitems;
nl->maxitems = l->maxitems;
nl->items = (void **) DohMalloc(l->maxitems*sizeof(void *));
@ -52,7 +52,7 @@ CopyList(DOH *lo) {
nl->file = l->file;
if (nl->file) Incref(nl->file);
nl->line = l->line;
return (DOH *) nl;
return DohObjMalloc(DOHTYPE_LIST, nl);
}
/* -----------------------------------------------------------------------------
@ -63,13 +63,12 @@ CopyList(DOH *lo) {
static void
DelList(DOH *lo) {
List *l;
List *l = (List *) ObjData(lo);
int i;
l = (List *) lo;
for (i = 0; i < l->nitems; i++)
Delete(l->items[i]);
DohFree(l->items);
DohObjFree(l);
DohFree(l);
}
/* -----------------------------------------------------------------------------
@ -80,9 +79,8 @@ DelList(DOH *lo) {
static void
List_clear(DOH *lo) {
List *l;
List *l = (List *) lo;
int i;
l = (List *) lo;
for (i = 0; i < l->nitems; i++) {
Delete(l->items[i]);
}
@ -98,18 +96,14 @@ List_clear(DOH *lo) {
static int
List_insert(DOH *lo, int pos, DOH *item) {
List *l;
DohBase *b;
List *l = (List *) ObjData(lo);
int i;
if (!item) return -1;
l = (List *) lo;
if (!DohCheck(item)) {
item = NewString(item);
Decref(item);
}
b = (DohBase *) item;
if (pos == DOH_END) pos = l->nitems;
if (pos < 0) pos = 0;
if (pos > l->nitems) pos = l->nitems;
@ -118,7 +112,7 @@ List_insert(DOH *lo, int pos, DOH *item) {
l->items[i] = l->items[i-1];
}
l->items[pos] = item;
b->refcount++;
Incref(item);
l->nitems++;
return 0;
}
@ -131,9 +125,8 @@ List_insert(DOH *lo, int pos, DOH *item) {
static int
List_remove(DOH *lo, int pos) {
List *l;
List *l = (List *) ObjData(lo);
int i;
l = (List *) lo;
if (pos == DOH_END) pos = l->nitems-1;
if (pos == DOH_BEGIN) pos = 0;
assert((pos < 0) || (pos >= l->nitems));
@ -153,7 +146,8 @@ List_remove(DOH *lo, int pos) {
static int
List_len(DOH *lo) {
return ((List *) lo)->nitems;
List *l = (List *) ObjData(lo);
return l->nitems;
}
/* -----------------------------------------------------------------------------
@ -164,8 +158,7 @@ List_len(DOH *lo) {
static DOH *
List_get(DOH *lo, int n) {
List *l;
l = (List *) lo;
List *l = (List *) ObjData(lo);
if (n == DOH_END) n = l->nitems-1;
if (n == DOH_BEGIN) n = 0;
assert((n < 0) || (n >= l->nitems));
@ -180,8 +173,7 @@ List_get(DOH *lo, int n) {
static int
List_set(DOH *lo, int n, DOH *val) {
List *l;
l = (List *) lo;
List *l = (List *) ObjData(lo);
if (!val) return -1;
assert((n < 0) || (n >= l->nitems));
if (!DohCheck(val)) {
@ -203,8 +195,7 @@ List_set(DOH *lo, int n, DOH *val) {
static DOH *
List_first(DOH *lo) {
List *l;
l = (List *) lo;
List *l = (List *) ObjData(lo);
l->iter = 0;
if (l->iter >= l->nitems) return 0;
return l->items[l->iter];
@ -218,8 +209,7 @@ List_first(DOH *lo) {
static DOH *
List_next(DOH *lo) {
List *l;
l = (List *) lo;
List *l = (List *) ObjData(lo);
l->iter++;
if (l->iter >= l->nitems) return 0;
return l->items[l->iter];
@ -228,20 +218,19 @@ List_next(DOH *lo) {
/* -----------------------------------------------------------------------------
* List_str()
*
* Create a string representation of the list
* Create a string representation of the list.
* ----------------------------------------------------------------------------- */
static DOH *
List_str(DOH *lo) {
DOH *s;
int i;
List *l = (List *) lo;
List *l = (List *) ObjData(lo);
s = NewString("");
if (l->flags & DOH_FLAG_PRINT) {
Printf(s,"List(0x%x)",l);
if (ObjGetMark(lo)) {
Printf(s,"List(%x)", lo);
return s;
}
l->flags = l->flags | DOH_FLAG_PRINT;
ObjSetMark(lo,1);
Printf(s,"List[ ");
for (i = 0; i < l->nitems; i++) {
Printf(s, "%s", l->items[i]);
@ -249,7 +238,7 @@ List_str(DOH *lo) {
Printf(s,", ");
}
Printf(s," ]\n");
l->flags = l->flags & ~DOH_FLAG_PRINT;
ObjSetMark(lo,0);
return s;
}
@ -263,7 +252,7 @@ static int
List_dump(DOH *lo, DOH *out) {
int nsent = 0;
int i,ret;
List *l = (List *) lo;
List *l = (List *) ObjData(lo);
for (i = 0; i < l->nitems; i++) {
ret = Dump(l->items[i],out);
if (ret < 0) return -1;
@ -272,80 +261,12 @@ List_dump(DOH *lo, DOH *out) {
return nsent;
}
#define MAXLISTITEMS 8
static DohListMethods ListListMethods = {
List_get,
List_set,
List_remove,
List_insert,
List_first,
List_next,
};
static DohObjInfo ListType = {
"List", /* objname */
sizeof(List), /* List size */
DelList, /* doh_del */
CopyList, /* doh_copy */
List_clear, /* doh_clear */
List_str, /* doh_str */
0, /* doh_data */
List_dump, /* doh_dump */
0, /* doh_load */
List_len, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_mapping */
&ListListMethods, /* doh_sequence */
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
0, /* doh_position */
};
/* -----------------------------------------------------------------------------
* List_check()
*
* Return 1 if an object is a List object.
* ----------------------------------------------------------------------------- */
int
List_check(const DOH *lo) {
List *l = (List *) lo;
if (!l) return 0;
if (!DohCheck(lo)) return 0;
if (l->objinfo != &ListType) return 0;
return 1;
}
/* -----------------------------------------------------------------------------
* NewList()
*
* Create a new list.
* ----------------------------------------------------------------------------- */
DOH *
NewList() {
List *l;
int i;
l = (List *) DohObjMalloc(sizeof(List));
l->objinfo = &ListType;
l->nitems = 0;
l->maxitems = MAXLISTITEMS;
l->items = (void **) DohMalloc(l->maxitems*sizeof(void *));
for (i = 0; i < MAXLISTITEMS; i++) {
l->items[i] = 0;
}
l->iter = 0;
return (DOH *) l;
}
/* -----------------------------------------------------------------------------
* List_sort()
*
* Sorts a list
* ----------------------------------------------------------------------------- */
static int objcmp(const void *s1, const void *s2) {
DOH **so1, **so2;
so1 = (DOH **) s1;
@ -354,9 +275,72 @@ static int objcmp(const void *s1, const void *s2) {
}
void
List_sort(DOH *so) {
List *l;
if (!List_check(so)) return;
l = (List *) so;
qsort(l->items,l->nitems,sizeof(DOH *), objcmp);
List_sort(DOH *lo, int opt) {
List *l = (List *) ObjData(lo);
qsort(l->items,l->nitems,sizeof(DOH *),objcmp);
}
static DohListMethods ListListMethods = {
List_get,
List_set,
List_remove,
List_insert,
List_first,
List_next,
List_sort
};
static DohObjInfo ListType = {
"List", /* objname */
DelList, /* doh_del */
CopyList, /* doh_copy */
List_clear, /* doh_clear */
List_str, /* doh_str */
0, /* doh_data */
List_dump, /* doh_dump */
List_len, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_setfile */
0, /* doh_getfile */
0, /* doh_setline */
0, /* doh_getline */
0, /* doh_mapping */
&ListListMethods, /* doh_sequence */
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
0, /* doh_position */
};
/* -----------------------------------------------------------------------------
* NewList()
*
* Create a new list.
* ----------------------------------------------------------------------------- */
#define MAXLISTITEMS 8
DOH *
NewList() {
List *l;
int i;
static int init = 0;
if (!init) {
DohRegisterType(DOHTYPE_LIST, &ListType);
init = 1;
}
l = (List *) DohMalloc(sizeof(List));
l->nitems = 0;
l->maxitems = MAXLISTITEMS;
l->items = (void **) DohMalloc(l->maxitems*sizeof(void *));
for (i = 0; i < MAXLISTITEMS; i++) {
l->items[i] = 0;
}
l->iter = 0;
l->file = 0;
l->line = 0;
return DohObjMalloc(DOHTYPE_LIST,l);
}

View file

@ -15,25 +15,22 @@ static char cvsroot[] = "$Header$";
#include "dohint.h"
#ifndef DOH_POOL_SIZE
#define DOH_POOL_SIZE 128000
#endif
#ifndef DOH_MAX_FRAG
#define DOH_MAX_FRAG 1024
#define DOH_POOL_SIZE 16384
#endif
static int PoolSize = DOH_POOL_SIZE;
DOH *DohNone = 0; /* The DOH None object */
static DohBase *FreeFragments[DOH_MAX_FRAG]; /* Array of free memory fragments */
typedef struct pool {
char *ptr; /* Start of pool */
DohBase *ptr; /* Start of pool */
int len; /* Length of pool */
int current; /* Current position for next allocation */
struct pool *next; /* Next pool */
} Pool;
DohBase *FreeList = 0; /* List of free objects */
static Pool *Pools = 0;
static int pools_initialized = 0;
@ -41,18 +38,15 @@ static int pools_initialized = 0;
* CreatePool() - Create a new memory pool
* ---------------------------------------------------------------------- */
static Pool *
CreatePool(int size) {
static void
CreatePool() {
Pool *p = 0;
char *c;
c = (char *) DohMalloc(size);
if (!c) return 0;
p = (Pool *) DohMalloc(sizeof(Pool));
p->ptr = c;
p->len = size;
assert((p = (Pool *) DohMalloc(sizeof(Pool))));
assert((p->ptr = (DohBase *) DohMalloc(sizeof(DohBase)*PoolSize)));
p->len = PoolSize;
p->current = 0;
p->next = 0;
return p;
p->next = Pools;
Pools = p;
}
/* ----------------------------------------------------------------------
@ -63,10 +57,7 @@ static void
InitPools() {
int i;
if (pools_initialized) return;
for (i = 0; i < DOH_MAX_FRAG; i++) {
FreeFragments[i] = 0;
}
Pools = CreatePool(PoolSize); /* Create initial pool */
CreatePool(); /* Create initial pool */
pools_initialized = 1;
DohNone = NewVoid(0,0); /* Create the None object */
DohIntern(DohNone);
@ -78,59 +69,54 @@ InitPools() {
* Returns 1 if an arbitrary pointer is a DOH object.
* ---------------------------------------------------------------------- */
/* Possible optimization: Reorder pools to move last match to the beginning */
int
DohCheck(const DOH *ptr) {
Pool *p = Pools;
char *cptr = (char *) ptr;
while (p) {
if ((cptr >= p->ptr) && (cptr < p->ptr+p->current)) {
return 1;
}
if ((cptr >= (char *) p->ptr) && (cptr < ((char *) p->ptr)+(p->current*sizeof(DohBase)))) return 1;
p = p->next;
}
return 0;
}
/* -----------------------------------------------------------------------------
* DohIntern()
* ----------------------------------------------------------------------------- */
void
DohIntern(DOH *obj) {
DohBase *b = (DohBase *) obj;
b->flag_intern = 1;
}
/* ----------------------------------------------------------------------
* DohObjMalloc()
*
* Allocate memory for a new object.
* ---------------------------------------------------------------------- */
void *
DohObjMalloc(size_t size) {
Pool *p;
DohBase *f;
void *ptr = 0;
if (size > DOH_MAX_FRAG) return 0;
DOH *
DohObjMalloc(int type, void *data) {
DohBase *obj;
if (!pools_initialized) InitPools();
/* adjust the size for double word alignment */
size = (size + 7) & ~0x07;
p = Pools;
f = FreeFragments[size];
if (f) {
ptr = (void *) f;
FreeFragments[size] = (DohBase *) f->file;
DohInit(ptr);
return ptr;
if (FreeList) {
obj = FreeList;
FreeList = (DohBase *) obj->data;
} else {
while (Pools->current == Pools->len) {
PoolSize *= 2;
CreatePool();
}
obj = Pools->ptr + Pools->current;
Pools->current++;
}
/* No free fragments. See if the pool is large enough */
if ((int) size < (p->len - p->current)) {
ptr = (void *) (p->ptr + p->current);
p->current = p->current + size;
DohInit(ptr);
return ptr;
}
PoolSize *= 2;
p = CreatePool(PoolSize);
p->next = Pools;
Pools = p;
return DohObjMalloc(size);
obj->type = type;
obj->data = data;
obj->refcount = 1;
obj->flag_intern = 0;
obj->flag_marked = 0;
return (DOH *) obj;
}
/* ----------------------------------------------------------------------
@ -140,13 +126,9 @@ DohObjMalloc(size_t size) {
void
DohObjFree(DOH *ptr) {
DohBase *b;
int len;
b = (DohBase *) ptr;
if (b->flags & DOH_FLAG_INTERN) return;
if (b->file) DohDelete(b->file);
len = (b->objinfo->objsize + 7) & ~0x07;
b->file = (DOH *) FreeFragments[len];
FreeFragments[len] = b;
b->objinfo = 0;
b->flags = b->flags | DOH_FLAG_DELETED;
if (b->flag_intern) return;
b->data = (void *) FreeList;
b->type = 0;
FreeList = b;
}

File diff suppressed because it is too large Load diff

View file

@ -15,7 +15,6 @@ static char cvsroot[] = "$Header$";
#include "dohint.h"
typedef struct {
DOHCOMMON;
void *ptr;
void (*del)(void *);
} VoidObj;
@ -28,11 +27,10 @@ typedef struct {
static void
Void_delete(DOH *vo) {
VoidObj *v = (VoidObj *) vo;
VoidObj *v = (VoidObj *) ObjData(vo);
if (v->del)
(*v->del)(v->ptr);
v->del = 0;
DohObjFree(v);
DohFree(v);
}
/* -----------------------------------------------------------------------------
@ -44,7 +42,7 @@ Void_delete(DOH *vo) {
static DOH *
Void_copy(DOH *vo) {
VoidObj *v = (VoidObj *) vo;
VoidObj *v = (VoidObj *) ObjData(vo);
return NewVoid(v->ptr,0);
}
@ -56,29 +54,31 @@ Void_copy(DOH *vo) {
static void *
Void_data(DOH *vo) {
VoidObj *v = (VoidObj *) vo;
VoidObj *v = (VoidObj *) ObjData(vo);
return v->ptr;
}
static DohObjInfo DohVoidType = {
"VoidObj", /* objname */
sizeof(VoidObj), /* objsize */
Void_delete, /* doh_del */
Void_copy, /* doh_copy */
0, /* doh_clear */
0, /* doh_str */
Void_data, /* doh_data */
0, /* doh_dump */
0, /* doh_load */
0, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_setfile */
0, /* doh_getfile */
0, /* doh_setline */
0, /* doh_getline */
0, /* doh_mapping */
0, /* doh_sequence */
0, /* doh_file */
0, /* doh_string */
0, /* doh_reserved1 */
0, /* doh_reserved2 */
0, /* doh_reserved */
0, /* clientdata */
};
/* -----------------------------------------------------------------------------
@ -89,10 +89,14 @@ static DohObjInfo DohVoidType = {
DOH *
NewVoid(void *obj, void (*del)(void *)) {
static int init = 0;
VoidObj *v;
v = (VoidObj *) DohObjMalloc(sizeof(VoidObj));
v->objinfo = &DohVoidType;
if (!init) {
DohRegisterType(DOHTYPE_VOID, &DohVoidType);
init = 1;
}
v = (VoidObj *) DohMalloc(sizeof(VoidObj));
v->ptr = obj;
v->del = del;
return (DOH *) v;
return DohObjMalloc(DOHTYPE_VOID,v);
}

View file

@ -17,10 +17,6 @@
#include <stdio.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
#define DOH_MAJOR_VERSION 0
#define DOH_MINOR_VERSION 1
@ -40,89 +36,10 @@ typedef void DOH;
#define DOHString_or_char DOH
#define DOHObj_or_char DOH
#define DOH_BEGIN -1
#define DOH_END -2
#define DOH_CUR -3
#define DOH_CURRENT -3
/* -----------------------------------------------------------------------------
* These structures define the interface to various categories of objects.
* ----------------------------------------------------------------------------- */
/* Hash 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 */
} DohHashMethods;
/* List objects */
typedef struct {
DOH *(*doh_getitem)(DOH *obj, int index); /* Get item */
int (*doh_setitem)(DOH *obj, int index, DOH *value); /* Set item */
int (*doh_delitem)(DOH *obj, int index); /* Delete item */
int (*doh_insitem)(DOH *obj, int index, DOH *value); /* Insert item */
DOH *(*doh_firstitem)(DOH *obj); /* Iterators */
DOH *(*doh_nextitem)(DOH *obj);
} DohListMethods;
/* File methods */
typedef struct {
int (*doh_read)(DOH *obj, void *buffer, int nbytes); /* Read bytes */
int (*doh_write)(DOH *obj, void *buffer, int nbytes); /* Write bytes */
int (*doh_putc)(DOH *obj, int ch); /* Put character */
int (*doh_getc)(DOH *obj); /* Get character */
int (*doh_ungetc)(DOH *obj, int ch); /* Unget character */
int (*doh_seek)(DOH *obj, long offset, int whence); /* Seek */
long (*doh_tell)(DOH *obj); /* Tell */
int (*doh_close)(DOH *obj); /* Close */
} DohFileMethods;
/* String methods */
typedef struct {
int (*doh_replace)(DOH *obj, DOH *old, DOH *rep, int flags);
void (*doh_chop)(DOH *obj);
} DohStringMethods;
/* -----------------------------------------------------------------------------
* DohObjInfo
*
* A pointer to this structure is included in all DOH types and is used to
* describe the properties of various objects.
* ----------------------------------------------------------------------------- */
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 */
int (*doh_dump)(DOH *obj, DOH *out); /* Serialize on out */
DOH *(*doh_load)(DOH *in); /* Unserialize from in */
/* Length and hash values */
int (*doh_len)(DOH *obj);
int (*doh_hashval)(DOH *obj);
/* Compare */
int (*doh_cmp)(DOH *obj1, DOH *obj2);
DohHashMethods *doh_hash; /* Hash methods */
DohListMethods *doh_list; /* List methods */
DohFileMethods *doh_file; /* File methods */
DohStringMethods *doh_string; /* String methods */
void *reserved1;
void *reserved2;
} DohObjInfo;
#define DOH_BEGIN -1
#define DOH_END -2
#define DOH_CUR -3
#define DOH_CURRENT -3
/* Memory management */
@ -136,83 +53,80 @@ typedef struct DohObjInfo {
#define DohFree free
#endif
extern void *DohObjMalloc(size_t size); /* Allocate a DOH object */
extern void DohObjFree(DOH *ptr); /* Free a DOH object */
extern void DohInit(DOH *obj); /* Initialize an object */
extern int DohCheck(const DOH *ptr); /* Check if a DOH object */
extern void DohIntern(DOH *); /* Intern an object */
extern int DohCheck(const DOH *ptr); /* Check if a DOH object */
extern void DohIntern(DOH *); /* Intern an object */
/* Basic object methods. Common to most objects */
/* Basic object methods. Common to most objects */
extern void DohDelete(DOH *obj); /* Delete an object */
extern DOH *DohCopy(const DOH *obj);
extern void DohClear(DOH *obj);
extern DOHString *DohStr(const DOH *obj);
extern void *DohData(const DOH *obj);
extern int DohDump(const DOH *obj, DOHFile *out);
extern int DohLen(const DOH *obj);
extern int DohHashval(const DOH *obj);
extern int DohCmp(const DOH *obj1, const DOH *obj2);
extern void DohDelete(DOH *obj); /* Delete an object */
extern DOH *DohCopy(const DOH *obj);
extern void DohClear(DOH *obj);
extern DOHString *DohStr(const DOH *obj);
extern void *DohData(const DOH *obj);
extern int DohDump(const DOH *obj, DOHFile *out);
extern int DohLen(const DOH *obj);
extern int DohHashval(const DOH *obj);
extern int DohCmp(const DOH *obj1, const DOH *obj2);
/* Mapping methods */
/* Mapping methods */
extern DOH *DohGetattr(DOH *obj, const DOHString_or_char *name);
extern int DohSetattr(DOH *obj, const DOHString_or_char *name, const DOHObj_or_char *value);
extern void DohDelattr(DOH *obj, const DOHString_or_char *name);
extern DOH *DohFirstkey(DOH *obj);
extern DOH *DohNextkey(DOH *obj);
extern int DohGetInt(DOH *obj, const DOHString_or_char *name);
extern double DohGetDouble(DOH *obj, const DOHString_or_char *name);
extern char *DohGetChar(DOH *obj, const DOHString_or_char *name);
extern void DohSetInt(DOH *obj, const DOHString_or_char *name, int);
extern void DohSetDouble(DOH *obj, const DOHString_or_char *name, double);
extern void *DohGetVoid(DOH *obj, const DOHString_or_char *name);
extern void DohSetVoid(DOH *obj, const DOHString_or_char *name, void *value);
extern DOH *DohGetattr(DOH *obj, const DOHString_or_char *name);
extern int DohSetattr(DOH *obj, const DOHString_or_char *name, const DOHObj_or_char *value);
extern void DohDelattr(DOH *obj, const DOHString_or_char *name);
extern DOH *DohFirstkey(DOH *obj);
extern DOH *DohNextkey(DOH *obj);
extern int DohGetInt(DOH *obj, const DOHString_or_char *name);
extern double DohGetDouble(DOH *obj, const DOHString_or_char *name);
extern char *DohGetChar(DOH *obj, const DOHString_or_char *name);
extern void DohSetInt(DOH *obj, const DOHString_or_char *name, int);
extern void DohSetDouble(DOH *obj, const DOHString_or_char *name, double);
extern void *DohGetVoid(DOH *obj, const DOHString_or_char *name);
extern void DohSetVoid(DOH *obj, const DOHString_or_char *name, void *value);
/* Sequence methods */
/* Sequence methods */
extern DOH *DohGetitem(DOH *obj, int index);
extern int DohSetitem(DOH *obj, int index, const DOHObj_or_char *value);
extern int DohDelitem(DOH *obj, int index);
extern int DohInsertitem(DOH *obj, int index, const DOHObj_or_char *value);
extern DOH *DohFirstitem(DOH *obj);
extern DOH *DohNextitem(DOH *obj);
extern DOH *DohGetitem(DOH *obj, int index);
extern int DohSetitem(DOH *obj, int index, const DOHObj_or_char *value);
extern int DohDelitem(DOH *obj, int index);
extern int DohInsertitem(DOH *obj, int index, const DOHObj_or_char *value);
extern DOH *DohFirstitem(DOH *obj);
extern DOH *DohNextitem(DOH *obj);
/* File methods */
/* File methods */
extern int DohWrite(DOHFile *obj, void *buffer, int length);
extern int DohRead(DOHFile *obj, void *buffer, int length);
extern int DohSeek(DOHFile *obj, long offset, int whence);
extern long DohTell(DOHFile *obj);
extern int DohGetc(DOHFile *obj);
extern int DohPutc(int ch, DOHFile *obj);
extern int DohUngetc(int ch, DOHFile *obj);
extern int DohWrite(DOHFile *obj, void *buffer, int length);
extern int DohRead(DOHFile *obj, void *buffer, int length);
extern int DohSeek(DOHFile *obj, long offset, int whence);
extern long DohTell(DOHFile *obj);
extern int DohGetc(DOHFile *obj);
extern int DohPutc(int ch, DOHFile *obj);
extern int DohUngetc(int ch, DOHFile *obj);
/* Positional */
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 DohGetline(DOH *obj);
extern void DohSetline(DOH *obj, int line);
extern DOH *DohGetfile(DOH *obj);
extern void DohSetfile(DOH *obj, DOH *file);
/* String Methods */
extern int DohReplace(DOHString *src, const DOHString_or_char *token, const DOHString_or_char *rep, int flags);
extern void DohChop(DOHString *src);
extern int DohReplace(DOHString *src, const DOHString_or_char *token, const DOHString_or_char *rep, int flags);
extern void DohChop(DOHString *src);
/* Utility functions */
extern void DohEncoding(char *name, DOH *(*fn)(DOH *s));
extern int DohPrintf(DOHFile *obj, const char *format, ...);
extern int DohvPrintf(DOHFile *obj, const char *format, va_list ap);
extern DOH *DohReadline(DOHFile *in);
extern void DohEncoding(char *name, DOH *(*fn)(DOH *s));
extern int DohPrintf(DOHFile *obj, const char *format, ...);
extern int DohvPrintf(DOHFile *obj, const char *format, va_list ap);
extern DOH *DohReadline(DOHFile *in);
/* Miscellaneous */
extern int DohIsMapping(const DOH *obj);
extern int DohIsSequence(const DOH *obj);
extern int DohIsString(const DOH *obj);
extern int DohIsFile(const DOH *obj);
extern int DohIsMapping(const DOH *obj);
extern int DohIsSequence(const DOH *obj);
extern int DohIsString(const DOH *obj);
extern int DohIsFile(const DOH *obj);
#ifndef DOH_LONG_NAMES
/* Macros to invoke the above functions. Includes the location of
@ -267,35 +181,6 @@ typedef struct DohObjInfo {
#define Chop DohChop
#endif
/* -----------------------------------------------------------------------------
* DohBase
*
* DohBase object type. Attributes common to all objects.
* ----------------------------------------------------------------------------- */
#define DOHCOMMON \
DohObjInfo *objinfo; \
int refcount; \
DOH *file; \
int line; \
unsigned char flags
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 Refcount(a) ((DohBase *) a)->refcount
#define Objname(a) ((DohBase *) a)->objinfo->objname
/* Flags for various internal operations */
#define DOH_FLAG_PRINT 0x02
#define DOH_FLAG_DELETED 0x04
#define DOH_FLAG_INTERN 0x10
/* -----------------------------------------------------------------------------
* Strings.
* ----------------------------------------------------------------------------- */
@ -303,14 +188,12 @@ typedef struct {
extern DOHString *NewString(const DOH *c);
extern DOHString *NewStringf(const DOH *fmt, ...);
extern int String_check(const DOH *s);
/* String replacement flags */
#define DOH_REPLACE_ANY 0x00
#define DOH_REPLACE_NOQUOTE 0x01
#define DOH_REPLACE_ID 0x02
#define DOH_REPLACE_FIRST 0x04
#define DOH_REPLACE_ANY 0x01
#define DOH_REPLACE_NOQUOTE 0x02
#define DOH_REPLACE_ID 0x04
#define DOH_REPLACE_FIRST 0x08
/* -----------------------------------------------------------------------------
* Files
@ -318,7 +201,6 @@ extern int String_check(const DOH *s);
extern DOHFile *NewFile(DOH *file, char *mode);
extern DOHFile *NewFileFromFile(FILE *f);
extern DOHFile *NewFileFromFd(int fd);
extern int DohCopyto(DOHFile *input, DOHFile *output);
#define Copyto DohCopyto
@ -328,15 +210,12 @@ extern int DohCopyto(DOHFile *input, DOHFile *output);
* ----------------------------------------------------------------------------- */
extern DOHList *NewList();
extern int List_check(const DOH *);
extern void List_sort(DOHList *);
/* -----------------------------------------------------------------------------
* Hash
* ----------------------------------------------------------------------------- */
extern DOHHash *NewHash();
extern int Hash_check(const DOH *h);
extern DOHList *Hash_keys(DOHHash *);
/* -----------------------------------------------------------------------------
@ -350,20 +229,6 @@ extern DOHList *DohSplit(DOHFile *input, char *chs, int nsplits);
extern DOH *DohNone;
/* -----------------------------------------------------------------------------
* Error handling levels.
* ----------------------------------------------------------------------------- */
#define DOH_UNSUPPORTED 0x01
#define DOH_UNKNOWN 0x02
#define DOH_MEMORY 0x04
#define DOH_CONVERSION 0x08
#define DOH_CALLS 0x10
#ifdef __cplusplus
}
#endif
#endif /* DOH_H */

View file

@ -6,3 +6,4 @@
#include <stdarg.h>
#include "doh.h"
#include "dohobj.h"