home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Boldly Go Collection
/
version40.iso
/
TS
/
17A
/
DRWIN101.ZIP
/
SYMBOL.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-23
|
4KB
|
207 lines
#include <alloc.h>
#include <stdlib.h>
#include <string.h>
#include "symbol.h"
symtab *symopen(int sz,symcmpf cf)
{
symtab *s;
s=malloc(sizeof(symtab)); /*allocate symbol table struct*/
if (s==NULL) return NULL; /*if no memory, return*/
s->siz=sz; /*record size of symbol records*/
s->num=0; /*reset number of records*/
s->cmp=cf; /*record compare function*/
s->sym=NULL; /*pointer to first symbol*/
s->cur=NULL; /*reset current record pointer*/
return s;
} /*symopen*/
void symclose(symtab *st)
{
symrec *sym;
symrec *lsym;
if (st==NULL) return;
sym=st->sym;
while (sym) {
lsym=sym;
sym=sym->nxt;
free(lsym);
} /*while*/
free(st);
} /*symclose*/
void *symfind(symtab *st,void *rec)
{
symrec *sym;
int cmp;
if (st==NULL) return NULL;
for (sym=st->sym;sym;sym=sym->nxt) {
cmp=st->cmp(sym->rec,rec);
if (cmp>0) break;
else if (cmp==0) {
st->cur=sym;
return (void*)sym->rec;
} /*else if found*/
} /*while*/
return NULL;
} /*symfind*/
void *symins(symtab *st,void *rec)
{
symrec *rsym,*lsym; /*left/right symbols in list*/
symrec *new;
int cmp;
if (st==NULL) return NULL;
lsym=NULL;
for (rsym=st->sym;rsym;rsym=rsym->nxt) {
cmp=st->cmp(rsym->rec,rec);
if (cmp>0) break;
else if (cmp==0) {
st->cur=rsym;
return (void *)rsym->rec;
}
lsym=rsym;
} /*for*/
new=malloc(sizeof(symrec)-1+(st->siz));
if (new==NULL) return NULL;
(st->num)++;
if (lsym)
lsym->nxt=new;
else
st->sym=new;
if (rsym) rsym->prv=new;
new->prv=lsym;
new->nxt=rsym;
memcpy(new->rec,rec,st->siz);
st->cur=new;
return (void*)new->rec;
} /*symins*/
void *symdel(symtab *st,void *rec)
{
symrec *sym,*lsym;
int cmp;
if (st==NULL) return NULL;
lsym=NULL;
for (sym=st->sym;sym;sym=sym->nxt) {
cmp=st->cmp(sym->rec,rec);
if (cmp>0) return NULL;
else if (cmp==0) {
if (lsym)
lsym->nxt=sym->nxt;
else
st->sym=sym->nxt;
if (sym->nxt) (sym->nxt)->prv=lsym;
free(sym);
(st->num)--;
if (st->num<0) st->num=0;
return symfirst(rec);
}
lsym=sym;
} /*for*/
return NULL;
} /*symdel*/
void *symfirst(symtab *st)
{
if (st==NULL) return NULL;
if (st->sym==NULL) return NULL;
st->cur=st->sym;
return (void*)(st->cur)->rec;
} /*symfirst*/
void *symcurr(symtab *st)
{
if (st==NULL) return NULL;
return (void*)(st->cur)->rec;
} /*symcurr*/
void *symnext(symtab *st)
{
if (st==NULL) return NULL;
if (st->cur==NULL) return NULL;
st->cur=(st->cur)->nxt;
if (st->cur==NULL) return NULL;
return (void*)(st->cur)->rec;
} /*symnext*/
void *symprev(symtab *st)
{
if (st==NULL) return NULL;
if (st->cur==NULL) return NULL;
st->cur=(st->cur)->prv;
if (st->cur==NULL) return NULL;
return (void*)(st->cur)->rec;
} /*symprev*/
void *symlast(symtab *st)
{
if (st==NULL) return NULL;
if (st->sym==NULL) return NULL;
for (st->cur=st->sym;(st->cur)->nxt;st->cur=(st->cur)->nxt) ;
return (void*)(st->cur)->rec;
} /*symlast*/
void *symsucc(symtab *st,void *rec)
{
symrec *sym;
int cmp;
if (st==NULL) return NULL;
for (sym=st->sym;sym;sym=sym->nxt) {
cmp=st->cmp(sym->rec,rec);
if (cmp>0) break;
else if (cmp==0) {
st->cur=sym;
if (!sym->nxt) return NULL;
return (void*)((sym->nxt)->rec);
} /*else if found*/
} /*while*/
return NULL;
} /*symsucc*/
void *sympred(symtab *st,void *rec)
{
symrec *sym;
int cmp;
if (st==NULL) return NULL;
for (sym=st->sym;sym;sym=sym->nxt) {
cmp=st->cmp(sym->rec,rec);
if (cmp>0) break;
else if (cmp==0) {
st->cur=sym;
if (!sym->prv) return NULL;
return (void*)((sym->prv)->rec);
} /*else if found*/
} /*while*/
return NULL;
} /*sympred*/
int symnum(symtab* st)
{
if (!st) return 0;
return st->num;
} /*symnum*/