void Tester::TestGrammar(bool benchmark) { mResult->clear(); fs::path grammar = fs::path("/tmp") / (sessionId() + ".lua"); ofstream ofs(grammar.string().c_str()); if (ofs) { ofs << mGrammar->text(); ofs.close(); } else { stringstream ss; ss << "failed to open: " << grammar; Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("error") << ss.str(); return; } lua_sandbox* sb = lsb_create(NULL, grammar.string().c_str(), "modules", 8*1024*1024, 1e6, 1024*63); if (!sb) { stringstream ss; ss << "lsb_create() failed"; Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("error") << ss.str(); return; } if (lsb_init(sb, nullptr)) { stringstream ss; string error = lsb_get_error(sb); size_t pos = error.find_first_of(':'); if (pos != string::npos) { ss << "line " << error.substr(pos + 1); } else { ss << error; } Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("info") << ss.str(); return; } if (benchmark) { Benchmark(sb, mInput->text().narrow()); } else { Match(sb, mInput->text().narrow()); } char* e = lsb_destroy(sb, nullptr); if (e) { stringstream ss; ss << "lsb_destroy() failed: " << e; Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("info") << ss.str(); free(e); } }
void Tester::ShareGrammar() { boost::hash<std::string> string_hash; string data(mInput->text().narrow() + mGrammar->text().narrow()); string h = boost::lexical_cast<string>(string_hash(data)); fs::path input = fs::path(appRoot()) / "share" / (h + ".data"); if (!exists(input)) { ofstream data_fs(input.string().c_str()); if (data_fs) { data_fs << mInput->text(); data_fs.close(); } else { stringstream ss; ss << "failed to open: " << input; Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("error") << ss.str(); return; } fs::path grammar = fs::path(appRoot()) / "share" / (h + ".lua"); ofstream lua_fs(grammar.string().c_str()); if (lua_fs) { lua_fs << mGrammar->text(); lua_fs.close(); } else { stringstream ss; ss << "failed to open: " << grammar; Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("error") << ss.str(); return; } } setInternalPath("/share/" + h); Wt::WMessageBox* messageBox = new Wt::WMessageBox("Share", makeAbsoluteUrl(bookmarkUrl()), Wt::Information, Wt::Ok); messageBox->buttonClicked().connect(std::bind([=]() { delete messageBox; })); messageBox->show(); }
// Static Functions /// Returns the value of pretty much all widget types static WString getValue(WFormWidget* widget) { Wt::WComboBox* asComboBox = dynamic_cast<Wt::WComboBox*>(widget); if (asComboBox != 0) return asComboBox->currentText(); Wt::WLineEdit* asLineEdit = dynamic_cast<Wt::WLineEdit*>(widget); if (asLineEdit != 0) return asLineEdit->text(); Wt::WSlider* asSlider = dynamic_cast<Wt::WSlider*>(widget); if (asSlider != 0) { std::stringstream out; out << asSlider->value(); return out.str(); } Wt::WTextArea* asTextArea = dynamic_cast<Wt::WTextArea*>(widget); if (asTextArea != 0) return asTextArea->text(); throw std::logic_error("I don't know how to get the value of whatever widget you passed me"); }