home *** CD-ROM | disk | FTP | other *** search
- /*
- symlist.c
-
- % symbol list functions
-
- OWL 1.1
- Copyright (c) 1988, by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 11/30/88 jmd Added omalloc
- 12/23/88 jmd Put ofree in oslist close
- 02/04/89 jdc fixed loop in oslist_DelSym()
- 2/08/89 jmd added _arg macro for alpha_find
- 3/03/89 jdc rewrote without class functions
- 4/16/89 jmd replaced iarray with jarray,
- preened return of alpha_find, oslist_DelSym
- 4/28/89 jmd fixed `strncpy' bug
- 8/07/89 jdc preened
- 8/09/89 jdc changed oslist_GetSym to oslist_Get
- */
-
- #include "oakhead.h"
- #include "jadecl.h"
-
- #include "symldecl.h"
-
- boolean alpha_find(_arg3(oslist_type, char*, int*));
-
- oslist_type oslist_Open(size, data_size)
- int size;
- int data_size;
- {
- oslist_type oslist;
-
- if ((oslist = (oslist_type) omalloc(OA_LIST, sizeof(struct oslist_struct))) == NULL) {
- goto QUIT3;
- }
- if ((oslist->seqnt = xa_Open(size)) == NULL) {
- goto QUIT2;
- }
- if ((oslist->alpha = ia_Open(size)) == NULL) {
- goto QUIT1;
- }
- oslist->count = 0;
- oslist->size = 0;
- oslist->dsize = data_size;
-
- return(oslist);
-
- QUIT1:
- xa_Close(oslist->seqnt);
- QUIT2:
- ofree(OA_LIST, oslist);
- QUIT3:
- return(NULL);
- }
-
- int _oslist_put(oslist, symbol, data)
- oslist_type oslist;
- char* symbol;
- VOID* data;
- {
- int i, alpha, len, temp;
- char *sym;
-
- if (oslist == NULL || symbol == NULL || *symbol == '\0') {
- return(OSLIST_BADNAME);
- }
-
- if (alpha_find(oslist, symbol, &alpha) == FALSE) {
-
- if ((len = strlen(symbol) + 1) > OSLIST_SYMMAXLEN) {
- len = OSLIST_SYMMAXLEN;
- }
- if ((sym = (char *) omalloc(OA_LSYM, len + oslist->dsize)) == NULL) {
- return(OSLIST_BADNAME);
- }
-
- strncpy(sym, symbol, len);
- sym[len - 1] = '\0';
-
- if (oslist->dsize > 0) {
- memcpy(sym + len, (char *)data, oslist->dsize);
- }
-
- /* insert into alpha oslist */
- for (i = oslist->size; i > alpha; i--) {
- temp = ia_Get(oslist->alpha, i - 1);
- ia_Put(oslist->alpha, i, temp);
- }
-
- i = oslist->count;
- oslist->count++;
- oslist->size++;
- ia_Put(oslist->alpha, alpha, i);
- xa_Put(oslist->seqnt, i, (VOID*)sym);
- }
- else {
- i = ia_Get(oslist->alpha, alpha);
- }
- return(i);
- }
-
- boolean alpha_find(oslist, name, alpha)
- oslist_type oslist;
- char *name;
- int *alpha;
- /*
- returns TRUE if found, FALSE if not.
-
- *alpha contains proper spot in alpha oslist.
- */
- {
- int cmp, flag;
- int guess, size;
-
- if (oslist->size == 0) {
- *alpha = 0;
- return(FALSE);
- }
- size = oslist->size / 2;
- guess = size;
- flag = 0;
-
- while ((cmp = strcmp(name, (char *) xa_Get(oslist->seqnt, ia_Get(oslist->alpha, guess)))) != 0) {
-
- cmp = (cmp < 0) ? -1 : 1;
-
- if (flag == -cmp) { /* repeating */
- if (cmp > 0) { /* name greater than guess */
- guess += 1;
- }
- break;
- }
- if (size <= 1) {
- size = 1;
- flag = cmp;
- }
- else {
- size /= 2;
- }
- guess += cmp * size;
- if (guess < 0) {
- guess = 0;
- break;
- }
- else if (guess >= oslist->size) {
- guess = oslist->size;
- break;
- }
- }
- *alpha = guess;
-
- return(cmp == 0);
- }
-
- char *oslist_Get(oslist, h)
- oslist_type oslist;
- int h;
- {
- if (oslist == NULL) {
- return(NULL);
- }
- return((char *)xa_Get(oslist->seqnt, h));
- }
-
- void oslist_SetData(oslist, h, data)
- oslist_type oslist;
- int h;
- VOID *data;
- {
- char *sym;
-
- if (oslist != NULL && (sym = oslist_Get(oslist, h)) != NULL) {
- memcpy(sym + strlen(sym) + 1, (char *)data, oslist->dsize);
- }
- }
-
- int oslist_AlphaHandle(oslist, a)
- oslist_type oslist;
- int a;
- {
- if (oslist == NULL) {
- return(OSLIST_BADNAME);
- }
- return(ia_Get(oslist->alpha, a));
- }
-
- int oslist_GetSize(oslist)
- oslist_type oslist;
- {
- if (oslist == NULL) {
- return(0);
- }
- return(oslist->size);
- }
-
- int oslist_FindSymHandle(oslist, symbol)
- oslist_type oslist;
- char *symbol;
- {
- int alpha;
-
- if (oslist == NULL || !alpha_find(oslist, symbol, &alpha)) {
- return(OSLIST_BADNAME);
- }
- else {
- return(ia_Get(oslist->alpha, alpha));
- }
- }
-
- int oslist_DelSym(oslist, h)
- oslist_type oslist;
- int h;
- /*
- */
- {
- char *sym;
- int alpha, i, temp;
-
- if (h == OSLIST_BADNAME || oslist == NULL
- || (sym = oslist_Get(oslist, h)) == NULL) {
-
- return(TRUE);
- }
- alpha_find(oslist, sym, &alpha);
- /* close up alpha oslist */
- oslist->size--;
- for (i = alpha; i < oslist->size; i++) {
- temp = ia_Get(oslist->alpha, i + 1);
- ia_Put(oslist->alpha, i, temp);
- }
-
- ofree(OA_LSYM, sym);
- xa_Put(oslist->seqnt, h, NULL);
-
- return(oslist->size > 0);
- }
-
- void oslist_Close(oslist)
- oslist_type oslist;
- {
- char *sym;
- int i;
-
- if (oslist == NULL) {
- return;
- }
- for (i = 0; i < oslist->count; i++) {
-
- if ((sym = oslist_Get(oslist, i)) != NULL) {
- ofree(OA_LSYM, sym);
- }
- }
- xa_Close(oslist->seqnt);
- ia_Close(oslist->alpha);
-
- ofree(OA_LIST, (VOID *) oslist);
- }
-
-