TextCompare: 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 202: Line 197:
</highlight-nsis>
</highlight-nsis>


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


<highlight-nsis>
<highlight-nsis>

Latest revision as of 12:01, 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

____________________________________________________________________________
 
                              TextCompare v1.4
____________________________________________________________________________
 
 
Compare two text files.
 
 
Syntax:
${TextCompare} "[File1]" "[File2]" "[Option]" "Function"
 
"[File1]"     ; File1      Compare these lines
"[File2]"     ; File2      Compare with these lines
"[Options]"   ; (line-by-line):
              ; FastDiff   Compare line N (File1) with line N (File2)
              ;            Call function if Different lines found
              ; FastEqual  Compare line N (File1) with line N (File2)
              ;            Call function if Equal lines found
              ; (line number independent):
              ; SlowDiff   Compare line N (File1) with all lines (File2)
              ;            Call function if line N (File1) Different
              ; SlowEqual  Compare line N (File1) with all lines (File2)
              ;            Call function if line N (File1) Equal
"Function"    ; Callback function
 
Function "Function"
	; $9    "Line File1"
	; $8    "Line number"
	; $7    "Line File2"  (empty if SlowDiff)
	; $6    "Line number" (empty if SlowDiff)
 
 
	; $R0-$R9  are not used (save data in them).
	; ...
 
 
	Push $var    ; If $var="StopTextCompare"  Then exit from function
FunctionEnd
 
 
Note:
-Error flag if File1 or File2 isn't exist
-Error flag if syntax error


Example (Different or Equal):

Section
	StrCpy $R0 ''
	${TextCompare} "C:\1.txt" "C:\2.txt" "FastDiff" "Example1"
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +4
 
	StrCmp $R0 NotEqual 0 +2
	MessageBox MB_OK "Files differ" IDOK +2
	MessageBox MB_OK "Files identical"
SectionEnd
 
Function Example1
	StrCpy $R0 NotEqual
	StrCpy $0 StopTextCompare
 
	Push $0
FunctionEnd


Example (Compare line-by-line - Different):

Section
	StrCpy $R0 'Text1.txt'
	StrCpy $R1 'Text2.txt'
 
	GetTempFileName $R2
	FileOpen $R3 $R2 w
	FileWrite $R3 "$R0 | $R1$\r$\n"
	${TextCompare} "$R0" "$R1" "FastDiff" "Example2"
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2
 
	Exec "notepad.exe $R2"
FunctionEnd
 
Function Example2
	FileWrite $R3 '$8=$9'
	FileWrite $R3 '$6=$7$\r$\n'
 
	Push $0
FunctionEnd


Example (Compare line-by-line - Equal):

Section
	StrCpy $R0 'Text1.txt'
	StrCpy $R1 'Text2.txt'
 
	GetTempFileName $R2
	FileOpen $R3 $R2 w
	FileWrite $R3 "$R0 | $R1$\r$\n"
	${TextCompare} "$R0" "$R1" "FastEqual" "Example3"
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2
 
	Exec "notepad.exe $R2"
FunctionEnd
 
Function Example3
	FileWrite $R3 '$8|$6=$9'
 
	Push $0
FunctionEnd


Example (Compare all lines - Different):

Section
	StrCpy $R0 'Text1.txt'
	StrCpy $R1 'Text2.txt'
 
	GetTempFileName $R2
	FileOpen $R3 $R2 w
	FileWrite $R3 "$R0 | $R1$\r$\n"
	${TextCompare} "$R0" "$R1" "SlowDiff" "Example4"
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK end
 
	FileWrite $R3 "$\r$\n$R1 | $R0$\r$\n"
	${TextCompare} "$R1" "$R0" "SlowDiff" "Example4"
	FileClose $R3
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK end
 
	Exec "notepad.exe $R2"
 
	end:
FunctionEnd
 
Function Example4
	FileWrite $R3 '$8=$9'
 
	Push $0
FunctionEnd


Example (Compare all lines - Equal):

