Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Wt WtTextUploader

 

WtTextUploader is example of a web application using the Wt library to let the user upload a text file and display its contents.

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppWtTextUploader.pro

 

#-------------------------------------------------
#
# Project created by QtCreator 2011-03-26T18:26:28
#
#-------------------------------------------------
QT       += core
QT       -= gui
LIBS += -lwt -lwthttp -lboost_program_options
QMAKE_CXXFLAGS += -DNDEBUG
TARGET = CppWtAutoRun
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
    wttextuploaddialog.cpp
HEADERS += \
    wttextuploaddialog.h

 

 

 

 

 

main.cpp

 

//---------------------------------------------------------------------------
#include <iostream>
//---------------------------------------------------------------------------
#include <boost/program_options.hpp>
#include <boost/signals2.hpp>
//---------------------------------------------------------------------------
#include <Wt/WApplication>
#include <Wt/WEnvironment>
//---------------------------------------------------------------------------
#include "wttextuploaddialog.h"
//---------------------------------------------------------------------------
struct WtApplication : public Wt::WApplication
{
  WtApplication(const Wt::WEnvironment& env)
    : Wt::WApplication(env),
    m_dialog(new WtTextUploadDialog)
  {
    this->setTitle("WtTextUploader");
    root()->addWidget(m_dialog);
  }
  private:
  WtTextUploadDialog * const m_dialog;
};
//---------------------------------------------------------------------------
Wt::WApplication *createApplication(
  const Wt::WEnvironment& env)
{
  return new WtApplication(env);
}
//---------------------------------------------------------------------------
int main(int argc, char **argv)
{
  // Declare the supported options.
  boost::program_options::options_description d(
    "Allowed options for CppWtTextUploader");
  std::string docroot;
  std::string http_address;
  std::string http_port;
  d.add_options()
      ("help",
        "produce this help message")
      ("docroot",
         boost::program_options::value<std::string>(&docroot)->default_value("."),
         "the docroot")
      ("http-address",
         boost::program_options::value<std::string>(&http_address)->default_value("0.0.0.0"),
         "the server's http address")
      ("http-port",
         boost::program_options::value<std::string>(&http_port)->default_value("8080"),
         "the server's http port")
      ;

  boost::program_options::variables_map m;
  boost::program_options::store(
    boost::program_options::parse_command_line(
      argc, argv, d), m);
  boost::program_options::notify(m);

  if (m.count("help"))
  {
    //Display the options_description
    std::cout << d << "\n";
    return 0;
  }

  //Create the arguments in std::string format
  std::vector<std::string> v(7);
  v[0] = argv[0];
  v[1] = "--docroot";
  v[2] = docroot;
  v[3] = "--http-address";
  v[4] = http_address;
  v[5] = "--http-port";
  v[6] = http_port;

  //Convert the arguments to char* format
  std::vector<char*> w(7);
  for (int i=0; i!=7; ++i) w[i] = &v[i][0];

  //Give Wt the modified parameters
  return WRun(w.size(), &w[0], &createApplication);
}
//---------------------------------------------------------------------------

 

 

 

 

 

wttextuploaddialog.cpp

 

//---------------------------------------------------------------------------
#include <algorithm>
#include <fstream>
#include <functional>
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
//---------------------------------------------------------------------------
#include <Wt/WBreak>
//---------------------------------------------------------------------------
#include "wttextuploaddialog.h"
//---------------------------------------------------------------------------
WtTextUploadDialog::WtTextUploadDialog()
  : m_fileupload(new Wt::WFileUpload),
    m_text_log(new Wt::WTextArea),
    m_text_file(new Wt::WTextArea)
{
  this->setContentAlignment(Wt::AlignCenter);
  this->addWidget(m_fileupload);
  this->addWidget(new Wt::WBreak);
  m_text_log->setMinimumSize(600,100);
  this->addWidget(m_text_log);
  this->addWidget(new Wt::WBreak);
  m_text_file->setMinimumSize(600,200);
  this->addWidget(m_text_file);

  //Upload automatically when the user entered a file
  m_fileupload->changed().connect(
    m_fileupload,
    &Wt::WFileUpload::upload);

  //Call WtTextUploadDialog::on_upload_done when file is uploaded
  m_fileupload->uploaded().connect(
    this,
    &WtTextUploadDialog::on_upload_done);

}
//---------------------------------------------------------------------------
void WtTextUploadDialog::on_upload_done()
{
  m_text_log->setText(
      std::string("Upload done")
    + boost::lexical_cast<std::string>('\n')
    + std::string("Client filename: ")
    + m_fileupload->clientFileName().toUTF8()
    + boost::lexical_cast<std::string>('\n')
    + std::string("Spoolfilename: ")
    + m_fileupload->spoolFileName()
    + boost::lexical_cast<std::string>('\n')
    + std::string("Does spoolfile exist: ")
    + std::string(
      boost::filesystem::exists(m_fileupload->spoolFileName())
      ? "Yes"
      : "No")
    );

  const std::vector<std::string> v
    = FileToVector(m_fileupload->spoolFileName());
  std::string s;
  BOOST_FOREACH(const std::string t,v)
  {
    s+=t+boost::lexical_cast<std::string>('\n');
  }

  m_text_file->setText(s);
}
//---------------------------------------------------------------------------
///FileToVector reads a file and converts it to a std::vector<std::string>
///From http://www.richelbilderbeek.nl/CppFileToVector.htm
const std::vector<std::string> WtTextUploadDialog::FileToVector(
  const std::string& filename)
{
  assert(boost::filesystem::exists(filename));
  std::vector<std::string> v;
  std::ifstream in(filename.c_str());
  std::string s;
  for (int i=0; !in.eof(); ++i)
  {
    std::getline(in,s);
    v.push_back(s);
  }
  return v;
}
//---------------------------------------------------------------------------

 

 

 

 

 

wttextuploaddialog.h

 

#ifndef WTTEXTUPLOADDIALOG_H
#define WTTEXTUPLOADDIALOG_H
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
#include <Wt/WContainerWidget>
#include <Wt/WFileUpload>
#include <Wt/WTextArea>
//---------------------------------------------------------------------------
struct WtTextUploadDialog : public Wt::WContainerWidget
{
  WtTextUploadDialog();

  private:
  Wt::WFileUpload * const m_fileupload;
  Wt::WTextArea * const m_text_log;
  Wt::WTextArea * const m_text_file;

  void on_upload_done();

  static const std::vector<std::string> FileToVector(const std::string& filename);
};
//---------------------------------------------------------------------------
#endif // WTTEXTUPLOADDIALOG_H

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict