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

  1. /*
  2.  * Copyright (C) 1990 Jay Konigsberg - see Makefile for details
  3.  * This is Free Software.
  4.  */
  5.  
  6. /*
  7.  * addlines - add text to the text buffer. This routine
  8.  * does double service reading from stdin or a file.
  9.  * and handeles inserts and appends.
  10.  */
  11. #include "simped.h"
  12.  
  13. char **addlines(text, overflow, count, location, fd, newfile)
  14. char **text;
  15. char *overflow;
  16. int  *count;
  17. int  location;        /* the line number after which the add occures */
  18. int  newfile;        /* newfile flag to statt in insert mode */
  19. FILE *fd;
  20. {
  21. int    printf();
  22.  
  23. extern    char    *getline(),
  24.         **allocate();
  25.  
  26. char    buffer[LINELEN+2];
  27.  
  28. int    char_read_in=0,        /* characters read in from fd */
  29.     text_entered=TRUE;    /* boolean to determin when to allocate
  30.                    space AND the flag for EOF */
  31.  
  32. if (*count >= 0)
  33.     ++(*count);
  34. for(;;)
  35.     {
  36.     if (fd == stdin || newfile)
  37.     {
  38.     printf("%3d> ",location);
  39.     overflow = getline(buffer, &text_entered, stdin, '\0', FALSE);
  40.     }
  41.     else
  42.     {
  43.     overflow = getline(buffer, &text_entered, fd, '\0', FALSE);
  44.  
  45.     /* see if a blank line is being read in. if so, add 1 sp */
  46.     if (text_entered != EOF && buffer[0] == '\n')
  47.         {
  48.         buffer[0]=' ';
  49.         buffer[1]='\n';
  50.         buffer[2]='\0';
  51.         text_entered=TRUE;
  52.         }
  53.     }
  54.     if (text_entered && text_entered != EOF)
  55.     {
  56.     /* make sure there is one blank line at BOF - for postnews */
  57.     if (*count == 1 && buffer[0] != ' ' && buffer[1] != '\n'
  58.         && ! newfile )
  59.         {
  60.         text = allocate(text, " \n\0", overflow, location, *count);
  61.         char_read_in += 2;
  62.         (*count)++;
  63.         location++;
  64.         }
  65.     text = allocate(text, buffer, overflow, location, *count);
  66.     char_read_in += (strlen(buffer));
  67.     (*count)++;
  68.     location++;
  69.     }
  70.     else
  71.     break;
  72.     }
  73. if(fd != stdin && ! newfile)
  74.     {
  75.     printf("%d characters read in\n", char_read_in);
  76.     }
  77. if (*count >= 1)
  78.     --(*count);
  79. return(text);
  80. }
  81.