Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Boost Boost.Filesystem example 2: count C++ webpages, GUI

 

This Boost.Filesystem example is the GUI version of Boost.Filesystem example 1: count C++ webpages.

 

 

 

 

 

 

Operating system: Ubuntu 10.04 LTS Lucid Lynx

IDE: Qt Creator 2.0.0

Project type: GUI Application

Compiler: G++ 4.4.1

Libraries used:

 

 

 

 

 

Qt project file

 

#-------------------------------------------------
#
# Project created by QtCreator 2010-08-02T16:56:42
#
#-------------------------------------------------
QT += core gui
TARGET = CppFilesystemExample2
TEMPLATE = app
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
FORMS += dialog.ui
LIBS += -L/usr/local/lib -lboost_system
LIBS += -L/usr/local/lib -lboost_filesystem
RESOURCES += \
resources.qrc

 

 

 

 

 

Source code

 

 

 

 

 

dialog.h

 

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
  class Dialog;
}

class Dialog : public QDialog
{
  Q_OBJECT

public:
  explicit Dialog(QWidget *parent = 0);
  ~Dialog();

protected:
  void changeEvent(QEvent *e);

private:
  Ui::Dialog *ui;
};

#endif // DIALOG_H

 

 

 

 

 

dialog.cpp

 

#include "dialog.h"
#include "ui_dialog.h"

#include <cassert>
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/timer.hpp>

int Count_my_cpp_pages()
{
  const std::string my_folder_name = "/home/richel/RichelbilderbeekNl";
  const int min_filesize = 1000; //in bytes

  const boost::filesystem::path my_folder
      = boost::filesystem::system_complete(
          boost::filesystem::path(my_folder_name ));

  assert(boost::filesystem::is_directory(my_folder));

  int n = 0;

  const boost::filesystem::directory_iterator j;
  for ( boost::filesystem::directory_iterator i(my_folder);
    i != j; ++i)
  {
    if ( boost::filesystem::is_regular_file( i->status() ) )
    {
      const std::string filename = i->path().filename();
      const std::string full_filename = my_folder_name + "/" + filename;
      const int filesize = boost::filesystem::file_size(full_filename);
      if (filename.find(".htm")!=std::string::npos
       && filename[0] == 'C'
       && filename[1] == 'p'
       && filename[2] == 'p'
       && filesize > min_filesize)
      {
        ++n;
        std::clog << "(" << filesize << ") " << filename << '\n';
      }
    }
  }
  std::clog
    << "\nNumber of Cpp***.htm files bigger than "
    << min_filesize << " bytes: " << n << '\n';
  return n;
}

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
  ui->setupUi(this);
  ui->lcdNumber->display(Count_my_cpp_pages());
}

Dialog::~Dialog()
{
  delete ui;
}

void Dialog::changeEvent(QEvent *e)
{
  QDialog::changeEvent(e);
  switch (e->type()) {
  case QEvent::LanguageChange:
    ui->retranslateUi(this);
    break;
  default:
    break;
  }
}

 

 

 

 

 

main.cpp

 

#include <QtGui/QApplication>
#include "dialog.h"

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  Dialog w;
  w.show();
  return a.exec();
}

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict