1 | #ifndef __cmt_awk_h__ |
---|
2 | #define __cmt_awk_h__ |
---|
3 | |
---|
4 | #include "cmt_std.h" |
---|
5 | #include "cmt_string.h" |
---|
6 | #include "cmt_regexp.h" |
---|
7 | |
---|
8 | //------------------------------------------------ |
---|
9 | // |
---|
10 | // Emulation of the awk program. |
---|
11 | // |
---|
12 | // The run method reads the specified file, and applies the |
---|
13 | // <filter> virtual method onto each selected line. It returns |
---|
14 | // a condition (see below) |
---|
15 | // |
---|
16 | // o empty lines are ignored |
---|
17 | // o when the optional pattern is specified, only lines matching it |
---|
18 | // are considered. |
---|
19 | // |
---|
20 | // The virtual <begin> method is applied before any filter |
---|
21 | // The virtual <filter> method is applied onto each line |
---|
22 | // The virtual <end> method is applied after all filters |
---|
23 | // |
---|
24 | // The condition (one of "ok", "stopped" or "failed") will be |
---|
25 | // generated if : |
---|
26 | // o everything run well (ok) |
---|
27 | // o the <stop> method is called (stopped) |
---|
28 | // o an error occurred or the <abort> method is called (failed) |
---|
29 | // |
---|
30 | // State variables are maintained for derived awk filters |
---|
31 | // |
---|
32 | // m_line_number the current line number (empty lines |
---|
33 | // are counted) |
---|
34 | // |
---|
35 | // m_continuation_allowed when true, take into account the |
---|
36 | // trailing backslashes, so as to |
---|
37 | // first accumulate continued lines |
---|
38 | // into one |
---|
39 | // |
---|
40 | // Derived classes: |
---|
41 | // ---------------- |
---|
42 | // |
---|
43 | // FAwk : scans a file |
---|
44 | // |
---|
45 | // State variables: |
---|
46 | // |
---|
47 | // m_dir_name the path of the currently read file |
---|
48 | // m_file_name the name of the currently read file |
---|
49 | // |
---|
50 | // PAwk : execute a command and scans its output |
---|
51 | // |
---|
52 | //------------------------------------------------ |
---|
53 | class Awk |
---|
54 | { |
---|
55 | public: |
---|
56 | typedef enum {ok, stopped, failed} condition; |
---|
57 | Awk (); |
---|
58 | virtual ~Awk (); |
---|
59 | condition run (const cmt_string& text, const cmt_string& pattern = ""); |
---|
60 | condition run (const cmt_string& text, const cmt_regexp& expression); |
---|
61 | void stop (); |
---|
62 | void abort (); |
---|
63 | void allow_continuation (); |
---|
64 | condition get_last_condition () const; |
---|
65 | |
---|
66 | virtual void begin (); |
---|
67 | virtual void filter (const cmt_string& line); |
---|
68 | virtual void end (); |
---|
69 | protected: |
---|
70 | int m_line_number; |
---|
71 | condition m_condition; |
---|
72 | bool m_continuation_allowed; |
---|
73 | }; |
---|
74 | |
---|
75 | class FAwk : public Awk |
---|
76 | { |
---|
77 | public: |
---|
78 | condition run (const cmt_string& file_name, const cmt_string& pattern = ""); |
---|
79 | condition run (const cmt_string& text, const cmt_regexp& expression); |
---|
80 | protected: |
---|
81 | cmt_string m_dir_name; |
---|
82 | cmt_string m_file_name; |
---|
83 | }; |
---|
84 | |
---|
85 | class PAwk : public Awk |
---|
86 | { |
---|
87 | public: |
---|
88 | condition run (const cmt_string& command, const cmt_string& pattern = ""); |
---|
89 | condition run (const cmt_string& text, const cmt_regexp& expression); |
---|
90 | }; |
---|
91 | |
---|
92 | #endif |
---|