Announce

PukiWiki contents have been moved into SONOTS Plugin (20070703)

boost

boostライブラリの使い方

Table of Contents

Install

Windows

Download http://www.boostpro.com/products/free and install.

MS Visual Studio

  • Tools > Options > Projects and Solutions > VC++ directories >
  • Show Directory for: > Include Files > Add
    C:\Program Files\boost\boost_1_35_0 (Make sure 1_35_0)
  • Show Directory for: > Library Files > Add
    C:\Program Files\boost\boost_1_35_0\lib

Try Example code

#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " " );
}

Linux

RedHat

yum install boost
yum install boost-devel

Debian (Ubuntsu)

apt-get install boost
apt-get install boost-devel

Else or non-root user

$ cd path/to/boost_1_36_0
$ ./configure --prefix=$HOME/usr
$ make install

http://www.boost.org/doc/libs/1_36_0/more/getting_started/unix-variants.html

Compile は

$ g++ -I$HOME/usr/include/boost-1_36 file.cpp -L$HOME/usr/lib/ -lboost_system-gcc41-mt -lboost_regex-gcc41-mt -lboost_filesystem-gcc41-mt

みたいな。ls $HOME/usr/lib で libboost_system-gcc41-mt.a などを確認。gcc41 のところなどが違ったりする。うざくね?

filesystem

basename

namespace fs = boost::filesystem;
path filename( "<path>" );
std:string basename = filename.leaf();

dirname

namespace fs = boost::filesystem;
path filename( "<path>" );
std:string dirname = filename.branch_path().native_file_string();

ls/get_filelist

#include <boost/filesystem.hpp>
#include <boost/regex.hpp>
#include <iostream>
#include <string>
using namespace std;
// namespace fs = boost::filesystem;

bool get_filelist( const boost::filesystem::path &dirpath, 
                  vector<boost::filesystem::path> & filelist, 
                  const boost::regex regex = boost::regex(".*"),
                  boost::filesystem::file_type file_type = boost::filesystem::type_unknown );


/**
* List Files in a directory
*
* @param const boost::filesystem::path &dirpath path to the target directory
* @param vector<boost::filesystem::path> &filelist vector of files to be stored (output)
* @param [const boost::regex regex = boost::regex(".*")] regular expression (instead of wild cards)
* @param [boost::filesystem::file_type file_type = boost::filesystem::type_unknown] list only specified file_type. The default is for all
* @return bool success (true) or failure (false)
* @requirements boost::filesystem, boost::regex, std::vector, std::string 
* @todo make option to list dir only or file only or both
* @todo why boost::regex( "" ) does not match with anything? boost is wierd?
*/
bool get_filelist( const boost::filesystem::path &dirpath, 
                  vector<boost::filesystem::path> & filelist, 
                  const boost::regex regex, boost::filesystem::file_type file_type )
{
    bool list_directory    = ( file_type == boost::filesystem::directory_file );
    bool list_regular_file = ( file_type == boost::filesystem::regular_file );
    bool list_symlink      = ( file_type == boost::filesystem::symlink_file );
    bool list_other        = ( !list_directory && !list_regular_file && !list_symlink );
    bool list_all          = ( file_type == boost::filesystem::type_unknown ); // just for now

    if( !boost::filesystem::exists( dirpath ) || !boost::filesystem::is_directory( dirpath ) )
    {
        return false;
    }

    boost::filesystem::directory_iterator iter( dirpath ), end_iter;
    for( ; iter != end_iter; ++iter )
    {
        boost::filesystem::path filename = iter->path();
        if( boost::regex_match( filename.native_file_string(), regex ) )
        {
            if( list_all )
            {
                filelist.push_back( filename );
            }
            else if( list_regular_file && boost::filesystem::is_regular( filename ) )
            {
                filelist.push_back( filename );                
            }
            else if( list_directory && boost::filesystem::is_directory( filename ) )
            {
                filelist.push_back( filename );
            }
            else if( list_symlink && boost::filesystem::is_symlink( filename ) )
            {
                filelist.push_back( filename );
            }
            else if( list_other && boost::filesystem::is_other( filename ) )
            {
                filelist.push_back( filename );
            }
        }
    }
    return true;
}

int main( int argc, char *argv[] )
{
    boost::filesystem::path dirpath( argv[1] );
    boost::regex imagetypes( ".*\.(bmp|dib|jpeg|jpg|jpe|png|pbm|pgm|ppm|sr|ras|tiff|exr|jp2)$", boost::regex_constants::icase );
    vector<string> filelist;
    ls( dirpath, filelist, imagetypes, boost::filesystem::regular_file );
    for( vector<string>::iterator filename = filelist.begin(); filename != filelist.end(); filename++ )
    {
        cout << *filename << endl;
    }
}

match/replace

    string ret = format;
    ret = boost::regex_replace( ret, boost::regex("%i"), filename, boost::match_default | boost::format_all );
    ret = boost::regex_replace( ret, boost::regex("%e"), extension, boost::match_default | boost::format_all );
    boost::smatch matches;
    if( boost::regex_match( ret, matches, boost::regex(".*(%\\d*)x.*"), boost::regex_constants::match_partial ) )
    {
        sprintf( tmp, (matches.str(1) + "d").c_str(), x );
        ret = boost::regex_replace( ret, boost::regex("(%\\d*)x"), string(tmp), boost::match_default | boost::format_all );
    }