sourcehook/generate/shworker/main.cpp
author David Anderson <dvander@alliedmods.net>
Sat Apr 23 16:07:14 2005 +0000 (2005-04-23)
changeset 37 f4d069676dc2
parent 1 586787053c73
permissions -rw-r--r--
Fixed GCC bug
     1 // SHWorker
     2 // Inspired by "Hopter" that comes with FastDelegate (http://www.codeproject.com/cpp/FastDelegate.asp)
     3 // Much more powerful (and ugly) though
     4 
     5 // Action: iter
     6 // @VARARGS@						begins session
     7 // @..@								is replaced by .. if num!=0 and removed if num=0. Any
     8 //									occurences of %% are replaced by the current iter
     9 // @$@								is replaced by the current iter
    10 // @ENDARGS@						ends session
    11 
    12 #include <iostream>
    13 #include <fstream>
    14 #include <string>
    15 #include <sstream>
    16 #include <list>
    17 #include "stdio.h"
    18 
    19 #ifdef __linux__
    20 # define stricmp strcasecmp
    21 #endif
    22 
    23 using namespace std;
    24 
    25 extern int hopter(int numargs, const char *filenamein, const char *filenameout);
    26 
    27 
    28 void TrimString(std::string &str)
    29 {
    30 	size_t first = str.find_first_not_of(" \t\v\n\r");
    31 	if (first == std::string::npos)
    32 	{
    33 		str.clear();
    34 		return;
    35 	}
    36 
    37 	size_t last = str.length();
    38 	for (std::string::reverse_iterator riter = str.rbegin(); riter != str.rend(); ++riter)
    39 	{
    40 		char ch = *riter;
    41 		if (ch != ' '	&&
    42 			ch != '\t'	&&
    43 			ch != '\v'	&&
    44 			ch != '\n'	&&
    45 			ch != '\r')
    46 			break;
    47 		--last;
    48 	}
    49 	str = str.substr(first, last - first);
    50 }
    51 
    52 bool ExtractToken(std::string &strin, std::string &strout)
    53 {
    54 	TrimString(strin);
    55 	if (strin.begin() == strin.end())
    56 	{
    57 		strout.clear();
    58 		return false;
    59 	}
    60 	size_t first = strin.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFHIJKLMNOPQRSTUVWXYZ_0123456789");
    61 	if (first == 0)
    62 	{
    63 		if (strin.size() > 1 && strin.at(0) == '/' && strin.at(1) == '/')
    64 		{
    65 			// One-line comment, find its end
    66 			first = strin.find('\n') + 1;
    67 		}
    68 		else if (strin.size() > 1 && strin.at(0) == '/' && strin.at(1) == '*')
    69 		{
    70 			// Multi-line comment, find its end
    71 			first = strin.find("*/") + 2;
    72 		}
    73 		strin = strin.substr(1);
    74 		strout.clear();
    75 		return true;
    76 	}
    77 	strout = strin.substr(0, first);
    78 	strin = strin.substr(first);
    79 	return true;
    80 }
    81 
    82 // Returns the number of occurencies replaced
    83 int DoReplace(string &str, const string &what, const string &with)
    84 {
    85 	int cnt=0;
    86 	size_t where = str.find(what);
    87 
    88 	while (where != string::npos)
    89 	{
    90 		str.replace(where, what.size(), with);
    91 		++cnt;
    92 		where = str.find(what, where);
    93 	}
    94 	return cnt;
    95 }
    96 
    97 int main(int argc, char *argv[])
    98 {
    99 	if (argc < 5)
   100 	{
   101 		cout << "Usage:" << endl << "   shworker [iter/hopter] ..." << endl;
   102 		cout << "   shworker iter [num-of-args] filename.in filename.out" << endl;
   103 		cout << "   shworker hopter [num-of-args] filename.in filename.our" << endl;
   104 		return 1;
   105 	}
   106 
   107 	const char *action = argv[1];
   108 	int argsnum = atoi(argv[2]);
   109 	const char *filenamein = argv[3];
   110 	const char *filenameout = argv[4];
   111 
   112 	if (stricmp(action, "hopter") == 0)
   113 	{
   114 		return hopter(argsnum, filenamein, filenameout);
   115 	}
   116 
   117 	ifstream fin(filenamein);
   118 	ofstream fout(filenameout);
   119 	if (!fin)
   120 	{
   121 		cout << "Could not open file \"" << filenamein << "\"." << endl;
   122 		return 1;
   123 	}
   124 	if (!fout)
   125 	{
   126 		cout << "Could not open file \"" << filenameout << "\"." << endl;
   127 		return 1;
   128 	}
   129 
   130 	if (stricmp(action, "iter") == 0)
   131 	{
   132 		// Generate versions of func macros for many parameters
   133 		string str;
   134 		string replacebuf;
   135 		while (!fin.eof())
   136 		{
   137 			getline(fin, str);
   138 			if (str == "@VARARGS@")
   139 			{
   140 				replacebuf.clear();
   141 
   142 				while (true)
   143 				{
   144 					getline(fin, str);
   145 					if (str == "@ENDARGS@")
   146 						break;
   147 
   148 					replacebuf += str;
   149 					replacebuf += "\n";
   150 					if (fin.eof())
   151 					{
   152 						cout << "No matching @ENDARGS@ !" << endl;
   153 						return 1;
   154 					}
   155 				}
   156 
   157 				for (int i = 0; i <= argsnum; ++i)
   158 				{
   159 					string cur = replacebuf;
   160 					size_t where = cur.find('@');
   161 
   162 					while (where != string::npos)
   163 					{
   164 						size_t where2 = cur.find('@', where+1);
   165 						if (where2 == string::npos)
   166 						{
   167 							cout << "No ending @";
   168 							return 1;
   169 						}
   170 						string tmp = cur.substr(where + 1, where2 - where - 1);
   171 						cur.erase(where, where2 - where + 1);
   172 
   173 						if (tmp.size() != 0 && tmp[0] == '$')
   174 						{
   175 							int tmp_out = i;
   176 							if (tmp.size() > 2)
   177 							{
   178 								if (tmp[1] == '+')
   179 								{
   180 									istringstream istr(tmp.substr(2));
   181 									int tmp;
   182 									istr >> tmp;
   183 									tmp_out += tmp;
   184 								}
   185 								else if (tmp[1] == '*')
   186 								{
   187 									istringstream istr(tmp.substr(2));
   188 									int tmp;
   189 									istr >> tmp;
   190 									tmp_out *= tmp;
   191 								}
   192 							}
   193 							stringstream istr;
   194 							istr << tmp_out;
   195 							cur.insert(where, istr.str());
   196 							where = cur.find('@', where);
   197 							continue;
   198 						}
   199 
   200 						// Find the |.. part, if any
   201 						string tmpsep;
   202 						size_t tmpsepbegin = tmp.rfind('|');
   203 						if (tmpsepbegin != string::npos)
   204 						{
   205 							tmpsep = tmp.substr(tmpsepbegin + 1);
   206 							tmp.erase(tmpsepbegin);
   207 						}
   208 
   209 						size_t pos = where;
   210 						if (tmp.find("%%") == string::npos)
   211 						{
   212 							if (i > 0)
   213 							{
   214 								cur.insert(pos, tmp);
   215 								pos += tmp.size();
   216 							}
   217 						}
   218 						else
   219 						{
   220 							for (int j = 1; j <= i; ++j)
   221 							{
   222 								stringstream istr;
   223 								istr << j;
   224 								string what = tmp;
   225 								DoReplace(what, string("%%"), istr.str());
   226 								cur.insert(pos, what);
   227 								pos += what.size();
   228 								if (j != i)
   229 								{
   230 									cur.insert(pos, tmpsep);
   231 									pos += tmpsep.size();
   232 								}
   233 							}
   234 						}
   235 
   236 						where = cur.find('@', where);
   237 					}
   238 					fout << cur;
   239 				}
   240 			}
   241 			else
   242 			{
   243 				fout << str << endl;
   244 			}
   245 		}
   246 	}
   247 }