00001 #ifndef __cmt_awk_h__ 00002 #define __cmt_awk_h__ 00003 00004 #include "cmt_std.h" 00005 #include "cmt_string.h" 00006 #include "cmt_regexp.h" 00007 00008 //------------------------------------------------ 00009 // 00010 // Emulation of the awk program. 00011 // 00012 // The run method reads the specified file, and applies the 00013 // <filter> virtual method onto each selected line. It returns 00014 // a condition (see below) 00015 // 00016 // o empty lines are ignored 00017 // o when the optional pattern is specified, only lines matching it 00018 // are considered. 00019 // 00020 // The virtual <begin> method is applied before any filter 00021 // The virtual <filter> method is applied onto each line 00022 // The virtual <end> method is applied after all filters 00023 // 00024 // The condition (one of "ok", "stopped" or "failed") will be 00025 // generated if : 00026 // o everything run well (ok) 00027 // o the <stop> method is called (stopped) 00028 // o an error occurred or the <abort> method is called (failed) 00029 // 00030 // State variables are maintained for derived awk filters 00031 // 00032 // m_line_number the current line number (empty lines 00033 // are counted) 00034 // 00035 // m_continuation_allowed when true, take into account the 00036 // trailing backslashes, so as to 00037 // first accumulate continued lines 00038 // into one 00039 // 00040 // Derived classes: 00041 // ---------------- 00042 // 00043 // FAwk : scans a file 00044 // 00045 // State variables: 00046 // 00047 // m_dir_name the path of the currently read file 00048 // m_file_name the name of the currently read file 00049 // 00050 // PAwk : execute a command and scans its output 00051 // 00052 //------------------------------------------------ 00053 class Awk 00054 { 00055 public: 00056 typedef enum {ok, stopped, failed} condition; 00057 Awk (); 00058 virtual ~Awk (); 00059 condition run (const cmt_string& text, const cmt_string& pattern = ""); 00060 condition run (const cmt_string& text, const cmt_regexp& expression); 00061 void stop (); 00062 void abort (); 00063 void allow_continuation (); 00064 condition get_last_condition () const; 00065 00066 virtual void begin (); 00067 virtual void filter (const cmt_string& line); 00068 virtual void end (); 00069 protected: 00070 int m_line_number; 00071 condition m_condition; 00072 bool m_continuation_allowed; 00073 }; 00074 00075 class FAwk : public Awk 00076 { 00077 public: 00078 condition run (const cmt_string& file_name, const cmt_string& pattern = ""); 00079 condition run (const cmt_string& text, const cmt_regexp& expression); 00080 protected: 00081 cmt_string m_dir_name; 00082 cmt_string m_file_name; 00083 }; 00084 00085 class PAwk : public Awk 00086 { 00087 public: 00088 condition run (const cmt_string& command, const cmt_string& pattern = ""); 00089 condition run (const cmt_string& text, const cmt_regexp& expression); 00090 }; 00091 00092 #endif