;========================================================================================================================================== ; Test for verifying a rule how lines in textfiles are counted. ;========================================================================================================================================== ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfFileGetLineCount (strFilename) intBBSize = FileSizeEx (strFilename) If !intBBSize Then Return 0 hdlBB = BinaryAlloc (intBBSize) intHigh = BinaryRead (hdlBB, strFilename) - 1 intHigh = intHigh - (26 == BinaryPeek (hdlBB, intHigh)) If intHigh < 0 Then Return 0 While 13 == BinaryPeek (hdlBB, intHigh) intHigh = intHigh - 1 If intHigh < 0 Then Return 0 EndWhile intLineCount = BinaryStrCnt (hdlBB, 0, intHigh, @LF) + (10 != BinaryPeek (hdlBB, intHigh)) hdlBB = BinaryFree (hdlBB) Return intLineCount ;.......................................................................................................................................... ; This UDF "udfFileGetLineCount" returns the number of lines counted in a textfile. ; The function takes only @LF characters into account. ; All @CR characters are ommitted. ; ; The function provides the same number of lines as counted by the WinBatch FileRead function. ;.......................................................................................................................................... ; EOF = Num2Char (26) ; @LF = Num2Char (10) ; @CR = Num2Char (13) ; ; Detlev Dalitz.20020820.20090428. ;.......................................................................................................................................... #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ AddExtender ("WWWSK44i.DLL") ; We use one extender function to make control chars human readable. strMsgTitle = "Demo: LineCount and EOL Delimiters" strMsgText = "" strFilenameTemp = FileCreateTemp ("TMP") sFolderTemp = FilePath (strFilenameTemp) FileDelete (strFilenameTemp) intTestLines = 18 strTestLine1 = "How many lines?" ; 1 strTestLine2 = @CR ; 0 strTestLine3 = @LF ; 1 strTestLine4 = @CRLF ; 1 strTestLine5 = Num2Char (26) ; 0 strTestLine6 = StrCat ("How many lines?", @CR) ; 1 strTestLine7 = StrCat ("How many lines?", @LF) ; 1 strTestLine8 = StrCat ("How many lines?", @CRLF) ; 1 strTestLine9 = StrCat ("How many lines?", Num2Char (26)) ; 1 strTestLine10 = StrCat ("How many lines?", @CR, @CR) ; 1 strTestLine11 = StrCat ("How many lines?", @CR, @CR, Num2Char (26)) ; 1 strTestLine12 = StrCat ("How many lines?", @LF, "test") ; 2 strTestLine13 = StrCat ("How many lines?", @LF, @LF) ; 2 strTestLine14 = StrCat ("How many lines?", @LF, @LF, @CR) ; 2 strTestLine15 = StrCat ("How many lines?", @LF, @LF, "test") ; 3 strTestLine16 = StrCat ("How many lines?", @LF, @LF, Num2Char (26)) ; 2 strTestLine17 = StrCat ("How many lines?", @CRLF, @CRLF) ; 2 strTestLine18 = StrCat ("How many lines?", @CRLF, @CRLF, Num2Char (26)) ; 2 IntControl (53, 0, 0, 0, 0) ; For the test: No line terminator for FileWrite. For intTest = 1 To intTestLines strFilenameTest = StrCat (sFolderTemp, "Test.", intTest, ".txt") hdlFW = FileOpen (strFilenameTest, "WRITE") FileWrite (hdlFW, strTestLine%intTest%) hdlFW = FileClose (hdlFW) Next strMsgText = StrCat (strMsgText, "FileRead", @LF) ; Using the standard FileRead function. For intTest = 1 To intTestLines strFilenameTest = StrCat (sFolderTemp, "Test.", intTest, ".txt") intFileSize%intTest% = FileSize (strFilenameTest) IntControl (65, 4096 * 256, 0, 0, 0) ; Enlarge fileread buffer for speedy access. ; Added DD.20030128 hdlFR = FileOpen (strFilenameTest, "READ") intLineCount%intTest% = 0 While @TRUE If "*EOF*" == FileRead (hdlFR) Then Break intLineCount%intTest% = 1 + intLineCount%intTest% EndWhile hdlFR = FileClose (hdlFR) strTestLine = urlEncode (strTestLine%intTest%) strTestLine = StrFix (strTestLine, " ", 40) strMsgText = StrCat (strMsgText, strTestLine, "FileSize", intTest, "=", intFileSize%intTest%, @TAB, "LineCount", intTest, "=", intLineCount%intTest%, @LF) Next strMsgText = StrCat (strMsgText, @LF, "udfFileGetLineCount", @LF) ; Using UDF udfFileGetLineCount. For intTest = 1 To intTestLines strFilenameTest = StrCat (sFolderTemp, "Test.", intTest, ".txt") intFileSize%intTest% = FileSize (strFilenameTest) intLineCount%intTest% = udfFileGetLineCount (strFilenameTest) strTestLine = urlEncode (strTestLine%intTest%) strTestLine = StrFix (strTestLine, " ", 40) strMsgText = StrCat (strMsgText, strTestLine, "FileSize", intTest, "=", intFileSize%intTest%, @TAB, "LineCount", intTest, "=", intLineCount%intTest%, @LF) Next For intTest = 1 To intTestLines strFilenameTest = StrCat (sFolderTemp, "Test.", intTest, ".txt") FileDelete (strFilenameTest) Next ; Display results. IntControl (28, 1, 0, 0, 0) IntControl (63, 200, 035, 800, 955) AskItemlist (strMsgTitle, strMsgText, @LF, @UNSORTED, @SINGLE) :CANCEL Exit ;.......................................................................................................................................... ; If the WinBatch function 'FileRead' can be seen as a standard method ; for linecounting and for the decision what is a line and what is not a line, ; then the following rule might be the general rule: ; ; 1. EOF character is ommitted and not counted. ; 2. All @CR characters are ommitted and not counted. ; 3. LineCount is the sum of all @LF characters in the file. ; 4. An uncomplete line at end of the file counts as a line and increments LineCount by 1. ;.......................................................................................................................................... ;==========================================================================================================================================