Section
	StrCpy $R0 'Text1.txt'
	StrCpy $R1 'Text2.txt'
 
	GetTempFileName $R2
	FileOpen $R3 $R2 w
	FileWrite $R3 "$R0 | $R1$\r$\n"
	${TextCompare} "$R0" "$R1" "SlowEqual" "Example5"
	IfErrors 0 +2
	MessageBox MB_OK "Error" IDOK +2
 
	Exec "notepad.exe $R2"
FunctionEnd
 
Function Example5
	FileWrite $R3 '$8|$6=$9'
 
	Push $0
FunctionEnd


Example (Show variables):

Section
	${TextCompare} "C:\1.txt" "C:\2.txt" "FastDiff" "Example6"
 
	IfErrors 0 +2
	MessageBox MB_OK "Error"
SectionEnd
 
Function Example6
	MessageBox MB_OKCANCEL '$$9    "Line File1" =[$9]$\n$$8    "Line #"      =[$8]$\n$$7    "Line File2" =[$7]$\n$$6    "Line #"      =[$6]' IDOK +2
	StrCpy $0 StopTextCompare
 
	Push $0
FunctionEnd

Function Code

Function TextCompare
	!define TextCompare `!insertmacro TextCompareCall`
 
	!macro TextCompareCall _FILE1 _FILE2 _OPTION _FUNC
		Push $0
		Push `${_FILE1}`
		Push `${_FILE2}`
		Push `${_OPTION}`
		GetFunctionAddress $0 `${_FUNC}`
		Push `$0`
		Call TextCompare
		Pop $0
	!macroend
 
	Exch $3
	Exch
	Exch $2
	Exch
	Exch 2
	Exch $1
	Exch 2
	Exch 3
	Exch $0
	Exch 3
	Push $4
	Push $5
	Push $6
	Push $7
	Push $8
	Push $9
	ClearErrors
 
	IfFileExists $0 0 error
	IfFileExists $1 0 error
	StrCmp $2 'FastDiff' +5
	StrCmp $2 'FastEqual' +4
	StrCmp $2 'SlowDiff' +3
	StrCmp $2 'SlowEqual' +2
	goto error
 
	FileOpen $4 $0 r
	IfErrors error
	FileOpen $5 $1 r
	IfErrors error
	SetDetailsPrint textonly
 
	StrCpy $6 0
	StrCpy $8 0
 
	nextline:
	StrCmp $4 '' fast
	IntOp $8 $8 + 1
	FileRead $4 $9
	IfErrors 0 +4
	FileClose $4
	StrCpy $4 ''
	StrCmp $5 '' end
	StrCmp $2 'FastDiff' fast
	StrCmp $2 'FastEqual' fast slow
 
	fast:
	StrCmp $5 '' call
	IntOp $6 $6 + 1
	FileRead $5 $7
	IfErrors 0 +5
	FileClose $5
	StrCpy $5 ''
	StrCmp $4 '' end
	StrCmp $2 'FastDiff' call close
	StrCmp $2 'FastDiff' 0 +2
	StrCmp $7 $9 nextline call
	StrCmp $7 $9 call nextline
 
	slow:
	StrCmp $4 '' close
	StrCpy $6 ''
	DetailPrint '$8. $9'
	FileSeek $5 0
 
	slownext:
	FileRead $5 $7
	IfErrors 0 +2
	StrCmp $2 'SlowDiff' call nextline
	StrCmp $2 'SlowDiff' 0 +2
	StrCmp $7 $9 nextline slownext
	IntOp $6 $6 + 1
	StrCmp $7 $9 0 slownext
 
	call:
	Push $2
	Push $3
	Push $4
	Push $5
	Push $6
	Push $7
	Push $8
	Push $9
	Call $3
	Pop $0
	Pop $9
	Pop $8
	Pop $7
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	StrCmp $0 'StopTextCompare' 0 nextline
 
	close:
	FileClose $4
	FileClose $5
	goto end
 
	error:
	SetErrors
 
	end:
	SetDetailsPrint both
	Pop $9
	Pop $8
	Pop $7
	Pop $6
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
FunctionEnd