Click here to Skip to main content
12,450,605 members (46,511 online)
Click here to Skip to main content

Stats

43K views
370 downloads
17 bookmarked
Posted

Win32 File Name Iteration Boost Way

, 3 Nov 2005 CPOL
A Port of ::FindFirstFile to Boost.Range and Boost.Foreach
// find_file_demo.cpp :
//

#include "stdafx.h"

#include <iterator>
#include <string>
#include <boost/foreach.hpp>
#include <boost/functional.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/result_iterator.hpp>
#include <boost/range_ex/algorithm.hpp>
#include <boost/range_ex/filter.hpp>
#include <boost/range_ex/transform.hpp>

#include "find_file/find_file.hpp"
#include "poost/istream_range.hpp"

int _tmain(int argc, _TCHAR* argv[])
{
	using namespace find_file;

	{
		std::cout << "\n---output all the files without foreach---\n";

		find_file_range ffrng("*.*");
		typedef boost::range_result_iterator<find_file_range>::type iter_t;
		for (iter_t it = boost::begin(ffrng), last = boost::end(ffrng); it != last; ++it) {
			std::cout << it->cFileName << std::endl;
		}
	}
	{
		std::cout << "\n---output all the files---\n";

		find_file_range ffrng("*.*");
		BOOST_FOREACH (WIN32_FIND_DATA& data, ffrng) {
			std::cout << data.cFileName << std::endl;
		}
	}
	{
		std::cout << "\n---output no files---\n";

		find_file_range ffrng("*.ppp");
		BOOST_FOREACH (WIN32_FIND_DATA& data, ffrng) {
			std::cout << data.cFileName << std::endl;
		}
	}
	{
		std::cout << "\n---output non dots files---\n";

		find_file_range ffrng("*.*");
		BOOST_FOREACH (
			WIN32_FIND_DATA& data,
			boost::make_filter_range(ffrng, boost::not1(find_file_is_dots()))
		)
		{
			std::cout << data.cFileName << std::endl;
		}
	}
	{
		std::cout << "\n---output all the files using std::string---\n";

		find_file_range ffrng("*.*");
		BOOST_FOREACH (
			std::string const& filename,
			boost::make_transform_range(ffrng, find_file_construct<std::string>())
		)
		{
			std::cout << filename << std::endl;
		}
	}
	{
		std::cout << "\n---output files that is not directory or hidden using range-generators---\n";

		find_file_range ffrng("*.*");
		BOOST_FOREACH (
			std::string const& filename,
			boost::make_transform_range(
				boost::make_filter_range(
					boost::make_filter_range(
						ffrng,
						boost::not1(find_file_is_hidden())
					),
					boost::not1(find_file_is_directory())
				),
				find_file_stringize<std::string>()
			)
		)
		{
			std::cout << filename << std::endl;
		}
	}
	{
		std::cout << "\n---output files that is not directory or hidden using range-adaptors---\n";

		find_file_range ffrng("*.*");
		BOOST_FOREACH (
			std::string const& filename,
			ffrng |
				boost::adaptor::filter(boost::not1(find_file_is_hidden())) |
				boost::adaptor::filter(boost::not1(find_file_is_directory())) |
				boost::adaptor::transform(find_file_construct<std::string>())
		)
		{
			std::cout << filename << std::endl;
		}
	}
	{
		using namespace poost;

		std::cout << "\n---istream_range demo: input something and press RETURN and CTRL+Z---" << std::endl;
		std::string inputs; {
			boost::copy(make_istream_range<char>(std::cin), std::back_inserter(inputs));
		}
		std::cout << inputs << std::endl;
	}

	(argc), (argv);
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

mb2sync
Japan Japan
I am worried about my poor English...

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 4 Nov 2005
Article Copyright 2005 by mb2sync
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid