Blame | Last modification | View Log | RSS feed
;------------------------------------------------------------------------------; Code originally from http://nsis.sourceforge.net/ReplaceInFile;; Description; This is a simple way to replace the occurrences of a certain string; throughout the lines of a file. Typically this is used to turn templates; for configuration files into 'real' ones.;; Usage/Syntax; !include StrRep.nsh; !include ReplaceInFile.nsh; ; instantiate the function for either the installer/uninstaller or both; !insertmacro Func_RIF ""; !insertmacro Func_RIF "un."; [...]; ; and now use the macro to call the function; !insertmacro _ReplaceInFile SOURCE_FILE SEARCH_TEXT REPLACEMENT;;------------------------------------------------------------------------------!macro _ReplaceInFile SOURCE_FILE SEARCH_TEXT REPLACEMENTPush "${SOURCE_FILE}"Push "${SEARCH_TEXT}"Push "${REPLACEMENT}"!ifdef __UNINSTALL__Call un.RIF!elseCall RIF!endif!macroend!macro Func_RIF unFunction ${un}RIFClearErrors ; want to be a newbornExch $0 ; REPLACEMENTExchExch $1 ; SEARCH_TEXTExch 2Exch $2 ; SOURCE_FILEPush $R0 ; SOURCE_FILE file handlePush $R1 ; temporary file handlePush $R2 ; unique temporary file namePush $R3 ; a line to sar/savePush $R4 ; shift pufferIfFileExists $2 +1 RIF_error ; knock-knockFileOpen $R0 $2 "r" ; open the doorGetTempFileName $R2 ; who's new?FileOpen $R1 $R2 "w" ; the escape, please!RIF_loop: ; round'n'round we goFileRead $R0 $R3 ; read one lineIfErrors RIF_leaveloop ; enough is enoughRIF_sar: ; sar - search and replacePush "$R3" ; (hair)stackPush "$1" ; needlePush "$0" ; bloodCall ${un}StrRep ; do the bartwalkStrCpy $R4 "$R3" ; remember previous statePop $R3 ; gimme s.th. back in return!StrCmp "$R3" "$R4" +1 RIF_sar ; loop, might change again!FileWrite $R1 "$R3" ; save the newbieGoto RIF_loop ; gimme moreRIF_leaveloop: ; over'n'out, Sir!FileClose $R1 ; S'rry, Ma'am - clos'n nowFileClose $R0 ; me 2Delete "$2.rif.old" ; go away, SireRename "$2" "$2.rif.old" ; step aside, Ma'amRename "$R2" "$2" ; hi, baby!ClearErrors ; now i AM a newbornGoto RIF_out ; out'n'awayRIF_error: ; ups - s.th. went wrong...SetErrors ; ...so cry, boy!RIF_out: ; your wardrobe?Delete "$2.rif.old"Pop $R4Pop $R3Pop $R2Pop $R1Pop $R0Pop $2Pop $0Pop $1FunctionEnd!macroend