Jump to content

_FileCountLines error?


 Share

Recommended Posts

Hi

I implemented the line counting in PERL because I did not have much time to whip it up in C++ or AutoIt.

The bonus of this code is I get to show people how to embed PERL in AutoIt :-)

Ah - but that means to do so/try out this example, you need ActivePerl to be installed on your system (but how does not ;-) )

Altough I did not compare the test results of the other functions, I would believe MrCreatoR's matches mine so far.

His results will differ with mine only in those cases where we don't want to count trailing blank.

Infact I will be interested in knowing why would anyone want to dismiss count trailing blank line?

Anyways, here is the code:

CODE
Func LineCountPerl($sFilePath, $ignoreTrailingBlankLine = 0)

Local $oSC = ObjCreate("ScriptControl")

$oSC.language = "PerlScript"

Local $code = 'sub countLines{'

$code &= @LF & '$/ = undef;'

$code &= @LF & 'open INPUTFILE, $_[0] or return 0;'

$code &= @LF & 'binmode INPUTFILE;'

$code &= @LF & 'my $inputLine = <INPUTFILE>;'

$code &= @LF & 'return 0 if (0 == length($inputLine));'

$code &= @LF & 'my $count = 1 + ($inputLine =~ tr/\x0D//);'

$code &= @LF & 'while ($inputLine =~ /[^\x0D]\x0A/g) { ++$count; }'

$code &= @LF & 'my $last2Characters = (unpack("x" . (length($inputLine) - 2) . " a2", $inputLine));'

$code &= @LF & 'if ((unpack("x1 a1", $last2Characters) =~ tr/\x0D\x0A//) && ($last2Characters =~ tr/\x0D\x0A//))'

$code &= @LF & '{'

$code &= @LF & '--$count if (1 == ' & $ignoreTrailingBlankLine & ');'

$code &= @LF & '}'

$code &= @LF & 'close INPUTFILE;'

$code &= @LF & 'return $count;'

$code &= @LF & '}'

$oSC.addcode($code);

return $oSC.eval('countLines("' & $sFilePath & '");');

EndFunc

Attached is the updated POC with other's code too.

I have not attached the test files, but screenshots are attached for proof ;-)

post-40614-1222337286_thumb.gif

post-40614-1222337291_thumb.gif

post-40614-1222337296_thumb.gif

post-40614-1222337311_thumb.gif

post-40614-1222337315_thumb.gif

post-40614-1222337320_thumb.gif

post-40614-1222337329_thumb.gif

post-40614-1222337338_thumb.gif

post-40614-1222337343_thumb.gif

post-40614-1222337349_thumb.gif

Link to comment
Share on other sites

I have not attached the test files, but screenshots are attached for proof

About the «expected», what you mean?

Please show me a file with the «--- expected 1» and my script return «2».

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Here is the version with additional parameter $iCountLastLine:

$iCountLines = _FileCountLinesEx("Test.txt", 0)
$sCL_Results = StringFormat("Lines = %i\n@error = %i", $iCountLines, @error)

ConsoleWrite($sCL_Results)

Func _FileCountLinesEx($sFilePath, $iCountLastLine=1)
    Local $iCountLines = 0
    Local $sFRead = FileRead($sFilePath)
    
    If @error = -1 Then Return SetError(1, 0, 0) ;The file is empty
    If @error <> 0 Then Return SetError(2, 0, 0) ;File not exists or other "Read" error.
    
    If Not $iCountLastLine Then $sFRead = StringStripWS($sFRead, 2)
    
    $sFRead = StringReplace($sFRead, @CRLF, "")
    $iCountLines += @extended
    
    $sFRead = StringReplace($sFRead, @CR, "")
    $iCountLines += @extended
    
    StringReplace($sFRead, @LF, "")
    $iCountLines += @extended
    
    Return $iCountLines + 1
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...