Files

87 lines
2.1 KiB
C++
Raw Permalink Normal View History

2000-09-26 11:48:28 +00:00
/*
*
* Copyright (c) 1998-2002
2005-01-21 17:28:42 +00:00
* John Maddock
2000-09-26 11:48:28 +00:00
*
2003-10-04 11:29:20 +00:00
* Use, modification and distribution are subject to the
2003-09-30 13:02:51 +00:00
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
2000-09-26 11:48:28 +00:00
*
*/
/*
* LOCATION: see http://www.boost.org for most recent version.
* FILE regex_split_example_2.cpp
* VERSION see <boost/version.hpp>
2000-09-26 11:48:28 +00:00
* DESCRIPTION: regex_split example: spit out linked URL's.
*/
#include <list>
#include <fstream>
#include <iostream>
#include <iterator>
2000-09-26 11:48:28 +00:00
#include <boost/regex.hpp>
boost::regex e("<\\s*A\\s+[^>]*href\\s*=\\s*\"([^\"]*)\"",
2003-05-17 11:45:48 +00:00
boost::regex::normal | boost::regbase::icase);
2000-09-26 11:48:28 +00:00
void load_file(std::string& s, std::istream& is)
{
s.erase();
2003-12-18 11:53:47 +00:00
if(is.bad()) return;
2000-09-26 11:48:28 +00:00
//
// attempt to grow string buffer to match file size,
// this doesn't always work...
2010-05-10 12:13:49 +00:00
s.reserve(static_cast<std::string::size_type>(is.rdbuf()->in_avail()));
2000-09-26 11:48:28 +00:00
char c;
while(is.get(c))
{
// use logarithmic growth stategy, in case
// in_avail (above) returned zero:
if(s.capacity() == s.size())
s.reserve(s.capacity() * 3);
s.append(1, c);
}
}
int main(int argc, char** argv)
{
std::string s;
std::list<std::string> l;
2002-01-19 12:38:14 +00:00
int i;
for(i = 1; i < argc; ++i)
2000-09-26 11:48:28 +00:00
{
std::cout << "Findings URL's in " << argv[i] << ":" << std::endl;
s.erase();
std::ifstream is(argv[i]);
load_file(s, is);
is.close();
2000-09-26 11:48:28 +00:00
boost::regex_split(std::back_inserter(l), s, e);
while(l.size())
{
s = *(l.begin());
l.pop_front();
std::cout << s << std::endl;
}
}
//
// alternative method:
// split one match at a time and output direct to
// cout via ostream_iterator<std::string>....
//
for(i = 1; i < argc; ++i)
2000-09-26 11:48:28 +00:00
{
std::cout << "Findings URL's in " << argv[i] << ":" << std::endl;
s.erase();
std::ifstream is(argv[i]);
load_file(s, is);
is.close();
2000-09-26 11:48:28 +00:00
while(boost::regex_split(std::ostream_iterator<std::string>(std::cout), s, e, boost::match_default, 1)) std::cout << std::endl;
}
return 0;
}