From d2fe7edb2bfea20987a1f69935179fa5fc9f3b37 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 23 Aug 2006 22:09:30 +0000 Subject: Really close...functions are doing their stuff, we have inputs, almost have rules. --- build.conf | 2 +- src/build.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ src/build.h | 11 ++++++ src/builder.cpp | 38 +++++++++++++++++++- src/builder.h | 8 +++-- src/function.h | 2 +- src/functionfilesin.cpp | 23 ++++++++++++ src/functionregexp.cpp | 17 +++++++++ src/regexp.cpp | 78 +++++++++++++++++++++++++++++++++++++++++ src/regexp.h | 36 +++++++++++++++++++ src/rule.h | 31 ++++++++++++++++- 11 files changed, 332 insertions(+), 7 deletions(-) create mode 100644 src/regexp.cpp create mode 100644 src/regexp.h diff --git a/build.conf b/build.conf index 6a1b611..32ce750 100644 --- a/build.conf +++ b/build.conf @@ -11,7 +11,7 @@ set "CXXFLAGS" += "-ggdb" target file, requires "libbu++/libbu++.a", set "CXXFLAGS" += "-Ilibbu++/src", - input filesIn("src") + input filesIn("src") filter regexp(".*\\.(cpp|y|l)$") rule "exe": matches regexp("(.*)\\.o$"), diff --git a/src/build.cpp b/src/build.cpp index d9a8b39..ec97ccb 100644 --- a/src/build.cpp +++ b/src/build.cpp @@ -29,6 +29,62 @@ void Build::addRequires( const std::string &who, const std::string &what ) mRequires[who].push_back( what ); } +void Build::addRule( Rule *pRule ) +{ + mRule[pRule->getName()] = pRule; +} + +void Build::set( const std::string &cont, const std::string &var, const std::string &val ) +{ + if( cont == "" ) + { + mVars[var] = val; + } + else + { + mContVars[cont][var] = val; + } +} + +void Build::setAdd( const std::string &cont, const std::string &var, const std::string &val ) +{ + if( cont == "" ) + { + mVars[var] = getVar( cont, var ) + " " + val; + } + else + { + mContVars[cont][var] = getVar( cont, var ) + " " + val; + } +} + +std::string Build::getVar( const std::string &cont, const std::string &var ) +{ + if( cont == "" ) + { + if( mVars.find(var) == mVars.end() ) + { + if( getenv( var.c_str() ) == NULL ) + { + mVars[var] = ""; + } + else + { + mVars[var] = getenv( var.c_str() ); + } + } + return mVars[var]; + } + else + { + if( mContVars[cont].find(var) == mContVars[cont].end() ) + { + mContVars[cont][var] = getVar( "", var ); + } + return mContVars[cont][var]; + } +} + void Build::debugDump() { printf("Requires:\n"); @@ -45,5 +101,42 @@ void Build::debugDump() } printf("\n"); } + + printf("Targets:\n"); + for( TargetMap::iterator i = mTarget.begin(); i != mTarget.end(); i++ ) + { + printf(" %s:\n", (*i).first.c_str() ); + printf(" Rule: %s\n", (*i).second->getRule().c_str() ); + printf(" Input: "); + for( StringList::iterator j = (*i).second->getInput().begin(); + j != (*i).second->getInput().end(); j++ ) + { + if( j != (*i).second->getInput().begin() ) + printf(", "); + printf("%s", (*j).c_str() ); + } + printf("\n"); + } + + printf("Global Variables:\n"); + for( VarMap::iterator i = mVars.begin(); i != mVars.end(); i++ ) + { + printf(" \"%s\" = \"%s\"\n", (*i).first.c_str(), (*i).second.c_str() ); + } + + printf("Context Variables:\n"); + for( ContextMap::iterator i = mContVars.begin(); i != mContVars.end(); i++ ) + { + printf(" %s:\n", (*i).first.c_str() ); + + for( VarMap::iterator j = (*i).second.begin(); + j != (*i).second.end(); j++ ) + { + printf(" \"%s\" = \"%s\"\n", + (*j).first.c_str(), + (*j).second.c_str() + ); + } + } } diff --git a/src/build.h b/src/build.h index 00acbfc..fe71d30 100644 --- a/src/build.h +++ b/src/build.h @@ -27,6 +27,11 @@ public: */ void addTarget( Target *pTarget ); void addRequires( const std::string &who, const std::string &what ); + void addRule( Rule *pRule ); + + void set( const std::string &cont, const std::string &var, const std::string &val ); + void setAdd( const std::string &cont, const std::string &var, const std::string &val ); + std::string getVar( const std::string &cont, const std::string &var ); void debugDump(); @@ -34,9 +39,15 @@ private: typedef std::map TargetMap; typedef std::list StringList; typedef std::map ReqMap; + typedef std::map VarMap; + typedef std::map ContextMap; + typedef std::map RuleMap; TargetMap mTarget; ReqMap mRequires; + VarMap mVars; + ContextMap mContVars; + RuleMap mRule; //std::map mRule; //Action *pActDefault; diff --git a/src/builder.cpp b/src/builder.cpp index 4d377a8..1e916ab 100644 --- a/src/builder.cpp +++ b/src/builder.cpp @@ -134,7 +134,14 @@ void Builder::addListFunc() void Builder::filterList() { - printf("Filters aren't done yet.\n"); + StringList lTmp2; + StringList lIn = buildToStringList( lTmp, StringList() ); + pTmpFunc->execute( lIn, lTmp2 ); + lTmp.clear(); + for( StringList::iterator i = lTmp2.begin(); i != lTmp2.end(); i++ ) + { + lTmp.push_back( BuildListItem( *i, NULL ) ); + } } StringList Builder::buildToStringList( const BuildList &lSrc, const StringList &lIn ) @@ -353,6 +360,21 @@ Build *Builder::genBuild() { Build *bld = new Build; + for( SetVarList::iterator i = lGlobalVars.begin(); + i != lGlobalVars.end(); i++ ) + { + switch( (*i).third ) + { + case setSet: + bld->set( "", (*i).first, (*i).second ); + break; + + case setAdd: + bld->setAdd( "", (*i).first, (*i).second ); + break; + } + } + for( TargetTmpList::iterator i = lTargetTmp.begin(); i != lTargetTmp.end(); i++ ) { @@ -388,6 +410,20 @@ Build *Builder::genBuild() { bld->addRequires( (*j), (*k) ); } + for( SetVarList::iterator k = (*i).second.lVar.begin(); + k != (*i).second.lVar.end(); k++ ) + { + switch( (*k).third ) + { + case setSet: + bld->set( *j, (*k).first, (*k).second ); + break; + + case setAdd: + bld->setAdd( *j, (*k).first, (*k).second ); + break; + } + } } } diff --git a/src/builder.h b/src/builder.h index 8d72627..367769f 100644 --- a/src/builder.h +++ b/src/builder.h @@ -20,6 +20,8 @@ class TargetFactory; YY_DECL; typedef std::list StringList; +typedef std::list FunctionList; +typedef std::list PerformList; template class Triplet @@ -137,14 +139,14 @@ private: // Rule variables Function *pMatches; BuildList lProduces; BuildList lRequires; - std::list lFilter; - std::list lPerform; + FunctionList lFilter; + PerformList lPerform; }; typedef std::list RuleTmpList; RuleTmpList lRuleTmp; -public: // Functions for dealing with actions +public: // Action functions void addAction(); void addAction( const char *sName ); void addCommand( int nType ); diff --git a/src/function.h b/src/function.h index 1a71f8a..0e8f49e 100644 --- a/src/function.h +++ b/src/function.h @@ -13,7 +13,7 @@ public: void addParam( const char *str ); virtual void execute( const StringList &lInput, StringList &lOutput )=0; -private: +protected: StringList lParams; }; diff --git a/src/functionfilesin.cpp b/src/functionfilesin.cpp index 0d65c13..3e1233a 100644 --- a/src/functionfilesin.cpp +++ b/src/functionfilesin.cpp @@ -1,5 +1,8 @@ +#include + #include "functionfilesin.h" #include "plugger.h" +#include "build.h" PluginInterface2(filesIn, FunctionFilesIn, Function, "Mike Buland", 0, 1 ); @@ -13,5 +16,25 @@ FunctionFilesIn::~FunctionFilesIn() void FunctionFilesIn::execute( const StringList &lInput, StringList &lOutput ) { + DIR *d = opendir( lParams.front().c_str() ); + if( d == NULL ) + throw BuildException( + "Can't open directory %s.", + lParams.front().c_str() + ); + + struct dirent *e; + + std::string prefix = lParams.front() + "/"; + + while( (e = readdir( d )) ) + { + if( e->d_type == DT_REG ) + { + lOutput.push_back( prefix + e->d_name ); + } + } + + closedir( d ); } diff --git a/src/functionregexp.cpp b/src/functionregexp.cpp index 462e93c..d1491e6 100644 --- a/src/functionregexp.cpp +++ b/src/functionregexp.cpp @@ -1,5 +1,6 @@ #include "functionregexp.h" #include "plugger.h" +#include "regexp.h" PluginInterface2(regexp, FunctionRegexp, Function, "Mike Buland", 0, 1 ); @@ -13,5 +14,21 @@ FunctionRegexp::~FunctionRegexp() void FunctionRegexp::execute( const StringList &lInput, StringList &lOutput ) { + if( lParams.size() == 1 ) + { + RegExp re( lParams.front().c_str() ); + + for( StringList::const_iterator i = lInput.begin(); + i != lInput.end(); i++ ) + { + if( re.execute( (*i).c_str() ) ) + { + lOutput.push_back( *i ); + } + } + } + else + { + } } diff --git a/src/regexp.cpp b/src/regexp.cpp new file mode 100644 index 0000000..f79be97 --- /dev/null +++ b/src/regexp.cpp @@ -0,0 +1,78 @@ +#include "regexp.h" +#include "build.h" // For BuildException +#include "staticstring.h" + +RegExp::RegExp() : + bCompiled( false ), + aSubStr( NULL ) +{ +} + +RegExp::RegExp( const char *sSrc ) : + bCompiled( false ), + aSubStr( NULL ) +{ + compile( sSrc ); +} + +RegExp::~RegExp() +{ + if( bCompiled ) + { + regfree( &re ); + delete[] aSubStr; + } +} + +void RegExp::compile( const char *sSrc ) +{ + if( bCompiled ) + { + regfree( &re ); + delete[] aSubStr; + bCompiled = false; + } + + int nErr = regcomp( &re, sSrc, REG_EXTENDED|REG_NEWLINE ); + if( nErr ) + { + size_t length = regerror( nErr, &re, NULL, 0 ); + char *buffer = new char[length]; + (void) regerror( nErr, &re, buffer, length ); + StaticString s( buffer ); + delete[] buffer; + throw BuildException( s.getString() ); + } + bCompiled = true; + this->sSrc = sSrc; + + nSubStr = re.re_nsub+1; + aSubStr = new regmatch_t[nSubStr]; +} + +int RegExp::getNumSubStrings() +{ + return nSubStr; +} + +bool RegExp::execute( const char *sSrc ) +{ + sTest = sSrc; + if( regexec( &re, sSrc, nSubStr, aSubStr, 0 ) ) + return false; + return true; +} + +std::pair RegExp::getSubStringRange( int nIndex ) +{ + return std::pair( aSubStr[nIndex].rm_so, aSubStr[nIndex].rm_eo ); +} + +std::string RegExp::getSubString( int nIndex ) +{ + return std::string( + sTest.getString()+aSubStr[nIndex].rm_so, + aSubStr[nIndex].rm_eo - aSubStr[nIndex].rm_so + ); +} + diff --git a/src/regexp.h b/src/regexp.h new file mode 100644 index 0000000..96f3747 --- /dev/null +++ b/src/regexp.h @@ -0,0 +1,36 @@ +#ifndef REG_EXP_H +#define REG_EXP_H + +#include +#include +#include +#include +#include "staticstring.h" + +class RegExp +{ +public: + RegExp(); + RegExp( const char *sSrc ); + virtual ~RegExp(); + + void compile( const char *sSrc ); + int getNumSubStrings(); + bool execute( const char *sSrc ); + std::pair getSubStringRange( int nIndex ); + std::string getSubString( int nIndex ); + const char *getSource() + { + return sSrc; + } + +private: + StaticString sSrc; + StaticString sTest; + regex_t re; + bool bCompiled; + int nSubStr; + regmatch_t *aSubStr; +}; + +#endif diff --git a/src/rule.h b/src/rule.h index 065ab9e..ff251ca 100644 --- a/src/rule.h +++ b/src/rule.h @@ -2,7 +2,14 @@ #define RULE_H #include +#include +#include +class Function; +class Perform; + +typedef std::list FunctionList; +typedef std::list PerformList; class Rule { @@ -10,8 +17,30 @@ public: Rule(); virtual ~Rule(); -private: + std::string getName() + { + return sName; + } + + void setName( const std::string &sName ) + { + this->sName = sName; + } + FunctionList &getFunctionList() + { + return lFilter; + } + + PerformList &getPerformList() + { + return lPerform; + } + +private: + std::string sName; + FunctionList lFilter; + PerformList lPerform; }; #endif -- cgit v1.2.3