1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/sourcehook/generate/shworker/shworker.cpp Fri Sep 23 20:14:10 2005 +0000
1.3 @@ -0,0 +1,247 @@
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 +// Action: iter
1.9 +// @VARARGS@ begins session
1.10 +// @..@ is replaced by .. if num!=0 and removed if num=0. Any
1.11 +// occurences of %% are replaced by the current iter
1.12 +// @$@ is replaced by the current iter
1.13 +// @ENDARGS@ ends session
1.14 +
1.15 +#include <iostream>
1.16 +#include <fstream>
1.17 +#include <string>
1.18 +#include <sstream>
1.19 +#include <list>
1.20 +#include "stdio.h"
1.21 +
1.22 +#ifdef __linux__
1.23 +# define stricmp strcasecmp
1.24 +#endif
1.25 +
1.26 +using namespace std;
1.27 +
1.28 +extern int hopter(int numargs, const char *filenamein, const char *filenameout);
1.29 +
1.30 +
1.31 +void TrimString(std::string &str)
1.32 +{
1.33 + size_t first = str.find_first_not_of(" \t\v\n\r");
1.34 + if (first == std::string::npos)
1.35 + {
1.36 + str.clear();
1.37 + return;
1.38 + }
1.39 +
1.40 + size_t last = str.length();
1.41 + for (std::string::reverse_iterator riter = str.rbegin(); riter != str.rend(); ++riter)
1.42 + {
1.43 + char ch = *riter;
1.44 + if (ch != ' ' &&
1.45 + ch != '\t' &&
1.46 + ch != '\v' &&
1.47 + ch != '\n' &&
1.48 + ch != '\r')
1.49 + break;
1.50 + --last;
1.51 + }
1.52 + str = str.substr(first, last - first);
1.53 +}
1.54 +
1.55 +bool ExtractToken(std::string &strin, std::string &strout)
1.56 +{
1.57 + TrimString(strin);
1.58 + if (strin.begin() == strin.end())
1.59 + {
1.60 + strout.clear();
1.61 + return false;
1.62 + }
1.63 + size_t first = strin.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFHIJKLMNOPQRSTUVWXYZ_0123456789");
1.64 + if (first == 0)
1.65 + {
1.66 + if (strin.size() > 1 && strin.at(0) == '/' && strin.at(1) == '/')
1.67 + {
1.68 + // One-line comment, find its end
1.69 + first = strin.find('\n') + 1;
1.70 + }
1.71 + else if (strin.size() > 1 && strin.at(0) == '/' && strin.at(1) == '*')
1.72 + {
1.73 + // Multi-line comment, find its end
1.74 + first = strin.find("*/") + 2;
1.75 + }
1.76 + strin = strin.substr(1);
1.77 + strout.clear();
1.78 + return true;
1.79 + }
1.80 + strout = strin.substr(0, first);
1.81 + strin = strin.substr(first);
1.82 + return true;
1.83 +}
1.84 +
1.85 +// Returns the number of occurencies replaced
1.86 +int DoReplace(string &str, const string &what, const string &with)
1.87 +{
1.88 + int cnt=0;
1.89 + size_t where = str.find(what);
1.90 +
1.91 + while (where != string::npos)
1.92 + {
1.93 + str.replace(where, what.size(), with);
1.94 + ++cnt;
1.95 + where = str.find(what, where);
1.96 + }
1.97 + return cnt;
1.98 +}
1.99 +
1.100 +int main(int argc, char *argv[])
1.101 +{
1.102 + if (argc < 5)
1.103 + {
1.104 + cout << "Usage:" << endl << " shworker [iter/hopter] ..." << endl;
1.105 + cout << " shworker iter [num-of-args] filename.in filename.out" << endl;
1.106 + cout << " shworker hopter [num-of-args] filename.in filename.our" << endl;
1.107 + return 1;
1.108 + }
1.109 +
1.110 + const char *action = argv[1];
1.111 + int argsnum = atoi(argv[2]);
1.112 + const char *filenamein = argv[3];
1.113 + const char *filenameout = argv[4];
1.114 +
1.115 + if (stricmp(action, "hopter") == 0)
1.116 + {
1.117 + return hopter(argsnum, filenamein, filenameout);
1.118 + }
1.119 +
1.120 + ifstream fin(filenamein);
1.121 + ofstream fout(filenameout);
1.122 + if (!fin)
1.123 + {
1.124 + cout << "Could not open file \"" << filenamein << "\"." << endl;
1.125 + return 1;
1.126 + }
1.127 + if (!fout)
1.128 + {
1.129 + cout << "Could not open file \"" << filenameout << "\"." << endl;
1.130 + return 1;
1.131 + }
1.132 +
1.133 + if (stricmp(action, "iter") == 0)
1.134 + {
1.135 + // Generate versions of func macros for many parameters
1.136 + string str;
1.137 + string replacebuf;
1.138 + while (!fin.eof())
1.139 + {
1.140 + getline(fin, str);
1.141 + if (str == "@VARARGS@")
1.142 + {
1.143 + replacebuf.clear();
1.144 +
1.145 + while (true)
1.146 + {
1.147 + getline(fin, str);
1.148 + if (str == "@ENDARGS@")
1.149 + break;
1.150 +
1.151 + replacebuf += str;
1.152 + replacebuf += "\n";
1.153 + if (fin.eof())
1.154 + {
1.155 + cout << "No matching @ENDARGS@ !" << endl;
1.156 + return 1;
1.157 + }
1.158 + }
1.159 +
1.160 + for (int i = 0; i <= argsnum; ++i)
1.161 + {
1.162 + string cur = replacebuf;
1.163 + size_t where = cur.find('@');
1.164 +
1.165 + while (where != string::npos)
1.166 + {
1.167 + size_t where2 = cur.find('@', where+1);
1.168 + if (where2 == string::npos)
1.169 + {
1.170 + cout << "No ending @";
1.171 + return 1;
1.172 + }
1.173 + string tmp = cur.substr(where + 1, where2 - where - 1);
1.174 + cur.erase(where, where2 - where + 1);
1.175 +
1.176 + if (tmp.size() != 0 && tmp[0] == '$')
1.177 + {
1.178 + int tmp_out = i;
1.179 + if (tmp.size() > 2)
1.180 + {
1.181 + if (tmp[1] == '+')
1.182 + {
1.183 + istringstream istr(tmp.substr(2));
1.184 + int tmp;
1.185 + istr >> tmp;
1.186 + tmp_out += tmp;
1.187 + }
1.188 + else if (tmp[1] == '*')
1.189 + {
1.190 + istringstream istr(tmp.substr(2));
1.191 + int tmp;
1.192 + istr >> tmp;
1.193 + tmp_out *= tmp;
1.194 + }
1.195 + }
1.196 + stringstream istr;
1.197 + istr << tmp_out;
1.198 + cur.insert(where, istr.str());
1.199 + where = cur.find('@', where);
1.200 + continue;
1.201 + }
1.202 +
1.203 + // Find the |.. part, if any
1.204 + string tmpsep;
1.205 + size_t tmpsepbegin = tmp.rfind('|');
1.206 + if (tmpsepbegin != string::npos)
1.207 + {
1.208 + tmpsep = tmp.substr(tmpsepbegin + 1);
1.209 + tmp.erase(tmpsepbegin);
1.210 + }
1.211 +
1.212 + size_t pos = where;
1.213 + if (tmp.find("%%") == string::npos)
1.214 + {
1.215 + if (i > 0)
1.216 + {
1.217 + cur.insert(pos, tmp);
1.218 + pos += tmp.size();
1.219 + }
1.220 + }
1.221 + else
1.222 + {
1.223 + for (int j = 1; j <= i; ++j)
1.224 + {
1.225 + stringstream istr;
1.226 + istr << j;
1.227 + string what = tmp;
1.228 + DoReplace(what, string("%%"), istr.str());
1.229 + cur.insert(pos, what);
1.230 + pos += what.size();
1.231 + if (j != i)
1.232 + {
1.233 + cur.insert(pos, tmpsep);
1.234 + pos += tmpsep.size();
1.235 + }
1.236 + }
1.237 + }
1.238 +
1.239 + where = cur.find('@', where);
1.240 + }
1.241 + fout << cur;
1.242 + }
1.243 + }
1.244 + else
1.245 + {
1.246 + fout << str << endl;
1.247 + }
1.248 + }
1.249 + }
1.250 +}