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_file */
0, /* doh_string */ 0, /* doh_string */
0, /* doh_callable */ 0, /* doh_callable */
0, /* reserved4 */ 0, /* doh_positional */
0, /* reserved5 */ 0, /* reserved5 */
0, /* reserved6 */ 0, /* reserved6 */
0, /* user1 */ 0, /* user1 */
@ -247,10 +247,9 @@ int DohGetline(DOH *obj) {
DohBase *b = (DohBase *) obj; DohBase *b = (DohBase *) obj;
DohError(DOH_CALLS,"DohGetline %x\n",obj); DohError(DOH_CALLS,"DohGetline %x\n",obj);
if (DohCheck(obj)) { if (DohCheck(obj)) {
if (b->objinfo->doh_file && b->objinfo->doh_file->doh_getline) { if (b->objinfo->doh_position && b->objinfo->doh_position->doh_getline) {
return (b->objinfo->doh_file->doh_getline)(obj); return (b->objinfo->doh_position->doh_getline)(obj);
} }
return b->line;
} else { } else {
DohError(DOH_UNKNOWN, "Unknown object %x passed to Getline.\n", obj); 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; DohBase *b = (DohBase *) obj;
DohError(DOH_CALLS,"DohSetline %x, %d\n",obj, line); DohError(DOH_CALLS,"DohSetline %x, %d\n",obj, line);
if (DohCheck(obj)) { if (DohCheck(obj)) {
if (b->objinfo->doh_file && b->objinfo->doh_file->doh_setline) { if (b->objinfo->doh_position && b->objinfo->doh_position->doh_setline) {
(b->objinfo->doh_file->doh_setline)(obj, line); (b->objinfo->doh_position->doh_setline)(obj, line);
return; return;
} }
b->line = line;
} else { } else {
DohError(DOH_UNKNOWN, "Unknown object %x passed to Setline.\n", obj); DohError(DOH_UNKNOWN, "Unknown object %x passed to Setline.\n", obj);
} }
@ -277,10 +275,9 @@ DOH *DohGetfile(DOH *obj) {
DohBase *b = (DohBase *) obj; DohBase *b = (DohBase *) obj;
DohError(DOH_CALLS,"DohGetfile %x\n",obj); DohError(DOH_CALLS,"DohGetfile %x\n",obj);
if (DohCheck(obj)) { if (DohCheck(obj)) {
if (b->objinfo->doh_file && b->objinfo->doh_file->doh_getfile) { if (b->objinfo->doh_position && b->objinfo->doh_position->doh_getfile) {
return (b->objinfo->doh_file->doh_getfile)(obj); return (b->objinfo->doh_position->doh_getfile)(obj);
} }
return b->file;
} else { } else {
DohError(DOH_UNKNOWN, "Unknown object %x passed to Getfile.\n", obj); DohError(DOH_UNKNOWN, "Unknown object %x passed to Getfile.\n", obj);
} }
@ -289,22 +286,13 @@ DOH *DohGetfile(DOH *obj) {
/* Set the file */ /* Set the file */
void DohSetfile(DOH *obj, DOH *file) { void DohSetfile(DOH *obj, DOH *file) {
DOH *nf; DOH *nf = 0;
DohBase *b = (DohBase *) obj; DohBase *b = (DohBase *) obj;
DohError(DOH_CALLS,"DohSetfile %x, %x\n",obj,file); DohError(DOH_CALLS,"DohSetfile %x, %x\n",obj,file);
if (DohCheck(obj)) { if (DohCheck(obj)) {
if (file) { if (b->objinfo->doh_position && b->objinfo->doh_position->doh_setfile) {
nf = find_internal(file); (b->objinfo->doh_position->doh_setfile)(obj,file);
if (b->objinfo->doh_file && b->objinfo->doh_file->doh_setfile) { return;
(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;
} }
} else { } else {
DohError(DOH_UNKNOWN, "Unknown object %x passed to Setfile.\n", obj); DohError(DOH_UNKNOWN, "Unknown object %x passed to Setfile.\n", obj);
@ -865,7 +853,45 @@ void DohInit(DOH *b) {
DohBase *bs = (DohBase *) b; DohBase *bs = (DohBase *) b;
bs->refcount = 1; bs->refcount = 1;
bs->objinfo = &DohBaseType; bs->objinfo = &DohBaseType;
bs->line = 0;
bs->file = 0;
bs->flags = 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_file */
0, /* doh_string */ 0, /* doh_string */
&doh_callable_methods, /* doh_callable */ &doh_callable_methods, /* doh_callable */
0, /* reserved4 */ 0, /* doh_position */
0, /* reserved5 */ 0, /* reserved5 */
0, /* reserved6 */ 0, /* reserved6 */
0, /* user1 */ 0, /* user1 */

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -75,10 +75,6 @@ typedef struct {
int (*doh_seek)(DOH *obj, long offset, int whence); int (*doh_seek)(DOH *obj, long offset, int whence);
long (*doh_tell)(DOH *obj); long (*doh_tell)(DOH *obj);
int (*doh_close)(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; } DohFileMethods;
/* String methods */ /* String methods */
@ -92,6 +88,16 @@ typedef struct {
DOH *(*doh_call)(DOH *obj, DOH *args); /* Callable */ DOH *(*doh_call)(DOH *obj, DOH *args); /* Callable */
} DohCallableMethods; } 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;
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* DohObjInfo * DohObjInfo
@ -128,6 +134,7 @@ typedef struct DohObjInfo {
DohFileMethods *doh_file; /* File methods */ DohFileMethods *doh_file; /* File methods */
DohStringMethods *doh_string; /* String methods */ DohStringMethods *doh_string; /* String methods */
DohCallableMethods *doh_callable; /* Callable methods */ DohCallableMethods *doh_callable; /* Callable methods */
DohPositionalMethods *doh_position; /* Positional methods */
void *reserved4; void *reserved4;
void *reserved5; void *reserved5;
void *reserved6; void *reserved6;
@ -277,15 +284,22 @@ extern DOH *DohCall(DOH *obj, DOH *args);
DohObjInfo *objinfo; \ DohObjInfo *objinfo; \
DOH *nextptr; \ DOH *nextptr; \
int refcount; \ int refcount; \
short line; \
unsigned char flags; \ unsigned char flags; \
unsigned char scope; \ unsigned char scope
DOH *file
typedef struct { typedef struct {
DOHCOMMON; DOHCOMMON;
} DohBase; } DohBase;
#define DOHXCOMMON \
DOHCOMMON; \
DOH *file; \
int line
typedef struct {
DOHXCOMMON;
} DohXBase;
/* Macros for decrefing and increfing (safe for null objects). */ /* Macros for decrefing and increfing (safe for null objects). */
#define Decref(a) if (a) ((DohBase *) a)->refcount-- #define Decref(a) if (a) ((DohBase *) a)->refcount--
#define Incref(a) if (a) ((DohBase *) a)->refcount++ #define Incref(a) if (a) ((DohBase *) a)->refcount++

View file

@ -6,3 +6,8 @@
#include <stdarg.h> #include <stdarg.h>
#include "doh.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 DOH is a small C library that provides a number of simple yet powerful
data structures for building flexible applications. The data 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. object is allowed to support one or more classes of operations.
Common Operations (for all types) Common Operations (for all types)
--------------------------------- ---------------------------------
Delete(obj) Decrease the reference count and destroy if zero Delete(obj) Decrease the reference count and destroy if zero
Copy(obj) Make a copy of an object. Copy(obj) Make a copy of an object.
Clear(obj) Clear an object.
Str(obj) Create a string representation of obj. Str(obj) Create a string representation of obj.
Data(obj) Return pointer to raw data in an object Data(obj) Return pointer to raw data in an object
Len(obj) Length of 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 Delattr(hash,key) Delete an attribute
Firstkey(hash,key) Get first key Firstkey(hash,key) Get first key
Nextkey(hash,key) Get next key Nextkey(hash,key) Get next key
Getattrf(hash,key,format,...) Formatted attribute get
Setattrf(hash,key,format,...) Formatted attribute set
Sequence Operations Sequence Operations
------------------- -------------------