File: CfgFileParser.cc

package info (click to toggle)
colortail 0.3.3-1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, squeeze, stretch, wheezy
  • size: 596 kB
  • ctags: 169
  • sloc: cpp: 1,547; sh: 705; makefile: 27
file content (553 lines) | stat: -rw-r--r-- 11,747 bytes parent folder | download
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
    colortail -- output last part of file(s) in color.
    Copyright (C) 2009  Joakim Andersson <ja@joakimandersson.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 "CfgFileParser.h"

#include <iostream>
#include <assert.h>
#include <cstring>
#include <stdlib.h>

using namespace std;

// ## class SearchData ##

SearchData::SearchData()
{
   // default constructor
   m_preg = NULL;
   m_ansi_color_code = NULL;
   m_pf = NULL;
   m_param_to_callback_fkn = NULL;
}

SearchData::~SearchData()
{
   if (m_preg)
   {
      // is allocated with malloc
      free(m_preg);
   }

   if (m_ansi_color_code)
   {
      delete m_ansi_color_code;
   }

   if (m_param_to_callback_fkn)
   {
      delete m_param_to_callback_fkn;
   }
}

void SearchData::set_color(char *color)
{
   // allocates memory and sets the m_ansi_color_code string to color

   if (m_ansi_color_code)
   {
      delete m_ansi_color_code;
   }

   m_ansi_color_code = new char[strlen(color)+1];
   strcpy(m_ansi_color_code, color);
}

CfgFileParser::CfgFileParser()
{
   m_items_list = NULL;
   m_filename = NULL;
   m_line = 0;
}

CfgFileParser::~CfgFileParser()
{
   free_items();
   if (m_filename)
   {
      delete m_filename;
   }
}

void CfgFileParser::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;
   }
}

char* CfgFileParser::read_line()
{
   // reads the next line from the file and increases the line counter
   // prints error message if no characters were read.
   // returns: string or NULL if error.

   char *str = new char[MAX_CFG_LINE_LENGTH];
   assert (str != NULL);
   
   // read next line
   m_infile.getline(str, MAX_CFG_LINE_LENGTH);

   // increase line counter
   m_line++;
   
   // read less than zero chars?
   if (m_infile.gcount() <= 0)
   {
      // found EOF
      delete str;
      return NULL;
   }

   return str;
}

int CfgFileParser::read_item()
{
   // reads the color for the reg exps, then eads the reg exps and makes
   // SearchData instances for them and adds to the m_items_list
   // only reads and processes ONE item
   // returns number of SearchData instances added to the list

   int nr_items_added = 0;
   
   // is there a list?
   assert (m_items_list != NULL);

   // is there a open file?
   assert (m_infile);

   // read color
   char *color = read_color();

   if (color == NULL)
   {
      // didn't get a color

      // quit, return 0
      return 0;
   }
   
   // TODO: add action reading
   // read action

   if (read_left() == 0)
   {
      // found '{'

      // loop until '}'
      while (1)
      {
	 // read string
	 char *regexp = read_regexp();
	 if (regexp)
	 {
	    // check if it's a '}'
	    if (regexp[0] == '}')
	    {
	       // free mem
	       delete regexp;
	       // stop looping
	       break;
	    }
	    
	    // has a regexp.. make a SearchData item
	    
	    SearchData *searchdata = new SearchData();
	    // check allocation
	    assert (searchdata != NULL);
	    
	    // set color
	    searchdata->set_color(color);
	    
	    // allocate memory to the pattern storage buffer
	    searchdata->m_preg = (regex_t*) malloc(sizeof(regex_t));
	    // check allocation
	    assert (searchdata->m_preg != NULL);
	    
	    // make compiled pattern buffer
	    if (regcomp(searchdata->m_preg, regexp, REG_EXTENDED) != 0)
	    {
	       // failed to make compiled reg exp pattern
	       cout << "colortail: Failed to make compiled reg exp pattern for "
		    << "reg exp in config file " << m_filename << " at line "
		    << m_line << ". Skipping this line." << endl;
	       
	       // free mem
	       delete searchdata;
	    }
	    else
	    {
	       // TODO: set callback fkn
	       // TODO: set param to callback fkn
	       
	       // add the search data item to the items list
	       m_items_list->add(searchdata);
	       // increase items added counter
	       nr_items_added++;
	    }
	    // free mem
	    delete regexp;
	 }
	 else
	 {
	    // error reading string, eof maybe..
	    // stop looping
	    break;
	 }
      }
   }
   return nr_items_added;
}

