home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1456 / allocate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  1.4 KB  |  60 lines

  1. /*
  2.  * Copyright (C) 1990 Jay Konigsberg - see Makefile for details
  3.  * This is Free Software, distrubited under the GNU Software Aggrement.
  4.  */
  5.  
  6. /*
  7.  * allocate - makes room in the text buffer for whatever was just entered.
  8.  */
  9. #include "simped.h"
  10.  
  11. char **allocate(text, buffer, overflow, location, count)
  12. char    **text;
  13. char    buffer[];
  14. char    *overflow;
  15. int    location;    /* where to allocate space for the buffer */
  16. int    count;        /* number of lines in the text area */
  17. {
  18. int    fprintf(),
  19.     cleanup();
  20.  
  21. extern char *malloc(),
  22.         *realloc();
  23.  
  24. int    linelen,    /* length of the input buffer */
  25.     textinx;    /* text index for insert adjustment */
  26.  
  27. if ( ! (count % PTR_CHUNK) )
  28.     {
  29.     if ( (text = (char **)realloc((char *)text, (unsigned)
  30.     ((count+PTR_CHUNK) * sizeof(char *)))) == (char **)0)
  31.     {
  32.     fprintf(stderr, "realloc: error=%d\n", errno);
  33.     cleanup(2);
  34.     }
  35.     }
  36. if (location < count) /* insert vs. append */
  37.     {
  38.     for(textinx=count; textinx >= location; --textinx)
  39.     text[textinx]=text[textinx-1];
  40.     }
  41. if (overflow)
  42.     {
  43.     linelen=strlen(buffer);
  44.     if((text[location-1]=malloc((unsigned)(linelen+FUDGE))) == NULL)
  45.     {
  46.     fprintf(stderr, "malloc: error=%d\n", errno);
  47.     cleanup(2);
  48.     }
  49.     text[location-1] = strcpy(text[location-1], buffer);
  50.     text[location-1][linelen]='\n';
  51.     text[location-1][linelen+1]='\0';
  52.     /* text[location-1] = strcat(text[location-1], "\n"); */
  53.     }
  54. else
  55.     {
  56.     text[location-1] = strdup(buffer);
  57.     }
  58. return(text);
  59. }
  60.