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

  1.  
  2. /*
  3.  * Copyright (C) 1990 by Jay Konigsberg. mail: jak@sactoh0
  4.  *
  5.  * Permission to use, copy, modify, and distribute this  software  and  its
  6.  * documentation is hereby  granted,  provided  that  the  above  copyright
  7.  * notice appear in all copies  and that  both  the  copyright  notice  and
  8.  * this permission notice appear in supporting documentation. This software
  9.  * is provided "as is" without express or implied  warranty.  However,  the
  10.  * author retains all Copyright priviliges and rights  to  renumeration  if
  11.  * this software is sold.
  12.  */
  13.  
  14. /*
  15.  * allocate - makes room in the text buffer for whatever was just entered.
  16.  */
  17. #include "simped.h"
  18.  
  19. char **allocate(text, buffer, overflow, location, count)
  20. char    **text;
  21. char    buffer[];
  22. char    *overflow;
  23. int    location;    /* where to allocate space for the buffer */
  24. int    count;        /* number of lines in the text area */
  25. {
  26. int    fprintf(),
  27.     cleanup();
  28.  
  29. extern char *malloc(),
  30.         *realloc();
  31.  
  32. int    linelen,    /* length of the input buffer */
  33.     textinx;    /* text index for insert adjustment */
  34.  
  35. if ( ! (count % PTR_CHUNK) )
  36.     {
  37.     if ( (text = (char **)realloc((char *)text, (unsigned)
  38.     ((count+PTR_CHUNK) * sizeof(char *)))) == (char **)0)
  39.     {
  40.     fprintf(stderr, "realloc: error=%d\n", errno);
  41.     cleanup(2);
  42.     }
  43.     }
  44. if (location < count) /* insert vs. append */
  45.     {
  46.     for(textinx=count; textinx >= location; --textinx)
  47.     text[textinx]=text[textinx-1];
  48.     }
  49. if (overflow)
  50.     {
  51.     linelen=strlen(buffer);
  52.     if((text[location-1]=malloc((unsigned)(linelen+FUDGE))) == NULL)
  53.     {
  54.     fprintf(stderr, "malloc: error=%d\n", errno);
  55.     cleanup(2);
  56.     }
  57.     text[location-1] = strcpy(text[location-1], buffer);
  58.     text[location-1][linelen]='\n';
  59.     text[location-1][linelen+1]='\0';
  60.     /* text[location-1] = strcat(text[location-1], "\n"); */
  61.     }
  62. else
  63.     {
  64.     text[location-1] = strdup(buffer);
  65.     }
  66. return(text);
  67. }
  68.