1 /*
2     colortail -- output last part of file(s) in color.
3     Copyright (C) 2009  Joakim Andersson <ja@joakimandersson.se>
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19 
20 #ifndef _CfgFileParser_h_
21 #define _CfgFileParser_h_
22 
23 #include "List.h"
24 #include "config.h"
25 
26 #include <sys/types.h>
27 #include <iostream>
28 #include <fstream>
29 #include <string>
30 
31 #ifdef HAVE_GNUREGEX_H
32 # include <gnuregex.h>
33 #else
34 # ifdef HAVE_REGEX_H
35 #  include <regex.h>
36 # else
37 #  error "You don't have regex.h or gnuregex.h"
38 # endif
39 #endif
40 
41 
42 class SearchData
43 {
44   public:
45    SearchData();
46    ~SearchData();
47    void set_color(char *color);
48 
49    regex_t *m_preg;               // pattern storage buffer
50    char *m_ansi_color_code;       // color for row if match
51    void (*m_pf)(char*);           // callback fkn for action
52    char *m_param_to_callback_fkn; // parameters to callback fkn
53 };
54 
55 
56 // ###- color defines -###
57 // foreground
58 #define BLACK         "\033[0;30m"
59 #define RED           "\033[0;31m"
60 #define GREEN         "\033[0;32m"
61 #define YELLOW        "\033[0;33m"
62 #define BLUE          "\033[0;34m"
63 #define MAGENTA       "\033[0;35m"
64 #define CYAN          "\033[0;36m"
65 #define WHITE         "\033[0;37m"
66 #define BRIGHTBLACK   "\033[1;30m"
67 #define BRIGHTRED     "\033[1;31m"
68 #define BRIGHTGREEN   "\033[1;32m"
69 #define BRIGHTYELLOW  "\033[1;33m"
70 #define BRIGHTBLUE    "\033[1;34m"
71 #define BRIGHTMAGENTA "\033[1;35m"
72 #define BRIGHTCYAN    "\033[1;36m"
73 #define BRIGHTWHITE   "\033[1;37m"
74 
75 // background
76 // TODO: fix this
77 
78 #define MAX_CFG_LINE_LENGTH 200
79 
80 class CfgFileParser
81 {
82   private:
83    List<SearchData*> *m_items_list;
84    std::ifstream m_infile;
85    char *m_filename;
86    int m_line;
87 
88    void free_items();
89    char* read_line();
90 
91    int read_item();
92    char* read_color();
93    int read_left();
94    char* read_regexp();
95 
96 
97   public:
98    CfgFileParser();
99    ~CfgFileParser();
100 
101    int parse(const char *filename);
102    List<SearchData*>* get_items_list();
103 };
104 
105 #endif
106