"Judson McClendon" <judmc@[EMAIL PROTECTED]
> wrote:
> "Anonymous" <r1a@[EMAIL PROTECTED]
> wrote:
>> The C code looks garbled in my news reader...
>
> The C source code is probably from a ?nix system with only a LF
> character at the end of the line instead of CRLF. PowerBASIC Console
> Compiler installs program CRLF in the Samples folder, and it will fix
the
> file. Or search powerbasic.com for the file LF2CRLF.ZIP which contains
> such a program.
Actually, this provoked me into writing a better one. It doesn't simply
change
'LF' into CRLF, which will screw up the file if it already has CRLF (e.g.
you
run it twice on the same file) but 'corrects' CRLF, even if it already has
CRLF
in some or all lines, or has CR instead of LF, which I have seen. You can
run
this program repeatedly on any correct ASCII CRLF text file and it won't
mess it up. However, if the file has CRLF reversed as LFCR, which I've
also
seen, it will change LFCR to double CRLF. For such (rare) files, modify
the
code to first change LFCR to LF, or simply remove all CR's first. I don't
know of a good way to certainly recognize such files at runtime. Even if
you
scanned for the first CR and checked for an LF before it, you wouldn't
know
for certain that it wasn't a ?nix LF file that happened to have a CR, or a
CR
format that happened to have a LF, both of which I have seen (it's fairly
common to see CRLF followed by one or more LF's or CR's when the file
is the redirected output of a program). But the following program will
work
for LF-only, CR-only, CRLF or mixed formats, changing them to CRLF. It
uses the overall best algorithm for CRLF correction.
'
' **************************************************
' * *
' * FixCRLF.bas *
' * *
' * Convert files to proper CRLF format *
' * *
' * Wildcards and drive/path may be used *
' * *
' * Compile using PB/CC *
' * *
' * Judson D. McClendon *
' * Sun Valley Systems *
' * 4522 Shadow Ridge Pkwy *
' * Pinson, AL 35126-2192 *
' * 205-680-0460 *
' * *
' **************************************************
'
#COMPILE EXE
#DIM ALL
#INCLUDE "WIN32API.INC"
DECLARE SUB ParseFiles(ParamStr AS STRING, FileTab() AS STRING, FileEnd AS
LONG)
DECLARE SUB ExpandWildcard(WildCard AS STRING, FileTab() AS STRING,
FileEnd AS LONG)
DECLARE SUB ParseParams(ParamStr AS STRING, ParamTab() AS STRING, ParamEnd
AS LONG)
FUNCTION PBMAIN () AS LONG
DIM FileTab(10) AS STRING
DIM FileEnd AS LONG
DIM FilePtr AS LONG
DIM TempParams AS STRING
DIM FileName AS STRING
DIM FileText AS STRING
TempParams = TRIM$(COMMAND$)
IF (TempParams = "/?" OR TempParams = "-?") THEN
STDOUT "FixCRLF: Convert files to proper CRLF format"
STDOUT " Usage: FixCRLF filespec [filespec [filespec ...]]"
STDOUT " Wildcards and drive/path may be used"
EXIT FUNCTION
END IF
ParseFiles(TempParams, FileTab(), FileEnd)
IF (FileEnd = 0) THEN
STDOUT "No files found"
EXIT FUNCTION
END IF
FOR FilePtr = 1 TO FileEnd
FileName = DIR$(FileTab(FilePtr),16)
IF (FileName = "") THEN
STDOUT $DQ & FileTab(FilePtr) & $DQ & " not found"
ITERATE FOR
END IF
IF ((GETATTR(FileTab(FilePtr)) AND 16) <> 0) THEN
STDOUT $DQ & FileTab(FilePtr) & $DQ & " is a folder, not a file"
ITERATE FOR
END IF
' Read entire file into string FileText
TRY
OPEN FileName FOR BINARY ACCESS READ AS #1
GET$ 1, LOF(1), FileText
CLOSE #1
CATCH
STDOUT "** Couldn't read file " & FileName
ITERATE FOR
END TRY
' Change CRLF to LF in case file was already CRLF
REPLACE $CRLF WITH $LF IN FileText
' Change CR to LF in case file uses CR only
REPLACE $CR WITH $LF IN FileText
' Change LF to CRLF
REPLACE $LF WITH $CRLF IN FileText
' Write modified file back to disk
TRY
KILL FileName
OPEN FileName FOR BINARY ACCESS READ WRITE LOCK READ WRITE AS #1
PUT$ 1, FileText
CLOSE #1
CATCH
STDOUT "** Couldn't rewrite file " & FileName
END TRY
NEXT FilePtr
END FUNCTION
'
' ** Parse COMMAND$ into an array of file names, expanding wildcards
'
SUB ParseFiles(ParamStr AS STRING, FileTab() AS STRING, FileEnd AS LONG)
LOCAL FileName AS STRING
DIM ParamTab(10) AS STRING
DIM ParamEnd AS LONG
DIM ParamPtr AS LONG
ParseParams(ParamStr, ParamTab(), ParamEnd)
FOR ParamPtr = 1 TO ParamEnd
FileName = ParamTab(ParamPtr)
IF (INSTR(FileName, ANY "?*") > 0) THEN
ExpandWildcard(FileName, FileTab(), FileEnd)
ITERATE FOR
END IF
' ' Missing files
' IF (DIR$(FileName,16) = "") THEN
' STDERR "** File " & FileName & " not found"
' ITERATE FOR
' END IF
' ' Subdirectories
' IF ((GETATTR(FileName) AND 16) <> 0) THEN
' STDERR "** " & FileName & " is a folder, not a file"
' ITERATE FOR
' END IF
FileEnd = FileEnd + 1
IF (FileEnd > UBOUND(FileTab)) THEN
REDIM PRESERVE FileTab(UBOUND(FileTab) + 10)
END IF
FileTab(FileEnd) = FileName
NEXT
END SUB
'
' ** Expand wildcard into array of file names
'
SUB ExpandWildcard(WildCard AS STRING, FileTab() AS STRING, FileEnd AS
LONG)
LOCAL FileName AS STRING
LOCAL FilePath AS STRING
LOCAL OldPath AS STRING
LOCAL NewPath AS STRING
LOCAL NewDrive AS STRING
LOCAL I AS LONG
LOCAL Attr AS LONG
OldPath = CURDIR$ ' Save old path
I = INSTR(-1,Wildcard, ANY ":\/")
IF (I = 0) THEN
FileName = WildCard
ELSE
FilePath = LEFT$(Wildcard,I)
FileName = MID$(Wildcard,I+1)
NewPath = FilePath
IF (MID$(NewPath,2,1) = ":") THEN
NewDrive = LEFT$(NewPath,2)
TRY
CHDRIVE NewDrive
CATCH
STDERR "** Drive " & NewDrive & " in " & Wildcard & " not
found"
EXIT SUB
END TRY
NewPath = MID$(NewPath,3)
END IF
IF (LEN(NewPath) > 1) _
AND _
(INSTR(RIGHT$(NewPath,1), ANY "\/") > 0) THEN
NewPath = LEFT$(NewPath, LEN(NewPath) - 1)
END IF
IF (NewPath <> "") THEN
TRY
CHDIR NewPath
CATCH
STDERR "** Path " & NewPath & " in " & Wildcard & " not found"
EXIT SUB
END TRY
END IF
END IF
FileName = DIR$(FileName,7)
DO WHILE (FileName <> "")
Attr = GETATTR(FileName)
IF ((Attr AND 16) = 0) THEN ' If not subdirectory, store it
FileEnd = FileEnd + 1
IF (FileEnd > UBOUND(FileTab)) THEN
REDIM PRESERVE FileTab(UBOUND(FileTab) + 10)
END IF
FileTab(FileEnd) = FilePath & FileName
END IF
FileName = DIR$
LOOP
' If path was changed, change back
IF (NewPath <> "" OR NewDrive <> "") THEN
CHDRIVE LEFT$(OldPath,2)
CHDIR OldPath
END IF
END SUB
'
' ** Parse arguments into an array
'
SUB ParseParams(ParamStr AS STRING, ParamTab() AS STRING, ParamEnd AS
LONG)
LOCAL CharPtr AS LONG
LOCAL TempParams AS STRING
CharPtr = 1
ParamEnd = 0
DO UNTIL (CharPtr > LEN(ParamStr))
TempParams = MID$(ParamStr,CharPtr,1)
IF (TempParams = " ") THEN
CharPtr = CharPtr + 1
ITERATE DO
END IF
ParamEnd = ParamEnd + 1
IF (ParamEnd > UBOUND(ParamTab)) THEN
REDIM PRESERVE ParamTab(UBOUND(ParamTab) + 10)
END IF
IF (TempParams = $DQ) THEN
CharPtr = CharPtr + 1
DO UNTIL (CharPtr > LEN(ParamStr))
TempParams = MID$(ParamStr,CharPtr,1)
IF (TempParams = $DQ) THEN
EXIT DO
ELSE
ParamTab(ParamEnd) = ParamTab(ParamEnd) + TempParams
END IF
CharPtr = CharPtr + 1
LOOP
CharPtr = CharPtr + 1
ELSE
DO UNTIL (CharPtr > LEN(ParamStr))
TempParams = MID$(ParamStr,CharPtr,1)
IF (TempParams = " ") THEN
EXIT DO
END IF
ParamTab(ParamEnd) = ParamTab(ParamEnd) + TempParams
CharPtr = CharPtr + 1
LOOP
END IF
LOOP
END SUB
--
Judson McClendon judmc@[EMAIL PROTECTED]
(remove zero)
Sun Valley Systems http://sunvaley.com
"For God so loved the world that He gave His only begotten Son, that
whoever believes in Him should not perish but have everlasting life."


|