]> git.evergreen-ils.org Git - working/Hatch.git/blob - installer/windows/ReplaceInFile.nsh
LP#1733692 Bumping Hatch versions to 0.1.3
[working/Hatch.git] / installer / windows / ReplaceInFile.nsh
1 ;---------------------------------------------
2 ;   Supplemental Function
3 ;   ReplaceInFile
4 ;   http://nsis.sourceforge.net/ReplaceInFile
5
6 !macro _ReplaceInFile SOURCE_FILE SEARCH_TEXT REPLACEMENT
7   Push "${SOURCE_FILE}"
8   Push "${SEARCH_TEXT}"
9   Push "${REPLACEMENT}"
10   Call RIF
11 !macroend
12
13 Function RIF
14  
15   ClearErrors
16  
17   Exch $0      ; REPLACEMENT
18   Exch
19   Exch $1      ; SEARCH_TEXT
20   Exch 2
21   Exch $2      ; SOURCE_FILE
22  
23   Push $R0     ; SOURCE_FILE file handle
24   Push $R1     ; temporary file handle
25   Push $R2     ; unique temporary file name
26   Push $R3     ; a line to sar/save
27   Push $R4     ; shift puffer
28  
29   IfFileExists $2 +1 RIF_error
30   FileOpen $R0 $2 "r"
31  
32   GetTempFileName $R2
33   FileOpen $R1 $R2 "w"
34
35   ;Loop through each line
36   RIF_loop:
37     FileRead $R0 $R3
38     IfErrors RIF_leaveloop
39
40     ;Search and Replace
41     RIF_sar:
42       Push "$R3"
43       Push "$1"
44       Push "$0"
45       Call StrRep
46       StrCpy $R4 "$R3"
47       Pop $R3
48       StrCmp "$R3" "$R4" +1 RIF_sar
49     FileWrite $R1 "$R3"
50   Goto RIF_loop
51  
52   RIF_leaveloop:
53     FileClose $R1
54     FileClose $R0
55  
56     ;Clean up $2
57     Delete "$2"
58     Rename "$R2" "$2"
59  
60     ClearErrors
61     Goto RIF_out
62  
63   RIF_error:
64     SetErrors
65  
66   RIF_out:
67   Pop $R4
68   Pop $R3
69   Pop $R2
70   Pop $R1
71   Pop $R0
72   Pop $2
73   Pop $0
74   Pop $1
75  
76 FunctionEnd