From cbd0823fde1b3feb4cfa9ef3a5affca5a4554d5e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 7 Jan 2010 00:36:55 +0000 Subject: Updated the general rules to use the new regex function, they're safer now, and everything works. Unfortunately, with this release, you'll have to rebuild with the shell script... --- build.sh | 8 +-- share/autoinclude/general-rules.bld | 13 ++-- src/functionplugger.cpp | 2 + src/functionregex.cpp | 126 ++++++++++++++++++++++++++++++++++++ src/functionregex.h | 24 +++++++ 5 files changed, 162 insertions(+), 11 deletions(-) create mode 100644 src/functionregex.cpp create mode 100644 src/functionregex.h diff --git a/build.sh b/build.sh index 53e9baa..c0ee4f5 100755 --- a/build.sh +++ b/build.sh @@ -1,7 +1,7 @@ #!/bin/bash -BUSRC="stack.cpp fstring.cpp fbasicstring.cpp hash.cpp list.cpp trace.cpp stream.cpp formatter.cpp util.cpp sharedcore.cpp exceptionbase.cpp heap.cpp archivebase.cpp archive.cpp queue.cpp archival.cpp sio.cpp stdstream.cpp process.cpp plugger.cpp optparser.cpp signals.cpp array.cpp membuf.cpp file.cpp" -BUHDR="stack.h fstring.h fbasicstring.h hash.h list.h trace.h stream.h formatter.h util.h sharedcore.h exceptionbase.h heap.h archivebase.h archive.h queue.h archival.h sio.h stdstream.h process.h osx_compatibility.h win32_compatibility.h linux_compatibility.h plugger.h singleton.h optparser.h signals.h array.h membuf.h file.h" +BUSRC="stack.cpp fstring.cpp fbasicstring.cpp hash.cpp list.cpp trace.cpp stream.cpp formatter.cpp util.cpp sharedcore.cpp exceptionbase.cpp heap.cpp archivebase.cpp archive.cpp queue.cpp archival.cpp sio.cpp stdstream.cpp process.cpp plugger.cpp optparser.cpp signals.cpp array.cpp membuf.cpp file.cpp regex.cpp" +BUHDR="stack.h fstring.h fbasicstring.h hash.h list.h trace.h stream.h formatter.h util.h sharedcore.h exceptionbase.h heap.h archivebase.h archive.h queue.h archival.h sio.h stdstream.h process.h osx_compatibility.h win32_compatibility.h linux_compatibility.h plugger.h singleton.h optparser.h signals.h array.h membuf.h file.h regex.h" function bld() { @@ -27,7 +27,7 @@ function cmd() function gpp() { - bld "$1" "$2" || cmd CXX "$1" g++ -ggdb -W -Wall -Iminibu -Iminibu/bu -c -o "$1" "$2" + bld "$1" "$2" || cmd CXX "$1" g++ -ggdb -fPIC -rdynamic -W -Wall -Iminibu -c -o "$1" "$2" } if [ ! -z "$1" ]; then @@ -64,5 +64,5 @@ for F in src/*.c src/*.cpp; do OUTPUT=${F%.*}.o gpp "$OUTPUT" "$F" done -bld build src/*.o minibu/src/*.o || cmd LINK build g++ -o build src/*.o minibu/src/*.o -ldl +bld build src/*.o minibu/src/*.o || cmd LINK build g++ -fPIC -rdynamic -Wl,-export-dynamic -o build src/*.o minibu/src/*.o -ldl bld build~ build || cmd CP build~ cp build build~ diff --git a/share/autoinclude/general-rules.bld b/share/autoinclude/general-rules.bld index 9f905a6..94b06eb 100644 --- a/share/autoinclude/general-rules.bld +++ b/share/autoinclude/general-rules.bld @@ -40,7 +40,7 @@ function cppToObj() DIR = OBJ_DIR; } - return DIR + "/" + INPUT.fileName().replace(".cpp", ".o"); + return DIR + "/" + INPUT.fileName().regex("\\.cpp$", ".o"); } function cToObj() @@ -54,7 +54,7 @@ function cToObj() DIR = OBJ_DIR; } - return DIR + "/" + INPUT.fileName().replace(".c", ".o"); + return DIR + "/" + INPUT.fileName().regex("\\.c$", ".o"); } rule "exe" @@ -100,7 +100,6 @@ rule "c" { input "*.c"; output INPUT.cToObj(); -// output replace(".c", ".o"); requires getMakeDeps("${CC} ${CFLAGS} -M ${INPUT}"); profile "build" { @@ -111,10 +110,10 @@ rule "c" rule "bison" { input "*.y"; - output [INPUT.replace(".y", ".tab.c"), INPUT.replace(".y", ".tab.h")]; + output [INPUT.regex("\\.y$", ".tab.c"), INPUT.regex("\\.y$", ".tab.h")]; profile "build" { - BASE = INPUT.replace(".y", ""); + BASE = INPUT.regex("\\.y", ""); execute("${BISON} -b${BASE} ${INPUT}"); // if you add a -v bison will produce a .output file } @@ -123,8 +122,8 @@ rule "bison" rule "flex" { input "*.l"; - output INPUT.replace(".l", ".yy.c"); - output INPUT.replace(".l", ".yy.h"); + output INPUT.regex("\\.l$", ".yy.c"); + output INPUT.regex("\\.l$", ".yy.h"); profile "build" { execute("${FLEX} ${FLEXFLAGS} ${INPUT}"); diff --git a/src/functionplugger.cpp b/src/functionplugger.cpp index 83435ae..c8d3fd2 100644 --- a/src/functionplugger.cpp +++ b/src/functionplugger.cpp @@ -15,6 +15,7 @@ extern Bu::PluginInfo pluginFunctionReplace; extern Bu::PluginInfo pluginFunctionTargets; extern Bu::PluginInfo pluginFunctionToString; extern Bu::PluginInfo pluginFunctionUnlink; +extern Bu::PluginInfo pluginFunctionRegEx; FunctionPlugger::FunctionPlugger() { @@ -30,6 +31,7 @@ FunctionPlugger::FunctionPlugger() registerBuiltinPlugin( &pluginFunctionTargets ); registerBuiltinPlugin( &pluginFunctionToString ); registerBuiltinPlugin( &pluginFunctionUnlink ); + registerBuiltinPlugin( &pluginFunctionRegEx ); DIR *dir = opendir("/usr/lib/build"); if( !dir ) diff --git a/src/functionregex.cpp b/src/functionregex.cpp new file mode 100644 index 0000000..f0abc35 --- /dev/null +++ b/src/functionregex.cpp @@ -0,0 +1,126 @@ +#include "functionregex.h" + +#include +#include +PluginInterface3( pluginFunctionRegEx, regex, FunctionRegEx, Function, + "Mike Buland", 0, 1 ); + +FunctionRegEx::FunctionRegEx() +{ +} + +FunctionRegEx::~FunctionRegEx() +{ +} + +Bu::FString FunctionRegEx::getName() const +{ + return "regex"; +} + +Variable FunctionRegEx::call( Variable &input, VarList lParams ) +{ + if( lParams.getSize() == 1 ) + { + Bu::RegEx re( lParams.first().getString() ); + switch( input.getType() ) + { + case Variable::typeString: + return re.execute( input.getString() ); + + case Variable::typeList: + { + Variable vOut( Variable::typeList ); + for( VarList::iterator i = input.begin(); i; i++ ) + { + if( re.execute( (*i).toString() ) ) + vOut.append( *i ); + } + return vOut; + } + break; + + default: + break; + } + } + else if( lParams.getSize() == 2 ) + { + Bu::RegEx re( lParams.first().getString() ); + Bu::FString sPat = lParams.last().getString(); + switch( input.getType() ) + { + case Variable::typeString: + if( re.execute( input.getString() ) ) + { + return replace( re, input.getString(), sPat ); + } + else + { + return input; + } + break; + + case Variable::typeList: + { + Variable vOut( Variable::typeList ); + for( VarList::iterator i = input.begin(); i; i++ ) + { + if( re.execute( (*i).toString() ) ) + vOut.append( replace( re, (*i).toString(), sPat ) ); + else + vOut.append( *i ); + } + return vOut; + } + break; + + default: + break; + } + } + throw Bu::ExceptionBase( + "regex does not work on non-string or non-list types."); +} + +Bu::FString FunctionRegEx::replace( Bu::RegEx &re, const Bu::FString &sSrc, + const Bu::FString &sPat ) +{ + Bu::FString sOut; + + int iStart, iEnd; + re.getSubStringRange( 0, iStart, iEnd ); // Get the range of the full match + + if( iStart > 0 ) + sOut.append( sSrc, 0, iStart ); + + for( Bu::FString::const_iterator i = sPat.begin(); i; i++ ) + { + if( *i == '\\' ) + { + i++; + if( *i <= '9' && *i >= '0' ) + { + int iInd = *i-'0'; + if( iInd < re.getNumSubStrings() ) + sOut += re.getSubString( iInd ); + } + else + { + sOut += *i; + } + } + else + { + sOut += *i; + } + } + + if( iEnd < sSrc.getSize() ) + { + sOut.append( sSrc, iEnd, -1 ); + } + + return sOut; +} + diff --git a/src/functionregex.h b/src/functionregex.h new file mode 100644 index 0000000..2096a64 --- /dev/null +++ b/src/functionregex.h @@ -0,0 +1,24 @@ +#ifndef FUNCTION_REG_EX_H +#define FUNCTION_REG_EX_H + +#include "function.h" + +namespace Bu +{ + class RegEx; +} + +class FunctionRegEx : public Function +{ +public: + FunctionRegEx(); + virtual ~FunctionRegEx(); + + virtual Bu::FString getName() const; + virtual Variable call( Variable &input, VarList lParams ); + + Bu::FString replace( Bu::RegEx &re, const Bu::FString &sSrc, + const Bu::FString &sPat ); +}; + +#endif -- cgit v1.2.3