2008-07-29 11:03:26 +00:00
|
|
|
#include "afeval.h"
|
|
|
|
#include "affunctions.h"
|
2008-08-07 14:35:45 +00:00
|
|
|
#include <cstdlib>
|
2008-07-31 16:28:24 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
2008-08-07 13:53:18 +00:00
|
|
|
#include <fstream>
|
2008-07-31 16:28:24 +00:00
|
|
|
#include <map>
|
2008-08-07 14:43:26 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <boost/format.hpp>
|
2008-07-31 16:28:24 +00:00
|
|
|
|
|
|
|
string filter;
|
|
|
|
map<string,AFPData> vars;
|
|
|
|
|
2008-08-07 14:43:26 +00:00
|
|
|
bool loadRequest(std::istream &);
|
2008-08-04 14:27:48 +00:00
|
|
|
void clearNulls();
|
2008-07-29 11:03:26 +00:00
|
|
|
|
|
|
|
int main( int argc, char** argv ) {
|
|
|
|
FilterEvaluator e;
|
2008-07-31 16:28:24 +00:00
|
|
|
registerBuiltinFunctions();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
try {
|
2008-08-07 13:53:18 +00:00
|
|
|
// Reset
|
2008-08-02 11:10:42 +00:00
|
|
|
e.reset();
|
2008-08-07 13:53:18 +00:00
|
|
|
vars.clear();
|
|
|
|
filter = "";
|
|
|
|
|
2008-08-07 14:43:26 +00:00
|
|
|
if (argv[1]) {
|
|
|
|
std::ifstream inf(argv[1]);
|
|
|
|
if (!inf) {
|
|
|
|
std::cerr << boost::format("%s: %s: %s\n")
|
|
|
|
% argv[0] % argv[1] % std::strerror(errno);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!loadRequest(inf))
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (!loadRequest(std::cin))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-07-31 16:28:24 +00:00
|
|
|
e.setVars( vars );
|
|
|
|
result = e.evaluateFilter( filter );
|
2008-08-07 14:43:26 +00:00
|
|
|
} catch (AFPException &excep) {
|
2008-08-02 11:10:42 +00:00
|
|
|
cout << "EXCEPTION: " << excep.what() << endl;
|
2008-07-31 16:28:24 +00:00
|
|
|
cerr << "EXCEPTION: " << excep.what() << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << ( result ? "MATCH\n" : "NOMATCH\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-04 14:27:48 +00:00
|
|
|
// Protocol:
|
|
|
|
// code NULL <key> NULL <value> NULL ... <value> NULL NULL
|
2008-07-31 16:28:24 +00:00
|
|
|
|
2008-08-07 14:43:26 +00:00
|
|
|
bool loadRequest(std::istream &inp) {
|
2008-08-04 14:27:48 +00:00
|
|
|
stringbuf codesb(ios::out | ios::in);
|
2008-07-31 16:28:24 +00:00
|
|
|
|
2008-08-04 14:27:48 +00:00
|
|
|
// Load the code
|
|
|
|
cin.get( codesb, '\0' );
|
|
|
|
cin.get();
|
|
|
|
filter = codesb.str();
|
2008-07-29 11:03:26 +00:00
|
|
|
|
2008-08-04 14:27:48 +00:00
|
|
|
while (true) {
|
|
|
|
stringbuf keysb(ios::out | ios::in);
|
|
|
|
stringbuf valsb(ios::out | ios::in);
|
|
|
|
|
|
|
|
// Double NULL = end
|
|
|
|
if (cin.peek() == 0) {
|
|
|
|
cin.get();
|
|
|
|
break;
|
|
|
|
} else if (cin.peek() == -1) {
|
|
|
|
exit(-1);
|
|
|
|
}
|
2008-07-31 16:28:24 +00:00
|
|
|
|
2008-08-04 14:27:48 +00:00
|
|
|
cin.get( keysb, '\0' );
|
|
|
|
cin.get();
|
2008-07-31 16:28:24 +00:00
|
|
|
|
2008-08-04 14:27:48 +00:00
|
|
|
if (cin.peek() == 0) {
|
|
|
|
cin.get();
|
|
|
|
// Leave blank.
|
2008-07-31 16:28:24 +00:00
|
|
|
} else {
|
2008-08-04 14:27:48 +00:00
|
|
|
cin.get( valsb, '\0' );
|
|
|
|
cin.get();
|
2008-07-31 16:28:24 +00:00
|
|
|
}
|
2008-08-04 14:27:48 +00:00
|
|
|
|
|
|
|
vars[keysb.str()] = AFPData( valsb.str() );
|
2008-07-29 11:03:26 +00:00
|
|
|
}
|
|
|
|
|
2008-07-31 16:28:24 +00:00
|
|
|
return true;
|
2008-07-29 11:03:26 +00:00
|
|
|
}
|