home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 2.ddi / OWLSRC.EXE / CSCAPE / SOURCE / SYMLIST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  4.8 KB  |  263 lines

  1. /*
  2.     symlist.c
  3.  
  4.     % symbol list functions
  5.  
  6.     OWL 1.1
  7.     Copyright (c) 1988, by Oakland Group, Inc.
  8.     ALL RIGHTS RESERVED.
  9.  
  10.     Revision History:
  11.     -----------------
  12.     11/30/88 jmd    Added omalloc
  13.     12/23/88 jmd    Put ofree in oslist close
  14.     02/04/89 jdc    fixed loop in oslist_DelSym()
  15.      2/08/89 jmd    added _arg macro for alpha_find
  16.      3/03/89 jdc    rewrote without class functions
  17.      4/16/89 jmd    replaced iarray with jarray, 
  18.                     preened return of alpha_find, oslist_DelSym
  19.      4/28/89 jmd    fixed `strncpy' bug
  20.      8/07/89 jdc    preened
  21.      8/09/89 jdc    changed oslist_GetSym to oslist_Get
  22. */
  23.  
  24. #include "oakhead.h"
  25. #include "jadecl.h"
  26.  
  27. #include "symldecl.h"
  28.  
  29. boolean alpha_find(_arg3(oslist_type, char*, int*));
  30.  
  31. oslist_type oslist_Open(size, data_size)
  32.     int size;
  33.     int data_size;
  34. {
  35.     oslist_type oslist;
  36.  
  37.     if ((oslist = (oslist_type) omalloc(OA_LIST, sizeof(struct oslist_struct))) == NULL) {
  38.         goto QUIT3;
  39.     }
  40.     if ((oslist->seqnt = xa_Open(size)) == NULL) {
  41.         goto QUIT2;
  42.     }
  43.     if ((oslist->alpha = ia_Open(size)) == NULL) {
  44.         goto QUIT1;
  45.     }
  46.     oslist->count = 0;
  47.     oslist->size = 0;
  48.     oslist->dsize = data_size;
  49.  
  50.     return(oslist);
  51.  
  52. QUIT1:
  53.     xa_Close(oslist->seqnt);
  54. QUIT2:
  55.     ofree(OA_LIST, oslist);
  56. QUIT3:
  57.     return(NULL);
  58. }
  59.     
  60. int _oslist_put(oslist, symbol, data)
  61.     oslist_type oslist;
  62.     char* symbol;
  63.     VOID* data;
  64. {
  65.     int i, alpha, len, temp;
  66.     char *sym;
  67.  
  68.     if (oslist == NULL || symbol == NULL || *symbol == '\0') {
  69.         return(OSLIST_BADNAME);
  70.     }
  71.  
  72.     if (alpha_find(oslist, symbol, &alpha) == FALSE) {
  73.  
  74.         if ((len = strlen(symbol) + 1) > OSLIST_SYMMAXLEN) {
  75.             len = OSLIST_SYMMAXLEN;
  76.         }
  77.         if ((sym = (char *) omalloc(OA_LSYM, len + oslist->dsize)) == NULL) {
  78.             return(OSLIST_BADNAME);
  79.         }
  80.  
  81.         strncpy(sym, symbol, len);
  82.         sym[len - 1] = '\0';
  83.  
  84.         if (oslist->dsize > 0) {
  85.             memcpy(sym + len, (char *)data, oslist->dsize);
  86.         }
  87.  
  88.         /* insert into alpha oslist */
  89.         for (i = oslist->size; i > alpha; i--) {
  90.             temp = ia_Get(oslist->alpha, i - 1);
  91.             ia_Put(oslist->alpha, i, temp);
  92.         }
  93.  
  94.         i = oslist->count;
  95.         oslist->count++;
  96.         oslist->size++;
  97.         ia_Put(oslist->alpha, alpha, i);
  98.         xa_Put(oslist->seqnt, i, (VOID*)sym);
  99.     }
  100.     else {
  101.         i = ia_Get(oslist->alpha, alpha);
  102.     }
  103.     return(i);
  104. }
  105.  
  106. boolean alpha_find(oslist, name, alpha)
  107.     oslist_type oslist;
  108.     char *name;
  109.     int *alpha;
  110. /*
  111.     returns TRUE if found, FALSE if not.
  112.  
  113.     *alpha contains proper spot in alpha oslist.
  114. */
  115. {
  116.     int cmp, flag;
  117.     int guess, size;
  118.  
  119.     if (oslist->size == 0) {
  120.         *alpha = 0;
  121.         return(FALSE);
  122.     }
  123.     size = oslist->size / 2;
  124.     guess = size;
  125.     flag = 0;
  126.  
  127.     while ((cmp = strcmp(name, (char *) xa_Get(oslist->seqnt, ia_Get(oslist->alpha, guess)))) != 0) {
  128.  
  129.         cmp = (cmp < 0) ? -1 : 1;
  130.  
  131.         if (flag == -cmp) {    /* repeating */
  132.             if (cmp > 0) {        /* name greater than guess */
  133.                 guess += 1;
  134.             }
  135.             break;
  136.         }
  137.         if (size <= 1) {
  138.             size = 1;
  139.             flag = cmp;
  140.         }
  141.         else {
  142.             size /= 2;
  143.         }
  144.         guess += cmp * size;
  145.         if (guess < 0) {
  146.             guess = 0;
  147.             break;
  148.         }
  149.         else if (guess >= oslist->size) {
  150.             guess = oslist->size;
  151.             break;
  152.         }
  153.     }                        
  154.     *alpha = guess;
  155.  
  156.     return(cmp == 0);
  157. }        
  158.  
  159. char *oslist_Get(oslist, h)
  160.     oslist_type oslist;
  161.     int h;
  162. {
  163.     if (oslist == NULL) {
  164.         return(NULL);
  165.     }
  166.     return((char *)xa_Get(oslist->seqnt, h));
  167. }
  168.  
  169. void oslist_SetData(oslist, h, data)
  170.     oslist_type oslist;
  171.     int h;
  172.     VOID *data;
  173. {
  174.     char *sym;
  175.     
  176.     if (oslist != NULL && (sym = oslist_Get(oslist, h)) != NULL) {
  177.         memcpy(sym + strlen(sym) + 1, (char *)data, oslist->dsize);
  178.     }
  179. }
  180.  
  181. int oslist_AlphaHandle(oslist, a)
  182.     oslist_type oslist;
  183.     int a;
  184. {
  185.     if (oslist == NULL) {
  186.         return(OSLIST_BADNAME);
  187.     }
  188.     return(ia_Get(oslist->alpha, a));
  189. }
  190.  
  191. int oslist_GetSize(oslist)
  192.     oslist_type oslist;
  193. {
  194.     if (oslist == NULL) {
  195.         return(0);
  196.     }
  197.     return(oslist->size);
  198. }
  199.  
  200. int oslist_FindSymHandle(oslist, symbol)
  201.     oslist_type oslist;
  202.     char *symbol;
  203. {
  204.     int alpha;
  205.  
  206.     if (oslist == NULL || !alpha_find(oslist, symbol, &alpha)) {
  207.         return(OSLIST_BADNAME);
  208.     }
  209.     else {
  210.         return(ia_Get(oslist->alpha, alpha));
  211.     }
  212. }
  213.  
  214. int oslist_DelSym(oslist, h)
  215.     oslist_type oslist;
  216.     int h;
  217. /*
  218. */
  219. {
  220.     char *sym;
  221.     int alpha, i, temp;
  222.  
  223.     if (h == OSLIST_BADNAME || oslist == NULL 
  224.         || (sym = oslist_Get(oslist, h)) == NULL) {
  225.  
  226.         return(TRUE);
  227.     }
  228.     alpha_find(oslist, sym, &alpha);
  229.     /* close up alpha oslist */
  230.     oslist->size--;
  231.     for (i = alpha; i < oslist->size; i++) {
  232.         temp = ia_Get(oslist->alpha, i + 1);
  233.         ia_Put(oslist->alpha, i, temp);
  234.     }
  235.  
  236.     ofree(OA_LSYM, sym);
  237.     xa_Put(oslist->seqnt, h, NULL);
  238.  
  239.     return(oslist->size > 0);
  240. }
  241.  
  242. void oslist_Close(oslist)
  243.     oslist_type oslist;
  244. {
  245.     char *sym;
  246.     int i;
  247.     
  248.     if (oslist == NULL) {
  249.         return;
  250.     }
  251.     for (i = 0; i < oslist->count; i++) {
  252.  
  253.         if ((sym = oslist_Get(oslist, i)) != NULL) {
  254.             ofree(OA_LSYM, sym);
  255.         }
  256.     }
  257.     xa_Close(oslist->seqnt);
  258.     ia_Close(oslist->alpha);
  259.  
  260.     ofree(OA_LIST, (VOID *) oslist);
  261. }
  262.  
  263.