Fixed file/line attributes

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@94 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-01-14 03:48:02 +00:00
commit 784ddea377
10 changed files with 126 additions and 55 deletions

View file

@ -58,7 +58,7 @@ static DohObjInfo DohBaseType = {
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
0, /* reserved4 */
0, /* doh_positional */
0, /* reserved5 */
0, /* reserved6 */
0, /* user1 */
@ -247,10 +247,9 @@ int DohGetline(DOH *obj) {
DohBase *b = (DohBase *) obj;
DohError(DOH_CALLS,"DohGetline %x\n",obj);
if (DohCheck(obj)) {
if (b->objinfo->doh_file && b->objinfo->doh_file->doh_getline) {
return (b->objinfo->doh_file->doh_getline)(obj);
if (b->objinfo->doh_position && b->objinfo->doh_position->doh_getline) {
return (b->objinfo->doh_position->doh_getline)(obj);
}
return b->line;
} else {
DohError(DOH_UNKNOWN, "Unknown object %x passed to Getline.\n", obj);
}
@ -262,11 +261,10 @@ void DohSetline(DOH *obj, int line) {
DohBase *b = (DohBase *) obj;
DohError(DOH_CALLS,"DohSetline %x, %d\n",obj, line);
if (DohCheck(obj)) {
if (b->objinfo->doh_file && b->objinfo->doh_file->doh_setline) {
(b->objinfo->doh_file->doh_setline)(obj, line);
if (b->objinfo->doh_position && b->objinfo->doh_position->doh_setline) {
(b->objinfo->doh_position->doh_setline)(obj, line);
return;
}
b->line = line;
} else {
DohError(DOH_UNKNOWN, "Unknown object %x passed to Setline.\n", obj);
}
@ -277,10 +275,9 @@ DOH *DohGetfile(DOH *obj) {
DohBase *b = (DohBase *) obj;
DohError(DOH_CALLS,"DohGetfile %x\n",obj);
if (DohCheck(obj)) {
if (b->objinfo->doh_file && b->objinfo->doh_file->doh_getfile) {
return (b->objinfo->doh_file->doh_getfile)(obj);
if (b->objinfo->doh_position && b->objinfo->doh_position->doh_getfile) {
return (b->objinfo->doh_position->doh_getfile)(obj);
}
return b->file;
} else {
DohError(DOH_UNKNOWN, "Unknown object %x passed to Getfile.\n", obj);
}
@ -289,22 +286,13 @@ DOH *DohGetfile(DOH *obj) {
/* Set the file */
void DohSetfile(DOH *obj, DOH *file) {
DOH *nf;
DOH *nf = 0;
DohBase *b = (DohBase *) obj;
DohError(DOH_CALLS,"DohSetfile %x, %x\n",obj,file);
if (DohCheck(obj)) {
if (file) {
nf = find_internal(file);
if (b->objinfo->doh_file && b->objinfo->doh_file->doh_setfile) {
(b->objinfo->doh_file->doh_setfile)(obj,nf);
return;
}
Incref(nf);
if (b->file) Delete(b->file);
b->file = nf;
} else {
Delete(b->file);
b->file = 0;
if (b->objinfo->doh_position && b->objinfo->doh_position->doh_setfile) {
(b->objinfo->doh_position->doh_setfile)(obj,file);
return;
}
} else {
DohError(DOH_UNKNOWN, "Unknown object %x passed to Setfile.\n", obj);
@ -865,7 +853,45 @@ void DohInit(DOH *b) {
DohBase *bs = (DohBase *) b;
bs->refcount = 1;
bs->objinfo = &DohBaseType;
bs->line = 0;
bs->file = 0;
bs->flags = 0;
}
void DohXInit(DOH *b) {
DohXBase *bs = (DohXBase *) b;
bs->file = 0;
bs->line = 0;
}
/* -----------------------------------------------------------------------------
* XBase_setfile(DOH *ho, DOH *file)
* ----------------------------------------------------------------------------- */
void XBase_setfile(DOH *ho, DOH *file) {
DohXBase *h = (DohXBase *) ho;
if (!DohCheck(file)) file = NewString(file);
h->file = file;
Incref(h->file);
}
/* -----------------------------------------------------------------------------
* DOH *XBase_getfile(DOH *ho)
* ----------------------------------------------------------------------------- */
DOH *XBase_getfile(DOH *ho) {
DohXBase *h = (DohXBase *) ho;
return h->file;
}
/* -----------------------------------------------------------------------------
* void XBase_setline(DOH *ho, int l)
* ----------------------------------------------------------------------------- */
void XBase_setline(DOH *ho, int l) {
DohXBase *h = (DohXBase *) ho;
h->line = l;
}
/* -----------------------------------------------------------------------------
* int XBase_getline(DOH *ho)
* ----------------------------------------------------------------------------- */
int XBase_getline(DOH *ho) {
DohXBase *h = (DohXBase *) ho;
return h->line;
}

View file

@ -53,7 +53,7 @@ static DohObjInfo DohCallableType = {
0, /* doh_file */
0, /* doh_string */
&doh_callable_methods, /* doh_callable */
0, /* reserved4 */
0, /* doh_position */
0, /* reserved5 */
0, /* reserved6 */
0, /* user1 */

View file

@ -51,10 +51,6 @@ static DohFileMethods FileFileMethods = {
File_seek,
File_tell,
0, /* close */
0, /* getfile */
0, /* setfile */
0, /* getline */
0 /* setline */
};
static DohObjInfo FileType = {
@ -76,6 +72,7 @@ static DohObjInfo FileType = {
&FileFileMethods,/* doh_file */
0, /* doh_string */
0, /* doh_callable */
0, /* doh_position */
};
DohObjInfo *File_type() {

View file

@ -32,7 +32,7 @@ typedef struct HashNode {
/* Hash object */
typedef struct Hash {
DOHCOMMON;
DOHXCOMMON;
HashNode **hashtable;
int hashsize;
int currentindex;
@ -40,7 +40,6 @@ typedef struct Hash {
HashNode *current;
} Hash;
/* -----------------------------------------------------------------------------
Key interning. This is used for getattr,setattr functions.
@ -126,6 +125,13 @@ static DohMappingMethods HashMappingMethods = {
Hash_nextkey,
};
static DohPositionalMethods HashPositionalMethods = {
XBase_setfile,
XBase_getfile,
XBase_setline,
XBase_getline
};
static DohObjInfo HashType = {
"Hash", /* objname */
sizeof(Hash), /* size */
@ -145,6 +151,7 @@ static DohObjInfo HashType = {
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
&HashPositionalMethods, /* doh_positional */
};
DohObjInfo *Hash_type() {
@ -167,6 +174,7 @@ DOH *NewHash() {
Hash *h;
int i;
h = (Hash *) DohObjMalloc(sizeof(Hash));
DohXInit(h);
h->hashsize = HASH_INIT_SIZE;
h->hashtable = (HashNode **) DohMalloc(h->hashsize*sizeof(HashNode *));
for (i = 0; i < h->hashsize; i++) {
@ -187,10 +195,9 @@ DOH *CopyHash(DOH *ho) {
Hash *h, *nh;
HashNode *n;
int i;
h = (Hash *) ho;
nh = (Hash *) DohObjMalloc(sizeof(Hash));
DohXInit(h);
nh->hashsize = h->hashsize;
nh->hashtable = (HashNode **) DohMalloc(nh->hashsize*sizeof(HashNode *));
for (i = 0; i < nh->hashsize; i++) {
@ -200,6 +207,9 @@ DOH *CopyHash(DOH *ho) {
nh->current = 0;
nh->nitems = 0;
nh->objinfo = h->objinfo;
nh->line = h->line;
nh->file = h->file;
if (nh->file) Incref(nh->file);
for (i = 0; i < h->hashsize; i++) {
if ((n = h->hashtable[i])) {
@ -235,6 +245,7 @@ void DelHash(DOH *ho)
DohFree(h->hashtable);
h->hashtable = 0;
h->hashsize = 0;
Delete(h->file);
DohObjFree(h);
}
@ -558,6 +569,3 @@ DOH *Hash_keys(DOH *so) {
/* List_sort(keys); */
return keys;
}

View file

@ -24,7 +24,7 @@
#include "dohint.h"
typedef struct List {
DOHCOMMON;
DOHXCOMMON;
int maxitems; /* Max size */
int nitems; /* Num items */
int iter; /* Iterator */
@ -58,6 +58,13 @@ static DohSequenceMethods ListSeqMethods = {
List_next,
};
static DohPositionalMethods ListPositionalMethods = {
XBase_setfile,
XBase_getfile,
XBase_setline,
XBase_getline
};
static DohObjInfo ListType = {
"List", /* objname */
sizeof(List), /* List size */
@ -77,6 +84,7 @@ static DohObjInfo ListType = {
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
&ListPositionalMethods, /* doh_position */
};
DohObjInfo *List_type() {
@ -123,6 +131,7 @@ NewList() {
List *l;
int i;
l = (List *) DohObjMalloc(sizeof(List));
DohXInit(l);
l->objinfo = &ListType;
l->nitems = 0;
l->maxitems = MAXLISTITEMS;
@ -144,6 +153,7 @@ CopyList(DOH *lo) {
int i;
l = (List *) lo;
nl = (List *) DohObjMalloc(sizeof(List));
DohXInit(nl);
nl->objinfo = l->objinfo;
nl->nitems = l->nitems;
nl->maxitems = l->maxitems;
@ -155,6 +165,9 @@ CopyList(DOH *lo) {
Incref(nl->items[i]);
}
}
nl->file = l->file;
if (nl->file) Incref(nl->file);
nl->line = l->line;
return (DOH *) nl;
}
@ -173,6 +186,7 @@ DelList(DOH *lo) {
}
DohFree(l->items);
l->items = 0;
Delete(l->file);
DohObjFree(l);
}

View file

@ -23,7 +23,7 @@
* --------------------------------------------------------------------------- */
typedef struct String {
DOHCOMMON;
DOHXCOMMON;
int maxsize; /* Max size allocated */
int len; /* Current length */
int hashkey; /* Hash key value */
@ -68,6 +68,13 @@ static DohSequenceMethods StringSeqMethods = {
0, /* doh_next */
};
static DohPositionalMethods StringPositionalMethods = {
XBase_setfile,
XBase_getfile,
XBase_setline,
XBase_getline
};
static DohFileMethods StringFileMethods = {
String_read,
String_write,
@ -77,10 +84,6 @@ static DohFileMethods StringFileMethods = {
String_seek,
String_tell,
0, /* close */
0, /* getfile */
0, /* setfile */
0, /* getline */
0 /* setline */
};
static DohStringMethods StringStringMethods = {
@ -107,6 +110,7 @@ static DohObjInfo StringType = {
&StringFileMethods,/* doh_file */
&StringStringMethods, /* doh_string */
0, /* doh_callable */
&StringPositionalMethods, /* doh_position */
};
#define INIT_MAXSIZE 16
@ -160,6 +164,7 @@ NewString(char *s)
str->lsp = 0;
str->pbi = 0;
str->line = 1;
str->file = 0;
max = INIT_MAXSIZE;
if (s) {
l = (int) strlen(s);
@ -191,7 +196,9 @@ CopyString(DOH *so) {
str->hashkey = -1;
str->sp = 0;
str->lsp = 0;
str->line = 1;
str->line = s->line;
str->file = s->file;
if (str->file) Incref(str->file);
str->pbi = 0;
max = s->maxsize;
str->str = (char *) DohMalloc(max);
@ -213,6 +220,7 @@ DelString(DOH *so) {
if (s->str)
DohFree(s->str);
s->str = 0;
Delete(s->file);
DohObjFree(s);
}

View file

@ -50,7 +50,7 @@ static DohObjInfo DohVoidType = {
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
0, /* reserved4 */
0, /* doh_position */
0, /* reserved5 */
0, /* reserved6 */
0, /* user1 */

View file

@ -75,10 +75,6 @@ typedef struct {
int (*doh_seek)(DOH *obj, long offset, int whence);
long (*doh_tell)(DOH *obj);
int (*doh_close)(DOH *obj);
DOH *(*doh_getfile)(DOH *obj);
void (*doh_setfile)(DOH *obj, DOH *f);
int (*doh_getline)(DOH *obj);
void (*doh_setline)(DOH *obj, int);
} DohFileMethods;
/* String methods */
@ -91,6 +87,16 @@ typedef struct {
typedef struct {
DOH *(*doh_call)(DOH *obj, DOH *args); /* Callable */
} DohCallableMethods;
/* Positional */
typedef struct {
void (*doh_setfile)(DOH *obj, DOH *file);
DOH *(*doh_getfile)(DOH *obj);
void (*doh_setline)(DOH *obj, int line);
int (*doh_getline)(DOH *obj);
} DohPositionalMethods;
/* -----------------------------------------------------------------------------
@ -128,6 +134,7 @@ typedef struct DohObjInfo {
DohFileMethods *doh_file; /* File methods */
DohStringMethods *doh_string; /* String methods */
DohCallableMethods *doh_callable; /* Callable methods */
DohPositionalMethods *doh_position; /* Positional methods */
void *reserved4;
void *reserved5;
void *reserved6;
@ -277,15 +284,22 @@ extern DOH *DohCall(DOH *obj, DOH *args);
DohObjInfo *objinfo; \
DOH *nextptr; \
int refcount; \
short line; \
unsigned char flags; \
unsigned char scope; \
DOH *file
unsigned char scope
typedef struct {
DOHCOMMON;
} DohBase;
#define DOHXCOMMON \
DOHCOMMON; \
DOH *file; \
int line
typedef struct {
DOHXCOMMON;
} DohXBase;
/* 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++

View file

@ -6,3 +6,8 @@
#include <stdarg.h>
#include "doh.h"
extern void XBase_setfile(DOH *, DOH *);
extern DOH *XBase_getfile(DOH *);
extern void XBase_setline(DOH *, int);
extern int XBase_getline(DOH *);

View file

@ -4,13 +4,14 @@ Overview:
---------
DOH is a small C library that provides a number of simple yet powerful
data structures for building flexible applications. The data
structures are built around a dynamic type model in which any given
structures are built around a dynamic typing model in which any given
object is allowed to support one or more classes of operations.
Common Operations (for all types)
---------------------------------
Delete(obj) Decrease the reference count and destroy if zero
Copy(obj) Make a copy of an object.
Clear(obj) Clear an object.
Str(obj) Create a string representation of obj.
Data(obj) Return pointer to raw data in an object
Len(obj) Length of an object
@ -27,8 +28,6 @@ Setattr(hash,key,value) Set an attribute
Delattr(hash,key) Delete an attribute
Firstkey(hash,key) Get first key
Nextkey(hash,key) Get next key
Getattrf(hash,key,format,...) Formatted attribute get
Setattrf(hash,key,format,...) Formatted attribute set
Sequence Operations
-------------------