home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0020 - 0029 / ibm0020-0029 / ibm0028.tar / ibm0028 / GRLF-C-2.ZIP / GFUNC / STRXLINE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-30  |  2.0 KB  |  88 lines

  1. /*
  2.  * strxline.c
  3.  * contains: strxline()
  4.  *
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include "gfuncts.h"
  9.  
  10. /*
  11.  *  char *
  12.  * strxline(outstr,instr,maxcols)
  13.  *
  14.  * ARGUMENT
  15.  *  (char *)    outstr    -    destination string
  16.  *  (char *)    instr    -    source string
  17.  *  (int)    maxcols    -    Maximum number of characters in destination
  18.  *
  19.  * DESCRIPTION
  20.  *   The source string is scanned left to right, and up to one line of
  21.  *   text is extracted to a destination string.  When a "gteol" (Technical
  22.  *   End Of Line) or a newline is encountered, the string up to that point
  23.  *   is terminated with a newline and EOS termination and copied to the
  24.  *   destination string.
  25.  *
  26.  *   Also, if gmaxcol characters have been transferred without a gteol or
  27.  *   a newline being encountered (or the end of string reached), the source
  28.  *   string pointer is backed up to the first non-whitespace character
  29.  *   preceding the current word, a newline is inserted, and the string up
  30.  *   to that point is transferred as though a newline or gteol had been found.
  31.  *
  32.  *   The utility of this function is to extract strings  from a text file,
  33.  *   even though the text may not be broken into logical lines.
  34.  *
  35.  * RETURNS
  36.  *    Pointer to next part of string following transfer,
  37.  *    or 0 if the end of the source string was reached.
  38.  *
  39.  * AUTHOR
  40.  *   Copyright (C)1987-1990 Greenleaf Software Inc. All Rights Reserved.
  41.  */
  42. char* GF_CONV strxline(outstr,instr,maxcols)
  43. char *outstr,*instr;
  44. int maxcols;
  45. {
  46.     char *pi,*po,c;
  47.     int i,k,imax;
  48.  
  49.     i=k=0;
  50.     imax=(maxcols<=gmaxcol?maxcols:gmaxcol );
  51.     po=outstr;
  52.     pi=instr;
  53.     while (FOREVER) {
  54.         if (i>=imax) {
  55.             k=0;
  56.             while(k<25) {
  57.                 if(!gisspace(*pi)) {
  58.                     --pi;
  59.                     --po;
  60.                     ++k;
  61.                 } else 
  62.                     break;
  63.             }
  64.             ++pi;
  65.             *++po='\n';
  66.             *++po='\0';
  67.             return pi;
  68.         } else {
  69.             if(((c = *pi)==0)&&i==0) 
  70.                 return (char *)0;
  71.             if(c==gteol)
  72.                 c='\n';
  73.             switch (c) {
  74.             case '\n':
  75.                 *po++='\n';
  76.             case '\0':
  77.                 *po='\0';
  78.                 return(c?++pi:pi);
  79.             default:
  80.                 *po++=c;
  81.                 pi++;
  82.                 ++i;
  83.             }
  84.         }
  85.     }
  86.     return((char *)0);
  87. }
  88.