FileReadFromEnd: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Adding new author and category links.)
No edit summary
Line 2: Line 2:


== Links ==
== Links ==
; Latest version of headers "nsh.zip":
; Latest version of headers "nsh.zip":
: http://forums.winamp.com/showthread.php?s=&threadid=203228&goto=lastpost
: http://forums.winamp.com/showthread.php?s=&threadid=203228&goto=lastpost


If function used without header then put function in script before call it  
If a function is used without header, put the function code in your script before calling it.
 
== The Function Description==


== The Function ==
<highlight-nsis>
<highlight-nsis>/*
____________________________________________________________________________
____________________________________________________________________________


Line 38: Line 40:
Note:
Note:
-Error flag if input file isn't exists
-Error flag if input file isn't exists
</highlight-nsis>






Example1:
<b>Example1:</b>
Section
<highlight-nsis>Section
${FileReadFromEnd} "C:\a.log" "Example1"
${FileReadFromEnd} "C:\a.log" "Example1"


Line 55: Line 58:
Push $0
Push $0
FunctionEnd
FunctionEnd
</highlight-nsis>




Example2 (Reverse text file):
<b>Example2 (Reverse text file):</b>
Section
<highlight-nsis>Section
GetTempFileName $R0
GetTempFileName $R0
FileOpen $R1 $R0 w
FileOpen $R1 $R0 w
Line 79: Line 83:


Push $0
Push $0
FunctionEnd*/
FunctionEnd
 
</highlight-nsis>


;---------------------------------------------------------------------------
== The Function Code==


<highlight-nsis>
Function FileReadFromEnd
Function FileReadFromEnd
!define FileReadFromEnd `!insertmacro FileReadFromEndCall`
!define FileReadFromEnd `!insertmacro FileReadFromEndCall`

Revision as of 14:24, 4 July 2005

Author: Instructor (talk, contrib)


Links

Latest version of headers "nsh.zip"
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

____________________________________________________________________________
 
                            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

The 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