summaryrefslogtreecommitdiff
path: root/src/libnml/rcs/rcs_print.cc
blob: b95421ee0d64bb2eaf97c9c95a8078c4ccaa3bdb (plain)
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/********************************************************************
* Description: rcs_print.cc
*
*   Derived from a work by Fred Proctor & Will Shackleford
*
* Author:
* License: LGPL Version 2
* System: Linux
*    
* Copyright (c) 2004 All rights reserved.
*
* Last change: 
********************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

#include <stdarg.h>		/* va_list, va_start(), va_end() */
#include <stdio.h>		/* __printf()'s */
#include <string.h>		/* strchr(), memmove() */
#include <stdlib.h>		/* malloc(), free(), realloc() */
#include <errno.h>		// errno()

#include <sys/types.h>
#include <unistd.h>		/* getpid() */

#ifdef __cplusplus
}
#endif
#include "rcs_print.hh"
#include "linklist.hh"
#ifndef _TIMER_H
extern "C" double etime(void);
#endif

LinkedList *rcs_print_list = NULL;
char **rcs_lines_table = NULL;
void (*rcs_print_notify) () = NULL;
RCS_PRINT_DESTINATION_TYPE rcs_print_destination = RCS_PRINT_TO_STDOUT;

int max_rcs_errors_to_print = 30;
int rcs_errors_printed = 0;

long rcs_print_mode_flags = PRINT_RCS_ERRORS;
FILE *rcs_print_file_stream = NULL;
char rcs_print_file_name[80] = "rcs_out.txt";

char last_error_bufs[4][100];
int error_bufs_initialized = 0;
int last_error_buf_filled = 0;

void set_rcs_print_destination(RCS_PRINT_DESTINATION_TYPE _dest)
{
    if (rcs_print_destination == RCS_PRINT_TO_NULL) {
	rcs_errors_printed = 0;
    }
    rcs_print_destination = _dest;
}

RCS_PRINT_DESTINATION_TYPE get_rcs_print_destination()
{
    return (rcs_print_destination);
}

char **get_rcs_lines_table()
{
    return (rcs_lines_table);
}

LinkedList *get_rcs_print_list()
{
    return (rcs_print_list);
}

int get_rcs_print_list_size()
{
    if (NULL != rcs_print_list) {
	return (rcs_print_list->list_size);
    } else {
	return (-1);
    }
}
void
set_rcs_print_list_sizing(int _new_max_size,
    LIST_SIZING_MODE _new_sizing_mode)
{
    if (NULL == rcs_print_list) {
	rcs_print_list = new LinkedList;
    }
    if (NULL != rcs_print_list) {
	rcs_print_list->set_list_sizing_mode(_new_max_size, _new_sizing_mode);
    }
}
void set_rcs_print_notify(RCS_PRINT_NOTIFY_FUNC_PTR _rcs_print_notify)
{
    rcs_print_notify = _rcs_print_notify;
}

void clean_print_list()
{
    if (NULL != rcs_print_list) {
	delete rcs_print_list;
	rcs_print_list = NULL;
    }
}

void output_print_list(int output_func(char *))
{
    if (NULL != rcs_print_list) {
	char *string_from_list;
	string_from_list = (char *) rcs_print_list->get_head();
	while (NULL != string_from_list) {
	    if (output_func(string_from_list) != EOF) {
		break;
	    }
	    string_from_list = (char *) rcs_print_list->get_next();
	}
    }
}

int count_characters_in_print_list()
{
    int count = 0;
    if (NULL != rcs_print_list) {
	char *string_from_list;
	string_from_list = (char *) rcs_print_list->get_head();
	while (NULL != string_from_list) {
	    count += strlen(string_from_list);
	    string_from_list = (char *) rcs_print_list->get_next();
	}
    }
    return (count);
}

int count_lines_in_print_list()
{
    int count = 1;
    if (NULL != rcs_print_list) {
	char *string_from_list;
	string_from_list = (char *) rcs_print_list->get_head();
	while (NULL != string_from_list) {
	    char *line;
	    line = strchr(string_from_list, '\n');
	    while (NULL != line) {
		count++;
		line = strchr(line + 1, '\n');
	    }
	    string_from_list = (char *) rcs_print_list->get_next();
	}
    }
    return (count);
}

