Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2031 ghuddy 1
;------------------------------------------------------------------------------
2
; Code originally from http://nsis.sourceforge.net/StrRep
3
;
4
; Description
5
;       This function searches and replaces all occurrences of a substring in a string.
6
;
7
; Usage/Syntax
8
;       !include StrRep.nsh
9
;       ; instantiate the function for either the installer/uninstaller or both
10
;       !insertmacro Func_StrRep ""
11
;       !insertmacro Func_StrRep "un."
12
;       [...]
13
;       ; and now use the macro to call the function
14
;       ${StrRep} '$0' 'C:\Documents and Settings\Dan\Desktop\PSCP Frontend.exe' '\' '\\'
15
;       MessageBox MB_OK $0 ; will be C:\\Documents and Settings\\Dan\\Desktop\\PSCP Frontend.exe
16
;
17
;Parameters
18
;       ${StrRep} "$result_var" "String" "SubString" "RepString"
19
;
20
;       $result_var
21
;               Variable where resulting operation of the replacement is returned.
22
;               If SubString is not found, the value is the same as String.
23
;
24
;       String
25
;               String where to search for SubString.
26
;
27
;       SubString
28
;               String to search in String and to be replaced by RepString.
29
;
30
;       RepString
31
;               String to replace all occurrences of SubString inside String.
32
;
33
;------------------------------------------------------------------------------
34
 
35
!define StrRep "!insertmacro StrRep"
36
 
37
!macro StrRep output string old new
38
    Push "${string}"
39
    Push "${old}"
40
    Push "${new}"
41
    !ifdef __UNINSTALL__
42
        Call un.StrRep
43
    !else
44
        Call StrRep
45
    !endif
46
    Pop ${output}
47
!macroend
48
 
49
!macro Func_StrRep un
50
    Function ${un}StrRep
51
        Exch $R2 ;new
52
        Exch 1
53
        Exch $R1 ;old
54
        Exch 2
55
        Exch $R0 ;string
56
        Push $R3
57
        Push $R4
58
        Push $R5
59
        Push $R6
60
        Push $R7
61
        Push $R8
62
        Push $R9
63
 
64
        StrCpy $R3 0
65
        StrLen $R4 $R1
66
        StrLen $R6 $R0
67
        StrLen $R9 $R2
68
        loop:
69
            StrCpy $R5 $R0 $R4 $R3
70
            StrCmp $R5 $R1 found
71
            StrCmp $R3 $R6 done
72
            IntOp $R3 $R3 + 1 ;move offset by 1 to check the next character
73
            Goto loop
74
        found:
75
            StrCpy $R5 $R0 $R3
76
            IntOp $R8 $R3 + $R4
77
            StrCpy $R7 $R0 "" $R8
78
            StrCpy $R0 $R5$R2$R7
79
            StrLen $R6 $R0
80
            IntOp $R3 $R3 + $R9 ;move offset by length of the replacement string
81
            Goto loop
82
        done:
83
 
84
        Pop $R9
85
        Pop $R8
86
        Pop $R7
87
        Pop $R6
88
        Pop $R5
89
        Pop $R4
90
        Pop $R3
91
        Push $R0
92
        Push $R1
93
        Pop $R0
94
        Pop $R1
95
        Pop $R0
96
        Pop $R2
97
        Exch $R1
98
    FunctionEnd
99
!macroend
100