sourcehook/generate/shworker/shworker.cpp
changeset 195 5f80856d32d8
parent 111 aec58c99dafc
child 196 25d98645f668
     1.1 --- a/sourcehook/generate/shworker/shworker.cpp	Fri May 05 10:49:55 2006 +0000
     1.2 +++ b/sourcehook/generate/shworker/shworker.cpp	Sat May 13 16:59:57 2006 +0000
     1.3 @@ -2,30 +2,99 @@
     1.4  // Inspired by "Hopter" that comes with FastDelegate (http://www.codeproject.com/cpp/FastDelegate.asp)
     1.5  // Much more powerful (and ugly) though
     1.6  
     1.7 -// Action: iter
     1.8 -// @VARARGS@						begins session
     1.9 -// @..@								is replaced by .. if num!=0 and removed if num=0. Any
    1.10 -//									occurences of %% are replaced by the current iter
    1.11 -// @$@								is replaced by the current iter
    1.12 -// @ENDARGS@						ends session
    1.13 +/*
    1.14 +
    1.15 +	INPUT FILE DIRECTIVES
    1.16 +
    1.17 +	$a is the first additional argument, %b the second, ...
    1.18 +
    1.19 +	---
    1.20 +	ITERATION
    1.21 +
    1.22 +	@[variable,min,max:code|separator@]
    1.23 +
    1.24 +	variable:	this will be replaced in code by its current value.
    1.25 +				vars are always $ and a number.
    1.26 +	min:		first value to be used for variable
    1.27 +	max:		last value to be used for variable
    1.28 +	code:		the code that will be inserted on each iteration.
    1.29 +	separator:	optional. this will be inserted between iterations.
    1.30 +				If you don't use a separator, you may leave out the |
    1.31 +	IMPORTANT:	iterations will only be performed if max >= min
    1.32 +
    1.33 +	--- ARITHMETIC EXPRESSION
    1.34 +
    1.35 +	@(expr)
    1.36 +
    1.37 +	expr may contain:
    1.38 +		variables
    1.39 +		constants
    1.40 +		operators (currently only + and * are supported)
    1.41 +
    1.42 +	--- CONDITION
    1.43 +
    1.44 +	@[expr operator expr:code@]
    1.45 +
    1.46 +	Example: @[$1!=0:hello@]
    1.47 +
    1.48 +	Currently only != and == are supported operators.
    1.49 +
    1.50 +
    1.51 +	Yes, error handling in here is weird, some stuff uses return values, other code uses exceptions.
    1.52 +*/
    1.53  
    1.54  #include <iostream>
    1.55  #include <fstream>
    1.56  #include <string>
    1.57  #include <sstream>
    1.58 -#include <list>
    1.59 +#include <algorithm>
    1.60 +#include <stack>
    1.61 +#include <map>
    1.62  #include "stdio.h"
    1.63  
    1.64  #ifdef __linux__
    1.65  # define stricmp strcasecmp
    1.66  #endif
    1.67  
    1.68 +// Ensure that the template version is being used!
    1.69 +#ifdef min
    1.70 +#undef min
    1.71 +#endif
    1.72 +
    1.73  using namespace std;
    1.74  
    1.75 -extern int hopter(int numargs, const char *filenamein, const char *filenameout);
    1.76 +extern int action_hopter(int numargs, const char *filenamein, const char *filenameout);
    1.77 +
    1.78 +typedef map<int,int> varmap;
    1.79 +
    1.80 +struct MyError
    1.81 +{
    1.82 +	const char *m_desc;
    1.83 +
    1.84 +	MyError(const char *desc) : m_desc(desc)
    1.85 +	{
    1.86 +	}
    1.87  
    1.88 +	void Print()
    1.89 +	{
    1.90 +		cout << m_desc << endl;
    1.91 +	}
    1.92 +};
    1.93  
    1.94 -void TrimString(std::string &str)
    1.95 +struct SyntaxError : MyError
    1.96 +{
    1.97 +	SyntaxError() : MyError("Syntax error in expression")
    1.98 +	{
    1.99 +	}
   1.100 +};
   1.101 +struct OtherError : MyError
   1.102 +{
   1.103 +	OtherError() : MyError("WTF")
   1.104 +	{
   1.105 +	}
   1.106 +};
   1.107 +
   1.108 +void trim_string(std::string &str)
   1.109  {
   1.110  	size_t first = str.find_first_not_of(" \t\v\n\r");
   1.111  	if (first == std::string::npos)
   1.112 @@ -49,9 +118,10 @@
   1.113  	str = str.substr(first, last - first);
   1.114  }
   1.115  
   1.116 +// unused
   1.117  bool ExtractToken(std::string &strin, std::string &strout)
   1.118  {
   1.119 -	TrimString(strin);
   1.120 +	trim_string(strin);
   1.121  	if (strin.begin() == strin.end())
   1.122  	{
   1.123  		strout.clear();
   1.124 @@ -94,28 +164,540 @@
   1.125  	return cnt;
   1.126  }
   1.127  
   1.128 -int main(int argc, char *argv[])
   1.129 +int DoReplace(string &str, const char *what, const char *with)
   1.130 +{
   1.131 +	int cnt=0;
   1.132 +	size_t where = str.find(what);
   1.133 +	size_t whatsize = strlen(what);
   1.134 +	while (where != string::npos)
   1.135 +	{
   1.136 +		str.replace(where, whatsize, with);
   1.137 +		++cnt;
   1.138 +		where = str.find(what, where);
   1.139 +	}
   1.140 +	return cnt;
   1.141 +}
   1.142 +
   1.143 +
   1.144 +class ExprParser
   1.145  {
   1.146 -	if (argc < 5)
   1.147 +	// grammar:
   1.148 +	/*
   1.149 +	expr -> expr + term			{ do_add }
   1.150 +	| expr - term			{ do_sub }
   1.151 +	| term
   1.152 +
   1.153 +	term -> term * factor		{ do_mul }
   1.154 +	| term / factor		{ do_div }
   1.155 +	| term % factor		{ do_mod }
   1.156 +
   1.157 +	factor -> (expr)
   1.158 +	| numeric constant	{ push }
   1.159 +
   1.160 +
   1.161 +	equivalent to:
   1.162 +
   1.163 +	expr -> term moreterms
   1.164 +	moreterms -> + term { do_add } moreterms
   1.165 +	moreterms -> - term { do_sub } moreterms
   1.166 +	moreterms -> epsilon
   1.167 +
   1.168 +	term -> factor morefactors
   1.169 +	morefactors -> * factor { do_mul } morefactors
   1.170 +	morefactors -> / factor { do_div } morefactors
   1.171 +	morefactors -> % factor { do_mod } morefactors
   1.172 +	morefactors -> epsilon
   1.173 +
   1.174 +	factor -> (expr)
   1.175 +	factor -> numeric constant { push }
   1.176 +
   1.177 +	*/
   1.178 +
   1.179 +	string::const_iterator m_begin;
   1.180 +	string::const_iterator m_end;
   1.181 +	string::const_iterator m_iter;
   1.182 +
   1.183 +	int m_lookahead;
   1.184 +	int m_tokenval;
   1.185 +
   1.186 +	stack<int> m_stack;
   1.187 +	static const int DONE = 256;
   1.188 +	static const int NUM = 257;
   1.189 +
   1.190 +	int lexan()
   1.191 +	{
   1.192 +		while (1)
   1.193 +		{
   1.194 +			if (m_iter == m_end)
   1.195 +				return DONE;
   1.196 +
   1.197 +			int t = *m_iter++;
   1.198 +
   1.199 +			if (t == ' ' || t == '\t')
   1.200 +				;			// Remove whitespace
   1.201 +			else if (isdigit(t))
   1.202 +			{
   1.203 +				--m_iter;
   1.204 +
   1.205 +				m_tokenval = 0;
   1.206 +				while (m_iter != m_end && isdigit(*m_iter))
   1.207 +				{
   1.208 +					m_tokenval *= 10;
   1.209 +					m_tokenval += *m_iter - '0';
   1.210 +					++m_iter;
   1.211 +				}
   1.212 +				return NUM;
   1.213 +			}
   1.214 +			else
   1.215 +				return t;
   1.216 +		}
   1.217 +	}
   1.218 +
   1.219 +	void match(int t)
   1.220  	{
   1.221 -		cout << "Usage:" << endl << "   shworker [iter/hopter] ..." << endl;
   1.222 -		cout << "   shworker iter [num-of-args] filename.in filename.out" << endl;
   1.223 -		cout << "   shworker hopter [num-of-args] filename.in filename.our" << endl;
   1.224 -		return 1;
   1.225 +		if (m_lookahead == t)
   1.226 +			m_lookahead = lexan();
   1.227 +		else
   1.228 +			throw SyntaxError();
   1.229 +	}
   1.230 +	void factor()
   1.231 +	{
   1.232 +		switch (m_lookahead)
   1.233 +		{
   1.234 +		case '(':
   1.235 +			match('('); expr(); match(')');
   1.236 +			break;
   1.237 +		case NUM:
   1.238 +			m_stack.push(m_tokenval); match(NUM);
   1.239 +			break;
   1.240 +		default:
   1.241 +			throw SyntaxError();
   1.242 +		}
   1.243 +	}
   1.244 +	void term()
   1.245 +	{
   1.246 +		factor();
   1.247 +		while (1)
   1.248 +		{
   1.249 +			switch (m_lookahead)
   1.250 +			{
   1.251 +			case '*':
   1.252 +				match('*'); factor(); do_mul();
   1.253 +				continue;
   1.254 +			case '/':
   1.255 +				match('/'); factor(); do_div();
   1.256 +				continue;
   1.257 +			case '%':
   1.258 +				match('%'); factor(); do_mod();
   1.259 +				continue;
   1.260 +			default:
   1.261 +				return;
   1.262 +			}
   1.263 +
   1.264 +		}
   1.265 +	}
   1.266 +
   1.267 +	void expr()
   1.268 +	{
   1.269 +		term();
   1.270 +		while (1)
   1.271 +		{
   1.272 +			switch (m_lookahead)
   1.273 +			{
   1.274 +			case '+':
   1.275 +				match('+'); term(); do_add();
   1.276 +				continue;
   1.277 +			case '-':
   1.278 +				match('-'); term(); do_sub();
   1.279 +				continue;
   1.280 +			default:
   1.281 +				return;
   1.282 +			}
   1.283 +		}
   1.284 +	}
   1.285 +
   1.286 +	void do_add()
   1.287 +	{
   1.288 +		int a2 = m_stack.top(); m_stack.pop();
   1.289 +		int a1 = m_stack.top(); m_stack.pop();
   1.290 +		m_stack.push(a1 + a2);
   1.291 +	}
   1.292 +
   1.293 +	void do_sub()
   1.294 +	{
   1.295 +		int a2 = m_stack.top(); m_stack.pop();
   1.296 +		int a1 = m_stack.top(); m_stack.pop();
   1.297 +		m_stack.push(a1 - a2);
   1.298 +	}
   1.299 +
   1.300 +	void do_mul()
   1.301 +	{
   1.302 +		int a2 = m_stack.top(); m_stack.pop();
   1.303 +		int a1 = m_stack.top(); m_stack.pop();
   1.304 +		m_stack.push(a1 * a2);
   1.305 +	}
   1.306 +
   1.307 +	void do_div()
   1.308 +	{
   1.309 +		int a2 = m_stack.top(); m_stack.pop();
   1.310 +		int a1 = m_stack.top(); m_stack.pop();
   1.311 +		m_stack.push(a1 / a2);
   1.312 +	}
   1.313 +
   1.314 +	void do_mod()
   1.315 +	{
   1.316 +		int a2 = m_stack.top(); m_stack.pop();
   1.317 +		int a1 = m_stack.top(); m_stack.pop();
   1.318 +		m_stack.push(a1 % a2);
   1.319 +	}
   1.320 +
   1.321 +public:
   1.322 +	ExprParser(string::const_iterator begin, string::const_iterator end) :
   1.323 +	m_begin(begin), m_end(end), m_iter(begin)
   1.324 +	{
   1.325 +		m_lookahead = lexan();
   1.326 +		expr();
   1.327  	}
   1.328  
   1.329 -	const char *action = argv[1];
   1.330 -	int argsnum = atoi(argv[2]);
   1.331 -	const char *filenamein = argv[3];
   1.332 -	const char *filenameout = argv[4];
   1.333 +	operator int()
   1.334 +	{
   1.335 +		if (m_stack.size() != 1)
   1.336 +			throw OtherError();
   1.337 +
   1.338 +		return m_stack.top();
   1.339 +	}
   1.340 +};
   1.341 +
   1.342 +int parse_expr(string::const_iterator begin, string::const_iterator end)
   1.343 +{
   1.344 +	return ExprParser(begin, end);
   1.345 +}
   1.346 +
   1.347 +size_t find_first_directive(const string &buf, size_t begin=0)
   1.348 +{
   1.349 +	for (;;)
   1.350 +	{
   1.351 +		if (begin >= buf.size())
   1.352 +			return string::npos;
   1.353 +
   1.354 +		size_t firstdirpos = buf.find('@', begin);
   1.355 +		if (firstdirpos == string::npos)
   1.356 +			return firstdirpos;
   1.357 +
   1.358 +		if (buf.size() > firstdirpos+1)
   1.359 +		{
   1.360 +			if (buf[firstdirpos+1] == '[' || buf[firstdirpos+1] == '(')
   1.361 +				return firstdirpos;
   1.362 +		}
   1.363 +		begin = firstdirpos+1;
   1.364 +	}
   1.365 +}
   1.366 +
   1.367 +// buf begins with a section. Find its end!
   1.368 +size_t find_section_end(const string &buf)
   1.369 +{
   1.370 +	int starttype = buf[1];
   1.371 +	int endtype = (buf[1] == '(') ? ')' : ']';
   1.372 +
   1.373 +	int nestlevel = 0;
   1.374  
   1.375 -	if (stricmp(action, "hopter") == 0)
   1.376 +	if (starttype == '(')
   1.377 +	{
   1.378 +		for (string::const_iterator iter = buf.begin(); iter != buf.end(); ++iter)
   1.379 +		{
   1.380 +			if (*iter == starttype)
   1.381 +				++nestlevel;
   1.382 +			if (*iter == endtype)
   1.383 +			{
   1.384 +				if (--nestlevel == 0)
   1.385 +					return iter - buf.begin() + 1;
   1.386 +			}
   1.387 +		}
   1.388 +		return string::npos;
   1.389 +	}
   1.390 +	else if (starttype == '[')
   1.391  	{
   1.392 -		return hopter(argsnum, filenamein, filenameout);
   1.393 +		int lastchar = 0;
   1.394 +		for (string::const_iterator iter = buf.begin(); iter != buf.end(); ++iter)
   1.395 +		{
   1.396 +			if (lastchar == '@' && *iter == starttype)
   1.397 +				++nestlevel;
   1.398 +			if (lastchar == '@' && *iter == endtype)
   1.399 +			{
   1.400 +				if (--nestlevel == 0)
   1.401 +					return iter - buf.begin() + 1;
   1.402 +			}
   1.403 +			lastchar = *iter;
   1.404 +		}
   1.405 +		return string::npos;
   1.406 +	}
   1.407 +	
   1.408 +	return string::npos;
   1.409 +}
   1.410 +
   1.411 +// replaces variables and additional arguments
   1.412 +void replace_vars(string &buf, int argc, int *argv, const varmap &vars)
   1.413 +{
   1.414 +	char varname[] = "$ ";
   1.415 +	char value[32];
   1.416 +	
   1.417 +	for (int i = 0; i < argc; ++i)
   1.418 +	{
   1.419 +		varname[1] = 'a' + i;
   1.420 +		itoa(argv[i], value, 10);
   1.421 +		DoReplace(buf, varname, value);
   1.422  	}
   1.423  
   1.424 +	for (varmap::const_iterator iter = vars.begin(); iter != vars.end(); ++iter)
   1.425 +	{
   1.426 +		varname[1] = '0' + iter->first;
   1.427 +		itoa(iter->second, value, 10);
   1.428 +		DoReplace(buf, varname, value);
   1.429 +	}
   1.430 +}
   1.431 +
   1.432 +// do_input
   1.433 +// params:
   1.434 +//  argc:			number of additional arguments
   1.435 +//	argv:			additional arguments
   1.436 +//  outfile:		output file
   1.437 +//  buf:			string to be processed.  IMPORTANT: buf is modified!
   1.438 +//  curvars:		variables buffer.
   1.439 +// retval:
   1.440 +//  0 on success, non-zero on error
   1.441 +
   1.442 +int do_input(int argc, int *argv, ofstream &outfile, string &buf, varmap &curvars)
   1.443 +{
   1.444 +	for (;;)
   1.445 +	{
   1.446 +		// Find the next directive.
   1.447 +		size_t firstdirpos = find_first_directive(buf);
   1.448 +
   1.449 +		// Output everything that came before, and remove it from buf
   1.450 +		outfile << buf.substr(0, firstdirpos);
   1.451 +		if (firstdirpos == string::npos)
   1.452 +			return 0;
   1.453 +		buf = buf.substr(firstdirpos);
   1.454 +
   1.455 +		// Now find the matching end.
   1.456 +		size_t sectionend = find_section_end(buf);
   1.457 +		if (sectionend == string::npos)
   1.458 +		{
   1.459 +			cout << "ERROR: Section not closed!" << endl;
   1.460 +			return 1;
   1.461 +		}
   1.462 +
   1.463 +		// Place the section in its own buffer and remove it from the input string.
   1.464 +		string sect(buf.begin(), buf.begin() + sectionend);
   1.465 +		buf = buf.substr(sectionend);
   1.466 +
   1.467 +		// CASE1: Arithmetic expression
   1.468 +		if (sect[1] == '(')
   1.469 +		{
   1.470 +			replace_vars(sect, argc, argv, curvars);
   1.471 +			outfile << parse_expr(sect.begin()+1, sect.end());
   1.472 +		}
   1.473 +		else if (sect[1] == '[')
   1.474 +		{
   1.475 +			int is_iter = 0;	// 0 -> no; 1 -> maybe (only used in check); 2 -> yes
   1.476 +			char lastchar = 0;
   1.477 +			// This could be an iteration OR a conditional thing.
   1.478 +			// Pretty braindead check: iterations begin with a variable, then a comma.
   1.479 +			for (string::iterator iter = sect.begin() + 2; iter != sect.end(); ++iter)
   1.480 +			{
   1.481 +				if (*iter == ' ' || *iter == '\t')
   1.482 +					;
   1.483 +				else if (is_iter == 0 && lastchar == '$' && isdigit(*iter))
   1.484 +					is_iter = 1;
   1.485 +				else if (is_iter == 1 && *iter == ',')
   1.486 +				{
   1.487 +					is_iter = 2;
   1.488 +					break;
   1.489 +				}
   1.490 +				else if (*iter == '$')
   1.491 +					;
   1.492 +				else
   1.493 +					break;
   1.494 +				lastchar = *iter;
   1.495 +			}
   1.496 +			if (is_iter == 2)
   1.497 +			{
   1.498 +				// CASE2: iteration
   1.499 +				// Looks like: @[var,min,max:code|sep@]
   1.500 +				// Replace known variables / additional arguments
   1.501 +				replace_vars(sect, argc, argv, curvars);
   1.502 +
   1.503 +				// get the parts!
   1.504 +				string varname;
   1.505 +				int varnum;
   1.506 +				int expr_min;
   1.507 +				int expr_max;
   1.508 +
   1.509 +				// varname
   1.510 +				size_t comma = sect.find(',');
   1.511 +				if (comma == string::npos)
   1.512 +				{
   1.513 +					cout << "Invalid iteration syntax" << endl;
   1.514 +					return 1;
   1.515 +				}
   1.516 +				varname.assign(sect.begin() + 2, sect.begin() + comma);
   1.517 +				trim_string(varname);
   1.518 +				if (varname.size() != 2 || varname[0] != '$' || !isdigit(varname[1]))
   1.519 +				{
   1.520 +					cout << "Invalid variable name" << endl;
   1.521 +					return 1;
   1.522 +				}
   1.523 +				varnum = varname[1] - '0';
   1.524 +
   1.525 +				// min
   1.526 +				++comma;
   1.527 +				size_t nextcomma = sect.find(',', comma);
   1.528 +				if (nextcomma == string::npos)
   1.529 +				{
   1.530 +					cout << "Invalid iteration syntax" << endl;
   1.531 +					return 1;
   1.532 +				}
   1.533 +				expr_min = parse_expr(sect.begin() + comma, sect.begin() + nextcomma);
   1.534 +
   1.535 +				// max
   1.536 +				comma = nextcomma + 1;
   1.537 +				nextcomma = sect.find(':', comma);
   1.538 +				if (nextcomma == string::npos)
   1.539 +				{
   1.540 +					cout << "Invalid iteration syntax" << endl;
   1.541 +					return 1;
   1.542 +				}
   1.543 +
   1.544 +				expr_max = parse_expr(sect.begin() + comma, sect.begin() + nextcomma);
   1.545 +				
   1.546 +				// separator
   1.547 +				size_t sepbegin = sect.find('|');
   1.548 +				size_t sepend = string::npos;
   1.549 +				if (sepbegin != string::npos && sepbegin < nextcomma)
   1.550 +				{
   1.551 +					// There's a separator!
   1.552 +					++sepbegin;
   1.553 +					sepend = nextcomma;
   1.554 +				}
   1.555 +				else
   1.556 +					sepbegin = string::npos;
   1.557 +
   1.558 +
   1.559 +				++nextcomma; // nextcomma is now where code begins!
   1.560 +
   1.561 +				size_t codeend = sect.size() - 2;
   1.562 +
   1.563 +				// Check whether the var is already taken
   1.564 +				if (curvars.find(varnum) != curvars.end())
   1.565 +				{
   1.566 +					cout << "Variable $" << varnum << "already taken!" << endl;
   1.567 +					return 1;
   1.568 +				}
   1.569 +
   1.570 +				// Do iterations!!
   1.571 +				for (int i = expr_min; i <= expr_max; ++i)
   1.572 +				{
   1.573 +					curvars[varnum] = i;
   1.574 +
   1.575 +					string code(sect.begin() + nextcomma, sect.begin() + codeend);
   1.576 +					replace_vars(code, argc, argv, curvars);
   1.577 +
   1.578 +					// Feed it through the input routine (RECURSE!!!!!! YEAH!)
   1.579 +					do_input(argc, argv, outfile, code, curvars);
   1.580 +					
   1.581 +					// Add separator if required
   1.582 +					if (sepbegin != string::npos && i != expr_max)
   1.583 +					{
   1.584 +						do_input(argc, argv, outfile, 
   1.585 +							string(sect.begin() + sepbegin, sect.begin() + sepend), curvars);
   1.586 +					}
   1.587 +				}
   1.588 +				// Remove the var!
   1.589 +				curvars.erase(varnum);
   1.590 +			}
   1.591 +			else
   1.592 +			{
   1.593 +				// CASE3: conditional thing.
   1.594 +				// Looks like: @[expr1 operator expr2:code@]
   1.595 +				
   1.596 +				// Find the operator position
   1.597 +
   1.598 +				enum OP_TYPE
   1.599 +				{
   1.600 +					OP_EQ,
   1.601 +					OP_NEQ
   1.602 +				};
   1.603 +
   1.604 +				OP_TYPE op;
   1.605 +				size_t oppos = sect.find("==");
   1.606 +				if (oppos != string::npos)
   1.607 +					op = OP_EQ;
   1.608 +				else
   1.609 +				{
   1.610 +					oppos = sect.find("!=");
   1.611 +					if (oppos != string::npos)
   1.612 +						op = OP_NEQ;
   1.613 +					else
   1.614 +					{
   1.615 +						cout << "Conditional expression without operator!?" << endl;
   1.616 +						return 1;
   1.617 +					}
   1.618 +				}
   1.619 +				size_t colon = sect.find(':');
   1.620 +
   1.621 +				// Now we've got everything. Parse first expr:
   1.622 +				int expr1 = parse_expr(sect.begin() + 2, sect.begin() + oppos);
   1.623 +				int expr2 = parse_expr(sect.begin() + oppos + 2, sect.begin() + colon);
   1.624 +				if ((op == OP_EQ  && expr1 == expr2) ||
   1.625 +					(op == OP_NEQ && expr1 != expr2))
   1.626 +				{
   1.627 +					// Condition is true, process it!
   1.628 +					// The text may still contain arithmetic exprs or other cond. exprs
   1.629 +					// so send it through do_input
   1.630 +					do_input(argc, argv, outfile, sect.substr(colon+1, sect.size() - colon - 3), curvars);
   1.631 +				}
   1.632 +			}
   1.633 +		}
   1.634 +		else
   1.635 +		{
   1.636 +			cout << "WTF" << endl;
   1.637 +			return 1;
   1.638 +		}
   1.639 +	}
   1.640 +	
   1.641 +	return 0;
   1.642 +}
   1.643 +
   1.644 +
   1.645 +
   1.646 +// action_iter
   1.647 +// params:
   1.648 +//  filenamein:		input file name
   1.649 +//  filenameout:	output file name
   1.650 +//  argc:			number of additional arguments
   1.651 +//	argv:			additional arguments
   1.652 +// retval: 0 on success, non-zero on error
   1.653 +
   1.654 +// Convert additional arguments 
   1.655 +// Read whole input file to memory and open output file
   1.656 +// Pass to do_input()
   1.657 +int action_iter(const char *filenamein, const char *filenameout, int argc, const char *argv[])
   1.658 +{
   1.659 +	// Convert additional arguments
   1.660 +	const int MAX_ARGC = 10;
   1.661 +	int converted_argv[MAX_ARGC];
   1.662 +
   1.663 +	for (int i = 0; i < argc && i < MAX_ARGC; ++i)
   1.664 +		converted_argv[i] = atoi(argv[i]);
   1.665 +
   1.666 +	if (argc != i)
   1.667 +		cout << "WARNING: Not all additional arguments processed!" << endl;
   1.668 +
   1.669 +
   1.670 +	// Read whole input file to memory and open output file
   1.671  	ifstream fin(filenamein);
   1.672  	ofstream fout(filenameout);
   1.673 +
   1.674  	if (!fin)
   1.675  	{
   1.676  		cout << "Could not open file \"" << filenamein << "\"." << endl;
   1.677 @@ -126,122 +708,58 @@
   1.678  		cout << "Could not open file \"" << filenameout << "\"." << endl;
   1.679  		return 1;
   1.680  	}
   1.681 -
   1.682 -	if (stricmp(action, "iter") == 0)
   1.683 -	{
   1.684 -		// Generate versions of func macros for many parameters
   1.685 -		string str;
   1.686 -		string replacebuf;
   1.687 -		while (!fin.eof())
   1.688 -		{
   1.689 -			getline(fin, str);
   1.690 -			if (str == "@VARARGS@")
   1.691 -			{
   1.692 -				replacebuf.clear();
   1.693 -
   1.694 -				while (true)
   1.695 -				{
   1.696 -					getline(fin, str);
   1.697 -					if (str == "@ENDARGS@")
   1.698 -						break;
   1.699 +	string input_str(
   1.700 +		istreambuf_iterator<char> (fin.rdbuf()),
   1.701 +		istreambuf_iterator<char> ());
   1.702  
   1.703 -					replacebuf += str;
   1.704 -					replacebuf += "\n";
   1.705 -					if (fin.eof())
   1.706 -					{
   1.707 -						cout << "No matching @ENDARGS@ !" << endl;
   1.708 -						return 1;
   1.709 -					}
   1.710 -				}
   1.711 -
   1.712 -				for (int i = 0; i <= argsnum; ++i)
   1.713 -				{
   1.714 -					string cur = replacebuf;
   1.715 -					size_t where = cur.find('@');
   1.716 -
   1.717 -					while (where != string::npos)
   1.718 -					{
   1.719 -						size_t where2 = cur.find('@', where+1);
   1.720 -						if (where2 == string::npos)
   1.721 -						{
   1.722 -							cout << "No ending @";
   1.723 -							return 1;
   1.724 -						}
   1.725 -						string tmp = cur.substr(where + 1, where2 - where - 1);
   1.726 -						cur.erase(where, where2 - where + 1);
   1.727  
   1.728 -						if (tmp.size() != 0 && tmp[0] == '$')
   1.729 -						{
   1.730 -							int tmp_out = i;
   1.731 -							if (tmp.size() > 2)
   1.732 -							{
   1.733 -								if (tmp[1] == '+')
   1.734 -								{
   1.735 -									istringstream istr(tmp.substr(2));
   1.736 -									int tmp;
   1.737 -									istr >> tmp;
   1.738 -									tmp_out += tmp;
   1.739 -								}
   1.740 -								else if (tmp[1] == '*')
   1.741 -								{
   1.742 -									istringstream istr(tmp.substr(2));
   1.743 -									int tmp;
   1.744 -									istr >> tmp;
   1.745 -									tmp_out *= tmp;
   1.746 -								}
   1.747 -							}
   1.748 -							stringstream istr;
   1.749 -							istr << tmp_out;
   1.750 -							cur.insert(where, istr.str());
   1.751 -							where = cur.find('@', where);
   1.752 -							continue;
   1.753 -						}
   1.754 -
   1.755 -						// Find the |.. part, if any
   1.756 -						string tmpsep;
   1.757 -						size_t tmpsepbegin = tmp.rfind('|');
   1.758 -						if (tmpsepbegin != string::npos)
   1.759 -						{
   1.760 -							tmpsep = tmp.substr(tmpsepbegin + 1);
   1.761 -							tmp.erase(tmpsepbegin);
   1.762 -						}
   1.763 -
   1.764 -						size_t pos = where;
   1.765 -						if (tmp.find("%%") == string::npos)
   1.766 -						{
   1.767 -							if (i > 0)
   1.768 -							{
   1.769 -								cur.insert(pos, tmp);
   1.770 -								pos += tmp.size();
   1.771 -							}
   1.772 -						}
   1.773 -						else
   1.774 -						{
   1.775 -							for (int j = 1; j <= i; ++j)
   1.776 -							{
   1.777 -								stringstream istr;
   1.778 -								istr << j;
   1.779 -								string what = tmp;
   1.780 -								DoReplace(what, string("%%"), istr.str());
   1.781 -								cur.insert(pos, what);
   1.782 -								pos += what.size();
   1.783 -								if (j != i)
   1.784 -								{
   1.785 -									cur.insert(pos, tmpsep);
   1.786 -									pos += tmpsep.size();
   1.787 -								}
   1.788 -							}
   1.789 -						}
   1.790 -
   1.791 -						where = cur.find('@', where);
   1.792 -					}
   1.793 -					fout << cur;
   1.794 -				}
   1.795 -			}
   1.796 -			else
   1.797 -			{
   1.798 -				fout << str << endl;
   1.799 -			}
   1.800 -		}
   1.801 +	// Begin processing input
   1.802 +	varmap vars;
   1.803 +	try
   1.804 +	{
   1.805 +		return do_input(argc, converted_argv, fout, input_str, vars);
   1.806 +	}
   1.807 +	catch (MyError err)
   1.808 +	{
   1.809 +		err.Print();
   1.810 +		return 1;
   1.811  	}
   1.812  }
   1.813 +
   1.814 +// MAIN
   1.815 +//  Prints usage if required
   1.816 +//  Calls action_hopter OR action_iter
   1.817 +int main(int argc, const char **argv)
   1.818 +{
   1.819 +	if (argc < 4)
   1.820 +	{
   1.821 +		cout << "Usage:" << endl << "   shworker [iter/hopter] ..." << endl;
   1.822 +		cout << "   shworker iter filename.in filename.out [param1, param2, ...]" << endl;
   1.823 +		cout << "   shworker hopter filename.in filename.out [num-of-args]" << endl;
   1.824 +		return 1;
   1.825 +	}
   1.826 +
   1.827 +	const char *action = argv[1];
   1.828 +
   1.829 +	if (stricmp(action, "hopter") == 0)
   1.830 +	{
   1.831 +		const char *filenamein = argv[2];
   1.832 +		const char *filenameout = argv[3];
   1.833 +		int argsnum = atoi(argv[4]);
   1.834 +
   1.835 +		return action_hopter(argsnum, filenamein, filenameout); 
   1.836 +	}
   1.837 +	else if (stricmp(action, "iter") == 0)
   1.838 +	{
   1.839 +		const char *filenamein = argv[2];
   1.840 +		const char *filenameout = argv[3];
   1.841 +		int additional_argc = argc - 4;
   1.842 +		const char ** additional_argv = argv + 4;
   1.843 +		return action_iter(filenamein, filenameout, additional_argc, additional_argv);
   1.844 +	}
   1.845 +	else
   1.846 +	{
   1.847 +		cout << "Unrecognized action: " << argv[1] << endl;
   1.848 +		return 1;
   1.849 +	}
   1.850 +}