home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / 09print / pr_getln.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  928 b   |  44 lines

  1. /*
  2.  *    pr_getln -- get a line of text while expanding tabs;
  3.  *    put text into an array and return the length of the line
  4.  *    including termination to the calling function.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <local\std.h>
  10.  
  11. int
  12. pr_getln(s, lim, fin)
  13. char *s;
  14. int lim;
  15. FILE *fin;
  16. {
  17.     int ch;
  18.     register char *cp;
  19.  
  20.     extern int tabstop();    /* query tabstop array */
  21.  
  22.     cp = s;
  23.     while (--lim > 0 && (ch = fgetc(fin)) != EOF && ch != '\n' && ch != '\f') {
  24.         if (ch == '\t')
  25.             /* loop and store spaces until next tabstop */
  26.             do
  27.                 *cp++ = ' ';
  28.             while (--lim > 0 && tabstop(cp - s) == 0);
  29.         else
  30.             *cp++ = ch;
  31.     }
  32.     if (ch == EOF && cp - s == 0)
  33.         ;
  34.     else if (ch == EOF || ch == '\n')
  35.         *cp++ = '\n';    /* assure correct line termination */
  36.     else if (ch == '\f' && cp - s == 0) {
  37.         *cp++ = '\f';
  38.         fgetc(fin);    /* toss the trailing newline */
  39.     }
  40.     *cp = '\0';
  41.  
  42.     return (cp - s);
  43. }
  44.