char* CfgFileParser::read_color()
{
   // reads and skips lines until a 'COLOR' statement is found,
   // it then extracts the color after 'COLOR' and returns a new string
   // containing the ANSI color code for that string.
   // RETURNS: the new string, NULL on error.

   // is there a open file?
   assert (m_infile);

   while (1)
   {
      // read line
      char *tmp = read_line();
      if (!tmp)
      {
	 	// found EOF
	 	return NULL;
      }

      // got a line to look at

      // process line if it's not empty or doesn't starts with a '#'
      if (tmp[0] != '\0' && tmp[0] != '#')
      {
	 if (strncmp(tmp, "COLOR", 5) != 0)
	 {
	    cout << "colortail: Error in config file: " << m_filename
		 << " at line " << m_line << ". Skipping this line." << endl;
	 }
	 else 
	 {
	    int linepos = 5;
	    char *color = new char[20];

	    // skip all spaces
	    while (tmp[linepos] == ' ')
	    {
	       linepos++;
	    }

	    // read which color it is
	    int found_color = 0;

	    if (strncmp(&tmp[linepos], "black", 5) == 0) {
	       strcpy(color, BLACK);
	       found_color = 1;
	    }

	    if (strncmp(&tmp[linepos], "red", 3) == 0) {
	       strcpy(color, RED);
	       found_color = 1;
	    }
	    
	    if (strncmp(&tmp[linepos], "green", 5) == 0) {
	       strcpy(color, GREEN);
	       found_color = 1;
	    }

	    if (strncmp(&tmp[linepos], "yellow", 6) == 0) {
	       strcpy(color, YELLOW);
	       found_color = 1;
	    }

	    if (strncmp(&tmp[linepos], "blue", 4) == 0) {
	       strcpy(color, BLUE);
	       found_color = 1;
	    }

	    if (strncmp(&tmp[linepos], "magenta", 7) == 0) {
	       strcpy(color, MAGENTA);
	       found_color = 1;
	    }

	    if (strncmp(&tmp[linepos], "cyan", 4) == 0) {
	       strcpy(color, CYAN);
	       found_color = 1;
	    }

	    if (strncmp(&tmp[linepos], "white", 6) == 0) {
	       strcpy(color, WHITE);
	       found_color = 1;
	    }

	    if (strncmp(&tmp[linepos], "brightblack", 6+5) == 0) {
	       strcpy(color, BRIGHTBLACK);
	       found_color = 1;
	    }

	    if (strncmp(&tmp[linepos], "brightred", 6+3) == 0) {
	       strcpy(color, BRIGHTRED);
	       found_color = 1;
	    }
	    
	    if (strncmp(&tmp[linepos], "brightgreen", 6+5) == 0) {
	       strcpy(color, BRIGHTGREEN);
	       found_color = 1;
	    }

	    if (strncmp(&tmp[linepos], "brightyellow", 6+6) == 0) {
	       strcpy(color, BRIGHTYELLOW);
	       found_color = 1;
	    }

	    if (strncmp(&tmp[linepos], "brightblue", 6+4) == 0) {
	       strcpy(color, BRIGHTBLUE);
	       found_color = 1;
	    }

	    if (strncmp(&tmp[linepos], "brightmagenta", 6+7) == 0) {
	       strcpy(color, BRIGHTMAGENTA);
	       found_color = 1;
	    }
	    
            if (strncmp(&tmp[linepos], "brightcyan", 6+4) == 0) {
	       strcpy(color, BRIGHTCYAN);
	       found_color = 1;
	    }

	    if (strncmp(&tmp[linepos], "brightwhite", 6+5) == 0) {
	       strcpy(color, BRIGHTWHITE);
	       found_color = 1;
	    }

	    if (found_color == 0)
	    {
	       // didn't found color
	       cout << "colortail: Don't recognize color in config file: "
		    << m_filename << " at line " << m_line << endl;

	       // free mem
	       delete tmp;
	       delete color;
	       // error, return NULL
	       return NULL;
	    }
	    else
	    {
	       // free mem
	       delete tmp;
	       // found color
	       return color;
	    }
	 }
	 // free memory for read line
	 delete tmp;
      }
   }
   // should never execute this
   return NULL;
}

