Ejemplo n.º 1
0
Upload::Upload( Wt::WContainerWidget* pcw ) {
  
  //Wt::WContainerWidget *container = new Wt::WContainerWidget();

  Wt::WFileUpload *fu = new Wt::WFileUpload( pcw );
  fu->setFileTextSize( 10000 ); // Set the maximum file size (in KB )
  fu->setProgressBar(new Wt::WProgressBar());
  fu->setMargin(10, Wt::Right);

  // Provide a button to start uploading.
  Wt::WPushButton *uploadButton = new Wt::WPushButton("Send", pcw );
  uploadButton->setMargin(10, Wt::Left | Wt::Right);

  Wt::WText *out = new Wt::WText( pcw );

  // Upload when the button is clicked.
  uploadButton->clicked().connect(std::bind([=] () {
      fu->upload();
      uploadButton->disable();
  }));

  // Upload automatically when the user entered a file.
  fu->changed().connect(std::bind([=] () {
      fu->upload();
      uploadButton->disable();
      std::string s( "File upload is changed." );
      out->setText( s );
  }));

  // React to a succesfull upload.
  fu->uploaded().connect(std::bind([=] () {
    std::string s( "File upload is finished: " );
    s += fu->clientFileName().toUTF8();
    s += ",";
    //s += fu->fileTextSize()
    s += fu->spoolFileName();
    //fu->stealSpooledFile()
    out->setText( s );
  }));

  // React to a file upload problem.
  fu->fileTooLarge().connect(std::bind([=] () {
      out->setText("File is too large.");
  }));
}
Ejemplo n.º 2
0
 void add_handler_() {
     Wt::WValidator::State V = Wt::WValidator::Valid;
     if (username_->validate() != V) {
         error_->setText(tr("facts.comment.Incorrect_username"));
         return;
     }
     if (email_->validate() != V) {
         error_->setText(tr("facts.comment.Incorrect_email"));
         return;
     }
     int input_length = text_->text().value().size();
     if (input_length < MIN_INPUT_SIZE || input_length > MAX_TEXT_SIZE) {
         error_->setText(tr("facts.comment.Incorrect_text"));
         return;
     }
     dbo::Transaction t(fApp->session());
     if (fApp->is_banned()) {
         error_->setText(tr("facts.common.BannedIp"));
         return;
     }
     int index;
     if (comments_->fact()->comments().size()) {
         index = 1 + fApp->session()
                 .query<int>("select max(comment_index) from facts_comment where fact_id=?")
                 .bind(comments_->fact().id());
     } else {
         index = 1;
     }
     CommentPtr c = fApp->session().add(new Comment(CommentId(comments_->fact(), index)));
     c.modify()->set_username(username_->text());
     c.modify()->set_email(email_->text().toUTF8());
     c.modify()->set_text(text_->text());
     c.modify()->set_ip(fApp->environment().clientAddress());
     comments_->comment_added_handler_(); // delete this
     t.commit();
 }
Ejemplo n.º 3
0
void Tester::Benchmark(lua_sandbox* lsb, const string& input)
{
    lua_State* lua = lsb_get_lua(lsb);
    if (!lua) return;
    bool nomatch = false;

    if (!CreateGlobalMatch(lua)) {
        stringstream ss;
        ss << "lpeg.match is not available";
        Wt::WText* t = new Wt::WText(ss.str(), mResult);
        t->setStyleClass("result_error");
        Wt::log("info") << ss.str();
        return;
    }

    clock_t t = clock();
    int x, iter = 10000;
    for (x = 0; x < iter; ++x) {
        if (lsb_pcall_setup(lsb, kMatchFunction)) {
            stringstream ss;
            ss << "lsb_pcall_setup() failed";
            Wt::WText* t = new Wt::WText(ss.str(), mResult);
            t->setStyleClass("result_error");
            Wt::log("info") << ss.str();
            return;
        }

        lua_getglobal(lua, "grammar");
        if (!lua_isuserdata(lua, -1)) {
            stringstream ss;
            ss << "no global grammar variable was found";
            Wt::WText* t = new Wt::WText(ss.str(), mResult);
            t->setStyleClass("result_error");
            Wt::log("info") << ss.str();
            return;
        }

        lua_pushstring(lua, input.c_str());
        if (lua_pcall(lua, 2, LUA_MULTRET, 0) != 0) {
            char err[LSB_ERROR_SIZE];
            int len = snprintf(err, LSB_ERROR_SIZE, "%s() %s", kMatchFunction,
                               lua_tostring(lua, -1));
            if (len >= LSB_ERROR_SIZE || len < 0) {
                err[LSB_ERROR_SIZE - 1] = 0;
            }
            stringstream ss;
            ss << err;
            Wt::WText* t = new Wt::WText(ss.str(), mResult);
            t->setStyleClass("result_error");
            Wt::log("info") << ss.str();
            lsb_terminate(lsb, err);
            return;
        } else {
            if (LUA_TNIL == lua_type(lua, 1)) {
                nomatch = true;
            }
        }
        lua_pop(lua, lua_gettop(lua)); // clear the stack
    }
    t = clock() - t;
    stringstream ss;
    Wt::WText* txt = new Wt::WText(mResult);
    if (nomatch) {
        txt->setStyleClass("result_error");
        ss << " *** MATCH FAILED ***<br/>";
    }
    ss << "Benchmark"
       << "<br/>samples: " << x
       << "<br/>seconds per match: " << (((float)t) / CLOCKS_PER_SEC / x)
       << "<br/>max memory (bytes): " << lsb_usage(lsb, LSB_UT_MEMORY, LSB_US_MAXIMUM)
       << "<br/>max Lua instructions: " << lsb_usage(lsb, LSB_UT_INSTRUCTION, LSB_US_MAXIMUM);
    txt->setText(ss.str());
    lsb_pcall_teardown(lsb);
}