Added possibility to specify your own g_SHPtr / g_Plug, removed all occurencies of The Word, fixed UnloadPlugin and CompleteShutdown
2 // Inspired by "Hopter" that comes with FastDelegate (http://www.codeproject.com/cpp/FastDelegate.asp)
3 // Much more powerful (and ugly) though
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
20 # define stricmp strcasecmp
25 extern int hopter(int numargs, const char *filenamein, const char *filenameout);
28 void TrimString(std::string &str)
30 size_t first = str.find_first_not_of(" \t\v\n\r");
31 if (first == std::string::npos)
37 size_t last = str.length();
38 for (std::string::reverse_iterator riter = str.rbegin(); riter != str.rend(); ++riter)
49 str = str.substr(first, last - first);
52 bool ExtractToken(std::string &strin, std::string &strout)
55 if (strin.begin() == strin.end())
60 size_t first = strin.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFHIJKLMNOPQRSTUVWXYZ_0123456789");
63 if (strin.size() > 1 && strin.at(0) == '/' && strin.at(1) == '/')
65 // One-line comment, find its end
66 first = strin.find('\n') + 1;
68 else if (strin.size() > 1 && strin.at(0) == '/' && strin.at(1) == '*')
70 // Multi-line comment, find its end
71 first = strin.find("*/") + 2;
73 strin = strin.substr(1);
77 strout = strin.substr(0, first);
78 strin = strin.substr(first);
82 // Returns the number of occurencies replaced
83 int DoReplace(string &str, const string &what, const string &with)
86 size_t where = str.find(what);
88 while (where != string::npos)
90 str.replace(where, what.size(), with);
92 where = str.find(what, where);
97 int main(int argc, char *argv[])
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;
107 const char *action = argv[1];
108 int argsnum = atoi(argv[2]);
109 const char *filenamein = argv[3];
110 const char *filenameout = argv[4];
112 if (stricmp(action, "hopter") == 0)
114 return hopter(argsnum, filenamein, filenameout);
117 ifstream fin(filenamein);
118 ofstream fout(filenameout);
121 cout << "Could not open file \"" << filenamein << "\"." << endl;
126 cout << "Could not open file \"" << filenameout << "\"." << endl;
130 if (stricmp(action, "iter") == 0)
132 // Generate versions of func macros for many parameters
138 if (str == "@VARARGS@")
145 if (str == "@ENDARGS@")
152 cout << "No matching @ENDARGS@ !" << endl;
157 for (int i = 0; i <= argsnum; ++i)
159 string cur = replacebuf;
160 size_t where = cur.find('@');
162 while (where != string::npos)
164 size_t where2 = cur.find('@', where+1);
165 if (where2 == string::npos)
167 cout << "No ending @";
170 string tmp = cur.substr(where + 1, where2 - where - 1);
171 cur.erase(where, where2 - where + 1);
173 if (tmp.size() != 0 && tmp[0] == '$')
180 istringstream istr(tmp.substr(2));
185 else if (tmp[1] == '*')
187 istringstream istr(tmp.substr(2));
195 cur.insert(where, istr.str());
196 where = cur.find('@', where);
200 // Find the |.. part, if any
202 size_t tmpsepbegin = tmp.rfind('|');
203 if (tmpsepbegin != string::npos)
205 tmpsep = tmp.substr(tmpsepbegin + 1);
206 tmp.erase(tmpsepbegin);
210 if (tmp.find("%%") == string::npos)
214 cur.insert(pos, tmp);
220 for (int j = 1; j <= i; ++j)
225 DoReplace(what, string("%%"), istr.str());
226 cur.insert(pos, what);
230 cur.insert(pos, tmpsep);
231 pos += tmpsep.size();
236 where = cur.find('@', where);