void convert_print_list_to_lines()
{
    char *temp_buf = NULL;
    static int last_id_converted = -1;
    if (NULL != rcs_print_list) {
	char *string_from_list;
	if (-1 == last_id_converted) {
	    string_from_list = (char *) rcs_print_list->get_head();
	} else {
	    string_from_list =
		(char *) rcs_print_list->get_first_newer(last_id_converted);
	}
	while (NULL != string_from_list) {
	    char *next_line;
	    next_line = strchr(string_from_list, '\n');
	    if (NULL == next_line) {
		if (NULL == temp_buf) {
		    temp_buf = (char *) malloc(strlen(string_from_list) + 1);
		    strcpy(temp_buf, string_from_list);
		} else {
		    temp_buf = (char *) realloc(temp_buf, strlen(temp_buf)
			+ strlen(string_from_list) + 1);
		    strcat(temp_buf, string_from_list);
		}
		rcs_print_list->delete_current_node();
	    } else {
		if (temp_buf != NULL) {
		    temp_buf = (char *) realloc(temp_buf, strlen(temp_buf)
			+ strlen(string_from_list) + 1);
		    strcat(temp_buf, string_from_list);
		    rcs_print_list->delete_current_node();
		    rcs_print_list->store_after_current_node(temp_buf,
			strlen(temp_buf)
			+ 1, 1);
		    free(temp_buf);
		    temp_buf = NULL;
		} else if (next_line[1] != 0) {
		    rcs_print_list->store_after_current_node(next_line + 1,
			strlen(next_line + 1) + 1, 1);
		    next_line[1] = 0;
		}
	    }
	    string_from_list = (char *) rcs_print_list->get_next();
	}
    }
    last_id_converted = rcs_print_list->get_newest_id();
    if (temp_buf != NULL) {
	rcs_print_list->store_at_tail(temp_buf, strlen(temp_buf) + 1, 1);
	free(temp_buf);
	temp_buf = NULL;
    }
}

void update_lines_table()
{
    if (NULL != rcs_lines_table) {
	free(rcs_lines_table);
	rcs_lines_table = NULL;
    }
    if (NULL != rcs_print_list) {
	convert_print_list_to_lines();
	rcs_lines_table = (char **) malloc(sizeof(char *)
	    * rcs_print_list->list_size);
	if (NULL != rcs_print_list) {
	    char *string_from_list;
	    string_from_list = (char *) rcs_print_list->get_head();
	    int i = 0;
	    while (NULL != string_from_list) {
		rcs_lines_table[i] = string_from_list;
		i++;
		string_from_list = (char *) rcs_print_list->get_next();
	    }
	}
    }
}

char *strip_control_characters(char *_dest, char *_src)
{
    static char line_buffer[255];
    char *destination;
    char *control_char_loc;
    if (NULL == _dest) {
	destination = line_buffer;
	if (strlen(_src) < 255) {
	    strcpy(line_buffer, _src);
	} else {
	    if (NULL == strpbrk(_src, "\n\r\t\b")) {
		return (_src);
	    } else {
		return (NULL);
	    }
	}
    } else {
	destination = _dest;
	if (_dest != _src) {
	    memmove(_dest, _src, strlen(_src));
	}
    }
    control_char_loc = strpbrk(destination, "\n\r\t\b");
    while (NULL != control_char_loc) {
	*control_char_loc = ' ';	/* Replace control character with
					   SPACE */
	control_char_loc = strpbrk(control_char_loc, "\n\r\t\b");
    }
    return (destination);

}

/* In windows DLLs for Microsoft Visual C++ sscanf is not supported so
use separate_words to parse the string followed by commands like strtod to
convert each word. */
int separate_words(char **_dest, int _max, char *_src)
{
    static char word_buffer[256];
    int i;
    if (NULL == _dest || NULL == _src) {
	return -1;
    }
    if (strlen(_src) > 255) {
	return -1;
    }
    strcpy(word_buffer, _src);
    _dest[0] = strtok(word_buffer, " \n\r\t");
    for (i = 0; NULL != _dest[i] && i < _max - 1; i++) {
	_dest[i + 1] = strtok(NULL, " \n\r\t");
    }
    if (_dest[_max - 1] == NULL && i == _max - 1) {
	i--;
    }
    return (i + 1);
}

