home *** CD-ROM | disk | FTP | other *** search
/ Der Mediaplex Sampler - Die 6 von Plex / 6_v_plex.zip / 6_v_plex / DISK5 / DOS_50 / PVIC.ZIP / ALLOC.C next >
C/C++ Source or Header  |  1993-04-21  |  5KB  |  221 lines

  1. /* 
  2.  *
  3.  * Various allocation routines and routines returning information about
  4.  * allocated objects.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include "pvic.h"
  9. #include "locdefs.h"
  10.  
  11. char * alloc(size)
  12. unsigned size;
  13. {
  14.     char    *p;             /* pointer to new storage space */
  15.  
  16.     p = malloc(size);
  17.     if ( p == (char *)NULL ) {      /* if there is no more room... */
  18.         error_message("alloc() is unable to find memory!");
  19.     }
  20.     return(p);
  21. }
  22.  
  23. char * strsave(string)
  24. char    *string;
  25. {
  26.     return(strcpy(alloc((unsigned)(strlen(string)+1)),string));
  27. }
  28.  
  29. alloc_screen()
  30. {
  31.     /*
  32.      * If we're changing the size of the screen, free the old arrays
  33.      */
  34.     if (real_screen != NULL)
  35.         free(real_screen);
  36.     if (next_screen != NULL)
  37.         free(next_screen);
  38.  
  39.     real_screen = malloc((unsigned)(current_lines*current_columns));
  40.     next_screen = malloc((unsigned)(current_lines*current_columns));
  41.     if (!real_screen || !next_screen)
  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] = '\0';
  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.  * file_alloc() - construct an initial empty file buffer
  73.  */
  74. void
  75. file_alloc()
  76. {
  77.     if ((file_memory->linep = newline(0)) == NULL) {
  78.         fprintf(stderr,"Unable to allocate file memory!\n");
  79.         exit(1);
  80.     }
  81.     if ((top_of_file->linep = newline(0)) == NULL) {
  82.         fprintf(stderr,"Unable to allocate file memory!\n");
  83.         exit(1);
  84.     }
  85.     if ((end_of_file->linep = newline(0)) == NULL) {
  86.         fprintf(stderr,"Unable to allocate file memory!\n");
  87.         exit(1);
  88.     }
  89.     file_memory->index = 0;
  90.     top_of_file->index = 0;
  91.     end_of_file->index = 0;
  92.  
  93.     top_of_file->linep->next = file_memory->linep;  /* connect top_of_file to file_memory */
  94.     file_memory->linep->prev = top_of_file->linep;
  95.  
  96.     file_memory->linep->next = end_of_file->linep;  /* connect file_memory to end_of_file */
  97.     end_of_file->linep->prev = file_memory->linep;
  98.  
  99.     *cursor_char = *file_memory;
  100.     *top_char  = *file_memory;
  101.  
  102.     file_memory->linep->num = 0;
  103.     end_of_file->linep->num = 0xffff;
  104.  
  105.     clear_all();               /* 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 = top_of_file->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.     cursor_char->linep = NULL;         /* clear pointers */
  127.     top_of_file->linep = NULL;
  128.     file_memory->linep = NULL;
  129.     end_of_file->linep = NULL;
  130.  
  131.     u_clear();
  132. }
  133.  
  134. /*
  135.  * buffer_empty() - return (1) if the buffer is empty
  136.  */
  137. int
  138. buffer_empty()
  139. {
  140.     return (buf1line() && file_memory->linep->s[0] == '\0');
  141. }
  142.  
  143. /*
  144.  * buf1line() - return (1) if there is only one line
  145.  */
  146. int
  147. buf1line()
  148. {
  149.     return (file_memory->linep->next == end_of_file->linep);
  150. }
  151.  
  152. /*
  153.  * line_empty() - return (1) if the current line is empty
  154.  */
  155. int
  156. line_empty()
  157. {
  158.     return (cursor_char->linep->s[0] == '\0');
  159. }
  160.  
  161. /*
  162.  * end_of_line() - return (1) 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 '\0' byte, but handle it correctly in case it happens.
  166.  */
  167. int
  168. end_of_line(p)
  169. register LPTR   *p;
  170. {
  171.     return (p->linep->s[p->index] == '\0' || p->linep->s[p->index+1] == '\0');
  172. }
  173. /*
  174.  * can_increase(n) - returns (1) 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 (0).
  179.  */
  180. int
  181. can_increase(n)
  182. register int    n;
  183. {
  184.     register int    nsize;
  185.     register char   *s;             /* pointer to new space */
  186.  
  187.     nsize = strlen(cursor_char->linep->s) + 1 + n;     /* size required */
  188.  
  189.     if (nsize <= cursor_char->linep->size)
  190.         return (1);
  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.         error_message("Can't add anything, file is too big!");
  199.         current_status = STATUS_NORMAL;
  200.         return (0);
  201.     }
  202.  
  203.     cursor_char->linep->size = nsize + SLOP;
  204.     strcpy(s, cursor_char->linep->s);
  205.     free(cursor_char->linep->s);
  206.     cursor_char->linep->s = s;
  207.     
  208.     return (1);
  209. }
  210.  
  211. char * mkstr(c)
  212. char    c;
  213. {
  214.     static  char    s[2];
  215.  
  216.     s[0] = c;
  217.     s[1] = '\0';
  218.  
  219.     return s;
  220. }
  221.