beautify/format source code with gnu indent

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@9505 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2006-11-01 23:54:54 +00:00
commit aa04c4c057
72 changed files with 27038 additions and 28016 deletions

File diff suppressed because it is too large Load diff

View file

@ -165,11 +165,11 @@ typedef void DOH;
/* Iterator objects */
typedef struct {
void *key; /* Current key (if any) */
void *item; /* Current item */
void *object; /* Object being iterated over */
void *_current; /* Internal use */
int _index; /* Internal use */
void *key; /* Current key (if any) */
void *item; /* Current item */
void *object; /* Object being iterated over */
void *_current; /* Internal use */
int _index; /* Internal use */
} DohIterator;
/* Memory management */
@ -184,77 +184,77 @@ typedef struct {
#define DohFree free
#endif
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 */
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 int DohEqual(const DOH *obj1, const DOH *obj2);
extern void DohIncref(DOH *obj);
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 int DohEqual(const DOH *obj1, const DOH *obj2);
extern void DohIncref(DOH *obj);
/* 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 int DohDelattr(DOH *obj, const DOHString_or_char *name);
extern DOH *DohKeys(DOH *obj);
extern int DohGetInt(DOH *obj, const DOHString_or_char *name);
extern void DohSetInt(DOH *obj, const DOHString_or_char *name, int);
extern double DohGetDouble(DOH *obj, const DOHString_or_char *name);
extern void DohSetDouble(DOH *obj, const DOHString_or_char *name, double);
extern char *DohGetChar(DOH *obj, const DOHString_or_char *name);
extern void DohSetChar(DOH *obj, const DOH *name, char *value);
extern void *DohGetFlagAttr(DOH *obj, const DOHString_or_char *name);
extern int DohGetFlag(DOH *obj, const DOHString_or_char *name);
extern void DohSetFlagAttr(DOH *obj, const DOHString_or_char *name, const DOHString_or_char *attr);
extern void DohSetFlag(DOH *obj, const DOHString_or_char *name);
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 int DohDelattr(DOH *obj, const DOHString_or_char *name);
extern DOH *DohKeys(DOH *obj);
extern int DohGetInt(DOH *obj, const DOHString_or_char *name);
extern void DohSetInt(DOH *obj, const DOHString_or_char *name, int);
extern double DohGetDouble(DOH *obj, const DOHString_or_char *name);
extern void DohSetDouble(DOH *obj, const DOHString_or_char *name, double);
extern char *DohGetChar(DOH *obj, const DOHString_or_char *name);
extern void DohSetChar(DOH *obj, const DOH *name, char *value);
extern void *DohGetFlagAttr(DOH *obj, const DOHString_or_char *name);
extern int DohGetFlag(DOH *obj, const DOHString_or_char *name);
extern void DohSetFlagAttr(DOH *obj, const DOHString_or_char *name, const DOHString_or_char *attr);
extern void DohSetFlag(DOH *obj, const DOHString_or_char *name);
extern void *DohGetVoid(DOH *obj, const DOHString_or_char *name);
extern void DohSetVoid(DOH *obj, const DOHString_or_char *name, void *value);
/* 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 int DohDelslice(DOH *obj, int sindex, int eindex);
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 int DohDelslice(DOH *obj, int sindex, int eindex);
/* 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);
/* Iterators */
extern DohIterator DohFirst(DOH *obj);
extern DohIterator DohNext(DohIterator x);
extern DohIterator DohFirst(DOH *obj);
extern DohIterator DohNext(DohIterator x);
/* 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);
extern int DohString_putc(DOH *so, int ch);
extern int DohString_getc(DOH *so);
@ -274,39 +274,39 @@ extern int DohString_delslice(DOH *so, int sindex, int eindex);
#define DohStringEqual(s1,s2) DohString_equal((DOH *)s1, (DOH *)s2)
/* Meta-variables */
extern DOH *DohGetmeta(DOH *, const DOH *);
extern int DohSetmeta(DOH *, const DOH *, const DOH *value);
extern int DohDelmeta(DOH *, const DOH *);
extern DOH *DohGetmeta(DOH *, const DOH *);
extern int DohSetmeta(DOH *, const DOH *, const DOH *value);
extern int DohDelmeta(DOH *, const DOH *);
/* 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 int DohPrintv(DOHFile *obj, ...);
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 int DohPrintv(DOHFile * obj, ...);
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);
extern void DohSetmark(DOH *obj, int x);
extern int DohGetmark(DOH *obj);
extern void DohSetmark(DOH *obj, int x);
extern int DohGetmark(DOH *obj);
/* -----------------------------------------------------------------------------
* Strings.
* ----------------------------------------------------------------------------- */
extern DOHString *DohNewStringEmpty();
extern DOHString *DohNewString(const DOH *c);
extern DOHString *DohNewStringWithSize(const DOH *c, int len);
extern DOHString *DohNewStringf(const DOH *fmt, ...);
extern DOHString *DohNewStringEmpty();
extern DOHString *DohNewString(const DOH *c);
extern DOHString *DohNewStringWithSize(const DOH *c, int len);
extern DOHString *DohNewStringf(const DOH *fmt, ...);
extern int DohStrcmp(const DOHString_or_char *s1, const DOHString_or_char *s2);
extern int DohStrncmp(const DOHString_or_char *s1, const DOHString_or_char *s2, int n);
extern int DohStrcmp(const DOHString_or_char *s1, const DOHString_or_char *s2);
extern int DohStrncmp(const DOHString_or_char *s1, const DOHString_or_char *s2, int n);
extern char *DohStrstr(const DOHString_or_char *s1, const DOHString_or_char *s2);
extern char *DohStrchr(const DOHString_or_char *s1, int ch);
@ -329,36 +329,36 @@ extern char *DohStrchr(const DOHString_or_char *s1, int ch);
extern DOHFile *DohNewFile(DOH *file, const char *mode);
extern DOHFile *DohNewFileFromFile(FILE *f);
extern DOHFile *DohNewFileFromFd(int fd);
extern void DohFileErrorDisplay(DOHString *filename);
extern int DohClose(DOH *file);
extern int DohCopyto(DOHFile *input, DOHFile *output);
extern void DohFileErrorDisplay(DOHString * filename);
extern int DohClose(DOH *file);
extern int DohCopyto(DOHFile * input, DOHFile * output);
/* -----------------------------------------------------------------------------
* List
* ----------------------------------------------------------------------------- */
extern DOHList *DohNewList();
extern void DohSortList(DOH *lo, int (*cmp)(const DOH *, const DOH *));
extern DOHList *DohNewList();
extern void DohSortList(DOH *lo, int (*cmp) (const DOH *, const DOH *));
/* -----------------------------------------------------------------------------
* Hash
* ----------------------------------------------------------------------------- */
extern DOHHash *DohNewHash();
extern DOH *DohHashGetAttr(DOH *hash, const DOH *key);
extern int DohHashCheckAttr(DOH *hash, DOH *key, DOH *value);
extern DOHHash *DohNewHash();
extern DOH *DohHashGetAttr(DOH *hash, const DOH *key);
extern int DohHashCheckAttr(DOH *hash, DOH *key, DOH *value);
/* -----------------------------------------------------------------------------
* Void
* ----------------------------------------------------------------------------- */
extern DOHVoid *DohNewVoid(void *ptr, void (*del)(void *));
extern DOHList *DohSplit(DOHFile *input, char ch, int nsplits);
extern DOHList *DohSplitLines(DOHFile *input);
extern DOH *DohNone;
extern DOHVoid *DohNewVoid(void *ptr, void (*del) (void *));
extern DOHList *DohSplit(DOHFile * input, char ch, int nsplits);
extern DOHList *DohSplitLines(DOHFile * input);
extern DOH *DohNone;
extern void DohMemoryDebug(void);
extern void DohMemoryDebug(void);
#ifndef DOH_LONG_NAMES
/* Macros to invoke the above functions. Includes the location of
@ -468,8 +468,4 @@ extern void DohMemoryDebug(void);
#define NIL (char *) NULL
#endif /* DOH_H */
#endif /* DOH_H */

View file

@ -26,37 +26,37 @@
/* 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_keys)(DOH *obj); /* All keys as a list */
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_keys) (DOH *obj); /* All keys as a list */
} 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 */
int (*doh_delslice)(DOH *obj, int sindex, int eindex); /* Delete slice */
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 */
int (*doh_delslice) (DOH *obj, int sindex, int eindex); /* Delete slice */
} 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 */
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);
int (*doh_replace) (DOH *obj, DOH *old, DOH *rep, int flags);
void (*doh_chop) (DOH *obj);
} DohStringMethods;
/* -----------------------------------------------------------------------------
@ -64,55 +64,55 @@ typedef struct {
* ----------------------------------------------------------------------------- */
typedef struct DohObjInfo {
char *objname; /* Object name */
char *objname; /* Object name */
/* 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 */
void (*doh_del) (DOH *obj); /* Delete object */
DOH *(*doh_copy) (DOH *obj); /* Copy and object */
void (*doh_clear) (DOH *obj); /* Clear an object */
/* I/O 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_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 */
/* Length and hash values */
int (*doh_len)(DOH *obj);
int (*doh_hashval)(DOH *obj);
int (*doh_len) (DOH *obj);
int (*doh_hashval) (DOH *obj);
/* Compare */
int (*doh_cmp)(DOH *obj1, DOH *obj2);
int (*doh_cmp) (DOH *obj1, DOH *obj2);
/* Equal */
int (*doh_equal)(DOH *obj1, DOH *obj2);
int (*doh_equal) (DOH *obj1, DOH *obj2);
/* Iterators */
DohIterator (*doh_first)(DOH *obj);
DohIterator (*doh_next)(DohIterator );
DohIterator (*doh_first) (DOH *obj);
DohIterator (*doh_next) (DohIterator);
/* Positional */
void (*doh_setfile)(DOH *obj, DOHString_or_char *file);
DOH *(*doh_getfile)(DOH *obj);
void (*doh_setline)(DOH *obj, int line);
int (*doh_getline)(DOH *obj);
void (*doh_setfile) (DOH *obj, DOHString_or_char *file);
DOH *(*doh_getfile) (DOH *obj);
void (*doh_setline) (DOH *obj, int line);
int (*doh_getline) (DOH *obj);
DohHashMethods *doh_hash; /* Hash methods */
DohListMethods *doh_list; /* List methods */
DohFileMethods *doh_file; /* File methods */
DohStringMethods *doh_string; /* String methods */
void *doh_reserved; /* Reserved */
void *clientdata; /* User data */
DohHashMethods *doh_hash; /* Hash methods */
DohListMethods *doh_list; /* List methods */
DohFileMethods *doh_file; /* File methods */
DohStringMethods *doh_string; /* String methods */
void *doh_reserved; /* Reserved */
void *clientdata; /* User data */
} DohObjInfo;
typedef struct {
void *data; /* Data pointer */
DohObjInfo *type;
void *meta; /* Meta data */
unsigned int flag_intern : 1; /* Interned object */
unsigned int flag_marked : 1; /* Mark flag. Used to avoid recursive loops in places */
unsigned int flag_user : 1; /* User flag */
unsigned int flag_usermark : 1; /* User marked */
unsigned int refcount : 28; /* Reference count (max 16 million) */
void *data; /* Data pointer */
DohObjInfo *type;
void *meta; /* Meta data */
unsigned int flag_intern:1; /* Interned object */
unsigned int flag_marked:1; /* Mark flag. Used to avoid recursive loops in places */
unsigned int flag_user:1; /* User flag */
unsigned int flag_usermark:1; /* User marked */
unsigned int refcount:28; /* Reference count (max 16 million) */
} DohBase;
/* Macros for decrefing and increfing (safe for null objects). */
@ -127,12 +127,7 @@ typedef struct {
#define ObjGetMark(a) ((DohBase *)a)->flag_marked
#define ObjType(a) ((DohBase *)a)->type
extern DOH *DohObjMalloc(DohObjInfo *type, void *data); /* Allocate a DOH object */
extern void DohObjFree(DOH *ptr); /* Free a DOH object */
#endif /* DOHINT_H */
extern DOH *DohObjMalloc(DohObjInfo *type, void *data); /* Allocate a DOH object */
extern void DohObjFree(DOH *ptr); /* Free a DOH object */
#endif /* DOHINT_H */

View file

@ -20,17 +20,16 @@ char cvsroot_file_c[] = "$Header$";
#include <errno.h>
typedef struct {
FILE *filep;
int fd;
int closeondel;
FILE *filep;
int fd;
int closeondel;
} DohFile;
/* -----------------------------------------------------------------------------
* DelFile()
* ----------------------------------------------------------------------------- */
static void
DelFile(DOH *fo) {
static void DelFile(DOH *fo) {
DohFile *f = (DohFile *) ObjData(fo);
if (f->closeondel) {
if (f->filep) {
@ -49,15 +48,14 @@ DelFile(DOH *fo) {
* File_read()
* ----------------------------------------------------------------------------- */
static int
File_read(DOH *fo, void *buffer, int len) {
static int File_read(DOH *fo, void *buffer, int len) {
DohFile *f = (DohFile *) ObjData(fo);
if (f->filep) {
return fread(buffer,1,len,f->filep);
return fread(buffer, 1, len, f->filep);
} else if (f->fd) {
#ifdef DOH_INTFILE
return read(f->fd,buffer,len);
return read(f->fd, buffer, len);
#endif
}
return -1;
@ -67,16 +65,15 @@ File_read(DOH *fo, void *buffer, int len) {
* File_write()
* ----------------------------------------------------------------------------- */
static int
File_write(DOH *fo, void *buffer, int len) {
static int File_write(DOH *fo, void *buffer, int len) {
DohFile *f = (DohFile *) ObjData(fo);
if (f->filep) {
int ret = (int) fwrite(buffer, 1, len, f->filep);
int err = (ret != len) ? ferror(f->filep) : 0;
return err ? -1: ret;
return err ? -1 : ret;
} else if (f->fd) {
#ifdef DOH_INTFILE
return write(f->fd,buffer,len);
return write(f->fd, buffer, len);
#endif
}
return -1;
@ -86,11 +83,10 @@ File_write(DOH *fo, void *buffer, int len) {
* File_seek()
* ----------------------------------------------------------------------------- */
static int
File_seek(DOH *fo, long offset, int whence) {
static int File_seek(DOH *fo, long offset, int whence) {
DohFile *f = (DohFile *) ObjData(fo);
if (f->filep) {
return fseek(f->filep,offset,whence);
return fseek(f->filep, offset, whence);
} else if (f->fd) {
#ifdef DOH_INTFILE
return lseek(f->fd, offset, whence);
@ -103,8 +99,7 @@ File_seek(DOH *fo, long offset, int whence) {
* File_tell()
* ----------------------------------------------------------------------------- */
static long
File_tell(DOH *fo) {
static long File_tell(DOH *fo) {
DohFile *f = (DohFile *) ObjData(fo);
if (f->filep) {
return ftell(f->filep);
@ -120,16 +115,15 @@ File_tell(DOH *fo) {
* File_putc()
* ----------------------------------------------------------------------------- */
static int
File_putc(DOH *fo, int ch) {
static int File_putc(DOH *fo, int ch) {
DohFile *f = (DohFile *) ObjData(fo);
if (f->filep) {
return fputc(ch,f->filep);
return fputc(ch, f->filep);
} else if (f->fd) {
#ifdef DOH_INTFILE
char c;
c = (char) ch;
return write(f->fd,&c,1);
return write(f->fd, &c, 1);
#endif
}
return -1;
@ -139,15 +133,15 @@ File_putc(DOH *fo, int ch) {
* File_getc()
* ----------------------------------------------------------------------------- */
static int
File_getc(DOH *fo) {
static int File_getc(DOH *fo) {
DohFile *f = (DohFile *) ObjData(fo);
if (f->filep) {
return fgetc(f->filep);
} else if (f->fd) {
#ifdef DOH_INTFILE
char c;
if (read(f->fd,&c,1) < 0) return EOF;
if (read(f->fd, &c, 1) < 0)
return EOF;
return c;
#endif
}
@ -160,8 +154,7 @@ File_getc(DOH *fo) {
* Put a character back onto the input
* ----------------------------------------------------------------------------- */
static int
File_ungetc(DOH *fo, int ch) {
static int File_ungetc(DOH *fo, int ch) {
DohFile *f = (DohFile *) ObjData(fo);
if (f->filep) {
return ungetc(ch, f->filep);
@ -179,8 +172,7 @@ File_ungetc(DOH *fo, int ch) {
* Close the file
* ----------------------------------------------------------------------------- */
static int
File_close(DOH *fo) {
static int File_close(DOH *fo) {
int ret = 0;
DohFile *f = (DohFile *) ObjData(fo);
if (f->filep) {
@ -196,40 +188,40 @@ File_close(DOH *fo) {
}
static DohFileMethods FileFileMethods = {
File_read,
File_read,
File_write,
File_putc,
File_getc,
File_ungetc,
File_seek,
File_tell,
File_close, /* close */
File_close, /* close */
};
static DohObjInfo DohFileType = {
"DohFile", /* objname */
DelFile, /* doh_del */
0, /* doh_copy */
0, /* doh_clear */
0, /* doh_str */
0, /* doh_data */
0, /* doh_dump */
0, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_equal */
0, /* doh_first */
0, /* doh_next */
0, /* doh_setfile */
0, /* doh_getfile */
0, /* doh_setline */
0, /* doh_getline */
0, /* doh_mapping */
0, /* doh_sequence */
&FileFileMethods,/* doh_file */
0, /* doh_string */
0, /* doh_callable */
0, /* doh_position */
"DohFile", /* objname */
DelFile, /* doh_del */
0, /* doh_copy */
0, /* doh_clear */
0, /* doh_str */
0, /* doh_data */
0, /* doh_dump */
0, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_equal */
0, /* doh_first */
0, /* doh_next */
0, /* doh_setfile */
0, /* doh_getfile */
0, /* doh_setline */
0, /* doh_getline */
0, /* doh_mapping */
0, /* doh_sequence */
&FileFileMethods, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
0, /* doh_position */
};
/* -----------------------------------------------------------------------------
@ -238,16 +230,15 @@ static DohObjInfo DohFileType = {
* Create a new file from a given filename and mode.
* ----------------------------------------------------------------------------- */
DOH *
DohNewFile(DOH *fn, const char *mode)
{
DOH *DohNewFile(DOH *fn, const char *mode) {
DohFile *f;
FILE *file;
char *filename;
filename = Char(fn);
file = fopen(filename,mode);
if (!file) return 0;
file = fopen(filename, mode);
if (!file)
return 0;
f = (DohFile *) DohMalloc(sizeof(DohFile));
if (!f) {
@ -257,7 +248,7 @@ DohNewFile(DOH *fn, const char *mode)
f->filep = file;
f->fd = 0;
f->closeondel = 1;
return DohObjMalloc(&DohFileType,f);
return DohObjMalloc(&DohFileType, f);
}
/* -----------------------------------------------------------------------------
@ -266,16 +257,15 @@ DohNewFile(DOH *fn, const char *mode)
* Create a file object from an already open FILE *.
* ----------------------------------------------------------------------------- */
DOH *
DohNewFileFromFile(FILE *file)
{
DOH *DohNewFileFromFile(FILE *file) {
DohFile *f;
f = (DohFile *) DohMalloc(sizeof(DohFile));
if (!f) return 0;
if (!f)
return 0;
f->filep = file;
f->fd = 0;
f->closeondel = 0;
return DohObjMalloc(&DohFileType,f);
return DohObjMalloc(&DohFileType, f);
}
/* -----------------------------------------------------------------------------
@ -284,16 +274,15 @@ DohNewFileFromFile(FILE *file)
* Create a file object from an already open FILE *.
* ----------------------------------------------------------------------------- */
DOH *
DohNewFileFromFd(int fd)
{
DOH *DohNewFileFromFd(int fd) {
DohFile *f;
f = (DohFile *) DohMalloc(sizeof(DohFile));
if (!f) return 0;
if (!f)
return 0;
f->filep = 0;
f->fd = fd;
f->closeondel = 0;
return DohObjMalloc(&DohFileType,f);
return DohObjMalloc(&DohFileType, f);
}
/* -----------------------------------------------------------------------------
@ -302,9 +291,6 @@ DohNewFileFromFd(int fd)
* Display cause of one of the NewFile functions failing.
* ----------------------------------------------------------------------------- */
void
DohFileErrorDisplay(DOHString *filename)
{
void DohFileErrorDisplay(DOHString * filename) {
Printf(stderr, "Unable to open file %s: %s\n", filename, strerror(errno));
}

View file

@ -16,7 +16,7 @@ char cvsroot_fio_c[] = "$Header$";
#define OBUFLEN 512
static DOH *encodings = 0; /* Encoding hash */
static DOH *encodings = 0; /* Encoding hash */
/* -----------------------------------------------------------------------------
* Writen()
@ -29,8 +29,9 @@ static int Writen(DOH *out, void *buffer, int len) {
int nw = len, ret;
char *cb = (char *) buffer;
while (nw) {
ret = Write(out,cb,nw);
if (ret < 0) return -1;
ret = Write(out, cb, nw);
if (ret < 0)
return -1;
nw = nw - ret;
cb += ret;
}
@ -44,36 +45,38 @@ static int Writen(DOH *out, void *buffer, int len) {
* two file-like objects and operate as a filter.
* ----------------------------------------------------------------------------- */
void
DohEncoding(char *name, DOH *(*fn)(DOH *s)) {
if (!encodings) encodings = NewHash();
Setattr(encodings,(void *) name, NewVoid((void *)fn,0));
void DohEncoding(char *name, DOH *(*fn) (DOH *s)) {
if (!encodings)
encodings = NewHash();
Setattr(encodings, (void *) name, NewVoid((void *) fn, 0));
}
/* internal function for processing an encoding */
static DOH *encode(char *name, DOH *s) {
static DOH *encode(char *name, DOH *s) {
DOH *handle, *ns;
DOH *(*fn)(DOH *);
long pos;
char *cfmt = strchr(name,':');
DOH *tmp = 0;
DOH *(*fn) (DOH *);
long pos;
char *cfmt = strchr(name, ':');
DOH *tmp = 0;
if (cfmt) {
tmp = NewString(cfmt + 1);
Append(tmp,s);
Setfile(tmp,Getfile((DOH *)s));
Setline(tmp,Getline((DOH *)s));
Append(tmp, s);
Setfile(tmp, Getfile((DOH *) s));
Setline(tmp, Getline((DOH *) s));
*cfmt = '\0';
}
if (!encodings || !(handle = Getattr(encodings,name))) {
if (!encodings || !(handle = Getattr(encodings, name))) {
return Copy(s);
}
if (tmp) s = tmp;
if (tmp)
s = tmp;
pos = Tell(s);
Seek(s,0,SEEK_SET);
Seek(s, 0, SEEK_SET);
fn = (DOH *(*)(DOH *)) Data(handle);
ns = (*fn)(s);
Seek(s,pos,SEEK_SET);
if (tmp) Delete(tmp);
ns = (*fn) (s);
Seek(s, pos, SEEK_SET);
if (tmp)
Delete(tmp);
return ns;
}
@ -95,37 +98,35 @@ static DOH *encode(char *name, DOH *s) {
* It's better to use Dump() or some other method instead.
* ----------------------------------------------------------------------------- */
int
DohvPrintf(DOH *so, const char *format, va_list ap)
{
int DohvPrintf(DOH *so, const char *format, va_list ap) {
static char *fmt_codes = "dioxXucsSfeEgGpn";
int state = 0;
const char *p = format;
char newformat[256];
char obuffer[OBUFLEN];
char newformat[256];
char obuffer[OBUFLEN];
char *fmt = 0;
char temp[64];
int widthval = 0;
int precval = 0;
int maxwidth;
char *w = 0;
int ivalue;
char temp[64];
int widthval = 0;
int precval = 0;
int maxwidth;
char *w = 0;
int ivalue;
double dvalue;
void *pvalue;
char *stemp;
int nbytes = 0;
char encoder[128], *ec = 0;
int plevel = 0;
void *pvalue;
char *stemp;
int nbytes = 0;
char encoder[128], *ec = 0;
int plevel = 0;
memset (newformat, 0, sizeof (newformat));
memset(newformat, 0, sizeof(newformat));
while (*p) {
switch(state) {
case 0: /* Ordinary text */
switch (state) {
case 0: /* Ordinary text */
if (*p != '%') {
Putc(*p,so);
Putc(*p, so);
nbytes++;
} else{
} else {
fmt = newformat;
widthval = 0;
precval = 0;
@ -134,26 +135,26 @@ DohvPrintf(DOH *so, const char *format, va_list ap)
state = 10;
}
break;
case 10: /* Look for a width and precision */
if (isdigit((int)*p) && (*p != '0')) {
case 10: /* Look for a width and precision */
if (isdigit((int) *p) && (*p != '0')) {
w = temp;
*(w++) = *p;
*(fmt++) = *p;
state = 20;
} else if (strchr(fmt_codes,*p)) {
} else if (strchr(fmt_codes, *p)) {
/* Got one of the formatting codes */
p--;
state = 100;
} else if (*p == '*') {
/* Width field is specified in the format list */
widthval = va_arg(ap,int);
sprintf(temp,"%d",widthval);
widthval = va_arg(ap, int);
sprintf(temp, "%d", widthval);
for (w = temp; *w; w++) {
*(fmt++) = *w;
}
state = 30;
} else if (*p == '%') {
Putc(*p,so);
Putc(*p, so);
fmt = newformat;
nbytes++;
state = 0;
@ -165,12 +166,12 @@ DohvPrintf(DOH *so, const char *format, va_list ap)
*(fmt++) = *p;
}
break;
case 20: /* Hmmm. At the start of a width field */
if (isdigit((int)*p)) {
case 20: /* Hmmm. At the start of a width field */
if (isdigit((int) *p)) {
*(w++) = *p;
*(fmt++) = *p;
} else if (strchr(fmt_codes,*p)) {
} else if (strchr(fmt_codes, *p)) {
/* Got one of the formatting codes */
/* Figure out width */
*w = 0;
@ -191,12 +192,12 @@ DohvPrintf(DOH *so, const char *format, va_list ap)
}
break;
case 30: /* Parsed a width from an argument. Look for a . */
case 30: /* Parsed a width from an argument. Look for a . */
if (*p == '.') {
w = temp;
*(fmt++) = *p;
state = 40;
} else if (strchr(fmt_codes,*p)) {
} else if (strchr(fmt_codes, *p)) {
/* Got one of the formatting codes */
/* Figure out width */
p--;
@ -209,19 +210,19 @@ DohvPrintf(DOH *so, const char *format, va_list ap)
case 40:
/* Start of precision expected */
if (isdigit((int)*p) && (*p != '0')) {
if (isdigit((int) *p) && (*p != '0')) {
*(fmt++) = *p;
*(w++) = *p;
state = 41;
} else if (*p == '*') {
/* Precision field is specified in the format list */
precval = va_arg(ap,int);
sprintf(temp,"%d",precval);
precval = va_arg(ap, int);
sprintf(temp, "%d", precval);
for (w = temp; *w; w++) {
*(fmt++) = *w;
}
state = 50;
} else if (strchr(fmt_codes,*p)) {
} else if (strchr(fmt_codes, *p)) {
p--;
state = 100;
} else {
@ -230,10 +231,10 @@ DohvPrintf(DOH *so, const char *format, va_list ap)
}
break;
case 41:
if (isdigit((int)*p)) {
if (isdigit((int) *p)) {
*(fmt++) = *p;
*(w++) = *p;
} else if (strchr(fmt_codes,*p)) {
} else if (strchr(fmt_codes, *p)) {
/* Got one of the formatting codes */
/* Figure out width */
*w = 0;
@ -249,7 +250,7 @@ DohvPrintf(DOH *so, const char *format, va_list ap)
break;
/* Hang out, wait for format specifier */
case 50:
if (strchr(fmt_codes,*p)) {
if (strchr(fmt_codes, *p)) {
p--;
state = 100;
} else {
@ -279,43 +280,47 @@ DohvPrintf(DOH *so, const char *format, va_list ap)
break;
case 100:
/* Got a formatting code */
if (widthval < precval) maxwidth = precval;
else maxwidth = widthval;
if ((*p == 's') || (*p == 'S')) { /* Null-Terminated string */
DOH *doh;
DOH *Sval;
DOH *enc = 0;
if (widthval < precval)
maxwidth = precval;
else
maxwidth = widthval;
if ((*p == 's') || (*p == 'S')) { /* Null-Terminated string */
DOH *doh;
DOH *Sval;
DOH *enc = 0;
doh = va_arg(ap, DOH *);
if (DohCheck(doh)) {
/* Is a DOH object. */
if (DohIsString(doh)) {
if (DohIsString(doh)) {
Sval = doh;
} else {
Sval = Str(doh);
}
if (strlen(encoder)) {
enc = encode(encoder,Sval);
maxwidth = maxwidth+strlen(newformat)+Len(enc);
enc = encode(encoder, Sval);
maxwidth = maxwidth + strlen(newformat) + Len(enc);
} else {
maxwidth = maxwidth+strlen(newformat)+Len(Sval);
maxwidth = maxwidth + strlen(newformat) + Len(Sval);
}
*(fmt++) = 's';
*fmt = 0;
if ((maxwidth + 1) < OBUFLEN) {
stemp = obuffer;
} else {
stemp = (char *) DohMalloc(maxwidth+1);
stemp = (char *) DohMalloc(maxwidth + 1);
}
if (enc) {
nbytes+=sprintf(stemp,newformat,Data(enc));
nbytes += sprintf(stemp, newformat, Data(enc));
} else {
nbytes+=sprintf(stemp,newformat,Data(Sval));
nbytes += sprintf(stemp, newformat, Data(Sval));
}
if (Writen(so,stemp,strlen(stemp)) < 0) return -1;
if (Writen(so, stemp, strlen(stemp)) < 0)
return -1;
if ((DOH *) Sval != doh) {
Delete(Sval);
}
if (enc) Delete(enc);
if (enc)
Delete(enc);
if (*p == 'S') {
Delete(doh);
}
@ -323,45 +328,48 @@ DohvPrintf(DOH *so, const char *format, va_list ap)
DohFree(stemp);
}
} else {
if (!doh) doh = (char *)"";
if (!doh)
doh = (char *) "";
if (strlen(encoder)) {
DOH *s = NewString(doh);
Seek(s,0, SEEK_SET);
enc = encode(encoder,s);
Seek(s, 0, SEEK_SET);
enc = encode(encoder, s);
Delete(s);
doh = Char(enc);
} else {
enc = 0;
}
maxwidth = maxwidth+strlen(newformat)+strlen((char *) doh);
maxwidth = maxwidth + strlen(newformat) + strlen((char *) doh);
*(fmt++) = 's';
*fmt = 0;
if ((maxwidth+1) < OBUFLEN) {
if ((maxwidth + 1) < OBUFLEN) {
stemp = obuffer;
} else {
stemp = (char *) DohMalloc(maxwidth + 1);
}
nbytes+=sprintf(stemp,newformat,doh);
if (Writen(so,stemp,strlen(stemp)) < 0) return -1;
nbytes += sprintf(stemp, newformat, doh);
if (Writen(so, stemp, strlen(stemp)) < 0)
return -1;
if (stemp != obuffer) {
DohFree(stemp);
}
if (enc) Delete(enc);
if (enc)
Delete(enc);
}
} else {
*(fmt++) = *p;
*fmt = 0;
maxwidth = maxwidth+strlen(newformat)+64;
maxwidth = maxwidth + strlen(newformat) + 64;
/* Only allocate a buffer if it is too big to fit. Shouldn't have to do
this very often */
this very often */
if (maxwidth < OBUFLEN)
stemp = obuffer;
else
stemp = (char *) DohMalloc(maxwidth+1);
switch(*p) {
else
stemp = (char *) DohMalloc(maxwidth + 1);
switch (*p) {
case 'd':
case 'i':
case 'o':
@ -369,26 +377,28 @@ DohvPrintf(DOH *so, const char *format, va_list ap)
case 'x':
case 'X':
case 'c':
ivalue = va_arg(ap,int);
nbytes+=sprintf(stemp,newformat,ivalue);
ivalue = va_arg(ap, int);
nbytes += sprintf(stemp, newformat, ivalue);
break;
case 'f':
case 'g':
case 'e':
case 'E':
case 'G':
dvalue = va_arg(ap,double);
nbytes+=sprintf(stemp,newformat,dvalue);
dvalue = va_arg(ap, double);
nbytes += sprintf(stemp, newformat, dvalue);
break;
case 'p':
pvalue = va_arg(ap,void *);
nbytes+=sprintf(stemp,newformat,pvalue);
pvalue = va_arg(ap, void *);
nbytes += sprintf(stemp, newformat, pvalue);
break;
default:
break;
}
if (Writen(so,stemp,strlen(stemp)) < 0) return -1;
if (stemp != obuffer) DohFree(stemp);
if (Writen(so, stemp, strlen(stemp)) < 0)
return -1;
if (stemp != obuffer)
DohFree(stemp);
}
state = 0;
break;
@ -398,8 +408,9 @@ DohvPrintf(DOH *so, const char *format, va_list ap)
if (state) {
int r;
*fmt = 0;
r = Writen(so,fmt,strlen(fmt));
if (r < 0) return -1;
r = Writen(so, fmt, strlen(fmt));
if (r < 0)
return -1;
nbytes += r;
}
return nbytes;
@ -411,12 +422,11 @@ DohvPrintf(DOH *so, const char *format, va_list ap)
* Variable length argument entry point to Printf
* ----------------------------------------------------------------------------- */
int
DohPrintf(DOH *obj, const char *format, ...) {
int DohPrintf(DOH *obj, const char *format, ...) {
va_list ap;
int ret;
va_start(ap,format);
ret = DohvPrintf(obj,format,ap);
va_start(ap, format);
ret = DohvPrintf(obj, format, ap);
va_end(ap);
return ret;
}
@ -427,18 +437,19 @@ DohPrintf(DOH *obj, const char *format, ...) {
* Print a null-terminated variable length list of DOH objects
* ----------------------------------------------------------------------------- */
int DohPrintv(DOHFile *f, ...) {
int DohPrintv(DOHFile * f, ...) {
va_list ap;
int ret = 0;
DOH *obj;
va_start(ap,f);
while(1) {
obj = va_arg(ap,void *);
if ((!obj) || (obj == DohNone)) break;
va_start(ap, f);
while (1) {
obj = va_arg(ap, void *);
if ((!obj) || (obj == DohNone))
break;
if (DohCheck(obj)) {
ret += DohDump(obj,f);
ret += DohDump(obj, f);
} else {
ret += DohWrite(f,obj,strlen((char *) obj));
ret += DohWrite(f, obj, strlen((char *) obj));
}
}
va_end(ap);
@ -452,22 +463,23 @@ int DohPrintv(DOHFile *f, ...) {
* number of bytes copied.
* ----------------------------------------------------------------------------- */
int
DohCopyto(DOH *in, DOH *out) {
int DohCopyto(DOH *in, DOH *out) {
int nbytes = 0, ret;
int nwrite = 0, wret;
char *cw;
char buffer[16384];
if ((!in) || (!out)) return 0;
if ((!in) || (!out))
return 0;
while (1) {
ret = Read(in,buffer,16384);
ret = Read(in, buffer, 16384);
if (ret > 0) {
nwrite = ret;
cw = buffer;
while (nwrite) {
wret = Write(out,cw,nwrite);
if (wret < 0) return -1;
wret = Write(out, cw, nwrite);
if (wret < 0)
return -1;
nwrite = nwrite - wret;
cw += wret;
}
@ -486,16 +498,15 @@ DohCopyto(DOH *in, DOH *out) {
* character. Optionally accepts a maximum number of splits to perform.
* ----------------------------------------------------------------------------- */
DOH *
DohSplit(DOH *in, char ch, int nsplits) {
DOH *DohSplit(DOH *in, char ch, int nsplits) {
DOH *list;
DOH *str;
int c;
list = NewList();
if (DohIsString(in)) {
Seek(in,0,SEEK_SET);
Seek(in, 0, SEEK_SET);
}
while (1) {
@ -504,17 +515,19 @@ DohSplit(DOH *in, char ch, int nsplits) {
c = Getc(in);
} while ((c != EOF) && (c == ch));
if (c != EOF) {
Putc(c,str);
Putc(c, str);
while (1) {
c = Getc(in);
if ((c == EOF) || ((c == ch) && (nsplits != 0))) break;
Putc(c,str);
if ((c == EOF) || ((c == ch) && (nsplits != 0)))
break;
Putc(c, str);
}
nsplits--;
}
Append(list,str);
Append(list, str);
Delete(str);
if (c == EOF) break;
if (c == EOF)
break;
}
return list;
}
@ -525,16 +538,15 @@ DohSplit(DOH *in, char ch, int nsplits) {
* Split an input stream into a list of strings delimited by newline characters.
* ----------------------------------------------------------------------------- */
DOH *
DohSplitLines(DOH *in) {
DOH *DohSplitLines(DOH *in) {
DOH *list;
DOH *str;
int c = 0;
int c = 0;
list = NewList();
if (DohIsString(in)) {
Seek(in,0,SEEK_SET);
Seek(in, 0, SEEK_SET);
}
while (c != EOF) {
@ -542,7 +554,7 @@ DohSplitLines(DOH *in) {
while ((c = Getc(in)) != '\n' && c != EOF) {
Putc(c, str);
}
Append(list,str);
Append(list, str);
Delete(str);
}
return list;
@ -555,24 +567,23 @@ DohSplitLines(DOH *in) {
* Read a single input line and return it as a string.
* ----------------------------------------------------------------------------- */
DOH *
DohReadline(DOH *in) {
DOH *DohReadline(DOH *in) {
char c;
int n = 0;
DOH *s = NewStringEmpty();
while (1) {
if (Read(in,&c,1) < 0) {
if (Read(in, &c, 1) < 0) {
if (n == 0) {
Delete(s);
return 0;
}
return s;
}
if (c == '\n') return s;
if (c == '\r') continue;
Putc(c,s);
if (c == '\n')
return s;
if (c == '\r')
continue;
Putc(c, s);
n++;
}
}

View file

@ -17,59 +17,65 @@ extern DohObjInfo DohHashType;
/* Hash node */
typedef struct HashNode {
DOH *key;
DOH *object;
struct HashNode *next;
DOH *key;
DOH *object;
struct HashNode *next;
} HashNode;
/* Hash object */
typedef struct Hash {
DOH *file;
int line;
HashNode **hashtable;
int hashsize;
int nitems;
DOH *file;
int line;
HashNode **hashtable;
int hashsize;
int nitems;
} Hash;
/* Key interning structure */
typedef struct KeyValue {
char *cstr;
DOH *sstr;
struct KeyValue *left;
struct KeyValue *right;
char *cstr;
DOH *sstr;
struct KeyValue *left;
struct KeyValue *right;
} KeyValue;
static KeyValue *root = 0;
/* Find or create a key in the interned key table */
static DOH *find_key (DOH *doh_c) {
static DOH *find_key(DOH *doh_c) {
char *c = (char *) doh_c;
KeyValue *r, *s;
int d = 0;
/* OK, sure, we use a binary tree for maintaining interned
symbols. Then we use their hash values for accessing secondary
hash tables. */
hash tables. */
r = root;
s = 0;
while (r) {
s = r;
d = strcmp(r->cstr,c);
if (d == 0) return r->sstr;
if (d < 0) r = r->left;
else r = r->right;
d = strcmp(r->cstr, c);
if (d == 0)
return r->sstr;
if (d < 0)
r = r->left;
else
r = r->right;
}
/* fprintf(stderr,"Interning '%s'\n", c);*/
/* fprintf(stderr,"Interning '%s'\n", c); */
r = (KeyValue *) DohMalloc(sizeof(KeyValue));
r->cstr = (char *) DohMalloc(strlen(c)+1);
strcpy(r->cstr,c);
r->cstr = (char *) DohMalloc(strlen(c) + 1);
strcpy(r->cstr, c);
r->sstr = NewString(c);
DohIntern(r->sstr);
r->left = 0;
r->right = 0;
if (!s) { root = r; }
else {
if (d < 0) s->left = r;
else s->right = r;
if (!s) {
root = r;
} else {
if (d < 0)
s->left = r;
else
s->right = r;
}
return r->sstr;
}
@ -78,20 +84,20 @@ static DOH *find_key (DOH *doh_c) {
/* Create a new hash node */
static HashNode *NewNode(DOH *k, void *obj) {
HashNode *hn = (HashNode *) DohMalloc(sizeof(HashNode));
hn->key = k;
Incref(hn->key);
hn->object = obj;
Incref(obj);
hn->next = 0;
return hn;
HashNode *hn = (HashNode *) DohMalloc(sizeof(HashNode));
hn->key = k;
Incref(hn->key);
hn->object = obj;
Incref(obj);
hn->next = 0;
return hn;
}
/* Delete a hash node */
static void DelNode(HashNode *hn) {
Delete(hn->key);
Delete(hn->object);
DohFree(hn);
Delete(hn->key);
Delete(hn->object);
DohFree(hn);
}
/* -----------------------------------------------------------------------------
@ -100,25 +106,24 @@ static void DelNode(HashNode *hn) {
* Delete a hash table.
* ----------------------------------------------------------------------------- */
static void
DelHash(DOH *ho) {
Hash *h = (Hash *) ObjData(ho);
HashNode *n,*next;
int i;
static void DelHash(DOH *ho) {
Hash *h = (Hash *) ObjData(ho);
HashNode *n, *next;
int i;
for (i = 0; i < h->hashsize; i++) {
if ((n = h->hashtable[i])) {
while (n) {
next = n->next;
DelNode(n);
n = next;
}
}
for (i = 0; i < h->hashsize; i++) {
if ((n = h->hashtable[i])) {
while (n) {
next = n->next;
DelNode(n);
n = next;
}
}
DohFree(h->hashtable);
h->hashtable = 0;
h->hashsize = 0;
DohFree(h);
}
DohFree(h->hashtable);
h->hashtable = 0;
h->hashsize = 0;
DohFree(h);
}
/* -----------------------------------------------------------------------------
@ -127,67 +132,67 @@ DelHash(DOH *ho) {
* Clear all of the entries in the hash table.
* ----------------------------------------------------------------------------- */
static void
Hash_clear(DOH *ho) {
Hash *h = (Hash *) ObjData(ho);
HashNode *n,*next;
int i;
static void Hash_clear(DOH *ho) {
Hash *h = (Hash *) ObjData(ho);
HashNode *n, *next;
int i;
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;
for (i = 0; i < h->hashsize; i++) {
if ((n = h->hashtable[i])) {
while (n) {
next = n->next;
DelNode(n);
n = next;
}
}
h->nitems = 0;
h->hashtable[i] = 0;
}
h->nitems = 0;
}
/* resize the hash table */
static void resize(Hash *h) {
HashNode *n, *next, **table;
int oldsize, newsize;
int i, p, hv;
HashNode *n, *next, **table;
int oldsize, newsize;
int i, p, hv;
if (h->nitems < 2*h->hashsize) return;
if (h->nitems < 2 * h->hashsize)
return;
/* Too big. We have to rescale everything now */
oldsize = h->hashsize;
/* 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;
/* 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 **) DohMalloc(newsize*sizeof(HashNode *));
for (i = 0; i < newsize; i++ ) {
table[i] = 0;
}
table = (HashNode **) DohMalloc(newsize * sizeof(HashNode *));
for (i = 0; i < newsize; i++) {
table[i] = 0;
}
/* Walk down the old set of nodes and re-place */
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;
}
/* Walk down the old set of nodes and re-place */
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;
}
DohFree(h->hashtable);
h->hashtable = table;
}
DohFree(h->hashtable);
h->hashtable = table;
}
/* -----------------------------------------------------------------------------
@ -197,46 +202,48 @@ static void resize(Hash *h) {
* exists.
* ----------------------------------------------------------------------------- */
static int
Hash_setattr(DOH *ho, DOH *k, DOH *obj) {
int hv;
HashNode *n, *prev;
Hash *h = (Hash *) ObjData(ho);
if (!obj) {
return DohDelattr(ho,k);
}
if (!DohCheck(k)) k = find_key(k);
if (!DohCheck(obj)) {
obj = NewString((char *) obj);
Decref(obj);
}
hv = (Hashval(k)) % h->hashsize;
n = h->hashtable[hv];
prev = 0;
while (n) {
if (Cmp(n->key,k) == 0) {
/* Node already exists. Just replace its contents */
if (n->object == obj) {
/* Whoa. Same object. Do nothing */
return 1;
}
Delete(n->object);
n->object = obj;
Incref(obj);
return 1; /* Return 1 to indicate a replacement */
} else {
prev = n;
n = n->next;
static int Hash_setattr(DOH *ho, DOH *k, DOH *obj) {
int hv;
HashNode *n, *prev;
Hash *h = (Hash *) ObjData(ho);
if (!obj) {
return DohDelattr(ho, k);
}
if (!DohCheck(k))
k = find_key(k);
if (!DohCheck(obj)) {
obj = NewString((char *) obj);
Decref(obj);
}
hv = (Hashval(k)) % h->hashsize;
n = h->hashtable[hv];
prev = 0;
while (n) {
if (Cmp(n->key, k) == 0) {
/* Node already exists. Just replace its contents */
if (n->object == obj) {
/* Whoa. Same object. Do nothing */
return 1;
}
Delete(n->object);
n->object = obj;
Incref(obj);
return 1; /* Return 1 to indicate a replacement */
} 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;
}
/* 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;
}
/* -----------------------------------------------------------------------------
@ -244,7 +251,7 @@ Hash_setattr(DOH *ho, DOH *k, DOH *obj) {
*
* Get an attribute from the hash table. Returns 0 if it doesn't exist.
* ----------------------------------------------------------------------------- */
typedef int (*binop)(DOH *obj1, DOH *obj2);
typedef int (*binop) (DOH *obj1, DOH *obj2);
#define _Hash_getattr(h, k, o) \
int hv = Hashval(k) % h->hashsize; \
@ -267,8 +274,7 @@ typedef int (*binop)(DOH *obj1, DOH *obj2);
}
static DOH *
Hash_getattr(DOH *h, DOH *k) {
static DOH *Hash_getattr(DOH *h, DOH *k) {
DOH *obj = 0;
Hash *ho = (Hash *) ObjData(h);
DOH *ko = DohCheck(k) ? k : find_key(k);
@ -276,8 +282,7 @@ Hash_getattr(DOH *h, DOH *k) {
return obj;
}
DOH *
DohHashGetAttr(DOH *h, const DOH *k) {
DOH *DohHashGetAttr(DOH *h, const DOH *k) {
DOH *obj = 0;
Hash *ho = (Hash *) ObjData(h);
_Hash_getattr(ho, (DOH *) k, obj);
@ -291,14 +296,13 @@ DohHashGetAttr(DOH *h, const DOH *k) {
* Check an attribute from the hash table.
* ----------------------------------------------------------------------------- */
int
DohHashCheckAttr(DOH *h, DOH *k, DOH *v) {
int DohHashCheckAttr(DOH *h, DOH *k, DOH *v) {
DOH *obj = 0;
Hash *ho = (Hash *) ObjData(h);
_Hash_getattr(ho, k, obj);
if (obj) {
DohObjInfo *o_type = ((DohBase*)obj)->type;
if (o_type == ((DohBase*)v)->type) {
DohObjInfo *o_type = ((DohBase *) obj)->type;
if (o_type == ((DohBase *) v)->type) {
binop equal = o_type->doh_equal;
return equal ? equal(obj, v) : (o_type->doh_cmp(obj, v) == 0);
}
@ -312,44 +316,43 @@ DohHashCheckAttr(DOH *h, DOH *k, DOH *v) {
* Delete an object from the hash table.
* ----------------------------------------------------------------------------- */
static int
Hash_delattr(DOH *ho, DOH *k) {
HashNode *n, *prev;
int hv;
Hash *h = (Hash *) ObjData(ho);
static int Hash_delattr(DOH *ho, DOH *k) {
HashNode *n, *prev;
int hv;
Hash *h = (Hash *) ObjData(ho);
if (!DohCheck(k)) k = find_key(k);
hv = Hashval(k) % h->hashsize;
n = h->hashtable[hv];
prev = 0;
while (n) {
if (Cmp(n->key, k) == 0) {
/* Found it, kill it */
if (!DohCheck(k))
k = find_key(k);
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;
if (prev) {
prev->next = n->next;
} else {
h->hashtable[hv] = n->next;
}
DelNode(n);
h->nitems--;
return 1;
}
return 0;
prev = n;
n = n->next;
}
return 0;
}
static DohIterator
Hash_firstiter(DOH *ho) {
static DohIterator Hash_firstiter(DOH *ho) {
DohIterator iter;
Hash *h = (Hash *) ObjData(ho);
iter.object = ho;
iter._current = 0;
iter.item = 0;
iter.key = 0;
iter._index = 0; /* Index in hash table */
iter._index = 0; /* Index in hash table */
while ((iter._index < h->hashsize) && !h->hashtable[iter._index])
iter._index++;
@ -359,15 +362,14 @@ Hash_firstiter(DOH *ho) {
iter._current = h->hashtable[iter._index];
iter.item = ((HashNode *) iter._current)->object;
iter.key = ((HashNode *) iter._current)->key;
/* Actually save the next slot in the hash. This makes it possible to
delete the item being iterated over without trashing the universe */
iter._current = ((HashNode*)iter._current)->next;
iter._current = ((HashNode *) iter._current)->next;
return iter;
}
static DohIterator
Hash_nextiter(DohIterator iter) {
static DohIterator Hash_nextiter(DohIterator iter) {
Hash *h = (Hash *) ObjData(iter.object);
if (!iter._current) {
iter._index++;
@ -386,7 +388,7 @@ Hash_nextiter(DohIterator iter) {
iter.item = ((HashNode *) iter._current)->object;
/* Store the next node to iterator on */
iter._current = ((HashNode*)iter._current)->next;
iter._current = ((HashNode *) iter._current)->next;
return iter;
}
@ -396,14 +398,13 @@ Hash_nextiter(DohIterator iter) {
* Return a list of keys
* ----------------------------------------------------------------------------- */
static DOH *
Hash_keys(DOH *so) {
static DOH *Hash_keys(DOH *so) {
DOH *keys;
Iterator i;
keys = NewList();
for (i = First(so); i.key; i = Next(i)) {
Append(keys,i.key);
Append(keys, i.key);
}
return keys;
}
@ -414,35 +415,36 @@ Hash_keys(DOH *so) {
* Create a string representation of a hash table (mainly for debugging).
* ----------------------------------------------------------------------------- */
static DOH *
Hash_str(DOH *ho) {
int i,j;
HashNode *n;
DOH *s;
static int indent = 4;
Hash *h = (Hash *) ObjData(ho);
static DOH *Hash_str(DOH *ho) {
int i, j;
HashNode *n;
DOH *s;
static int indent = 4;
Hash *h = (Hash *) ObjData(ho);
s = NewStringEmpty();
if (ObjGetMark(ho)) {
Printf(s,"Hash(0x%x)",ho);
return s;
}
ObjSetMark(ho,1);
Printf(s,"Hash {\n");
for (i = 0; i < h->hashsize; i++) {
n = h->hashtable[i];
while (n) {
for (j = 0; j < indent; j++) Putc(' ',s);
indent+=4;
Printf(s,"'%s' : %s, \n", n->key, n->object);
indent-=4;
n = n->next;
}
}
for (j = 0; j < (indent-4); j++) Putc(' ',s);
Printf(s,"}\n");
ObjSetMark(ho,0);
s = NewStringEmpty();
if (ObjGetMark(ho)) {
Printf(s, "Hash(0x%x)", ho);
return s;
}
ObjSetMark(ho, 1);
Printf(s, "Hash {\n");
for (i = 0; i < h->hashsize; i++) {
n = h->hashtable[i];
while (n) {
for (j = 0; j < indent; j++)
Putc(' ', s);
indent += 4;
Printf(s, "'%s' : %s, \n", n->key, n->object);
indent -= 4;
n = n->next;
}
}
for (j = 0; j < (indent - 4); j++)
Putc(' ', s);
Printf(s, "}\n");
ObjSetMark(ho, 0);
return s;
}
/* -----------------------------------------------------------------------------
@ -451,8 +453,7 @@ Hash_str(DOH *ho) {
* Return number of entries in the hash table.
* ----------------------------------------------------------------------------- */
static int
Hash_len(DOH *ho) {
static int Hash_len(DOH *ho) {
Hash *h = (Hash *) ObjData(ho);
return h->nitems;
}
@ -463,67 +464,64 @@ Hash_len(DOH *ho) {
* Make a copy of a hash table. Note: this is a shallow copy.
* ----------------------------------------------------------------------------- */
static DOH *
CopyHash(DOH *ho) {
Hash *h, *nh;
HashNode *n;
DOH *nho;
int i;
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++) {
nh->hashtable[i] = 0;
}
nh->nitems = 0;
nh->line = h->line;
nh->file = h->file;
if (nh->file) Incref(nh->file);
static DOH *CopyHash(DOH *ho) {
Hash *h, *nh;
HashNode *n;
DOH *nho;
nho = DohObjMalloc(&DohHashType, nh);
for (i = 0; i < h->hashsize; i++) {
if ((n = h->hashtable[i])) {
while (n) {
Hash_setattr(nho, n->key, n->object);
n = n->next;
}
}
int i;
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++) {
nh->hashtable[i] = 0;
}
nh->nitems = 0;
nh->line = h->line;
nh->file = h->file;
if (nh->file)
Incref(nh->file);
nho = DohObjMalloc(&DohHashType, nh);
for (i = 0; i < h->hashsize; i++) {
if ((n = h->hashtable[i])) {
while (n) {
Hash_setattr(nho, n->key, n->object);
n = n->next;
}
}
return nho;
}
return nho;
}
static void
Hash_setfile(DOH *ho, DOH *file) {
static void Hash_setfile(DOH *ho, DOH *file) {
DOH *fo;
Hash *h = (Hash *) ObjData(ho);
if (!DohCheck(file)) {
fo = NewString(file);
Decref(fo);
} else fo = file;
} else
fo = file;
Incref(fo);
Delete(h->file);
h->file = fo;
}
static DOH *
Hash_getfile(DOH *ho) {
static DOH *Hash_getfile(DOH *ho) {
Hash *h = (Hash *) ObjData(ho);
return h->file;
}
static void
Hash_setline(DOH *ho, int line) {
static void Hash_setline(DOH *ho, int line) {
Hash *h = (Hash *) ObjData(ho);
h->line = line;
}
static int
Hash_getline(DOH *ho) {
static int Hash_getline(DOH *ho) {
Hash *h = (Hash *) ObjData(ho);
return h->line;
}
@ -540,29 +538,29 @@ static DohHashMethods HashHashMethods = {
};
DohObjInfo DohHashType = {
"Hash", /* objname */
DelHash, /* doh_del */
CopyHash, /* doh_copy */
Hash_clear, /* doh_clear */
Hash_str, /* doh_str */
0, /* doh_data */
0, /* doh_dump */
Hash_len, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_equal */
Hash_firstiter, /* doh_first */
Hash_nextiter, /* doh_next */
Hash_setfile, /* doh_setfile */
Hash_getfile, /* doh_getfile */
Hash_setline, /* doh_setline */
Hash_getline, /* doh_getline */
&HashHashMethods, /* doh_mapping */
0, /* doh_sequence */
0, /* doh_file */
0, /* doh_string */
0, /* doh_positional */
0,
"Hash", /* objname */
DelHash, /* doh_del */
CopyHash, /* doh_copy */
Hash_clear, /* doh_clear */
Hash_str, /* doh_str */
0, /* doh_data */
0, /* doh_dump */
Hash_len, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_equal */
Hash_firstiter, /* doh_first */
Hash_nextiter, /* doh_next */
Hash_setfile, /* doh_setfile */
Hash_getfile, /* doh_getfile */
Hash_setline, /* doh_setline */
Hash_getline, /* doh_getline */
&HashHashMethods, /* doh_mapping */
0, /* doh_sequence */
0, /* doh_file */
0, /* doh_string */
0, /* doh_positional */
0,
};
/* -----------------------------------------------------------------------------
@ -571,18 +569,17 @@ DohObjInfo DohHashType = {
* Create a new hash table.
* ----------------------------------------------------------------------------- */
DOH *
DohNewHash() {
Hash *h;
int i;
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++) {
h->hashtable[i] = 0;
}
h->nitems = 0;
h->file = 0;
h->line = 0;
return DohObjMalloc(&DohHashType,h);
DOH *DohNewHash() {
Hash *h;
int i;
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++) {
h->hashtable[i] = 0;
}
h->nitems = 0;
h->file = 0;
h->line = 0;
return DohObjMalloc(&DohHashType, h);
}

View file

@ -14,21 +14,21 @@ char cvsroot_list_c[] = "$Header$";
#include "dohint.h"
typedef struct List {
int maxitems; /* Max size */
int nitems; /* Num items */
DOH *file;
int line;
DOH **items;
int maxitems; /* Max size */
int nitems; /* Num items */
DOH *file;
int line;
DOH **items;
} List;
extern DohObjInfo DohListType;
/* Doubles amount of memory in a list */
static
static
void more(List *l) {
l->items = (void **) DohRealloc(l->items, l->maxitems*2*sizeof(void *));
assert(l->items);
l->maxitems *= 2;
l->items = (void **) DohRealloc(l->items, l->maxitems * 2 * sizeof(void *));
assert(l->items);
l->maxitems *= 2;
}
/* -----------------------------------------------------------------------------
@ -36,23 +36,23 @@ void more(List *l) {
*
* Make a shallow copy of a list.
* ----------------------------------------------------------------------------- */
static DOH *
CopyList(DOH *lo) {
List *l,*nl;
int i;
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 *));
for (i = 0; i < l->nitems; i++) {
nl->items[i] = l->items[i];
Incref(nl->items[i]);
}
nl->file = l->file;
if (nl->file) Incref(nl->file);
nl->line = l->line;
return DohObjMalloc(&DohListType, nl);
static DOH *CopyList(DOH *lo) {
List *l, *nl;
int i;
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 *));
for (i = 0; i < l->nitems; i++) {
nl->items[i] = l->items[i];
Incref(nl->items[i]);
}
nl->file = l->file;
if (nl->file)
Incref(nl->file);
nl->line = l->line;
return DohObjMalloc(&DohListType, nl);
}
/* -----------------------------------------------------------------------------
@ -61,14 +61,13 @@ CopyList(DOH *lo) {
* Delete a list.
* ----------------------------------------------------------------------------- */
static void
DelList(DOH *lo) {
List *l = (List *) ObjData(lo);
int i;
for (i = 0; i < l->nitems; i++)
Delete(l->items[i]);
DohFree(l->items);
DohFree(l);
static void DelList(DOH *lo) {
List *l = (List *) ObjData(lo);
int i;
for (i = 0; i < l->nitems; i++)
Delete(l->items[i]);
DohFree(l->items);
DohFree(l);
}
/* -----------------------------------------------------------------------------
@ -77,14 +76,13 @@ DelList(DOH *lo) {
* Remove all of the list entries, but keep the list object intact.
* ----------------------------------------------------------------------------- */
static void
List_clear(DOH *lo) {
List *l = (List *) ObjData(lo);
int i;
for (i = 0; i < l->nitems; i++) {
Delete(l->items[i]);
}
l->nitems = 0;
static void List_clear(DOH *lo) {
List *l = (List *) ObjData(lo);
int i;
for (i = 0; i < l->nitems; i++) {
Delete(l->items[i]);
}
l->nitems = 0;
}
/* -----------------------------------------------------------------------------
@ -94,22 +92,26 @@ List_clear(DOH *lo) {
* to be a 'char *' and is used to construct an equivalent string object.
* ----------------------------------------------------------------------------- */
static int
List_insert(DOH *lo, int pos, DOH *item) {
static int List_insert(DOH *lo, int pos, DOH *item) {
List *l = (List *) ObjData(lo);
int i;
if (!item) return -1;
if (!item)
return -1;
if (!DohCheck(item)) {
item = NewString(item);
Decref(item);
}
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);
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[i] = l->items[i - 1];
}
l->items[pos] = item;
Incref(item);
@ -122,20 +124,21 @@ List_insert(DOH *lo, int pos, DOH *item) {
*
* Remove an item from a list.
* ----------------------------------------------------------------------------- */
static int
List_remove(DOH *lo, int pos) {
List *l = (List *) ObjData(lo);
int i;
if (pos == DOH_END) pos = l->nitems-1;
if (pos == DOH_BEGIN) pos = 0;
assert(!((pos < 0) || (pos >= l->nitems)));
Delete(l->items[pos]);
for (i = pos; i < l->nitems-1; i++) {
l->items[i] = l->items[i+1];
}
l->nitems--;
return 0;
static int List_remove(DOH *lo, int pos) {
List *l = (List *) ObjData(lo);
int i;
if (pos == DOH_END)
pos = l->nitems - 1;
if (pos == DOH_BEGIN)
pos = 0;
assert(!((pos < 0) || (pos >= l->nitems)));
Delete(l->items[pos]);
for (i = pos; i < l->nitems - 1; i++) {
l->items[i] = l->items[i + 1];
}
l->nitems--;
return 0;
}
/* -----------------------------------------------------------------------------
@ -144,8 +147,7 @@ List_remove(DOH *lo, int pos) {
* Return the number of elements in the list
* ----------------------------------------------------------------------------- */
static int
List_len(DOH *lo) {
static int List_len(DOH *lo) {
List *l = (List *) ObjData(lo);
return l->nitems;
}
@ -156,13 +158,14 @@ List_len(DOH *lo) {
* Get the nth item from the list.
* ----------------------------------------------------------------------------- */
static DOH *
List_get(DOH *lo, int n) {
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)));
return l->items[n];
static DOH *List_get(DOH *lo, int n) {
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)));
return l->items[n];
}
/* -----------------------------------------------------------------------------
@ -171,20 +174,20 @@ List_get(DOH *lo, int n) {
* Set the nth item in the list replacing any previous item.
* ----------------------------------------------------------------------------- */
static int
List_set(DOH *lo, int n, DOH *val) {
List *l = (List *) ObjData(lo);
if (!val) return -1;
assert(!((n < 0) || (n >= l->nitems)));
if (!DohCheck(val)) {
val = NewString(val);
Decref(val);
}
Delete(l->items[n]);
l->items[n] = val;
Incref(val);
Delete(val);
return 0;
static int List_set(DOH *lo, int n, DOH *val) {
List *l = (List *) ObjData(lo);
if (!val)
return -1;
assert(!((n < 0) || (n >= l->nitems)));
if (!DohCheck(val)) {
val = NewString(val);
Decref(val);
}
Delete(l->items[n]);
l->items[n] = val;
Incref(val);
Delete(val);
return 0;
}
/* -----------------------------------------------------------------------------
@ -193,8 +196,7 @@ List_set(DOH *lo, int n, DOH *val) {
* Return the first item in the list.
* ----------------------------------------------------------------------------- */
static DohIterator
List_first(DOH *lo) {
static DohIterator List_first(DOH *lo) {
DohIterator iter;
List *l = (List *) ObjData(lo);
iter.object = lo;
@ -215,8 +217,7 @@ List_first(DOH *lo) {
* Return the next item in the list.
* ----------------------------------------------------------------------------- */
static DohIterator
List_next(DohIterator iter) {
static DohIterator List_next(DohIterator iter) {
List *l = (List *) ObjData(iter.object);
iter._index = iter._index + 1;
if (iter._index >= l->nitems) {
@ -233,26 +234,25 @@ List_next(DohIterator iter) {
*
* Create a string representation of the list.
* ----------------------------------------------------------------------------- */
static DOH *
List_str(DOH *lo) {
DOH *s;
int i;
List *l = (List *) ObjData(lo);
s = NewStringEmpty();
if (ObjGetMark(lo)) {
Printf(s,"List(%x)", lo);
return s;
}
ObjSetMark(lo,1);
Printf(s,"List[ ");
for (i = 0; i < l->nitems; i++) {
Printf(s, "%s", l->items[i]);
if ((i+1) < l->nitems)
Printf(s,", ");
}
Printf(s," ]\n");
ObjSetMark(lo,0);
static DOH *List_str(DOH *lo) {
DOH *s;
int i;
List *l = (List *) ObjData(lo);
s = NewStringEmpty();
if (ObjGetMark(lo)) {
Printf(s, "List(%x)", lo);
return s;
}
ObjSetMark(lo, 1);
Printf(s, "List[ ");
for (i = 0; i < l->nitems; i++) {
Printf(s, "%s", l->items[i]);
if ((i + 1) < l->nitems)
Printf(s, ", ");
}
Printf(s, " ]\n");
ObjSetMark(lo, 0);
return s;
}
/* -----------------------------------------------------------------------------
@ -261,41 +261,39 @@ List_str(DOH *lo) {
* Dump the items to an output stream.
* ----------------------------------------------------------------------------- */
static int
List_dump(DOH *lo, DOH *out) {
static int List_dump(DOH *lo, DOH *out) {
int nsent = 0;
int i,ret;
int i, ret;
List *l = (List *) ObjData(lo);
for (i = 0; i < l->nitems; i++) {
ret = Dump(l->items[i],out);
if (ret < 0) return -1;
ret = Dump(l->items[i], out);
if (ret < 0)
return -1;
nsent += ret;
}
return nsent;
}
static void
List_setfile(DOH *lo, DOH *file) {
static void List_setfile(DOH *lo, DOH *file) {
DOH *fo;
List *l = (List *) ObjData(lo);
if (!DohCheck(file)) {
fo = NewString(file);
Decref(fo);
} else fo = file;
} else
fo = file;
Incref(fo);
Delete(l->file);
l->file = fo;
}
static DOH *
List_getfile(DOH *lo) {
static DOH *List_getfile(DOH *lo) {
List *l = (List *) ObjData(lo);
return l->file;
}
static void
List_setline(DOH *lo, int line) {
static void List_setline(DOH *lo, int line) {
List *l = (List *) ObjData(lo);
l->line = line;
}
@ -310,33 +308,33 @@ static DohListMethods ListListMethods = {
List_set,
List_remove,
List_insert,
0, /* delslice */
0, /* delslice */
};
DohObjInfo DohListType = {
"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_equal */
List_first, /* doh_first */
List_next, /* doh_next */
List_setfile, /* doh_setfile */
List_getfile, /* doh_getfile */
List_setline, /* doh_setline */
List_getline, /* doh_getline */
0, /* doh_mapping */
&ListListMethods, /* doh_sequence */
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
0, /* doh_position */
"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_equal */
List_first, /* doh_first */
List_next, /* doh_next */
List_setfile, /* doh_setfile */
List_getfile, /* doh_getfile */
List_setline, /* doh_setline */
List_getline, /* doh_getline */
0, /* doh_mapping */
&ListListMethods, /* doh_sequence */
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
0, /* doh_position */
};
/* -----------------------------------------------------------------------------
@ -347,29 +345,28 @@ DohObjInfo DohListType = {
#define MAXLISTITEMS 8
DOH *
DohNewList() {
List *l;
int i;
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->file = 0;
l->line = 0;
return DohObjMalloc(&DohListType,l);
DOH *DohNewList() {
List *l;
int i;
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->file = 0;
l->line = 0;
return DohObjMalloc(&DohListType, l);
}
static int (*List_sort_compare_func)(const DOH *, const DOH *);
static int (*List_sort_compare_func) (const DOH *, const DOH *);
static int List_qsort_compare(const void *a, const void *b) {
return List_sort_compare_func(*((DOH **)a), *((DOH **)b));
return List_sort_compare_func(*((DOH **) a), *((DOH **) b));
}
/* Sort a list */
void DohSortList(DOH *lo, int (*cmp)(const DOH *, const DOH *)) {
void DohSortList(DOH *lo, int (*cmp) (const DOH *, const DOH *)) {
List *l = (List *) ObjData(lo);
if (cmp) {
List_sort_compare_func = cmp;

View file

@ -18,40 +18,39 @@ char cvsroot_memory_c[] = "$Header$";
#define DOH_POOL_SIZE 16384
#endif
static int PoolSize = DOH_POOL_SIZE;
static int PoolSize = DOH_POOL_SIZE;
DOH *DohNone = 0; /* The DOH None object */
DOH *DohNone = 0; /* The DOH None object */
typedef struct pool {
DohBase *ptr; /* Start of pool */
int len; /* Length of pool */
int blen; /* Byte length of pool */
int current; /* Current position for next allocation */
char* pbeg; /* Beg of pool */
char* pend; /* End of pool */
struct pool *next; /* Next pool */
DohBase *ptr; /* Start of pool */
int len; /* Length of pool */
int blen; /* Byte length of pool */
int current; /* Current position for next allocation */
char *pbeg; /* Beg of pool */
char *pend; /* End of pool */
struct pool *next; /* Next pool */
} Pool;
static DohBase *FreeList = 0; /* List of free objects */
static Pool *Pools = 0;
static int pools_initialized = 0;
static DohBase *FreeList = 0; /* List of free objects */
static Pool *Pools = 0;
static int pools_initialized = 0;
/* ----------------------------------------------------------------------
* CreatePool() - Create a new memory pool
* ---------------------------------------------------------------------- */
static void
CreatePool() {
static void CreatePool() {
Pool *p = 0;
p = (Pool *) DohMalloc(sizeof(Pool));
assert(p);
p->ptr = (DohBase *) DohMalloc(sizeof(DohBase)*PoolSize);
p->ptr = (DohBase *) DohMalloc(sizeof(DohBase) * PoolSize);
assert(p->ptr);
memset(p->ptr,0,sizeof(DohBase)*PoolSize);
memset(p->ptr, 0, sizeof(DohBase) * PoolSize);
p->len = PoolSize;
p->blen = PoolSize*sizeof(DohBase);
p->blen = PoolSize * sizeof(DohBase);
p->current = 0;
p->pbeg = ((char *)p->ptr);
p->pbeg = ((char *) p->ptr);
p->pend = p->pbeg + p->blen;
p->next = Pools;
Pools = p;
@ -61,12 +60,12 @@ CreatePool() {
* InitPools() - Initialize the memory allocator
* ---------------------------------------------------------------------- */
static void
InitPools() {
if (pools_initialized) return;
CreatePool(); /* Create initial pool */
static void InitPools() {
if (pools_initialized)
return;
CreatePool(); /* Create initial pool */
pools_initialized = 1;
DohNone = NewVoid(0,0); /* Create the None object */
DohNone = NewVoid(0, 0); /* Create the None object */
DohIntern(DohNone);
}
@ -76,15 +75,15 @@ InitPools() {
* Returns 1 if an arbitrary pointer is a DOH object.
* ---------------------------------------------------------------------- */
int
DohCheck(const DOH *ptr) {
int DohCheck(const DOH *ptr) {
register Pool *p = Pools;
register char *cptr = (char *) ptr;
while (p) {
if ((cptr >= p->pbeg) && (cptr < p->pend)) return 1;
if ((cptr >= p->pbeg) && (cptr < p->pend))
return 1;
/*
pptr = (char *) p->ptr;
if ((cptr >= pptr) && (cptr < (pptr+(p->current*sizeof(DohBase))))) return 1; */
pptr = (char *) p->ptr;
if ((cptr >= pptr) && (cptr < (pptr+(p->current*sizeof(DohBase))))) return 1; */
p = p->next;
}
return 0;
@ -94,8 +93,7 @@ DohCheck(const DOH *ptr) {
* DohIntern()
* ----------------------------------------------------------------------------- */
void
DohIntern(DOH *obj) {
void DohIntern(DOH *obj) {
DohBase *b = (DohBase *) obj;
b->flag_intern = 1;
}
@ -106,10 +104,10 @@ DohIntern(DOH *obj) {
* Allocate memory for a new object.
* ---------------------------------------------------------------------- */
DOH *
DohObjMalloc(DohObjInfo *type, void *data) {
DOH *DohObjMalloc(DohObjInfo *type, void *data) {
DohBase *obj;
if (!pools_initialized) InitPools();
if (!pools_initialized)
InitPools();
if (FreeList) {
obj = FreeList;
FreeList = (DohBase *) obj->data;
@ -135,12 +133,12 @@ DohObjMalloc(DohObjInfo *type, void *data) {
* DohObjFree() - Free a DOH object
* ---------------------------------------------------------------------- */
void
DohObjFree(DOH *ptr) {
DohBase *b, *meta;
void DohObjFree(DOH *ptr) {
DohBase *b, *meta;
b = (DohBase *) ptr;
if (b->flag_intern) return;
meta = (DohBase *)b->meta;
if (b->flag_intern)
return;
meta = (DohBase *) b->meta;
b->data = (void *) FreeList;
b->meta = 0;
b->type = 0;
@ -156,42 +154,45 @@ DohObjFree(DOH *ptr) {
* Display memory usage statistics
* ---------------------------------------------------------------------- */
void
DohMemoryDebug(void) {
void DohMemoryDebug(void) {
extern DohObjInfo DohStringType;
extern DohObjInfo DohListType;
extern DohObjInfo DohHashType;
Pool *p;
int totsize = 0;
int totused = 0;
int totfree = 0;
int totsize = 0;
int totused = 0;
int totfree = 0;
int numstring = 0;
int numlist = 0;
int numhash = 0;
int numstring = 0;
int numlist = 0;
int numhash = 0;
printf("Memory statistics:\n\n");
printf("Pools:\n");
p = Pools;
while(p) {
while (p) {
/* Calculate number of used, free items */
int i;
int nused = 0, nfree = 0;
for (i = 0; i < p->len; i++) {
if (p->ptr[i].refcount <= 0) nfree++;
if (p->ptr[i].refcount <= 0)
nfree++;
else {
nused++;
if (p->ptr[i].type == &DohStringType) numstring++;
else if (p->ptr[i].type == &DohListType) numlist++;
else if (p->ptr[i].type == &DohHashType) numhash++;
if (p->ptr[i].type == &DohStringType)
numstring++;
else if (p->ptr[i].type == &DohListType)
numlist++;
else if (p->ptr[i].type == &DohHashType)
numhash++;
}
}
printf(" Pool %8p: size = %10d. used = %10d. free = %10d\n", (void *)p, p->len, nused, nfree);
printf(" Pool %8p: size = %10d. used = %10d. free = %10d\n", (void *) p, p->len, nused, nfree);
totsize += p->len;
totused+= nused;
totfree+= nfree;
totused += nused;
totfree += nfree;
p = p->next;
}
printf("\n Total: size = %10d, used = %10d, free = %10d\n", totsize, totused, totfree);
@ -203,12 +204,12 @@ DohMemoryDebug(void) {
#if 0
p = Pools;
while(p) {
while (p) {
int i;
for (i = 0; i < p->len; i++) {
if (p->ptr[i].refcount > 0) {
if (p->ptr[i].type == &DohStringType) {
Printf(stdout,"%s\n", p->ptr+i);
Printf(stdout, "%s\n", p->ptr + i);
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -15,8 +15,8 @@ char cvsroot_void_c[] = "$Header$";
#include "dohint.h"
typedef struct {
void *ptr;
void (*del)(void *);
void *ptr;
void (*del) (void *);
} VoidObj;
/* -----------------------------------------------------------------------------
@ -25,11 +25,10 @@ typedef struct {
* Delete a void object. Invokes the destructor supplied at the time of creation.
* ----------------------------------------------------------------------------- */
static void
Void_delete(DOH *vo) {
static void Void_delete(DOH *vo) {
VoidObj *v = (VoidObj *) ObjData(vo);
if (v->del)
(*v->del)(v->ptr);
(*v->del) (v->ptr);
DohFree(v);
}
@ -40,10 +39,9 @@ Void_delete(DOH *vo) {
* function is not copied in order to avoid potential double-free problems.
* ----------------------------------------------------------------------------- */
static DOH *
Void_copy(DOH *vo) {
static DOH *Void_copy(DOH *vo) {
VoidObj *v = (VoidObj *) ObjData(vo);
return NewVoid(v->ptr,0);
return NewVoid(v->ptr, 0);
}
/* -----------------------------------------------------------------------------
@ -52,36 +50,35 @@ Void_copy(DOH *vo) {
* Returns the void * stored in the object.
* ----------------------------------------------------------------------------- */
static void *
Void_data(DOH *vo) {
static void *Void_data(DOH *vo) {
VoidObj *v = (VoidObj *) ObjData(vo);
return v->ptr;
}
static DohObjInfo DohVoidType = {
"VoidObj", /* objname */
Void_delete, /* doh_del */
Void_copy, /* doh_copy */
0, /* doh_clear */
0, /* doh_str */
Void_data, /* doh_data */
0, /* doh_dump */
0, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_equal */
0, /* doh_first */
0, /* doh_next */
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_reserved */
0, /* clientdata */
"VoidObj", /* objname */
Void_delete, /* doh_del */
Void_copy, /* doh_copy */
0, /* doh_clear */
0, /* doh_str */
Void_data, /* doh_data */
0, /* doh_dump */
0, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_equal */
0, /* doh_first */
0, /* doh_next */
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_reserved */
0, /* clientdata */
};
/* -----------------------------------------------------------------------------
@ -90,11 +87,10 @@ static DohObjInfo DohVoidType = {
* Creates a new Void object given a void * and an optional destructor function.
* ----------------------------------------------------------------------------- */
DOH *
DohNewVoid(void *obj, void (*del)(void *)) {
DOH *DohNewVoid(void *obj, void (*del) (void *)) {
VoidObj *v;
v = (VoidObj *) DohMalloc(sizeof(VoidObj));
v->ptr = obj;
v->del = del;
return DohObjMalloc(&DohVoidType,v);
return DohObjMalloc(&DohVoidType, v);
}