int rcs_vprint(const char *_fmt, va_list _args, int save_string)
{
    static char temp_string[256];

    if (NULL == _fmt) {
	return (EOF);
    }
    if (strlen(_fmt) > 200) {	/* Might overflow temp_string. */
	return (EOF);
    }
    if (EOF == (int) vsnprintf(temp_string, sizeof(temp_string), _fmt, _args)) {
	return (EOF);
    }
    if (save_string) {
	if (!error_bufs_initialized) {
	    memset(last_error_bufs[0], 0, 100);
	    memset(last_error_bufs[1], 0, 100);
	    memset(last_error_bufs[2], 0, 100);
	    memset(last_error_bufs[3], 0, 100);
	    error_bufs_initialized = 1;
	}
	last_error_buf_filled++;
	last_error_buf_filled %= 4;
	strncpy(last_error_bufs[last_error_buf_filled], temp_string, 99);
    }
    return (rcs_fputs(temp_string));
}

int rcs_puts(const char *_str)
{
    int retval, retval2;
    retval = rcs_fputs(const_cast< char * >(_str));
    if (retval != EOF) {
	retval2 = rcs_fputs(const_cast< char * >("\n"));
	if (retval2 != EOF) {
	    retval += retval;
	} else {
	    retval = EOF;
	}
    }
    return (retval);
}

int rcs_fputs(const char *_str)
{
    int retval = EOF;
    if (NULL != _str) {
	if (0 == _str[0]) {
	    return (0);
	}
	switch (rcs_print_destination) {
	case RCS_PRINT_TO_LOGGER:

	case RCS_PRINT_TO_STDOUT:
	    retval = fputs(_str, stdout);
	    fflush(stdout);
	    break;

	case RCS_PRINT_TO_STDERR:
	    retval = fputs(_str, stderr);
	    fflush(stderr);
	    break;

	case RCS_PRINT_TO_LIST:
	    if (NULL == rcs_print_list) {
		rcs_print_list = new LinkedList;
		if (NULL != rcs_print_list) {
		    rcs_print_list->set_list_sizing_mode(256,
			DELETE_FROM_HEAD);
		}
	    }
	    if (NULL != rcs_print_list) {
		if (-1 ==
		    rcs_print_list->store_at_tail((void*)_str,
			(retval = strlen(_str)) + 1, 1)) {
		    retval = EOF;
		}
	    }
	    break;
	case RCS_PRINT_TO_NULL:
	    retval = strlen(_str);
	    break;
	case RCS_PRINT_TO_FILE:
	    if (NULL == rcs_print_file_stream) {
		if (NULL == rcs_print_file_name) {
		    return EOF;
		}
		rcs_print_file_stream = fopen(rcs_print_file_name, "a+");
	    }
	    if (NULL == rcs_print_file_stream) {
		return EOF;
	    }
	    retval = fputs(_str, rcs_print_file_stream);
	    fflush(rcs_print_file_stream);
	    break;

	default:
	    break;
	}
	if (NULL != rcs_print_notify) {
	    (*rcs_print_notify) ();
	}
    }
    return (retval);
}

void close_rcs_printing()
{
    switch (rcs_print_destination) {
    case RCS_PRINT_TO_LIST:
	clean_print_list();
	break;

    case RCS_PRINT_TO_FILE:
	if (NULL != rcs_print_file_stream) {
	    fclose(rcs_print_file_stream);
	    rcs_print_file_stream = NULL;
	}
	break;
    default:
	break;
    }
    return;
}

int set_rcs_print_file(char *_file_name)
{
    if (_file_name == NULL) {
	return -1;
    }
    if (strlen(_file_name) > 80) {
	return -1;
    }
    strcpy(rcs_print_file_name, _file_name);
    if (NULL != rcs_print_file_stream) {
	fclose(rcs_print_file_stream);
    }
    rcs_print_file_stream = fopen(rcs_print_file_name, "a+");
    if (NULL == rcs_print_file_stream) {
	return -1;
    }
    return 0;
}

int rcs_print(const char *_fmt, ...)
{
    static char temp_buffer[400];
    int retval;
    va_list args;
    va_start(args, _fmt);
    retval = vsnprintf(temp_buffer, sizeof(temp_buffer), _fmt, args);
    va_end(args);
    if (retval == (EOF)) {
	return EOF;
    }
    retval = rcs_fputs(temp_buffer);
    return (retval);
}

