Boost logo

Boost :

Subject: Re: [boost] [spirit][qi] Fastest way to parse file
From: Jeff Flinn (Jeffrey.Flinn_at_[hidden])
Date: 2012-02-23 11:29:37

Antony Polukhin wrote:
> Hi,
> Boost.Spirit documentation advices to use multi_pass iterators for
> parsing files (or reading data to a STL container and then passing the
> begin and end of the container to Spirit.Qi).
> Much better solution would be to use a memory mapped file:
> boost::interprocess::file_mapping fm(filename.c_str(),
> boost::interprocess::read_only);
> boost::interprocess::mapped_region region(fm,
> boost::interprocess::read_only, 0, 0);
> const char* begin = reinterpret_cast<const char*>(region.get_address());
> const char* const end = b + region.get_size();

I switched from interprocess::file_mapping to
iostreams::mapped_file_source because of the formers lack of support for
wchar_t/unicode file names. It's also directly usable with
boost::filesystem::path. Here's an encapsulation of iterator/range view
of the mapped file:

#include <boost/filesystem/path.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/range/iterator_range.hpp>

class const_mapped_file
     typedef boost::filesystem::path Path;
     typedef const char* Iterator;
     typedef boost::iterator_range<Iterator> IteratorRange;

     explicit const_mapped_file(const Path& p) : m_src(p) {}

        Iterator begin() const { return; }
        Iterator end() const { return + m_src.size(); }
        IteratorRange range() const { return IteratorRange(begin(), end()); }

        boost::iostreams::mapped_file_source m_src;


Boost list run by bdawes at, gregod at, cpdaniel at, john at