'===========================================================================
' Subject: WORD WRAPPER (FOR TEXT FILES)      Date: 04-08-97 (10:10)       
'  Author: The ABC Programmer                 Code: QB, QBasic, PDS        
'  Origin: voxel@freenet.edmonton.ab.ca     Packet: TEXT.ABC
'===========================================================================
'========================================================================
' Simple word wrapper for text files.  (Code: William Yu  Date: 04-08-97)
' 100% PUBLIC DOMAIN, please use freely.
'
' Purpose: Wraps a line exceeding the specified wrap limit (WrapLimit).
'          Made especially for Windows documents saved as plain text.
'========================================================================

DEFINT A-Z

CONST False = 0
CONST True = NOT False

WrapLimit = 78                      ' Start wrapping at which column?

FileName$ = "OLDDOC.TXT"            ' Location of input file.
OutFile$ = "NEWDOC.TXT"             ' Name of output file.

FileNum = FREEFILE
OPEN FileName$ FOR INPUT AS FileNum     ' No file checking done.

OutFileNum = FREEFILE
OPEN OutFile$ FOR OUTPUT AS OutFileNum  ' No file exist checking done.

DO
  LINE INPUT #FileNum, Text$            ' Read in text.
  IF LEN(Text$) <= WrapLimit THEN       ' If line does not exceed wrap limit
    PRINT #OutFileNum, Text$            ' then no need to parse line.
  ELSE
    CantWrap = False      ' Used as a dummy error (ie. WrapLimit = 1..etc)
    WHILE (LEN(Text$) > WrapLimit) AND (CantWrap = False)
      CantWrap = True
      FOR I = WrapLimit TO 1 STEP -1      ' Parse backwards.
        IF MID$(Text$, I, 1) = " " THEN   ' For first occurence of a space.
          NewText$ = LEFT$(Text$, I - 1)  ' Get new text.
          CantWrap = False                ' Set back to false.
          EXIT FOR                        ' No more parsing needed.
        END IF
      NEXT I
      IF CantWrap = False THEN
        Text$ = MID$(Text$, I + 1, LEN(Text$))
        PRINT #OutFileNum, NewText$
      END IF
    WEND
    PRINT #OutFileNum, Text$              ' Dump remaining text to file.
  END IF
LOOP UNTIL EOF(FileNum)                   ' parse entire file

CLOSE FileNum, OutFileNum

END
