FileReadFromEnd: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author links.)
m (Added category links.)
Line 152: Line 152:
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>
[[{{ns:14}}:Text Files Manipulation Functions]]

Revision as of 20:46, 30 April 2005

Author: Instructor (talk, contrib)


Links

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

The Function

/*
____________________________________________________________________________
 
                            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 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