From 4ed3f49268af39ec38ab297dee18ca955684355c Mon Sep 17 00:00:00 2001 From: justin1014 Date: Sat, 20 Dec 2008 07:22:25 +0000 Subject: [PATCH] Added !searchreplace preprocessor command for compiletime text search/replaces git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@5852 212acab6-be3b-0410-9dea-997c60f758d6 --- Docs/src/defines.but | 9 +++++++++ Source/script.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++- Source/tokens.cpp | 1 + Source/tokens.h | 1 + 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Docs/src/defines.but b/Docs/src/defines.but index d1d8e4a2..b090cbd6 100644 --- a/Docs/src/defines.but +++ b/Docs/src/defines.but @@ -148,4 +148,13 @@ If \e{/file} is specified, the file is treated as a series of lines. The file is \c # search filename.cpp for a line '#define APP_VERSION "2.5"' and set ${VER_MAJOR} to 2, ${VER_MINOR} to 5. \c !searchparse /file filename.cpp `#define APP_VERSION "` VER_MAJOR `.` VER_MINOR `"` +\S1{searchreplace} !searchreplace + +\c [/ignorecase] symbol_out source_string searchfor replacewith + +Searches \e{source_string}, looking for \e{searchfor} and replacing all instances of it with \e{replacewith}. Unlike !define, !searchreplace allows you to redefine \e{symbol_out} without warning or error. + +\c # defines ${blah} to "i like ponies" +\c !searchreplace blah "i love ponies" "love" "like" + diff --git a/Source/script.cpp b/Source/script.cpp index 4755ab93..41bbd8c6 100644 --- a/Source/script.cpp +++ b/Source/script.cpp @@ -3076,7 +3076,7 @@ int CEXEBuild::doCommand(int which_token, LineParser &line) return PS_OK; case TOK_P_ECHO: SCRIPT_MSG("%s (%s:%d)\n", line.gettoken_str(1),curfilename,linecnt); - return PS_OK; + return PS_OK; case TOK_P_SEARCHPARSESTRING: { bool ignCase=false; @@ -3194,6 +3194,49 @@ int CEXEBuild::doCommand(int which_token, LineParser &line) delete list; } return PS_OK; + case TOK_P_SEARCHREPLACESTRING: + { + int ignoreCase=!stricmp(line.gettoken_str(1),"/ignorecase"); + if (line.getnumtokens()!=5+ignoreCase) PRINTHELP() + + char *define=line.gettoken_str(1+ignoreCase); + char *src = line.gettoken_str(2+ignoreCase); + char *search = line.gettoken_str(3+ignoreCase); + char *replace = line.gettoken_str(4+ignoreCase); + int searchlen=strlen(search); + int replacelen=strlen(replace); + if (!searchlen) + { + ERROR_MSG("!searchreplace: search string must not be empty for search/replace!\n"); + return PS_ERROR; + } + + GrowBuf valout; + + while (*src) + { + if (ignoreCase ? strnicmp(src,search,searchlen) : strncmp(src,search,searchlen)) + valout.add(src++,1); + else + { + valout.add(replace,replacelen); + src+=searchlen; + } + } + + valout.add("",1); + + definedlist.del(define); // allow changing variables since we'll often use this in series + + if (definedlist.add(define,(char*)valout.get())) + { + ERROR_MSG("!searchreplace: error defining \"%s\"!\n",define); + return PS_ERROR; + } + SCRIPT_MSG("!searchreplace: \"%s\"=\"%s\"\n",define,(char*)valout.get()); + + } + return PS_OK; case TOK_P_VERBOSE: { diff --git a/Source/tokens.cpp b/Source/tokens.cpp index 4f3d6d1c..320e9685 100644 --- a/Source/tokens.cpp +++ b/Source/tokens.cpp @@ -258,6 +258,7 @@ static tokenType tokenlist[TOK__LAST] = {TOK_P_APPENDFILE,"!appendfile",2,0,"file appended_line",TP_ALL}, {TOK_P_SEARCHPARSESTRING,"!searchparse",3,-1,"[/ignorecase] [/noerrors] [/file] source_string substring OUTPUTSYM1 [substring [OUTPUTSYM2 [substring ...]]]",TP_ALL}, +{TOK_P_SEARCHREPLACESTRING,"!searchreplace",4,1,"[/ignorecase] output_name source_string substring replacestring", TP_ALL}, {TOK_MISCBUTTONTEXT,"MiscButtonText",0,4,"[back button text] [next button text] [cancel button text] [close button text]",TP_GLOBAL}, {TOK_DETAILSBUTTONTEXT,"DetailsButtonText",0,1,"[details button text]",TP_PG}, diff --git a/Source/tokens.h b/Source/tokens.h index 2d0683d8..1bdd7fb7 100644 --- a/Source/tokens.h +++ b/Source/tokens.h @@ -118,6 +118,7 @@ enum TOK_P_APPENDFILE, TOK_P_SEARCHPARSESTRING, + TOK_P_SEARCHREPLACESTRING, // section/function shit TOK_SECTION,