home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / STVI369G.ZIP / ALLOC.C next >
C/C++ Source or Header  |  1990-05-01  |  5KB  |  222 lines

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