• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright (c) 1998-2002
4  * John Maddock
5  *
6  * Use, modification and distribution are subject to the
7  * Boost Software License, Version 1.0. (See accompanying file
8  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9  *
10  */
11 
12  /*
13   *   LOCATION:    see http://www.boost.org for most recent version.
14   *   FILE         partial_regex_grep.cpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: Search example using partial matches.
17   */
18 
19 #include <boost/regex.hpp>
20 #include <iostream>
21 #include <fstream>
22 #include <sstream>
23 #include <string>
24 #include <cstring>
25 
26 #ifdef BOOST_NO_STDC_NAMESPACE
27 namespace std{ using ::memmove; }
28 #endif
29 
30 // match some kind of html tag:
31 boost::regex e("<[^>]*>");
32 // count how many:
33 unsigned int tags = 0;
34 // saved position of partial match:
35 const char* next_pos = 0;
36 
grep_callback(const boost::match_results<const char * > & m)37 bool grep_callback(const boost::match_results<const char*>& m)
38 {
39    if(m[0].matched == false)
40    {
41       // save position and return:
42       next_pos = m[0].first;
43    }
44    else
45       ++tags;
46    return true;
47 }
48 
search(std::istream & is)49 void search(std::istream& is)
50 {
51    char buf[4096];
52    next_pos = buf + sizeof(buf);
53    bool have_more = true;
54    while(have_more)
55    {
56       // how much do we copy forward from last try:
57       std::ptrdiff_t leftover = (buf + sizeof(buf)) - next_pos;
58       // and how much is left to fill:
59       std::ptrdiff_t size = next_pos - buf;
60       // copy forward whatever we have left:
61       std::memmove(buf, next_pos, leftover);
62       // fill the rest from the stream:
63       is.read(buf + leftover, size);
64       std::streamsize read = is.gcount();
65       // check to see if we've run out of text:
66       have_more = read == size;
67       // reset next_pos:
68       next_pos = buf + sizeof(buf);
69       // and then grep:
70       boost::regex_grep<bool(*)(const boost::cmatch&), const char*>(grep_callback,
71                         static_cast<const char*>(buf),
72                         static_cast<const char*>(buf + read + leftover),
73                         e,
74                         boost::match_default | boost::match_partial);
75    }
76 }
77 
main(int argc,char * argv[])78 int main(int argc, char* argv[])
79 {
80    if(argc > 1)
81    {
82       for(int i = 1; i < argc; ++i)
83       {
84          std::ifstream fs(argv[i]);
85          if(fs.bad()) continue;
86          search(fs);
87          fs.close();
88       }
89    }
90    else
91    {
92       std::string one("<META NAME=\"keywords\" CONTENT=\"regex++, regular expressions, regular expression library, C++\">");
93       std::string what;
94       while(what.size() < 10000)
95       {
96          what.append(one);
97          what.append(13, ' ');
98       }
99       std::stringstream ss;
100       ss.str(what);
101       search(ss);
102    }
103    std::cout << "total tag count was " << tags << std::endl;
104    return 0;
105 }
106 
107 
108 
109 
110