FileReadFromEnd: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
No edit summary
 
Line 1: Line 1:
{{PageAuthor|Instructor}}
{{PageAuthor|Instructor}}


== Links ==
{{User:Instructor/Headers/Template}}


; Latest version of headers "nsh.zip":
== Function Description ==
: http://forums.winamp.com/showthread.php?s=&threadid=203228&goto=lastpost
 
If a function is used without header, put the function code in your script before calling it.
 
== The Function Description==


<highlight-nsis>
<highlight-nsis>
Line 86: Line 81:
</highlight-nsis>
</highlight-nsis>


== The Function Code==
== Function Code ==


<highlight-nsis>
<highlight-nsis>

Latest revision as of 11:29, 30 November 2005

Author: Instructor (talk, contrib)


Page for NSIS 2.07 and below users

You can use the latest version of headers (recommended) or the following function code (put the function code in your script before calling it)

Function Description

____________________________________________________________________________
 
                            FileReadFromEnd v1.2
____________________________________________________________________________
 
 
Read text file from end line by line.
 
 
Syntax:
${FileReadFromEnd} "[File]" "Function"
 
"[File]"      ; Input text file
"Function"    ; Callback function
 
Function "Function"
	; $9       current line
	; $8       current line number
	; $7       current line negative number
 
	; $R0-$R9  are not used (save data in them).
	; ...
 
	Push $var      ; If $var="StopFileReadFromEnd"  Then exit from function
FunctionEnd
 
 
Note:
-Error flag if input file isn't exists


Example1:

Section
	${FileReadFromEnd} "C:\a.log" "Example1"
 
	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd
 
Function Example1
	MessageBox MB_OKCANCEL '"Line"=[$9]$\n   "#"=[$8]$\n  "-#"=[$7]' IDOK +2
	StrCpy $0 StopFileReadFromEnd
 
	Push $0
FunctionEnd


Example2 (Reverse text file):

Section
	GetTempFileName $R0
	FileOpen $R1 $R0 w
	${FileReadFromEnd} "C:\a.log" "Example2"
	FileClose $R1
 
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2
	Exec '"notepad.exe" "$R0"'
SectionEnd
 
Function Example2
	StrCmp $7 -1 0 +5
	StrCpy $1 $9 1 -1
	StrCmp $1 '$\n' +3
	StrCmp $1 '$\r' +2
	StrCpy $9 '$9$\r$\n'
 
	FileWrite $R1 "$9"
 
	Push $0
FunctionEnd

Function Code

Function FileReadFromEnd
	!define FileReadFromEnd `!insertmacro FileReadFromEndCall`
 
	!macro FileReadFromEndCall _FILE _FUNC
		Push $0
		Push `${_FILE}`
		GetFunctionAddress $0 `${_FUNC}`
		Push `$0`
		Call FileReadFromEnd
		Pop $0
	!macroend
 
	Exch $1
	Exch
	Exch $0
	Exch
	Push $7
	Push $8
	Push $9
	ClearErrors
 
	StrCpy $7 -1
	StrCpy $8 0
	IfFileExists $0 0 error
	FileOpen $0 $0 r
	IfErrors error
	FileRead $0 $9
	IfErrors +4
	Push $9
	IntOp $8 $8 + 1
	goto -4
	FileClose $0
 
	nextline:
	StrCmp $8 0 end
	Pop $9
	Push $1
	Push $7
	Push $8
	Call $1
	Pop $0
	Pop $8
	Pop $7
	Pop $1
	IntOp $7 $7 - 1
	IntOp $8 $8 - 1
	IfErrors error
	StrCmp $0 'StopFileReadFromEnd' clearstack nextline
 
	error:
	SetErrors
 
	clearstack:
	StrCmp $8 0 end
	Pop $9
	IntOp $8 $8 - 1
	goto clearstack
 
	end:
	Pop $9
	Pop $8
	Pop $7
	Pop $1
	Pop $0
FunctionEnd