X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=buildtool.cpp;h=6d1dd25a8795f0a920d576bc27d204492d8eaa28;hb=ee526a340556b691b04740ba2fcad279ab34f815;hp=11d6b7fa990d52b6da5c9660af73142777238d36;hpb=be7b0d5f126473157a9fe026e7a9fd54ee4b27c5;p=inkscape.git diff --git a/buildtool.cpp b/buildtool.cpp index 11d6b7fa9..6d1dd25a8 100644 --- a/buildtool.cpp +++ b/buildtool.cpp @@ -4,7 +4,7 @@ * Authors: * Bob Jamison * - * Copyright (C) 2006 Bob Jamison + * Copyright (C) 2006-2007 Bob Jamison * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -21,25 +21,33 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -/* - - - - - - - - - +/** + * To use this file, compile with: + *
+ * g++ -O3 buildtool.cpp -o btool.exe + * (or whatever your compiler might be) + * Then + * btool + * or + * btool {target} + * + * Note: if you are using MinGW, and a not very recent version of it, + * gettimeofday() might be missing. If so, just build this file with + * this command: + * g++ -O3 -DNEED_GETTIMEOFDAY buildtool.cpp -o btool.exe + * + */ -*/ +#define BUILDTOOL_VERSION "BuildTool v0.6.12, 2007 Bob Jamison" #include+#include #include #include #include #include #include +#include #include #include @@ -52,6 +60,45 @@ #endif +#include + + +//######################################################################## +//# Definition of gettimeofday() for those who don't have it +//######################################################################## +#ifdef NEED_GETTIMEOFDAY +#include + +struct timezone { + int tz_minuteswest; /* minutes west of Greenwich */ + int tz_dsttime; /* type of dst correction */ + }; + +static int gettimeofday (struct timeval *tv, struct timezone *tz) +{ + struct _timeb tb; + + if (!tv) + return (-1); + + _ftime (&tb); + tv->tv_sec = tb.time; + tv->tv_usec = tb.millitm * 1000 + 500; + if (tz) + { + tz->tz_minuteswest = -60 * _timezone; + tz->tz_dsttime = _daylight; + } + return 0; +} + +#endif + + + + + + namespace buildtool { @@ -59,6 +106,749 @@ namespace buildtool +//######################################################################## +//######################################################################## +//## R E G E X P +//######################################################################## +//######################################################################## + +/** + * This is the T-Rex regular expression library, which we + * gratefully acknowledge. It's clean code and small size allow + * us to embed it in BuildTool without adding a dependency + * + */ + +//begin trex.h + +#ifndef _TREX_H_ +#define _TREX_H_ +/*************************************************************** + T-Rex a tiny regular expression library + + Copyright (C) 2003-2006 Alberto Demichelis + + This software is provided 'as-is', without any express + or implied warranty. In no event will the authors be held + liable for any damages arising from the use of this software. + + Permission is granted to anyone to use this software for + any purpose, including commercial applications, and to alter + it and redistribute it freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; + you must not claim that you wrote the original software. + If you use this software in a product, an acknowledgment + in the product documentation would be appreciated but + is not required. + + 2. Altered source versions must be plainly marked as such, + and must not be misrepresented as being the original software. + + 3. This notice may not be removed or altered from any + source distribution. + +****************************************************************/ + +#ifdef _UNICODE +#define TRexChar unsigned short +#define MAX_CHAR 0xFFFF +#define _TREXC(c) L##c +#define trex_strlen wcslen +#define trex_printf wprintf +#else +#define TRexChar char +#define MAX_CHAR 0xFF +#define _TREXC(c) (c) +#define trex_strlen strlen +#define trex_printf printf +#endif + +#ifndef TREX_API +#define TREX_API extern +#endif + +#define TRex_True 1 +#define TRex_False 0 + +typedef unsigned int TRexBool; +typedef struct TRex TRex; + +typedef struct { + const TRexChar *begin; + int len; +} TRexMatch; + +TREX_API TRex *trex_compile(const TRexChar *pattern,const TRexChar **error); +TREX_API void trex_free(TRex *exp); +TREX_API TRexBool trex_match(TRex* exp,const TRexChar* text); +TREX_API TRexBool trex_search(TRex* exp,const TRexChar* text, const TRexChar** out_begin, const TRexChar** out_end); +TREX_API TRexBool trex_searchrange(TRex* exp,const TRexChar* text_begin,const TRexChar* text_end,const TRexChar** out_begin, const TRexChar** out_end); +TREX_API int trex_getsubexpcount(TRex* exp); +TREX_API TRexBool trex_getsubexp(TRex* exp, int n, TRexMatch *subexp); + +#endif + +//end trex.h + +//start trex.c + + +#include +#include + +/* see copyright notice in trex.h */ +#include +#include +#include +#include +//#include "trex.h" + +#ifdef _UINCODE +#define scisprint iswprint +#define scstrlen wcslen +#define scprintf wprintf +#define _SC(x) L(x) +#else +#define scisprint isprint +#define scstrlen strlen +#define scprintf printf +#define _SC(x) (x) +#endif + +#ifdef _DEBUG +#include + +static const TRexChar *g_nnames[] = +{ + _SC("NONE"),_SC("OP_GREEDY"), _SC("OP_OR"), + _SC("OP_EXPR"),_SC("OP_NOCAPEXPR"),_SC("OP_DOT"), _SC("OP_CLASS"), + _SC("OP_CCLASS"),_SC("OP_NCLASS"),_SC("OP_RANGE"),_SC("OP_CHAR"), + _SC("OP_EOL"),_SC("OP_BOL"),_SC("OP_WB") +}; + +#endif +#define OP_GREEDY (MAX_CHAR+1) // * + ? {n} +#define OP_OR (MAX_CHAR+2) +#define OP_EXPR (MAX_CHAR+3) //parentesis () +#define OP_NOCAPEXPR (MAX_CHAR+4) //parentesis (?:) +#define OP_DOT (MAX_CHAR+5) +#define OP_CLASS (MAX_CHAR+6) +#define OP_CCLASS (MAX_CHAR+7) +#define OP_NCLASS (MAX_CHAR+8) //negates class the [^ +#define OP_RANGE (MAX_CHAR+9) +#define OP_CHAR (MAX_CHAR+10) +#define OP_EOL (MAX_CHAR+11) +#define OP_BOL (MAX_CHAR+12) +#define OP_WB (MAX_CHAR+13) + +#define TREX_SYMBOL_ANY_CHAR ('.') +#define TREX_SYMBOL_GREEDY_ONE_OR_MORE ('+') +#define TREX_SYMBOL_GREEDY_ZERO_OR_MORE ('*') +#define TREX_SYMBOL_GREEDY_ZERO_OR_ONE ('?') +#define TREX_SYMBOL_BRANCH ('|') +#define TREX_SYMBOL_END_OF_STRING ('$') +#define TREX_SYMBOL_BEGINNING_OF_STRING ('^') +#define TREX_SYMBOL_ESCAPE_CHAR ('\\') + + +typedef int TRexNodeType; + +typedef struct tagTRexNode{ + TRexNodeType type; + int left; + int right; + int next; +}TRexNode; + +struct TRex{ + const TRexChar *_eol; + const TRexChar *_bol; + const TRexChar *_p; + int _first; + int _op; + TRexNode *_nodes; + int _nallocated; + int _nsize; + int _nsubexpr; + TRexMatch *_matches; + int _currsubexp; + void *_jmpbuf; + const TRexChar **_error; +}; + +static int trex_list(TRex *exp); + +static int trex_newnode(TRex *exp, TRexNodeType type) +{ + TRexNode n; + int newid; + n.type = type; + n.next = n.right = n.left = -1; + if(type == OP_EXPR) + n.right = exp->_nsubexpr++; + if(exp->_nallocated < (exp->_nsize + 1)) { + //int oldsize = exp->_nallocated; + exp->_nallocated *= 2; + exp->_nodes = (TRexNode *)realloc(exp->_nodes, exp->_nallocated * sizeof(TRexNode)); + } + exp->_nodes[exp->_nsize++] = n; + newid = exp->_nsize - 1; + return (int)newid; +} + +static void trex_error(TRex *exp,const TRexChar *error) +{ + if(exp->_error) *exp->_error = error; + longjmp(*((jmp_buf*)exp->_jmpbuf),-1); +} + +static void trex_expect(TRex *exp, int n){ + if((*exp->_p) != n) + trex_error(exp, _SC("expected paren")); + exp->_p++; +} + +static TRexChar trex_escapechar(TRex *exp) +{ + if(*exp->_p == TREX_SYMBOL_ESCAPE_CHAR){ + exp->_p++; + switch(*exp->_p) { + case 'v': exp->_p++; return '\v'; + case 'n': exp->_p++; return '\n'; + case 't': exp->_p++; return '\t'; + case 'r': exp->_p++; return '\r'; + case 'f': exp->_p++; return '\f'; + default: return (*exp->_p++); + } + } else if(!scisprint(*exp->_p)) trex_error(exp,_SC("letter expected")); + return (*exp->_p++); +} + +static int trex_charclass(TRex *exp,int classid) +{ + int n = trex_newnode(exp,OP_CCLASS); + exp->_nodes[n].left = classid; + return n; +} + +static int trex_charnode(TRex *exp,TRexBool isclass) +{ + TRexChar t; + if(*exp->_p == TREX_SYMBOL_ESCAPE_CHAR) { + exp->_p++; + switch(*exp->_p) { + case 'n': exp->_p++; return trex_newnode(exp,'\n'); + case 't': exp->_p++; return trex_newnode(exp,'\t'); + case 'r': exp->_p++; return trex_newnode(exp,'\r'); + case 'f': exp->_p++; return trex_newnode(exp,'\f'); + case 'v': exp->_p++; return trex_newnode(exp,'\v'); + case 'a': case 'A': case 'w': case 'W': case 's': case 'S': + case 'd': case 'D': case 'x': case 'X': case 'c': case 'C': + case 'p': case 'P': case 'l': case 'u': + { + t = *exp->_p; exp->_p++; + return trex_charclass(exp,t); + } + case 'b': + case 'B': + if(!isclass) { + int node = trex_newnode(exp,OP_WB); + exp->_nodes[node].left = *exp->_p; + exp->_p++; + return node; + } //else default + default: + t = *exp->_p; exp->_p++; + return trex_newnode(exp,t); + } + } + else if(!scisprint(*exp->_p)) { + + trex_error(exp,_SC("letter expected")); + } + t = *exp->_p; exp->_p++; + return trex_newnode(exp,t); +} +static int trex_class(TRex *exp) +{ + int ret = -1; + int first = -1,chain; + if(*exp->_p == TREX_SYMBOL_BEGINNING_OF_STRING){ + ret = trex_newnode(exp,OP_NCLASS); + exp->_p++; + }else ret = trex_newnode(exp,OP_CLASS); + + if(*exp->_p == ']') trex_error(exp,_SC("empty class")); + chain = ret; + while(*exp->_p != ']' && exp->_p != exp->_eol) { + if(*exp->_p == '-' && first != -1){ + int r,t; + if(*exp->_p++ == ']') trex_error(exp,_SC("unfinished range")); + r = trex_newnode(exp,OP_RANGE); + if(first>*exp->_p) trex_error(exp,_SC("invalid range")); + if(exp->_nodes[first].type == OP_CCLASS) trex_error(exp,_SC("cannot use character classes in ranges")); + exp->_nodes[r].left = exp->_nodes[first].type; + t = trex_escapechar(exp); + exp->_nodes[r].right = t; + exp->_nodes[chain].next = r; + chain = r; + first = -1; + } + else{ + if(first!=-1){ + int c = first; + exp->_nodes[chain].next = c; + chain = c; + first = trex_charnode(exp,TRex_True); + } + else{ + first = trex_charnode(exp,TRex_True); + } + } + } + if(first!=-1){ + int c = first; + exp->_nodes[chain].next = c; + chain = c; + first = -1; + } + /* hack? */ + exp->_nodes[ret].left = exp->_nodes[ret].next; + exp->_nodes[ret].next = -1; + return ret; +} + +static int trex_parsenumber(TRex *exp) +{ + int ret = *exp->_p-'0'; + int positions = 10; + exp->_p++; + while(isdigit(*exp->_p)) { + ret = ret*10+(*exp->_p++-'0'); + if(positions==1000000000) trex_error(exp,_SC("overflow in numeric constant")); + positions *= 10; + }; + return ret; +} + +static int trex_element(TRex *exp) +{ + int ret = -1; + switch(*exp->_p) + { + case '(': { + int expr,newn; + exp->_p++; + + + if(*exp->_p =='?') { + exp->_p++; + trex_expect(exp,':'); + expr = trex_newnode(exp,OP_NOCAPEXPR); + } + else + expr = trex_newnode(exp,OP_EXPR); + newn = trex_list(exp); + exp->_nodes[expr].left = newn; + ret = expr; + trex_expect(exp,')'); + } + break; + case '[': + exp->_p++; + ret = trex_class(exp); + trex_expect(exp,']'); + break; + case TREX_SYMBOL_END_OF_STRING: exp->_p++; ret = trex_newnode(exp,OP_EOL);break; + case TREX_SYMBOL_ANY_CHAR: exp->_p++; ret = trex_newnode(exp,OP_DOT);break; + default: + ret = trex_charnode(exp,TRex_False); + break; + } + + { + int op; + TRexBool isgreedy = TRex_False; + unsigned short p0 = 0, p1 = 0; + switch(*exp->_p){ + case TREX_SYMBOL_GREEDY_ZERO_OR_MORE: p0 = 0; p1 = 0xFFFF; exp->_p++; isgreedy = TRex_True; break; + case TREX_SYMBOL_GREEDY_ONE_OR_MORE: p0 = 1; p1 = 0xFFFF; exp->_p++; isgreedy = TRex_True; break; + case TREX_SYMBOL_GREEDY_ZERO_OR_ONE: p0 = 0; p1 = 1; exp->_p++; isgreedy = TRex_True; break; + case '{': + exp->_p++; + if(!isdigit(*exp->_p)) trex_error(exp,_SC("number expected")); + p0 = (unsigned short)trex_parsenumber(exp); + /*******************************/ + switch(*exp->_p) { + case '}': + p1 = p0; exp->_p++; + break; + case ',': + exp->_p++; + p1 = 0xFFFF; + if(isdigit(*exp->_p)){ + p1 = (unsigned short)trex_parsenumber(exp); + } + trex_expect(exp,'}'); + break; + default: + trex_error(exp,_SC(", or } expected")); + } + /*******************************/ + isgreedy = TRex_True; + break; + + } + if(isgreedy) { + int nnode = trex_newnode(exp,OP_GREEDY); + op = OP_GREEDY; + exp->_nodes[nnode].left = ret; + exp->_nodes[nnode].right = ((p0)<<16)|p1; + ret = nnode; + } + } + if((*exp->_p != TREX_SYMBOL_BRANCH) && (*exp->_p != ')') && (*exp->_p != TREX_SYMBOL_GREEDY_ZERO_OR_MORE) && (*exp->_p != TREX_SYMBOL_GREEDY_ONE_OR_MORE) && (*exp->_p != '\0')) { + int nnode = trex_element(exp); + exp->_nodes[ret].next = nnode; + } + + return ret; +} + +static int trex_list(TRex *exp) +{ + int ret=-1,e; + if(*exp->_p == TREX_SYMBOL_BEGINNING_OF_STRING) { + exp->_p++; + ret = trex_newnode(exp,OP_BOL); + } + e = trex_element(exp); + if(ret != -1) { + exp->_nodes[ret].next = e; + } + else ret = e; + + if(*exp->_p == TREX_SYMBOL_BRANCH) { + int temp,tright; + exp->_p++; + temp = trex_newnode(exp,OP_OR); + exp->_nodes[temp].left = ret; + tright = trex_list(exp); + exp->_nodes[temp].right = tright; + ret = temp; + } + return ret; +} + +static TRexBool trex_matchcclass(int cclass,TRexChar c) +{ + switch(cclass) { + case 'a': return isalpha(c)?TRex_True:TRex_False; + case 'A': return !isalpha(c)?TRex_True:TRex_False; + case 'w': return (isalnum(c) || c == '_')?TRex_True:TRex_False; + case 'W': return (!isalnum(c) && c != '_')?TRex_True:TRex_False; + case 's': return isspace(c)?TRex_True:TRex_False; + case 'S': return !isspace(c)?TRex_True:TRex_False; + case 'd': return isdigit(c)?TRex_True:TRex_False; + case 'D': return !isdigit(c)?TRex_True:TRex_False; + case 'x': return isxdigit(c)?TRex_True:TRex_False; + case 'X': return !isxdigit(c)?TRex_True:TRex_False; + case 'c': return iscntrl(c)?TRex_True:TRex_False; + case 'C': return !iscntrl(c)?TRex_True:TRex_False; + case 'p': return ispunct(c)?TRex_True:TRex_False; + case 'P': return !ispunct(c)?TRex_True:TRex_False; + case 'l': return islower(c)?TRex_True:TRex_False; + case 'u': return isupper(c)?TRex_True:TRex_False; + } + return TRex_False; /*cannot happen*/ +} + +static TRexBool trex_matchclass(TRex* exp,TRexNode *node,TRexChar c) +{ + do { + switch(node->type) { + case OP_RANGE: + if(c >= node->left && c <= node->right) return TRex_True; + break; + case OP_CCLASS: + if(trex_matchcclass(node->left,c)) return TRex_True; + break; + default: + if(c == node->type)return TRex_True; + } + } while((node->next != -1) && (node = &exp->_nodes[node->next])); + return TRex_False; +} + +static const TRexChar *trex_matchnode(TRex* exp,TRexNode *node,const TRexChar *str,TRexNode *next) +{ + + TRexNodeType type = node->type; + switch(type) { + case OP_GREEDY: { + //TRexNode *greedystop = (node->next != -1) ? &exp->_nodes[node->next] : NULL; + TRexNode *greedystop = NULL; + int p0 = (node->right >> 16)&0x0000FFFF, p1 = node->right&0x0000FFFF, nmaches = 0; + const TRexChar *s=str, *good = str; + + if(node->next != -1) { + greedystop = &exp->_nodes[node->next]; + } + else { + greedystop = next; + } + + while((nmaches == 0xFFFF || nmaches < p1)) { + + const TRexChar *stop; + if(!(s = trex_matchnode(exp,&exp->_nodes[node->left],s,greedystop))) + break; + nmaches++; + good=s; + if(greedystop) { + //checks that 0 matches satisfy the expression(if so skips) + //if not would always stop(for instance if is a '?') + if(greedystop->type != OP_GREEDY || + (greedystop->type == OP_GREEDY && ((greedystop->right >> 16)&0x0000FFFF) != 0)) + { + TRexNode *gnext = NULL; + if(greedystop->next != -1) { + gnext = &exp->_nodes[greedystop->next]; + }else if(next && next->next != -1){ + gnext = &exp->_nodes[next->next]; + } + stop = trex_matchnode(exp,greedystop,s,gnext); + if(stop) { + //if satisfied stop it + if(p0 == p1 && p0 == nmaches) break; + else if(nmaches >= p0 && p1 == 0xFFFF) break; + else if(nmaches >= p0 && nmaches <= p1) break; + } + } + } + + if(s >= exp->_eol) + break; + } + if(p0 == p1 && p0 == nmaches) return good; + else if(nmaches >= p0 && p1 == 0xFFFF) return good; + else if(nmaches >= p0 && nmaches <= p1) return good; + return NULL; + } + case OP_OR: { + const TRexChar *asd = str; + TRexNode *temp=&exp->_nodes[node->left]; + while( (asd = trex_matchnode(exp,temp,asd,NULL)) ) { + if(temp->next != -1) + temp = &exp->_nodes[temp->next]; + else + return asd; + } + asd = str; + temp = &exp->_nodes[node->right]; + while( (asd = trex_matchnode(exp,temp,asd,NULL)) ) { + if(temp->next != -1) + temp = &exp->_nodes[temp->next]; + else + return asd; + } + return NULL; + break; + } + case OP_EXPR: + case OP_NOCAPEXPR:{ + TRexNode *n = &exp->_nodes[node->left]; + const TRexChar *cur = str; + int capture = -1; + if(node->type != OP_NOCAPEXPR && node->right == exp->_currsubexp) { + capture = exp->_currsubexp; + exp->_matches[capture].begin = cur; + exp->_currsubexp++; + } + + do { + TRexNode *subnext = NULL; + if(n->next != -1) { + subnext = &exp->_nodes[n->next]; + }else { + subnext = next; + } + if(!(cur = trex_matchnode(exp,n,cur,subnext))) { + if(capture != -1){ + exp->_matches[capture].begin = 0; + exp->_matches[capture].len = 0; + } + return NULL; + } + } while((n->next != -1) && (n = &exp->_nodes[n->next])); + + if(capture != -1) + exp->_matches[capture].len = cur - exp->_matches[capture].begin; + return cur; + } + case OP_WB: + if(str == exp->_bol && !isspace(*str) + || (str == exp->_eol && !isspace(*(str-1))) + || (!isspace(*str) && isspace(*(str+1))) + || (isspace(*str) && !isspace(*(str+1))) ) { + return (node->left == 'b')?str:NULL; + } + return (node->left == 'b')?NULL:str; + case OP_BOL: + if(str == exp->_bol) return str; + return NULL; + case OP_EOL: + if(str == exp->_eol) return str; + return NULL; + case OP_DOT:{ + *str++; + } + return str; + case OP_NCLASS: + case OP_CLASS: + if(trex_matchclass(exp,&exp->_nodes[node->left],*str)?(type == OP_CLASS?TRex_True:TRex_False):(type == OP_NCLASS?TRex_True:TRex_False)) { + *str++; + return str; + } + return NULL; + case OP_CCLASS: + if(trex_matchcclass(node->left,*str)) { + *str++; + return str; + } + return NULL; + default: /* char */ + if(*str != node->type) return NULL; + *str++; + return str; + } + return NULL; +} + +/* public api */ +TRex *trex_compile(const TRexChar *pattern,const TRexChar **error) +{ + TRex *exp = (TRex *)malloc(sizeof(TRex)); + exp->_eol = exp->_bol = NULL; + exp->_p = pattern; + exp->_nallocated = (int)scstrlen(pattern) * sizeof(TRexChar); + exp->_nodes = (TRexNode *)malloc(exp->_nallocated * sizeof(TRexNode)); + exp->_nsize = 0; + exp->_matches = 0; + exp->_nsubexpr = 0; + exp->_first = trex_newnode(exp,OP_EXPR); + exp->_error = error; + exp->_jmpbuf = malloc(sizeof(jmp_buf)); + if(setjmp(*((jmp_buf*)exp->_jmpbuf)) == 0) { + int res = trex_list(exp); + exp->_nodes[exp->_first].left = res; + if(*exp->_p!='\0') + trex_error(exp,_SC("unexpected character")); +#ifdef _DEBUG + { + int nsize,i; + TRexNode *t; + nsize = exp->_nsize; + t = &exp->_nodes[0]; + scprintf(_SC("\n")); + for(i = 0;i < nsize; i++) { + if(exp->_nodes[i].type>MAX_CHAR) + scprintf(_SC("[%02d] %10s "),i,g_nnames[exp->_nodes[i].type-MAX_CHAR]); + else + scprintf(_SC("[%02d] %10c "),i,exp->_nodes[i].type); + scprintf(_SC("left %02d right %02d next %02d\n"),exp->_nodes[i].left,exp->_nodes[i].right,exp->_nodes[i].next); + } + scprintf(_SC("\n")); + } +#endif + exp->_matches = (TRexMatch *) malloc(exp->_nsubexpr * sizeof(TRexMatch)); + memset(exp->_matches,0,exp->_nsubexpr * sizeof(TRexMatch)); + } + else{ + trex_free(exp); + return NULL; + } + return exp; +} + +void trex_free(TRex *exp) +{ + if(exp) { + if(exp->_nodes) free(exp->_nodes); + if(exp->_jmpbuf) free(exp->_jmpbuf); + if(exp->_matches) free(exp->_matches); + free(exp); + } +} + +TRexBool trex_match(TRex* exp,const TRexChar* text) +{ + const TRexChar* res = NULL; + exp->_bol = text; + exp->_eol = text + scstrlen(text); + exp->_currsubexp = 0; + res = trex_matchnode(exp,exp->_nodes,text,NULL); + if(res == NULL || res != exp->_eol) + return TRex_False; + return TRex_True; +} + +TRexBool trex_searchrange(TRex* exp,const TRexChar* text_begin,const TRexChar* text_end,const TRexChar** out_begin, const TRexChar** out_end) +{ + const TRexChar *cur = NULL; + int node = exp->_first; + if(text_begin >= text_end) return TRex_False; + exp->_bol = text_begin; + exp->_eol = text_end; + do { + cur = text_begin; + while(node != -1) { + exp->_currsubexp = 0; + cur = trex_matchnode(exp,&exp->_nodes[node],cur,NULL); + if(!cur) + break; + node = exp->_nodes[node].next; + } + *text_begin++; + } while(cur == NULL && text_begin != text_end); + + if(cur == NULL) + return TRex_False; + + --text_begin; + + if(out_begin) *out_begin = text_begin; + if(out_end) *out_end = cur; + return TRex_True; +} + +TRexBool trex_search(TRex* exp,const TRexChar* text, const TRexChar** out_begin, const TRexChar** out_end) +{ + return trex_searchrange(exp,text,text + scstrlen(text),out_begin,out_end); +} + +int trex_getsubexpcount(TRex* exp) +{ + return exp->_nsubexpr; +} + +TRexBool trex_getsubexp(TRex* exp, int n, TRexMatch *subexp) +{ + if( n<0 || n >= exp->_nsubexpr) return TRex_False; + *subexp = exp->_matches[n]; + return TRex_True; +} + + +//######################################################################## +//######################################################################## +//## E N D R E G E X P +//######################################################################## +//######################################################################## + + + //######################################################################## @@ -172,18 +962,18 @@ friend class Parser; public: Element() { - parent = NULL; + init(); } Element(const String &nameArg) { - parent = NULL; + init(); name = nameArg; } Element(const String &nameArg, const String &valueArg) { - parent = NULL; + init(); name = nameArg; value = valueArg; } @@ -251,9 +1041,18 @@ public: * @param elem the element to output */ void print(); + + int getLine() + { return line; } protected: + void init() + { + parent = NULL; + line = 0; + } + void assign(const Element &other) { parent = other.parent; @@ -262,6 +1061,7 @@ protected: namespaces = other.namespaces; name = other.name; value = other.value; + line = other.line; } void findElementsRecursive(std::vector &res, const String &name); @@ -277,7 +1077,8 @@ protected: String name; String value; - + + int line; }; @@ -345,15 +1146,17 @@ private: currentPosition = 0; } - void getLineAndColumn(long pos, long *lineNr, long *colNr); + int countLines(int begin, int end); + + void getLineAndColumn(int pos, int *lineNr, int *colNr); void error(char *fmt, ...); - int peek(long pos); + int peek(int pos); - int match(long pos, const char *text); + int match(int pos, const char *text); - int skipwhite(long p); + int skipwhite(int p); int getWord(int p0, String &buf); @@ -369,12 +1172,10 @@ private: bool keepGoing; Element *currentNode; - long parselen; + int parselen; XMLCh *parsebuf; - String cdatabuf; - long currentPosition; - int colNr; - + String cdatabuf; + int currentPosition; }; @@ -390,6 +1191,7 @@ Element *Element::clone() elem->parent = parent; elem->attributes = attributes; elem->namespaces = namespaces; + elem->line = line; std::vector ::iterator iter; for (iter = children.begin(); iter != children.end() ; iter++) @@ -568,10 +1370,24 @@ String Parser::trim(const String &s) return res; } -void Parser::getLineAndColumn(long pos, long *lineNr, long *colNr) + +int Parser::countLines(int begin, int end) +{ + int count = 0; + for (int i=begin ; i = parselen) return -1; @@ -640,7 +1456,7 @@ String Parser::encode(const String &str) } -int Parser::match(long p0, const char *text) +int Parser::match(int p0, const char *text) { int p = p0; while (*text) @@ -654,7 +1470,7 @@ int Parser::match(long p0, const char *text) -int Parser::skipwhite(long p) +int Parser::skipwhite(int p) { while (p line = lineNr + countLines(p0, p); n->parent = par; par->addChild(n); @@ -929,7 +1751,7 @@ int Parser::parseElement(int p0, Element *par,int depth) //# CHILD ELEMENT if (ch == '<') { - p2 = parseElement(p, n, depth+1); + p2 = parseElement(p, n, lineNr + countLines(p0, p)); if (p2 == p) { /* @@ -1021,7 +1843,7 @@ Element *Parser::parse(XMLCh *buf,int pos,int len) Element *rootNode = new Element("root"); pos = parseVersion(pos); pos = parseDoctype(pos); - pos = parseElement(pos, rootNode, 0); + pos = parseElement(pos, rootNode, 1); return rootNode; } @@ -1088,8 +1910,6 @@ Element *Parser::parseFile(const String &fileName) return n; } - - //######################################################################## //######################################################################## //## E N D X M L @@ -1097,6 +1917,10 @@ Element *Parser::parseFile(const String &fileName) //######################################################################## + + + + //######################################################################## //######################################################################## //## U R I @@ -1542,6 +2366,7 @@ URI URI::resolve(const URI &other) const } + /** * This follows the Java URI algorithm: * 1. All "." segments are removed. @@ -1660,6 +2485,7 @@ void URI::trace(const char *fmt, ...) + //######################################################################### //# P A R S I N G //######################################################################### @@ -1868,7 +2694,8 @@ int URI::parse(int p0) bool URI::parse(const String &str) { - + init(); + parselen = str.size(); String tmp; @@ -1912,8 +2739,131 @@ bool URI::parse(const String &str) //######################################################################## //######################################################################## - - +//######################################################################## +//# F I L E S E T +//######################################################################## +/** + * This is the descriptor for a item + */ +class FileSet +{ +public: + + /** + * + */ + FileSet() + {} + + /** + * + */ + FileSet(const FileSet &other) + { assign(other); } + + /** + * + */ + FileSet &operator=(const FileSet &other) + { assign(other); return *this; } + + /** + * + */ + virtual ~FileSet() + {} + + /** + * + */ + String getDirectory() + { return directory; } + + /** + * + */ + void setDirectory(const String &val) + { directory = val; } + + /** + * + */ + void setFiles(const std::vector &val) + { files = val; } + + /** + * + */ + std::vector getFiles() + { return files; } + + /** + * + */ + void setIncludes(const std::vector &val) + { includes = val; } + + /** + * + */ + std::vector getIncludes() + { return includes; } + + /** + * + */ + void setExcludes(const std::vector &val) + { excludes = val; } + + /** + * + */ + std::vector getExcludes() + { return excludes; } + + /** + * + */ + unsigned int size() + { return files.size(); } + + /** + * + */ + String operator[](int index) + { return files[index]; } + + /** + * + */ + void clear() + { + directory = ""; + files.clear(); + includes.clear(); + excludes.clear(); + } + + +private: + + void assign(const FileSet &other) + { + directory = other.directory; + files = other.files; + includes = other.includes; + excludes = other.excludes; + } + + String directory; + std::vector files; + std::vector includes; + std::vector excludes; +}; + + + + //######################################################################## //# M A K E B A S E //######################################################################## @@ -1923,13 +2873,14 @@ bool URI::parse(const String &str) class MakeBase { public: + MakeBase() - {} + { line = 0; } virtual ~MakeBase() {} /** - * Return the URI of the file associated with this object + * Return the URI of the file associated with this object */ URI getURI() { return uri; } @@ -1954,11 +2905,23 @@ public: * Get an element value, performing substitutions if necessary */ bool getValue(Element *elem, String &result); + + /** + * Set the current line number in the file + */ + void setLine(int val) + { line = val; } + + /** + * Get the current line number in the file + */ + int getLine() + { return line; } protected: /** - * The path to the file associated with this object + * The path to the file associated with this object */ URI uri; @@ -1978,6 +2941,11 @@ protected: */ void trace(char *fmt, ...); + /** + * Check if a given string matches a given regex pattern + */ + bool regexMatch(const String &str, const String &pattern); + /** * */ @@ -1988,7 +2956,17 @@ protected: * in delimiters. Null-length substrings are ignored */ std::vector tokenize(const String &val, - const String &delimiters); + const String &delimiters); + + /** + * replace runs of whitespace with a space + */ + String strip(const String &s); + + /** + * remove leading whitespace from each line + */ + String leftJustify(const String &s); /** * remove leading and trailing whitespace from string @@ -2004,45 +2982,47 @@ protected: /** * Execute a shell command. Outbuf is a ref to a string * to catch the result. - */ + */ bool executeCommand(const String &call, - const String &inbuf, - String &outbuf, - String &errbuf); + const String &inbuf, + String &outbuf, + String &errbuf); /** * List all directories in a given base and starting directory * It is usually called like: - * bool ret = listDirectories("src", "", result); - */ + * bool ret = listDirectories("src", "", result); + */ bool listDirectories(const String &baseName, const String &dirname, std::vector &res); /** - * Find all files in the named directory whose short names (no path) match - * the given regex pattern - */ + * Find all files in the named directory + */ bool listFiles(const String &baseName, const String &dirname, - std::vector &excludes, - std::vector &res); + std::vector &result); + + /** + * Perform a listing for a fileset + */ + bool listFiles(MakeBase &propRef, FileSet &fileSet); /** * Parse a */ - bool getPatternSet(Element *elem, + bool parsePatternSet(Element *elem, MakeBase &propRef, - std::vector &includes, - std::vector &excludes); + std::vector &includes, + std::vector &excludes); /** * Parse a entry, and determine which files * should be included */ - bool getFileSet(Element *elem, + bool parseFileSet(Element *elem, MakeBase &propRef, - String &dir, - std::vector &result); + FileSet &fileSet); /** * Return this object's property list @@ -2068,13 +3048,13 @@ protected: /** * Turn 'true' and 'false' into boolean values - */ + */ bool getBool(const String &str, bool &val); /** * Create a directory, making intermediate dirs * if necessary - */ + */ bool createDirectory(const String &dirname); /** @@ -2087,6 +3067,16 @@ protected: */ bool copyFile(const String &srcFile, const String &destFile); + /** + * Tests if the file exists and is a regular file + */ + bool isRegularFile(const String &fileName); + + /** + * Tests if the file exists and is a directory + */ + bool isDirectory(const String &fileName); + /** * Tests is the modification date of fileA is newer than fileB */ @@ -2096,9 +3086,10 @@ private: /** * replace variable refs like ${a} with their values - */ + */ bool getSubstitutions(const String &s, String &result); + int line; }; @@ -2113,7 +3104,7 @@ void MakeBase::error(char *fmt, ...) { va_list args; va_start(args,fmt); - fprintf(stderr, "Make error: "); + fprintf(stderr, "Make error line %d: ", line); vfprintf(stderr, fmt, args); fprintf(stderr, "\n"); va_end(args) ; @@ -2162,6 +3153,40 @@ void MakeBase::trace(char *fmt, ...) } + +/** + * Check if a given string matches a given regex pattern + */ +bool MakeBase::regexMatch(const String &str, const String &pattern) +{ + const TRexChar *terror = NULL; + const TRexChar *cpat = pattern.c_str(); + TRex *expr = trex_compile(cpat, &terror); + if (!expr) + { + if (!terror) + terror = "undefined"; + error("compilation error [%s]!\n", terror); + return false; + } + + bool ret = true; + + const TRexChar *cstr = str.c_str(); + if (trex_match(expr, cstr)) + { + ret = true; + } + else + { + ret = false; + } + + trex_free(expr); + + return ret; +} + /** * Return the suffix, if any, of a file name */ @@ -2223,6 +3248,78 @@ std::vector MakeBase::tokenize(const String &str, +/** + * replace runs of whitespace with a single space + */ +String MakeBase::strip(const String &s) +{ + int len = s.size(); + String stripped; + for (int i = 0 ; i 0 && !WriteFile(stdinWrite, inbuf.c_str(), inbuf.size(), &bytesWritten, NULL)) { error("executeCommand: could not write to pipe"); - return false; - } + return false; + } if (!CloseHandle(stdinWrite)) - { + { error("executeCommand: could not close write pipe"); - return false; - } + return false; + } if (!CloseHandle(stdoutWrite)) - { + { error("executeCommand: could not close read pipe"); - return false; - } + return false; + } if (!CloseHandle(stderrWrite)) - { + { error("executeCommand: could not close read pipe"); - return false; - } - while (true) + return false; + } + + bool lastLoop = false; + while (true) { - //trace("## stderr"); DWORD avail; - if (!PeekNamedPipe(stderrRead, NULL, 0, NULL, &avail, NULL)) - break; + DWORD bytesRead; + char readBuf[4096]; + + //trace("## stderr"); + PeekNamedPipe(stderrRead, NULL, 0, NULL, &avail, NULL); if (avail > 0) { - DWORD bytesRead = 0; - char readBuf[1025]; - if (avail>1024) avail = 1024; - if (!ReadFile(stderrRead, readBuf, avail, &bytesRead, NULL) - || bytesRead == 0) + bytesRead = 0; + if (avail>4096) avail = 4096; + ReadFile(stderrRead, readBuf, avail, &bytesRead, NULL); + if (bytesRead > 0) { - break; + for (unsigned int i=0 ; i 0) { - DWORD bytesRead = 0; - char readBuf[1025]; - if (avail>1024) avail = 1024; - if (!ReadFile(stdoutRead, readBuf, avail, &bytesRead, NULL) - || bytesRead==0) + bytesRead = 0; + if (avail>4096) avail = 4096; + ReadFile(stdoutRead, readBuf, avail, &bytesRead, NULL); + if (bytesRead > 0) { - break; + for (unsigned int i=0 ; i &excludes, std::vector &res) { String fullDir = baseDir; @@ -2582,6 +3690,12 @@ bool MakeBase::listFiles(const String &baseDir, std::vector subdirs; DIR *dir = opendir(dirNative.c_str()); + if (!dir) + { + error("Could not open directory %s : %s", + dirNative.c_str(), strerror(errno)); + return false; + } while (true) { struct dirent *de = readdir(dir); @@ -2603,34 +3717,22 @@ bool MakeBase::listFiles(const String &baseDir, fullChild.append("/"); fullChild.append(childName); - if (std::find(excludes.begin(), excludes.end(), childName) - != excludes.end()) - { - //trace("EXCLUDED:%s", childName.c_str()); - continue; - } - - struct stat finfo; - String nativeName = getNativePath(fullChild); - if (stat(nativeName.c_str(), &finfo)<0) - { - error("cannot stat file:%s", childName.c_str()); - return false; - } - else if (S_ISDIR(finfo.st_mode)) + if (isDirectory(fullChild)) { //trace("directory: %s", childName.c_str()); - if (!listFiles(baseDir, childName, excludes, res)) + if (!listFiles(baseDir, childName, res)) return false; + continue; } - else if (!S_ISREG(finfo.st_mode)) - { - trace("not regular: %s", childName.c_str()); - } - else + else if (!isRegularFile(fullChild)) { - res.push_back(childName); + error("unknown file:%s", childName.c_str()); + return false; } + + //all done! + res.push_back(childName); + } closedir(dir); @@ -2638,107 +3740,145 @@ bool MakeBase::listFiles(const String &baseDir, } +bool MakeBase::listFiles(MakeBase &propRef, FileSet &fileSet) +{ + String baseDir = propRef.resolve(fileSet.getDirectory()); + std::vector fileList; + if (!listFiles(baseDir, "", fileList)) + return false; + std::vector includes = fileSet.getIncludes(); + std::vector excludes = fileSet.getExcludes(); + std::vector incs; + std::vector ::iterator iter; + std::sort(fileList.begin(), fileList.end()); - - -bool MakeBase::getSubstitutions(const String &str, String &result) -{ - String s = trim(str); - int len = (int)s.size(); - String val; - for (int i=0 ; i , then add files to the output + //in the order of the include list + if (includes.size()==0) + incs = fileList; + else { - char ch = s[i]; - if (ch == '$' && s[i+1] == '{') - { - String varname; - int j = i+2; - for ( ; j ::iterator iter; - iter = properties.find(trim(varname)); - if (iter != properties.end()) - { - val.append(iter->second); - } - else - { - error("property ${%s} not found", varname.c_str()); - return false; - } - break; - } - else - { - varname.push_back(ch); - } - } - i = j; - } - else - { - val.push_back(ch); - } - } - result = val; + for (iter = includes.begin() ; iter != includes.end() ; iter++) + { + String pattern = *iter; + std::vector ::iterator siter; + for (siter = fileList.begin() ; siter != fileList.end() ; siter++) + { + String s = *siter; + if (regexMatch(s, pattern)) + { + //trace("INCLUDED:%s", s.c_str()); + incs.push_back(s); + } + } + } + } + + //Now trim off the + std::vector res; + for (iter = incs.begin() ; iter != incs.end() ; iter++) + { + String s = *iter; + bool skipme = false; + std::vector ::iterator siter; + for (siter = excludes.begin() ; siter != excludes.end() ; siter++) + { + String pattern = *siter; + if (regexMatch(s, pattern)) + { + //trace("EXCLUDED:%s", s.c_str()); + skipme = true; + break; + } + } + if (!skipme) + res.push_back(s); + } + + fileSet.setFiles(res); + return true; } -bool MakeBase::getAttribute(Element *elem, const String &name, - String &result) -{ - String s = elem->getAttribute(name); - return getSubstitutions(s, result); -} -bool MakeBase::getValue(Element *elem, String &result) + +bool MakeBase::getSubstitutions(const String &str, String &result) { - String s = elem->getValue(); - int len = s.size(); - //Replace all runs of whitespace with a single space - String stripped; - for (int i = 0 ; i ::iterator iter; + iter = properties.find(trim(varname)); + if (iter != properties.end()) + { + val.append(iter->second); + } + else + { + error("property ${%s} not found", varname.c_str()); + return false; + } break; } + else + { + varname.push_back(ch); + } } + i = j; } else { - stripped.push_back(ch); + val.push_back(ch); } } - return getSubstitutions(stripped, result); + result = val; + return true; +} + + +bool MakeBase::getAttribute(Element *elem, const String &name, + String &result) +{ + String s = elem->getAttribute(name); + return getSubstitutions(s, result); +} + + +bool MakeBase::getValue(Element *elem, String &result) +{ + String s = elem->getValue(); + //Replace all runs of whitespace with a single space + return getSubstitutions(s, result); } /** * Turn 'true' and 'false' into boolean values - */ + */ bool MakeBase::getBool(const String &str, bool &val) { if (str == "true") @@ -2759,11 +3899,11 @@ bool MakeBase::getBool(const String &str, bool &val) /** * Parse a entry */ -bool MakeBase::getPatternSet(Element *elem, +bool MakeBase::parsePatternSet(Element *elem, MakeBase &propRef, - std::vector &includes, - std::vector &excludes - ) + std::vector &includes, + std::vector &excludes + ) { std::vector children = elem->getChildren(); for (unsigned int i=0 ; i entry, and determine which files * should be included */ -bool MakeBase::getFileSet(Element *elem, +bool MakeBase::parseFileSet(Element *elem, MakeBase &propRef, - String &dir, - std::vector &result) + FileSet &fileSet) { String name = elem->getName(); if (name != "fileset") @@ -2815,7 +3954,7 @@ bool MakeBase::getFileSet(Element *elem, std::vector excludes; //A fileset has one implied patternset - if (!getPatternSet(elem, propRef, includes, excludes)) + if (!parsePatternSet(elem, propRef, includes, excludes)) { return false; } @@ -2827,43 +3966,43 @@ bool MakeBase::getFileSet(Element *elem, String tagName = child->getName(); if (tagName == "patternset") { - if (!getPatternSet(child, propRef, includes, excludes)) + if (!parsePatternSet(child, propRef, includes, excludes)) { return false; } } } + String dir; //Now do the stuff //Get the base directory for reading file names if (!propRef.getAttribute(elem, "dir", dir)) return false; + fileSet.setDirectory(dir); + fileSet.setIncludes(includes); + fileSet.setExcludes(excludes); + + /* std::vector fileList; if (dir.size() > 0) { String baseDir = propRef.resolve(dir); - if (!listFiles(baseDir, "", excludes, fileList)) - return false; - } - - std::vector ::iterator iter; - for (iter=includes.begin() ; iter!=includes.end() ; iter++) + if (!listFiles(baseDir, "", includes, excludes, fileList)) + return false; + } + std::sort(fileList.begin(), fileList.end()); + result = fileList; + */ + + + /* + for (unsigned int i=0 ; i 0 && pos != dirname.npos) { String subpath = dirname.substr(0, pos); + //A letter root (c:) ? if (!createDirectory(subpath)) return false; } //## 3: now make +#ifdef __WIN32__ if (mkdir(cnative)<0) +#else + if (mkdir(cnative, S_IRWXU | S_IRWXG | S_IRWXO)<0) +#endif { - error("cannot make directory %s", cnative); + error("cannot make directory '%s' : %s", + cnative, strerror(errno)); return false; } @@ -2961,14 +4110,14 @@ bool MakeBase::removeDirectory(const String &dirName) else if (S_ISDIR(finfo.st_mode)) { //trace("DEL dir: %s", childName.c_str()); - if (!removeDirectory(childName)) - { - return false; - } + if (!removeDirectory(childName)) + { + return false; + } } else if (!S_ISREG(finfo.st_mode)) { - trace("not regular: %s", cnative); + //trace("not regular: %s", cnative); } else { @@ -2976,9 +4125,9 @@ bool MakeBase::removeDirectory(const String &dirName) if (remove(cnative)<0) { error("error deleting %s : %s", - cnative, strerror(errno)); - return false; - } + cnative, strerror(errno)); + return false; + } } } closedir(dir); @@ -3008,7 +4157,7 @@ bool MakeBase::copyFile(const String &srcFile, const String &destFile) if (stat(srcNative.c_str(), &srcinfo)<0) { error("source file %s for copy does not exist", - srcNative.c_str()); + srcNative.c_str()); return false; } @@ -3030,6 +4179,8 @@ bool MakeBase::copyFile(const String &srcFile, const String &destFile) } //# 3 do the data copy +#ifndef __WIN32__ + FILE *srcf = fopen(srcNative.c_str(), "rb"); if (!srcf) { @@ -3054,7 +4205,59 @@ bool MakeBase::copyFile(const String &srcFile, const String &destFile) fclose(destf); fclose(srcf); +#else + + if (!CopyFile(srcNative.c_str(), destNative.c_str(), false)) + { + error("copyFile from %s to %s failed", + srcNative.c_str(), destNative.c_str()); + return false; + } + +#endif /* __WIN32__ */ + + + return true; +} + + + +/** + * Tests if the file exists and is a regular file + */ +bool MakeBase::isRegularFile(const String &fileName) +{ + String native = getNativePath(fileName); + struct stat finfo; + + //Exists? + if (stat(native.c_str(), &finfo)<0) + return false; + + + //check the file mode + if (!S_ISREG(finfo.st_mode)) + return false; + + return true; +} + +/** + * Tests if the file exists and is a directory + */ +bool MakeBase::isDirectory(const String &fileName) +{ + String native = getNativePath(fileName); + struct stat finfo; + + //Exists? + if (stat(native.c_str(), &finfo)<0) + return false; + + //check the file mode + if (!S_ISDIR(finfo.st_mode)) + return false; return true; } @@ -3072,22 +4275,22 @@ bool MakeBase::isNewerThan(const String &fileA, const String &fileB) //IF source does not exist, NOT newer if (stat(nativeA.c_str(), &infoA)<0) { - return false; - } + return false; + } String nativeB = getNativePath(fileB); struct stat infoB; //IF dest does not exist, YES, newer if (stat(nativeB.c_str(), &infoB)<0) { - return true; - } + return true; + } //check the actual times if (infoA.st_mtime > infoB.st_mtime) { - return true; - } + return true; + } return false; } @@ -3503,7 +4706,7 @@ bool PkgConfig::parse(const String &buf) void PkgConfig::dumpAttrs() { - trace("### PkgConfig attributes for %s", fileName.c_str()); + //trace("### PkgConfig attributes for %s", fileName.c_str()); std::map ::iterator iter; for (iter=attrs.begin() ; iter!=attrs.end() ; iter++) { @@ -3532,7 +4735,7 @@ bool PkgConfig::readFile(const String &fileNameArg) } fclose(f); - trace("####### File:\n%s", buf.c_str()); + //trace("####### File:\n%s", buf.c_str()); if (!parse(buf)) { return false; @@ -3572,23 +4775,23 @@ public: * Constructor */ FileRec() - {init(); type = UNKNOWN;} + { init(); type = UNKNOWN; } /** * Copy constructor */ FileRec(const FileRec &other) - {init(); assign(other);} + { init(); assign(other); } /** * Constructor */ FileRec(int typeVal) - {init(); type = typeVal;} + { init(); type = typeVal; } /** * Assignment operator */ FileRec &operator=(const FileRec &other) - {init(); assign(other); return *this;} + { init(); assign(other); return *this; } /** @@ -3710,7 +4913,7 @@ private: path = other.path; name = other.name; suffix = other.suffix; - files = other.files; + files = other.files; //avoid recursion } }; @@ -3724,19 +4927,19 @@ public: * Constructor */ DepTool() - {init();} + { init(); } /** * Copy constructor */ DepTool(const DepTool &other) - {init(); assign(other);} + { init(); assign(other); } /** * Assignment operator */ DepTool &operator=(const DepTool &other) - {init(); assign(other); return *this;} + { init(); assign(other); return *this; } /** @@ -3804,7 +5007,8 @@ public: /** * Load a dependency file, generating one if necessary */ - std::vector getDepFile(const String &fileName); + std::vector getDepFile(const String &fileName, + bool forceRefresh); /** * Save a dependency file @@ -3856,9 +5060,7 @@ private: /** * */ - bool processDependency(FileRec *ofile, - FileRec *include, - int depth); + bool processDependency(FileRec *ofile, FileRec *include); /** * @@ -3877,8 +5079,7 @@ private: /** * A list of all files which will be processed for - * dependencies. This is the only list that has the actual - * records. All other lists have pointers to these records. + * dependencies. */ std::map allFiles; @@ -3886,11 +5087,13 @@ private: * The list of .o files, and the * dependencies upon them. */ - std::map depFiles; + std::map oFiles; int depFileSize; char *depFileBuf; - + + static const int readBufSize = 8192; + char readBuf[8193];//byte larger }; @@ -3909,13 +5112,15 @@ void DepTool::init() fileList.clear(); directories.clear(); - //clear refs - depFiles.clear(); - //clear records + //clear output file list std::map ::iterator iter; - for (iter=allFiles.begin() ; iter!=allFiles.end() ; iter++) - delete iter->second; + for (iter=oFiles.begin(); iter!=oFiles.end() ; iter++) + delete iter->second; + oFiles.clear(); + //allFiles actually contains the master copies. delete them + for (iter= allFiles.begin(); iter!=allFiles.end() ; iter++) + delete iter->second; allFiles.clear(); } @@ -3975,7 +5180,7 @@ bool DepTool::createFileList() String sfx; parseName(fileName, path, basename, sfx); if (sfx == "cpp" || sfx == "c" || sfx == "cxx" || - sfx == "cc" || sfx == "CC") + sfx == "cc" || sfx == "CC") { FileRec *fe = new FileRec(FileRec::CFILE); fe->path = path; @@ -4080,7 +5285,8 @@ bool DepTool::sequ(int pos, char *key) */ bool DepTool::addIncludeFile(FileRec *frec, const String &iname) { - + //# if the name is an exact match to a path name + //# in allFiles, like "myinc.h" std::map ::iterator iter = allFiles.find(iname); if (iter != allFiles.end()) //already exists @@ -4093,6 +5299,7 @@ bool DepTool::addIncludeFile(FileRec *frec, const String &iname) } else { + //## Ok, it was not found directly //look in other dirs std::vector ::iterator diter; for (diter=directories.begin() ; @@ -4101,12 +5308,17 @@ bool DepTool::addIncludeFile(FileRec *frec, const String &iname) String dfname = *diter; dfname.append("/"); dfname.append(iname); - iter = allFiles.find(dfname); + URI fullPathURI(dfname); //normalize path name + String fullPath = fullPathURI.getPath(); + if (fullPath[0] == '/') + fullPath = fullPath.substr(1); + //trace("Normalized %s to %s", dfname.c_str(), fullPath.c_str()); + iter = allFiles.find(fullPath); if (iter != allFiles.end()) { FileRec *other = iter->second; //trace("other: '%s'", iname.c_str()); - frec->files[dfname] = other; + frec->files[fullPath] = other; return true; } } @@ -4138,12 +5350,11 @@ bool DepTool::scanFile(const String &fname, FileRec *frec) return false; } String buf; - while (true) + while (!feof(f)) { - int ch = fgetc(f); - if (ch < 0) - break; - buf.push_back((char)ch); + int len = fread(readBuf, 1, readBufSize, f); + readBuf[len] = '\0'; + buf.append(readBuf); } fclose(f); @@ -4214,9 +5425,7 @@ bool DepTool::scanFile(const String &fname, FileRec *frec) * Recursively check include lists to find all files in allFiles to which * a given file is dependent. */ -bool DepTool::processDependency(FileRec *ofile, - FileRec *include, - int depth) +bool DepTool::processDependency(FileRec *ofile, FileRec *include) { std::map ::iterator iter; for (iter=include->files.begin() ; iter!=include->files.end() ; iter++) @@ -4230,7 +5439,7 @@ bool DepTool::processDependency(FileRec *ofile, FileRec *child = iter->second; ofile->files[fname] = child; - processDependency(ofile, child, depth+1); + processDependency(ofile, child); } @@ -4263,23 +5472,23 @@ bool DepTool::generateDependencies() FileRec *include = iter->second; if (include->type == FileRec::CFILE) { - String cFileName = iter->first; - FileRec *ofile = new FileRec(FileRec::OFILE); - ofile->path = include->path; - ofile->baseName = include->baseName; - ofile->suffix = include->suffix; - String fname = include->path; + String cFileName = iter->first; + FileRec *ofile = new FileRec(FileRec::OFILE); + ofile->path = include->path; + ofile->baseName = include->baseName; + ofile->suffix = include->suffix; + String fname = include->path; if (fname.size()>0) fname.append("/"); fname.append(include->baseName); fname.append(".o"); - depFiles[fname] = ofile; + oFiles[fname] = ofile; //add the .c file first? no, don't //ofile->files[cFileName] = include; //trace("ofile:%s", fname.c_str()); - processDependency(ofile, include, 0); + processDependency(ofile, include); } } @@ -4327,13 +5536,13 @@ bool DepTool::saveDepFile(const String &fileName) fprintf(f, " \n\n", sourceDir.c_str()); std::map ::iterator iter; - for (iter=depFiles.begin() ; iter!=depFiles.end() ; iter++) + for (iter=oFiles.begin() ; iter!=oFiles.end() ; iter++) { FileRec *frec = iter->second; if (frec->type == FileRec::OFILE) { fprintf(f, "