use a confsimple to parse the additional filter attributes

This commit is contained in:
dockes 2009-11-21 11:18:02 +00:00
parent a5c937d56c
commit 1cd62ace41

View File

@ -64,61 +64,56 @@ static Dijon::Filter *mhFactory(const string &mime)
/** /**
* Create a filter that executes an external program or script * Create a filter that executes an external program or script
* A filter def can look like. * A filter def can look like:
* exec someprog -v -t " h i j";charset= xx; mimetype=yy * exec someprog -v -t " h i j";charset= xx; mimetype=yy
* We don't support ';' inside a quoted string for now. Can't see a use * A semi-colon list of attr=value pairs may come after the exec spec.
* for it * This list is treated by replacing semi-colons with newlines and building
* a confsimple. This is done quite brutally and we don't support having
* a ';' inside a quoted string for now. Can't see a use for it.
*/ */
MimeHandlerExec *mhExecFactory(RclConfig *cfg, const string& mtype, string& hs, MimeHandlerExec *mhExecFactory(RclConfig *cfg, const string& mtype, string& hs,
bool multiple) bool multiple)
{ {
list<string>semicolist; string::size_type semicol0 = hs.find_first_of(";");
stringToTokens(hs, semicolist, ";"); string cmdstr, attrstr;
if (hs.size() < 1) { if (semicol0 != string::npos) {
LOGERR(("mhExecFactory: bad filter def: [%s]\n", hs.c_str())); cmdstr = hs.substr(0, semicol0);
return 0; if (semicol0 < hs.size() - 1)
attrstr = hs.substr(semicol0+1);
} else {
cmdstr = hs;
} }
string& cmd = *(semicolist.begin());
list<string> toks; // Split command name and args, and build exec object
stringToStrings(cmd, toks); list<string> cmdtoks;
if (toks.size() < 2) { stringToStrings(cmdstr, cmdtoks);
if (cmdtoks.size() < 2) {
LOGERR(("mhExecFactory: bad config line for [%s]: [%s]\n", LOGERR(("mhExecFactory: bad config line for [%s]: [%s]\n",
mtype.c_str(), hs.c_str())); mtype.c_str(), hs.c_str()));
return 0; return 0;
} }
MimeHandlerExec *h = multiple ? MimeHandlerExec *h = multiple ?
new MimeHandlerExecMultiple(mtype.c_str()) : new MimeHandlerExecMultiple(mtype.c_str()) :
new MimeHandlerExec(mtype.c_str()); new MimeHandlerExec(mtype.c_str());
list<string>::iterator it; // cmdtoks size is at least 2, this has been checked just before
list<string>::iterator it = cmdtoks.begin();
// toks size is at least 2, this has been checked by caller.
it = toks.begin();
it++; it++;
h->params.push_back(cfg->findFilter(*it++)); h->params.push_back(cfg->findFilter(*it++));
h->params.insert(h->params.end(), it, toks.end()); h->params.insert(h->params.end(), it, cmdtoks.end());
// Handle additional parameters // Handle additional attributes. We substitute the semi-colons
it = semicolist.begin(); // with newlines and use a ConfSimple
it++; if (!attrstr.empty()) {
for (;it != semicolist.end(); it++) { for (string::size_type i = 0; i < attrstr.size(); i++)
string &line = *it; if (attrstr[i] == ';')
string::size_type eqpos = line.find("="); attrstr[i] = '\n';
if (eqpos == string::npos) ConfSimple attrs(attrstr);
continue; string value;
// Compute name and value, trim white space if (attrs.get("charset", value))
string nm, val; h->cfgCharset = stringtolower((const string&)value);
nm = line.substr(0, eqpos); if (attrs.get("mimetype", value))
trimstring(nm); h->cfgMtype = stringtolower((const string&)value);
val = line.substr(eqpos+1, string::npos);
trimstring(val);
if (!nm.compare("charset")) {
h->cfgCharset = stringtolower((const string&)val);
} else if (!nm.compare("mimetype")) {
h->cfgMtype = stringtolower((const string&)val);
}
} }
#if 0 #if 0