FileReadFromEnd: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Wikipedia python library)
 
No edit summary
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Links ==
{{PageAuthor|Instructor}}
; Latest version of headers "nsh.zip":
: http://forums.winamp.com/showthread.php?s=&threadid=203228&goto=lastpost


If function used without header then put function in script before call it
{{User:Instructor/Headers/Template}}


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


                             FileReadFromEnd v1.1
                             FileReadFromEnd v1.2
____________________________________________________________________________
____________________________________________________________________________


Line 36: Line 35:
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 53: Line 53:
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 68: Line 69:


Function Example2
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"
FileWrite $R1 "$9"


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


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


<highlight-nsis>
Function FileReadFromEnd
Function FileReadFromEnd
!define FileReadFromEnd `!insertmacro FileReadFromEndCall`
!define FileReadFromEnd `!insertmacro FileReadFromEndCall`
Line 101: Line 109:
IfFileExists $0 0 error
IfFileExists $0 0 error
FileOpen $0 $0 r
FileOpen $0 $0 r
IfErrors end
IfErrors error
FileRead $0 $9
FileRead $0 $9
IfErrors +4
IfErrors +4
Line 132: Line 140:
Pop $9
Pop $9
IntOp $8 $8 - 1
IntOp $8 $8 - 1
StrCmp $8 0 0 -2
goto clearstack


end:
end:
Line 143: Line 151:
</highlight-nsis>
</highlight-nsis>


 
[[Category:Text Files Manipulation Functions]]
Page author: Instructor

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