(view source code of crlf.rex as plain text)
/* CrLf, Version 2.10 */
/* Replace orphaned carriage returns and linefeeds with CRLF pairs */
/* Written by Rob van der Woude */
/* http://www.robvanderwoude.com */
/* Original version: March 7, 1998 */
/* Latest update: February 27, 2003 */
/* Initialize RexxUtil */
call RxFuncAdd "sysloadfuncs", "RexxUtil", "sysloadfuncs"
call sysloadfuncs
/* Check if file to be converted does exist */
parse upper arg filespec outfile .
if filespec = "" | filespec = "/?" then call Syntax
call SysFileTree filespec, "dir.", "F", , "**---"
parse value dir.1 with . . filesize . filename
filename = translate( strip( filename ) )
filesize = filesize + 0
/* Check if output file exists */
if outfile = "" then outfile = filename||".CLP"
call SysFileTree outfile, "exist.", "F"
/* Generate error message if necessary */
select
when dir.0 = 0 then do
msg = "File "||filespec( "N", filename )||" not found"
call ErrorMessage
end
when dir.0 > 1 then do
msg = "Wildcards not allowed"
call ErrorMessage
end
when exist.0 <> 0 then do
msg = "Output file "||filespec( "N", outfile )||,
" already exists"
call ErrorMessage
end
otherwise nop
end
/* Read file */
say "Reading file . . ."
filestring = charin( filename, 1, filesize )
call stream filename, "C", "CLOSE"
/* Convert CRLF pairs to CR only */
say "Converting CRLF pairs to CR only . . ."
do forever
charpos = pos( "0D0A"X, filestring )
if charpos = 0 then leave
filestring = substr( filestring, 1, charpos )||,
substr( filestring, charpos + 2 )
end
/* Translate CR to LF */
say "Converting CR to LF . . ."
filestring = translate( filestring, "0A"X, "0D"X )
/* Replace LF with CRLF */
say "Converting LF to CRLF pairs . . ."
startpos = 1
do forever
charpos = pos( "0A"X, filestring, startpos )
if charpos = 0 then leave
startpos = charpos + 2
filestring = substr( filestring, 1, charpos - 1 )||"0D"X||,
substr( filestring, charpos )
end
/* Write result to new file */
say "Writing result to file . . ."
rc = charout( outfile, filestring )
call stream outfile, "C", "CLOSE"
if rc > 0 then do
msg = "Error writing output to file "||filespec( "N", outfile )
call ErrorMessage
end
say "The file "||filespec( "N", filename )||,
" was successfully converted"
say "and saved as "||filespec( "N", outfile )
/* End of main program */
EXIT 0
ErrorMessage: procedure expose msg
call SysCls
call beep 220, 240
say
say msg
say
Exit 1
Return
Syntax: procedure expose msg
call SysCls
say
say "CRLF, Version 2.10"
say "Replaces orphaned carriage returns (CR, 0Dh) and line feeds (LF, 0Ah) with"
Say "CRLF pairs (0D0Ah)"
say
say "Usage: CRLF infile [ outfile ]"
say
say 'Where: "infile" is the ASCII file with improper line terminations'
say ' "outfile" is the corrected ASCII file (default: infile.CLP)'
say
say "Written by Rob van der Woude"
say "http://www.robvanderwoude.com"
Exit 1
Return
page last modified: 2024-04-16; loaded in 0.0069 seconds