;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfFileGetMaxLineLen (strFilename) intFileSize = FileSize (strFilename) If intFileSize == 0 Then Return 0 ; Nothing to do. InStudio = RtStatus () == 10 If InStudio Then sMsgText = StrCat ("udfFileGetMaxLineLen (", strFilename, ") LineSize=") hdlBB = BinaryAlloc (intFileSize + 2) BinaryReadEx (hdlBB, 1, strFilename, 0, intFileSize) BinaryPokeStr (hdlBB, 0, @LF) BinaryPokeStr (hdlBB, BinaryEodGet (hdlBB), @CR) intLineCount = BinaryStrCnt (hdlBB, 0, BinaryEodGet (hdlBB) - 1, @CRLF) intLineAvg = intFileSize / Max (intLineCount, 1) intLineSize = 0 Switch intLineAvg < 30 && intLineCount > 1000 ; The magic point is to set the proper values for automatically switching between the methods. Is there any rule? Case @TRUE BinaryPokeStr (hdlBB, 0, StrClean (BinaryPeekStr (hdlBB, 0, BinaryEodGet (hdlBB) - 1), @CRLF, " ", @TRUE, 2)) sSearch = StrCat (@LF, @CR) While @TRUE If InStudio Then wStatusMsg (StrCat (sMsgText, intLineSize)) BinaryReplace (hdlBB, sSearch, "", @TRUE) If BinaryEodGet (hdlBB) <= intLineSize Then Break intLineSize = intLineSize + 1 sSearch = StrCat (@LF, StrFill (" ", intLineSize), @CR) EndWhile Break Case @FALSE strBBTag = BinaryTagInit (hdlBB, @LF, @CR) While @TRUE If InStudio Then wStatusMsg (StrCat (sMsgText, intLineSize)) strBBTag = BinaryTagFind (strBBTag) If strBBTag == "" Then Break intLineSize = Max (intLineSize, BinaryTagLen (strBBTag, 0)) EndWhile Break EndSwitch hdlBB = BinaryFree (hdlBB) Return intLineSize ;.......................................................................................................................................... ; This UDF "udfFileGetMaxLineLen" returns the maximal line length of lines in a text file. ; This UDF combines two lookup algorithms: ; 1. If the file has many lines with each line of small line length (I claim this special algorithm as my invention). ; 2. If the file has not so many lines but each line with larger length (standard algorithm). ;.......................................................................................................................................... ; Detlev Dalitz.20010714.20090426. ;.......................................................................................................................................... #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. strFilename1 = IntControl (1004, 0, 0, 0, 0) ; We use this file as test input. intLineLengthMax1 = udfFileGetMaxLineLen (strFilename1) strFilename2 = StrCat (DirHome (), "wil.clr") ; We use "wil.clr" file as test input. Many lines with small length. intLineLengthMax2 = udfFileGetMaxLineLen (strFilename2) Exit ;------------------------------------------------------------------------------------------------------------------------------------------