TextCompare: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Added category links.)
No edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{|align=right
{{PageAuthor|Instructor}}
|<small>Author: [[{{ns:2}}:Instructor|Instructor]] ([[{{ns:3}}:Instructor|talk]], [[{{ns:-1}}:Contributions/Instructor|contrib]])</small>
|}
<br style="clear:both;">
== 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.
{{User:Instructor/Headers/Template}}


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


                               TextCompare v1.4
                               TextCompare v1.4
Line 54: Line 50:
-Error flag if File1 or File2 isn't exist
-Error flag if File1 or File2 isn't exist
-Error flag if syntax error
-Error flag if syntax error
</highlight-nsis>






Example (Different or Equal):
<b>Example (Different or Equal):</b>
Section
<highlight-nsis>Section
StrCpy $R0 ''
StrCpy $R0 ''
${TextCompare} "C:\1.txt" "C:\2.txt" "FastDiff" "Example1"
${TextCompare} "C:\1.txt" "C:\2.txt" "FastDiff" "Example1"
Line 75: Line 72:
Push $0
Push $0
FunctionEnd
FunctionEnd
</highlight-nsis>






Example (Compare line-by-line - Different):
<b>Example (Compare line-by-line - Different):</b>
Section
<highlight-nsis>Section
StrCpy $R0 'Text1.txt'
StrCpy $R0 'Text1.txt'
StrCpy $R1 'Text2.txt'
StrCpy $R1 'Text2.txt'
Line 99: Line 97:
Push $0
Push $0
FunctionEnd
FunctionEnd
</highlight-nsis>






Example (Compare line-by-line - Equal):
<b>Example (Compare line-by-line - Equal):</b>
Section
<highlight-nsis>Section
StrCpy $R0 'Text1.txt'
StrCpy $R0 'Text1.txt'
StrCpy $R1 'Text2.txt'
StrCpy $R1 'Text2.txt'
Line 122: Line 121:
Push $0
Push $0
FunctionEnd
FunctionEnd
</highlight-nsis>






Example (Compare all lines - Different):
<b>Example (Compare all lines - Different):</b>
Section
<highlight-nsis>Section
StrCpy $R0 'Text1.txt'
StrCpy $R0 'Text1.txt'
StrCpy $R1 'Text2.txt'
StrCpy $R1 'Text2.txt'
Line 153: Line 153:
Push $0
Push $0
FunctionEnd
FunctionEnd
</highlight-nsis>






Example (Compare all lines - Equal):
<b>Example (Compare all lines - Equal):</b>
Section
<highlight-nsis>Section
StrCpy $R0 'Text1.txt'
StrCpy $R0 'Text1.txt'
StrCpy $R1 'Text2.txt'
StrCpy $R1 'Text2.txt'
Line 175: Line 176:


Push $0
Push $0
FunctionEnd*/
FunctionEnd
</highlight-nsis>
 
 
 
<b>Example (Show variables):</b>
<highlight-nsis>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
</highlight-nsis>


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


<highlight-nsis>
Function TextCompare
Function TextCompare
!define TextCompare `!insertmacro TextCompareCall`
!define TextCompare `!insertmacro TextCompareCall`
Line 312: Line 332:
</highlight-nsis>
</highlight-nsis>


[[{{ns:14}}:Text Files Manipulation Functions]]
[[Category:Text Files Manipulation Functions]]

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