1 /**
2  * Copyright (c) 2007-2012, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef sequence_sink_hh
31 #define sequence_sink_hh
32 
33 #include <map>
34 
35 #include "bookmarks.hh"
36 #include "grep_proc.hh"
37 #include "sequence_matcher.hh"
38 #include "listview_curses.hh"
39 
40 class sequence_sink : public grep_proc_sink<vis_line_t> {
41 public:
sequence_sink(sequence_matcher & sm,bookmark_vector<vis_line_t> & bv)42     sequence_sink(sequence_matcher &sm, bookmark_vector<vis_line_t> &bv)
43         : ss_matcher(sm),
44           ss_bookmarks(bv) {};
45 
grep_match(grep_proc<vis_line_t> & gp,vis_line_t line,int start,int end)46     void grep_match(grep_proc<vis_line_t> &gp,
47                     vis_line_t line,
48                     int start,
49                     int end)
50     {
51         this->ss_line_values.clear();
52     };
53 
grep_capture(grep_proc<vis_line_t> & gp,vis_line_t line,int start,int end,char * capture)54     void grep_capture(grep_proc<vis_line_t> &gp,
55                       vis_line_t line,
56                       int start,
57                       int end,
58                       char *capture)
59     {
60         if (start == -1) {
61             this->ss_line_values.push_back("");
62         }
63         else{
64             this->ss_line_values.push_back(std::string(capture));
65         }
66     };
67 
grep_match_end(grep_proc<vis_line_t> & gp,vis_line_t line)68     void grep_match_end(grep_proc<vis_line_t> &gp, vis_line_t line)
69     {
70         sequence_matcher::id_t line_id;
71 
72         this->ss_matcher.identity(this->ss_line_values, line_id);
73 
74         std::vector<vis_line_t> &line_state = this->ss_state[line_id];
75         if (this->ss_matcher.match(this->ss_line_values,
76                                    line_state,
77                                    line)) {
78             std::vector<vis_line_t>::iterator iter;
79 
80             for (iter = line_state.begin();
81                  iter != line_state.end();
82                  ++iter) {
83                 this->ss_bookmarks.insert_once(vis_line_t(*iter));
84             }
85             line_state.clear();
86         }
87     };
88 
89 private:
90     sequence_matcher &           ss_matcher;
91     bookmark_vector<vis_line_t> &ss_bookmarks;
92     std::vector<std::string>     ss_line_values;
93     std::map<sequence_matcher::id_t, std::vector<vis_line_t> > ss_state;
94 };
95 #endif
96