/
Colorizer.cc
255 lines (208 loc) · 5.64 KB
/
Colorizer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
colortail -- output last part of file(s) in color.
Copyright (C) 2009 Joakim Andersson <joakim@pjek.se>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "Colorizer.h"
#include "CfgFileParser.h"
#include "TailFile.h"
#include <assert.h>
#include <iostream>
#include <string.h>
using namespace std;
Colorizer::Colorizer()
{
// default constructor
m_items_list = NULL;
}
void Colorizer::free_items()
{
// frees the memory that the items uses
// check if there is a list
if (m_items_list)
{
SearchData *tmp;
// go through all the elements in the list,
// and free each SearchData instance
while (!m_items_list->is_empty())
{
tmp = m_items_list->first_element();
m_items_list->remove_first();
delete tmp;
}
// delete the list
delete m_items_list;
m_items_list = NULL;
}
}
Colorizer::Colorizer(const char *cfg_file)
{
// other constructor
// sends the cfg_file to the CfgFileParser
// gets the items_list from the CfgFileParser and saves it
// check that cfg_file isn't NULL
assert (cfg_file != NULL);
CfgFileParser parser;
// parse the cfg file
int n = parser.parse(cfg_file);
// get the items list
m_items_list = parser.get_items_list();
}
Colorizer::~Colorizer()
{
// destructor
// free the memory the search data items use
free_items();
}
string Colorizer::colorize(const char *str)
{
// colorize the string, returns a new string containing
// the colorized version of str
// RETURN: new string with result
// check that a list exists
assert (m_items_list != NULL);
regmatch_t pmatch[10];
int found = 0, submatch = 0, j;
char color[MAX_CHARS_READ][20];
// set all the color strings to empty strings
for (int f = 0 ; f < MAX_CHARS_READ ; f++)
{
color[f][0] = '\0';
}
// make an iterator
ListIterator<SearchData*> itr(*m_items_list);
// will contain the new string
ostringstream newstr;
SearchData *current;
int i = 0;
// go through all the elements in the list
for (itr.init() ; !itr ; ++itr, i++)
{
current = itr();
// check for match
if (regexec(¤t->m_preg, str, 10, pmatch, 0) == 0)
{
// TODO: check for callback function
// TODO: call callback function
found = 1;
// Check to see if this is a substring match
if (pmatch[1].rm_so != -1)
{
submatch = 1;
// Note: start at offset 1 to get only submatches
for (j = 1 ; pmatch[j].rm_so != -1 && j <= 10 ; j++)
{
// go through all the character positions
// that the submatch is for
for (int k = pmatch[j].rm_so ; k < (pmatch[j].rm_eo) ; k++)
{
// set the color in the color string array
strcpy(color[k], current->m_ansi_color_code);
}
}
}
else
{
// not a substring match, colorize whole string.
// stop looking for other matches as well
submatch = 0;
// write ansi color code
newstr << current->m_ansi_color_code;
// check if str ends in '\n'
int len = strlen(str);
if (str[len-1] == '\n')
{
for (int a = 0 ; a < len-1 ; a++)
{
newstr.put(str[a]);
}
}
else
{
// doesn't end in '\n'
newstr << str;
}
// write ansi reset str and a newline
newstr << ANSI_RESET_STR << endl << ends;
// return the new string
return newstr.str();
}
}
}
// did we get a match
if (found == 0)
{
// no we didn't
// print without color
// check if str ends in '\n'
if (str[strlen(str)-1] == '\n')
{
newstr << str << ends;
}
else
{
// doesn't end in '\n'
newstr << str << endl << ends;
}
// return the new string
return newstr.str();
}
// did we find submatches?
if (submatch == 1)
{
int last_was_reset_str = 1;
string last_color_was;
// iiterate through all the characters
int l = strlen(str);
for (i = 0 ; i < l ; i++)
{
if (color[i][0] == '\0')
{
// no color for this position
if (last_was_reset_str != 1)
{
// write reset string
newstr << ANSI_RESET_STR;
// remeber that we wrote it
last_was_reset_str = 1;
last_color_was = string();
}
}
else if ( last_color_was != color[i] )
{
// a color for this position
// reset ansi reset string checker
last_was_reset_str = 0;
// write color
newstr << color[i];
// save color
last_color_was = color[i];
}
// write current character
newstr.put(str[i]);
}
// check if last wasn't the ansi reset string
if (last_was_reset_str != 1)
{
// write reset string
newstr << ANSI_RESET_STR;
}
// write newline and null
//newstr << endl << ends;
newstr << ends;
return newstr.str();
}
// should never get here
string empty = "";
return empty;
}