1 | //----------------------------------------------------------- |
---|
2 | // Copyright Christian Arnault LAL-Orsay CNRS |
---|
3 | // arnault@lal.in2p3.fr |
---|
4 | // See the complete license in cmt_license.txt "http://www.cecill.info". |
---|
5 | //----------------------------------------------------------- |
---|
6 | |
---|
7 | #ifndef __cmt_awk_h__ |
---|
8 | #define __cmt_awk_h__ |
---|
9 | |
---|
10 | #include "cmt_std.h" |
---|
11 | #include "cmt_string.h" |
---|
12 | #include "cmt_regexp.h" |
---|
13 | |
---|
14 | //------------------------------------------------ |
---|
15 | // |
---|
16 | // Emulation of the awk program. |
---|
17 | // |
---|
18 | // The run method reads the specified file, and applies the |
---|
19 | // <filter> virtual method onto each selected line. It returns |
---|
20 | // a condition (see below) |
---|
21 | // |
---|
22 | // o empty lines are ignored |
---|
23 | // o when the optional pattern is specified, only lines matching it |
---|
24 | // are considered. |
---|
25 | // |
---|
26 | // The virtual <begin> method is applied before any filter |
---|
27 | // The virtual <filter> method is applied onto each line |
---|
28 | // The virtual <end> method is applied after all filters |
---|
29 | // |
---|
30 | // The condition (one of "ok", "stopped" or "failed") will be |
---|
31 | // generated if : |
---|
32 | // o everything run well (ok) |
---|
33 | // o the <stop> method is called (stopped) |
---|
34 | // o an error occurred or the <abort> method is called (failed) |
---|
35 | // |
---|
36 | // State variables are maintained for derived awk filters |
---|
37 | // |
---|
38 | // m_line_number the current line number (empty lines |
---|
39 | // are counted) |
---|
40 | // |
---|
41 | // m_continuation_allowed when true, take into account the |
---|
42 | // trailing backslashes, so as to |
---|
43 | // first accumulate continued lines |
---|
44 | // into one |
---|
45 | // |
---|
46 | // Derived classes: |
---|
47 | // ---------------- |
---|
48 | // |
---|
49 | // FAwk : scans a file |
---|
50 | // |
---|
51 | // State variables: |
---|
52 | // |
---|
53 | // m_dir_name the path of the currently read file |
---|
54 | // m_file_name the name of the currently read file |
---|
55 | // |
---|
56 | // PAwk : execute a command and scans its output |
---|
57 | // |
---|
58 | //------------------------------------------------ |
---|
59 | class Awk |
---|
60 | { |
---|
61 | public: |
---|
62 | typedef enum {ok, stopped, failed} condition; |
---|
63 | Awk (); |
---|
64 | virtual ~Awk (); |
---|
65 | condition run (const cmt_string& text, const cmt_string& pattern = ""); |
---|
66 | condition run (const cmt_string& text, const cmt_regexp& expression); |
---|
67 | void stop (); |
---|
68 | void abort (); |
---|
69 | void allow_continuation (); |
---|
70 | condition get_last_condition () const; |
---|
71 | |
---|
72 | virtual void begin (); |
---|
73 | virtual void filter (const cmt_string& line); |
---|
74 | virtual void end (); |
---|
75 | |
---|
76 | void inc_line_number (); |
---|
77 | |
---|
78 | protected: |
---|
79 | int m_line_number; |
---|
80 | condition m_condition; |
---|
81 | bool m_continuation_allowed; |
---|
82 | }; |
---|
83 | |
---|
84 | class FAwk : public Awk |
---|
85 | { |
---|
86 | public: |
---|
87 | condition run (const cmt_string& file_name, const cmt_string& pattern = ""); |
---|
88 | condition run (const cmt_string& text, const cmt_regexp& expression); |
---|
89 | protected: |
---|
90 | cmt_string m_dir_name; |
---|
91 | cmt_string m_file_name; |
---|
92 | }; |
---|
93 | |
---|
94 | class PAwk : public Awk |
---|
95 | { |
---|
96 | public: |
---|
97 | condition run (const cmt_string& command, const cmt_string& pattern = ""); |
---|
98 | condition run (const cmt_string& text, const cmt_regexp& expression); |
---|
99 | }; |
---|
100 | |
---|
101 | /** |
---|
102 | * This scans a path and looks for CMT packages. |
---|
103 | * Whenever it finds one, it applies the specified actor to it. |
---|
104 | */ |
---|
105 | class PathScanner |
---|
106 | { |
---|
107 | public: |
---|
108 | class actor |
---|
109 | { |
---|
110 | public: |
---|
111 | virtual void run (const cmt_string& package, |
---|
112 | const cmt_string& version, |
---|
113 | const cmt_string& path, |
---|
114 | const bool without_version_directory=false) |
---|
115 | { |
---|
116 | } |
---|
117 | }; |
---|
118 | |
---|
119 | PathScanner (); |
---|
120 | bool scan_path (const cmt_string& path, actor& a); |
---|
121 | bool scan_package (const cmt_string& path, const cmt_string& package); |
---|
122 | |
---|
123 | private: |
---|
124 | void scan_path (const cmt_string& path, int level, actor& a); |
---|
125 | |
---|
126 | bool _running; |
---|
127 | int _level; |
---|
128 | }; |
---|
129 | |
---|
130 | |
---|
131 | #endif |
---|