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/ReplaceInFile
3
;
4
; Description
5
;       This is a simple way to replace the occurrences of a certain string
6
;       throughout the lines of a file. Typically this is used to turn templates
7
;       for  configuration files into 'real' ones.
8
;
9
; Usage/Syntax
10
;       !include StrRep.nsh
11
;       !include ReplaceInFile.nsh
12
;       ; instantiate the function for either the installer/uninstaller or both
13
;       !insertmacro Func_RIF ""
14
;       !insertmacro Func_RIF "un."
15
;       [...]
16
;       ; and now use the macro to call the function
17
;       !insertmacro _ReplaceInFile SOURCE_FILE SEARCH_TEXT REPLACEMENT
18
;
19
;------------------------------------------------------------------------------
20
 
21
!macro _ReplaceInFile SOURCE_FILE SEARCH_TEXT REPLACEMENT
22
    Push "${SOURCE_FILE}"
23
    Push "${SEARCH_TEXT}"
24
    Push "${REPLACEMENT}"
25
    !ifdef __UNINSTALL__
26
        Call un.RIF
27
    !else
28
        Call RIF
29
    !endif
30
!macroend
31
 
32
 
33
!macro Func_RIF un
34
    Function ${un}RIF
35
        ClearErrors  ; want to be a newborn
36
 
37
        Exch $0      ; REPLACEMENT
38
        Exch
39
        Exch $1      ; SEARCH_TEXT
40
        Exch 2
41
        Exch $2      ; SOURCE_FILE
42
 
43
        Push $R0     ; SOURCE_FILE file handle
44
        Push $R1     ; temporary file handle
45
        Push $R2     ; unique temporary file name
46
        Push $R3     ; a line to sar/save
47
        Push $R4     ; shift puffer
48
 
49
        IfFileExists $2 +1 RIF_error    ; knock-knock
50
        FileOpen $R0 $2 "r"             ; open the door
51
 
52
        GetTempFileName $R2             ; who's new?
53
        FileOpen $R1 $R2 "w"            ; the escape, please!
54
 
55
        RIF_loop:                           ; round'n'round we go
56
            FileRead $R0 $R3                ; read one line
57
            IfErrors RIF_leaveloop          ; enough is enough
58
        RIF_sar:                            ; sar - search and replace
59
            Push "$R3"                      ; (hair)stack
60
            Push "$1"                       ; needle
61
            Push "$0"                       ; blood
62
            Call ${un}StrRep                ; do the bartwalk
63
            StrCpy $R4 "$R3"                ; remember previous state
64
            Pop $R3                         ; gimme s.th. back in return!
65
            StrCmp "$R3" "$R4" +1 RIF_sar   ; loop, might change again!
66
            FileWrite $R1 "$R3"             ; save the newbie
67
            Goto RIF_loop                   ; gimme more
68
 
69
        RIF_leaveloop:                      ; over'n'out, Sir!
70
            FileClose $R1                   ; S'rry, Ma'am - clos'n now
71
            FileClose $R0                   ; me 2
72
 
73
            Delete "$2.rif.old"             ; go away, Sire
74
            Rename "$2" "$2.rif.old"        ; step aside, Ma'am
75
            Rename "$R2" "$2"               ; hi, baby!
76
 
77
            ClearErrors                     ; now i AM a newborn
78
            Goto RIF_out                    ; out'n'away
79
 
80
        RIF_error:                          ; ups - s.th. went wrong...
81
            SetErrors                       ; ...so cry, boy!
82
 
83
        RIF_out:                            ; your wardrobe?
84
            Delete "$2.rif.old"
85
            Pop $R4
86
            Pop $R3
87
            Pop $R2
88
            Pop $R1
89
            Pop $R0
90
            Pop $2
91
            Pop $0
92
            Pop $1
93
    FunctionEnd
94
!macroend
95
 
96