#ifndef DO_NOT_USE_RCS_PRINT_ERROR_NEW
static const char *rcs_error_filename = NULL;
static int rcs_error_linenum = -1;
int set_print_rcs_error_info(const char *file, int line)
{
    rcs_error_filename = file;
    rcs_error_linenum = line;
    return (0);
}

int print_rcs_error_new(const char *_fmt, ...)
{
    int retval = 0;
    va_list args;
    va_start(args, _fmt);
    if ((rcs_print_mode_flags & PRINT_RCS_ERRORS)
	&& ((max_rcs_errors_to_print >= rcs_errors_printed)
	    || max_rcs_errors_to_print < 0)) {
	if (NULL != rcs_error_filename && rcs_error_linenum > 0) {
	    rcs_print("%s %d: ", rcs_error_filename, rcs_error_linenum);
	    rcs_error_filename = NULL;
	    rcs_error_linenum = -1;
	}
	retval = rcs_vprint(_fmt, args, 1);
	if (max_rcs_errors_to_print == rcs_errors_printed &&
	    max_rcs_errors_to_print >= 0) {
	    rcs_print("\nMaximum number of errors to print exceeded!\n");
	}
    }
    if (rcs_print_destination != RCS_PRINT_TO_NULL) {
	rcs_errors_printed++;
    }
    va_end(args);
    return (retval);
}

#endif

int rcs_print_debug(long flag_to_check, const char *_fmt, ...)
{
    int retval = 0;
    int pid = 0;
    va_list args;
    va_start(args, _fmt);

    if (flag_to_check & rcs_print_mode_flags) {
	pid = getpid();
	rcs_print("(time=%f,pid=%d): ", etime(), pid);
	retval = rcs_vprint(_fmt, args, 0);
    }
    va_end(args);
    return (retval);
}

void set_rcs_print_flag(long flag_to_set)
{
    rcs_print_mode_flags |= flag_to_set;
}

void clear_rcs_print_flag(long flag_to_clear)
{
    rcs_print_mode_flags &= ~(flag_to_clear);
}

int rcs_print_sys_error(int error_source, const char *_fmt, ...)
{
    static char temp_string[256];
    static char message_string[512];
    va_list args;
    int r;

    if (NULL == _fmt) {
	return (EOF);
    }
    if (strlen(_fmt) > 200) {	/* Might overflow temp_string. */
	return (EOF);
    }

    va_start(args, _fmt);
    r = vsnprintf(temp_string, sizeof(temp_string), _fmt, args);
    va_end(args);

    if (r < 0) {
	return EOF;
    }

    if (max_rcs_errors_to_print == rcs_errors_printed &&
	max_rcs_errors_to_print >= 0) {
	rcs_print("\nMaximum number of errors to print exceeded!\n");
    }
    rcs_errors_printed++;
    if (max_rcs_errors_to_print <= rcs_errors_printed &&
	max_rcs_errors_to_print >= 0) {
	return (EOF);
    }

    switch (error_source) {
    case ERRNO_ERROR_SOURCE:
	snprintf(message_string, sizeof(message_string),
	    "%s %d %s\n", temp_string, errno,
	    strerror(errno));
	rcs_puts(message_string);
	break;

    default:
	rcs_puts(temp_string);
	break;
    }
    return (strlen(temp_string));
}

#ifdef rcs_print_error
#undef rcs_print_error
#endif
extern "C" int rcs_print_error(const char *_fmt, ...);

int rcs_print_error(const char *_fmt, ...)
{
    int retval = 0;
    va_list args;

    va_start(args, _fmt);
    if ((rcs_print_mode_flags & PRINT_RCS_ERRORS)
	&& ((max_rcs_errors_to_print >= rcs_errors_printed)
	    || max_rcs_errors_to_print < 0)) {
	retval = rcs_vprint(_fmt, args, 1);
	if (max_rcs_errors_to_print == rcs_errors_printed &&
	    max_rcs_errors_to_print >= 0) {
	    rcs_print("\nMaximum number of errors to print exceeded!\n");
	}
    }
    if (rcs_print_destination != RCS_PRINT_TO_NULL) {
	rcs_errors_printed++;
    }
    va_end(args);
    return (retval);
}