int CfgFileParser::read_left()
{
   // reads lines (skips empty and comments lines) until a '{' is found
   // RETURNS: 0 if a '{' is found, 1 if not found, 2 if error

   // is there a open file?
   assert (m_infile);

   while (1)
   {
      // read line
      char *tmp = read_line();
      if (!tmp)
      {
	 // error reading line
	 cout << "colortail: Error reading line in config file: "
	      << m_filename << " at line " << m_line << "." << endl;
	 // error, return 2
	 return 2;
      }

      // got a line to look at

      // process line if it's not empty or doesn't starts with a '#'
      if (tmp[0] != '\n' && tmp[0] != '#')
      {
	 if (tmp[0] == '{')
	 {
	    // free mem
	    delete tmp;

	    return 0;
	 }
	 else
	 {
	    // not a '{'
	    cout << "colortail: Error, expected '{' but found '"
		 << tmp[0] << "' in config file: " << m_filename
		 << " at line " << m_line << "." << endl;
	    // free mem
	    delete tmp;

	    return 1;
	 }
      }

      // free mem
      delete tmp;
   }

   // should never get this far
   return 1;
}

char* CfgFileParser::read_regexp()
{
   // reads and skips empty and comments lines until a regexp is found
   // RETURNS: a new string containing the reg exp, NULL on error

   // is there a open file?
   assert (m_infile);

   while (1)
   {
      // read line
      char *tmp = read_line();

      if (!tmp)
      {
	 // error reading line
	 cout << "colortail: Error reading line in config file: "
	      << m_filename << " at line " << m_line << "." << endl;

	 // error, return NULL
	 return NULL;
      }

      // got a line

      // process line if it's not empty or doesn't starts with a '#'
      if (tmp[0] != '\n' && tmp[0] != '#')
      {
	 // found a string, return it
	 return tmp;
      }

      // this is a empty line or a comment, skip it

      // free mem
      delete tmp;
   }

   // should never get this far
   return NULL;
}
	 

int CfgFileParser::parse(const char *filename)
{
   // parses the cfg file and sets up the list of SearchData elements
   // returns number of SearchData items created

   // is there a list?
   if (m_items_list)
   {
      // delete it
      free_items();
   }

   // make a new list
   m_items_list = new List<SearchData*>;

   // check the allocation
   assert (m_items_list != NULL);
  
   // try to open the file
   m_infile.open(filename, ios::in);

   if (!m_infile)
   {
      // open failed
      cout << "colortail: Failed to open config file: " << filename << endl;
      return 0;
   }

   // save filename of config file
   if (m_filename)
   {
      delete m_filename;
   }

   m_filename = new char[strlen(filename) + 1];
   strcpy(m_filename, filename);
   

   int items_counter = 0;
   
   // read the items
   while (!m_infile.eof())
   {
      // read in a item
      int n = read_item();

      items_counter += n;
   }
   
   return items_counter;
}
      
List<SearchData*>* CfgFileParser::get_items_list()
{
   // returns the items list, and sets this class item list to NULL

   List<SearchData*>* tmp = m_items_list;
   m_items_list = NULL;

   return tmp;
}