home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tel2305s.zip / TEK / TEKSTOR.C next >
C/C++ Source or Header  |  1992-02-25  |  4KB  |  193 lines

  1. /*
  2. *
  3. *  tekstor.c by Aaron Contorer, NCSA
  4. *
  5. *  Character storage routines for use by Telnet VG 
  6. *    (virtual graphics screen) routines.
  7. *  Allocates storage incrementally as more data comes in.
  8. *  Uses larger and larger increments to avoid inefficiency.
  9. *  Full data abstraction is provided.
  10. *
  11. *  Data structure:  
  12. *    A unique-type header node begins the data structure.  This points
  13. *  to the head of a linked list of "handles".  Each handle contains:
  14. *  > a pointer to a "pool" of storage memory created by malloc()
  15. *  > an int stating the number of bytes in the pool
  16. *  > a pointer to the next handle
  17. *
  18. *  IDEAS FOR IMPROVEMENT:
  19. *  Store pool as part of handle, rather than pointed to by handle
  20. *
  21. */
  22.  
  23. #define MINPOOL 0x0200    /* smallest allowable pool */
  24. #define MAXPOOL 0x2000    /* largest allowable pool */
  25. #define STORMASTER
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #ifdef MSC
  30. #include <malloc.h>
  31. #endif
  32. #include "tekstor.h"
  33. #include "externs.h"
  34.  
  35. #define TRUE 1
  36. #define FALSE 0
  37.  
  38. /*
  39. *    Create a new, empty store and return a pointer to it.
  40. *    Returns NULL if not enough memory to create a new store.
  41. */
  42. STOREP newstore(void )
  43. {
  44.     STOREP s;
  45.     
  46.     s=(STOREP) malloc(sizeof(STORE));
  47.     if(s==NULL)
  48.         return(NULL);
  49.     else {
  50.         s->lasth=s->thish=s->firsth=(HANDLEP) malloc(sizeof(HANDLE));
  51.         if(s->firsth==NULL) {
  52.             free(s);
  53.             return(NULL);
  54.           }    /* end if */
  55.         else {
  56.             s->firsth->pool=malloc(MINPOOL);
  57.             if(s->firsth->pool==NULL) {
  58.                 free(s->firsth);
  59.                 free(s);
  60.                 return(NULL);
  61.               }
  62.             else {
  63.                 s->lastelnum=s->thiselnum=-1;
  64.                 s->firsth->poolsize=MINPOOL;
  65.                 s->firsth->next=NULL;
  66.               }
  67.           }
  68.       }
  69.     return(s);
  70. }
  71.  
  72. /*
  73.     Frees all pools and other memory space associated with store s.
  74. */
  75. void freestore(STOREP s)
  76. {
  77.     HANDLEP h,h2;
  78.     h=s->firsth;
  79.     while(h!=NULL) {
  80.         h2=h;
  81.         free(h->pool);
  82.         h=h->next;
  83.         free(h2);
  84.       }
  85.     free(s);
  86. }
  87.  
  88. /*
  89.     Adds character d to the end of store s.
  90.     Returns 0 if successful, -1 if unable to add character (no memory).
  91. */
  92. int addstore(STOREP s,char d)
  93. {
  94.     unsigned int n; /* temp storage */
  95.     int size;
  96.     HANDLEP h;
  97.  
  98.     n=++(s->lastelnum);
  99.     size=s->lasth->poolsize;
  100.     if(n < (s->lasth->poolsize)) 
  101.         s->lasth->pool[n]=d;
  102.     else {            /* Pool full; allocate a new one. */
  103.         if(size<MAXPOOL) 
  104.             size <<= 1;
  105.         h=(HANDLEP)malloc(sizeof(HANDLE));
  106.         if(h==NULL) {
  107.             (s->lastelnum)--;
  108.             return(-1);
  109.           }
  110.         else {
  111.             h->pool=malloc(size);
  112.             if(h->pool==NULL) {
  113.                 free(h);
  114.                 (s->lastelnum)--;
  115.                 return(-1);
  116.               }
  117.             else {
  118.                 h->poolsize=size;
  119.                 h->next=NULL;
  120.                 s->lasth->next=h;
  121.                 s->lasth=h;
  122.                 s->lastelnum=0;
  123.                 h->pool[0]=d;
  124.               }
  125.           }
  126.       }             /* end of new pool allocation */
  127.     return(0);
  128. }   /* end addstore() */
  129.  
  130. /*
  131.     Reset stats so that a call to nextitem(s) will be retrieving the
  132.     first item in store s.
  133. */
  134. void topstore(STOREP s)
  135. {
  136.     s->thish=s->firsth;
  137.     s->thiselnum=-1;
  138. }
  139.  
  140. /*
  141.     Increment the current location in store s.  Then return the
  142.     character at that location.  Returns -1 if no more characters.
  143. */
  144. int nextitem(STOREP s)
  145. {
  146.     HANDLEP h;
  147.  
  148.     if(s->thish==s->lasth&&s->thiselnum==s->lastelnum) 
  149.         return(-1);
  150.     else {
  151.         h=s->thish;
  152.         if((++(s->thiselnum)) < (int)(s->thish->poolsize)) 
  153.             return((int)(s->thish->pool[s->thiselnum]));
  154.         else {            /* move to next pool */
  155.             h=h->next;
  156.             s->thish=h;
  157.             s->thiselnum=0;
  158.             return((int)(h->pool[0]));
  159.           }
  160.       }
  161. }   /* end nextitem() */
  162.  
  163. /*
  164.     Removes ("pops") the last item from the specified store.
  165.     Returns that item (in range 0-255), or returns -1 if there
  166.     are no items in the store.
  167. */
  168. int unstore(STOREP s)
  169. {
  170.     HANDLEP nextolast;
  171.  
  172.     if(s->lastelnum>-1)              /* last pool not empty */
  173.         return((int)(s->lasth->pool[(s->lastelnum)--]));
  174.     else {                         /* last pool empty */
  175.         if(s->lasth==s->firsth) 
  176.             return(-1);
  177.         else {                     /* move back one pool */
  178.             nextolast=s->firsth;
  179.             while(nextolast->next!=s->lasth)
  180.                 nextolast=nextolast->next;
  181.             free(nextolast->next);
  182.             s->lasth=nextolast;
  183.             s->lastelnum=nextolast->poolsize-2;
  184.             if(s->thish==nextolast->next) {
  185.                 s->thish=nextolast;
  186.                 s->thiselnum=s->lastelnum;
  187.               }
  188.             nextolast->next=NULL;
  189.             return((int)(nextolast->pool[s->lastelnum+1]));
  190.           }
  191.       }
  192. }
  193.