Imported Upstream version 0.2.5
[platform/upstream/libdatrie.git] / nsis / SearchReplace.nsh
1 ; search & replace line in file.\r
2 ;-------------------------\r
3 ; parameter description\r
4 ; $9 is filename to search\r
5 ; $8 is string to search\r
6 ; $7 is string to replace\r
7 ;------------------------\r
8 ; How to call function:\r
9 ;   Push "filename.txt"\r
10 ;   Push "the string to search"\r
11 ;   Push "the string to replace"\r
12 ;   Call un.ReplaceLine\r
13 ;-----------------------\r
14 Function un.ReplaceLine\r
15  \r
16  Pop $7\r
17  Pop $8\r
18  Pop $9\r
19 \r
20 ClearErrors\r
21 FileOpen $0 $9 "r"                     ; open target file for reading\r
22 GetTempFileName $R0                            ; get new temp file name\r
23 FileOpen $1 $R0 "w"                            ; open temp file for writing\r
24 loop:\r
25    FileRead $0 $2                              ; read line from target file\r
26    IfErrors done                               ; check if end of file reached\r
27    StrCmp $2 "$8$\r$\n" 0 +2      ; compare line with search string with CR/LF\r
28    StrCpy $2 "$7$\r$\n"    ; change line\r
29    StrCmp $2 "$8" 0 +2            ; compare line with search string without CR/LF (at the end of the file)\r
30    StrCpy $2 "$7"          ; change line\r
31    FileWrite $1 $2                             ; write changed or unchanged line to temp file\r
32    Goto loop\r
33  \r
34 done:\r
35    FileClose $0                                ; close target file\r
36    FileClose $1                                ; close temp file\r
37    Delete $9                            ; delete target file\r
38    CopyFiles /SILENT $R0 $9            ; copy temp file to target file\r
39    Delete $R0                                  ; delete temp file\r
40    \r
41 FunctionEnd\r
42 \r
43 ;---------\r
44 \r
45 \r
46 ; search & delete line in file.\r
47 ;-------------------------\r
48 ; parameter description\r
49 ; $9 is filename to search\r
50 ; $8 is string to search and delete\r
51\r
52 ;------------------------\r
53 ; How to call function:\r
54 ;   \r
55 ;   Push "filename.txt"\r
56 ;   Push "the string to search and delete"\r
57 ;   Call un.DeleteLine\r
58 ;\r
59 ;-----------------------\r
60 Function un.DeleteLine\r
61  \r
62  Pop $8\r
63  Pop $9\r
64 \r
65 ClearErrors\r
66 FileOpen $0 $9 "r"                     ; open target file for reading\r
67 GetTempFileName $R0                            ; get new temp file name\r
68 FileOpen $1 $R0 "w"                            ; open temp file for writing\r
69 loop:\r
70    FileRead $0 $2                              ; read line from target file\r
71    IfErrors done                               ; check if end of file reached\r
72    StrCmp $2 "$8$\r$\n" loop 0    ; if search string (with CR/LF) is found, goto loop\r
73    StrCmp $2 "$8" loop 0          ; if search string is found at the end of the file, goto loop\r
74    FileWrite $1 $2                             ; write changed or unchanged line to temp file\r
75    Goto loop\r
76  \r
77 done:\r
78    FileClose $0                                ; close target file\r
79    FileClose $1                                ; close temp file\r
80    Delete $9                            ; delete target file\r
81    CopyFiles /SILENT $R0 $9            ; copy temp file to target file\r
82    Delete $R0                                  ; delete temp file\r
83    \r
84 FunctionEnd\r
85 \r
86 ;---------