home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / gprim / discgrp / stack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-16  |  1.3 KB  |  56 lines

  1. #include <stdio.h>
  2. #include "enum.h"
  3. #include "ooglutil.h"
  4. #define CHUNKSIZE 10000
  5.  
  6.     typedef struct entry {char word[MAXDEPTH]; } entry;
  7.     static entry *stack = NULL, *old, *oldtop, *oldbase, *new;
  8.     static int numchunks = 1;
  9.     /* new points to the next empty space in the stack */
  10.     /* old points to the first non-empty space in the stack */
  11.     
  12. init_stack()
  13. {
  14.     if (stack) OOGLFree(stack);
  15.     stack = OOGLNewN(entry, CHUNKSIZE);
  16.     if (stack == NULL) exit(fprintf(stderr,"init_stack: no space\n"));
  17.     old =  oldbase = new = stack;
  18.     oldtop = stack - 1;
  19. }
  20.  
  21. make_new_old()
  22. {
  23.     /* oldbase and oldtop are inclusive limits if non-empty words */
  24.     oldbase = oldtop+1;
  25.     old = oldtop = new-1;
  26. }
  27.  
  28. char *
  29. pop_old_stack()
  30. {
  31.     if (old >= oldbase)    return((old--)->word);
  32.     else return(NULL);
  33. }
  34.  
  35. push_new_stack(word)
  36. char *word;
  37. {
  38.     entry * oldstack;
  39.     if (new >= stack + numchunks * CHUNKSIZE)    
  40.         {
  41.         oldstack = stack;
  42.         numchunks *= 2;
  43.             if ((stack = OOGLRenewN(entry, stack, numchunks * CHUNKSIZE )) == (entry *) NULL) return (0);
  44.         new = (entry *) stack + (new - oldstack);
  45.         old = (entry *) stack + (old - oldstack);
  46.         oldtop = (entry *) stack + (oldtop - oldstack);
  47.         oldbase = (entry *) stack + (oldbase - oldstack);
  48.         if (stack == NULL)
  49.         exit(fprintf(stderr,"push_new_stack: no space\n"));
  50.         }
  51.  
  52.      strcpy(new->word, word);
  53.     new++;
  54. }    
  55.  
  56.