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