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

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