home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 313_01 / alloc.c < prev    next >
C/C++ Source or Header  |  1990-04-22  |  5KB  |  219 lines

  1. /* $Header: /nw2/tony/src/stevie/src/RCS/alloc.c,v 1.5 89/08/06 09:49:22 tony Exp $
  2.  *
  3.  * Various allocation routines and routines returning information about
  4.  * allocated objects.
  5.  */
  6.  
  7. #include "stevie.h"
  8.  
  9. char *
  10. alloc(size)
  11. unsigned size;
  12. {
  13.     char    *p;        /* pointer to new storage space */
  14.  
  15.     p = malloc(size);
  16.     if ( p == (char *)NULL ) {    /* if there is no more room... */
  17.         emsg("alloc() is unable to find memory!");
  18.     }
  19.     return(p);
  20. }
  21.  
  22. char *
  23. strsave(string)
  24. char    *string;
  25. {
  26.     return(strcpy(alloc((unsigned)(strlen(string)+1)),string));
  27. }
  28.  
  29. void
  30. screenalloc()
  31. {
  32.     /*
  33.      * If we're changing the size of the screen, free the old arrays
  34.      */
  35.     if (Realscreen != NULL)
  36.         free(Realscreen);
  37.     if (Nextscreen != NULL)
  38.         free(Nextscreen);
  39.  
  40.     Realscreen = malloc((unsigned)(Rows*Columns));
  41.     Nextscreen = malloc((unsigned)(Rows*Columns));
  42. }
  43.  
  44. /*
  45.  * Allocate and initialize a new line structure with room for
  46.  * 'nchars'+1 characters. We add one to nchars here to allow for
  47.  * null termination because all the callers would just do it otherwise.
  48.  */
  49. LINE *
  50. newline(nchars)
  51. int    nchars;
  52. {
  53.     register LINE    *l;
  54.  
  55.     if ((l = (LINE *) alloc(sizeof(LINE))) == NULL)
  56.         return (LINE *) NULL;
  57.  
  58.     l->s = alloc((unsigned) (nchars+1));    /* the line is empty */
  59.     l->s[0] = NUL;
  60.     l->size = nchars + 1;
  61.  
  62.     l->prev = (LINE *) NULL;    /* should be initialized by caller */
  63.     l->next = (LINE *) NULL;
  64.  
  65.     return l;
  66. }
  67.  
  68. /*
  69.  * filealloc() - construct an initial empty file buffer
  70.  */
  71. void
  72. filealloc()
  73. {
  74.     if ((Filemem->linep = newline(0)) == NULL) {
  75.         fprintf(stderr,"Unable to allocate file memory!\n");
  76.         exit(1);
  77.     }
  78.     if ((Filetop->linep = newline(0)) == NULL) {
  79.         fprintf(stderr,"Unable to allocate file memory!\n");
  80.         exit(1);
  81.     }
  82.     if ((Fileend->linep = newline(0)) == NULL) {
  83.         fprintf(stderr,"Unable to allocate file memory!\n");
  84.         exit(1);
  85.     }
  86.     Filemem->index = 0;
  87.     Filetop->index = 0;
  88.     Fileend->index = 0;
  89.  
  90.     Filetop->linep->next = Filemem->linep;    /* connect Filetop to Filemem */
  91.     Filemem->linep->prev = Filetop->linep;
  92.  
  93.     Filemem->linep->next = Fileend->linep;    /* connect Filemem to Fileend */
  94.     Fileend->linep->prev = Filemem->linep;
  95.  
  96.     *Curschar = *Filemem;
  97.     *Topchar  = *Filemem;
  98.  
  99.     Filemem->linep->num = 0;
  100.     Fileend->linep->num = 0xffff;
  101.  
  102.     clrall();        /* clear all marks */
  103.     u_clear();        /* clear the undo buffer */
  104. }
  105.  
  106. /*
  107.  * freeall() - free the current buffer
  108.  *
  109.  * Free all lines in the current buffer.
  110.  */
  111. void
  112. freeall()
  113. {
  114.     register LINE    *lp, *xlp;
  115.  
  116.     for (lp = Filetop->linep; lp != NULL ;lp = xlp) {
  117.         if (lp->s != NULL)
  118.             free(lp->s);
  119.         xlp = lp->next;
  120.         free((char *)lp);
  121.     }
  122.  
  123.     Curschar->linep = NULL;        /* clear pointers */
  124.     Filetop->linep = NULL;
  125.     Filemem->linep = NULL;
  126.     Fileend->linep = NULL;
  127.  
  128.     u_clear();
  129. }
  130.  
  131. /*
  132.  * bufempty() - return TRUE if the buffer is empty
  133.  */
  134. bool_t
  135. bufempty()
  136. {
  137.     return (buf1line() && Filemem->linep->s[0] == NUL);
  138. }
  139.  
  140. /*
  141.  * buf1line() - return TRUE if there is only one line
  142.  */
  143. bool_t
  144. buf1line()
  145. {
  146.     return (Filemem->linep->next == Fileend->linep);
  147. }
  148.  
  149. /*
  150.  * lineempty() - return TRUE if the current line is empty
  151.  */
  152. bool_t
  153. lineempty()
  154. {
  155.     return (Curschar->linep->s[0] == NUL);
  156. }
  157.  
  158. /*
  159.  * endofline() - return TRUE if the given position is at end of line
  160.  *
  161.  * This routine will probably never be called with a position resting
  162.  * on the NUL byte, but handle it correctly in case it happens.
  163.  */
  164. bool_t
  165. endofline(p)
  166. register LPTR    *p;
  167. {
  168.     return (p->linep->s[p->index] == NUL || p->linep->s[p->index+1] == NUL);
  169. }
  170. /*
  171.  * canincrease(n) - returns TRUE if the current line can be increased 'n' bytes
  172.  *
  173.  * This routine returns immediately if the requested space is available.
  174.  * If not, it attempts to allocate the space and adjust the data structures
  175.  * accordingly. If everything fails it returns FALSE.
  176.  */
  177. bool_t
  178. canincrease(n)
  179. register int    n;
  180. {
  181.     register int    nsize;
  182.     register char    *s;        /* pointer to new space */
  183.  
  184.     nsize = strlen(Curschar->linep->s) + 1 + n;    /* size required */
  185.  
  186.     if (nsize <= Curschar->linep->size)
  187.         return TRUE;
  188.  
  189.     /*
  190.      * Need to allocate more space for the string. Allow some extra
  191.      * space on the assumption that we may need it soon. This avoids
  192.      * excessive numbers of calls to malloc while entering new text.
  193.      */
  194.     if ((s = alloc((unsigned) (nsize + SLOP))) == NULL) {
  195.         emsg("Can't add anything, file is too big!");
  196.         State = NORMAL;
  197.         return FALSE;
  198.     }
  199.  
  200.     Curschar->linep->size = nsize + SLOP;
  201.     strcpy(s, Curschar->linep->s);
  202.     free(Curschar->linep->s);
  203.     Curschar->linep->s = s;
  204.     
  205.     return TRUE;
  206. }
  207.  
  208. char *
  209. mkstr(c)
  210. char    c;
  211. {
  212.     static    char    s[2];
  213.  
  214.     s[0] = c;
  215.     s[1] = NUL;
  216.  
  217.     return s;
  218. }
  219.