File: error.cc

package info (click to toggle)
freehdl 0.0.8-2.2
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 8,632 kB
  • ctags: 10,443
  • sloc: cpp: 45,275; sh: 11,405; yacc: 4,206; ansic: 2,026; lex: 486; perl: 430; makefile: 390; tcl: 100
file content (133 lines) | stat: -rw-r--r-- 2,976 bytes parent folder | download | duplicates (5)
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
/* error handling

   Copyright (C) 1994-1997 University of Dortmund
   Department of Electrical Engineering, AG SIV

   VAUL is free software; you can redistribute it and/or modify it
   under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   VAUL 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 Library General
   Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with VAUL; see the file COPYING.LIB.  If not, write
   to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
   Boston, MA 02111-1307 USA.


*/

#include <freehdl/vaul-errors.h>
#include <freehdl/vaul-util.h>

#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>

vaul_error_source::vaul_error_source()
{
    error_code = 0;
    error_desc = NULL;
}

vaul_error_source::~vaul_error_source()
{
    set_error_desc(NULL);
}

void vaul_error_source::set_error()
{
    set_error("");
}

void vaul_error_source::set_error(const char *fmt, ...)
{
    if(errno) {
	error_code = errno;
	va_list ap;
	va_start(ap, fmt);
	set_error_desc (vaul_vaprintf (fmt, ap));
	more_error_desc(strerror(error_code));
	va_end(ap);
    } else
	clear_error();
}

void vaul_error_source::set_error(int code, const char *fmt, ...)
{
    if(code) {
	error_code = code;
	va_list ap;
	va_start(ap, fmt);
	set_error_desc (vaul_vaprintf (fmt, ap));
	va_end(ap);
    } else
	clear_error();
}

void vaul_error_source::set_error(vaul_error_source *es, const char *fmt, ...)
{
    if(es->error_code) {
	error_code = es->error_code;
	va_list ap;
	va_start(ap, fmt);
	set_error_desc (vaul_vaprintf (fmt, ap));
	more_error_desc(es->error_desc);
	va_end(ap);
    } else
	clear_error();
}

void vaul_error_source::set_error(vaul_error_source &es, const char *fmt, ...)
{
    if(es.error_code) {
	error_code = es.error_code;
	va_list ap;
	va_start(ap, fmt);
	set_error_desc (vaul_vaprintf (fmt, ap));
	more_error_desc(es.error_desc);
	va_end(ap);
    } else
	clear_error();
}

void vaul_error_source::clear_error()
{
    error_code = 0;
    set_error_desc(NULL);
}

void vaul_error_source::print_err(FILE *f, const char *msg)
{
    fprintf(f, "%s: ", vaul_application_name);
    if(msg)
	fprintf(f, "%s: ", msg);
    if(error_desc)
	fprintf(f, "%s\n", error_desc);
    else
	fprintf(f, "BOGUS ERROR REPORT\n");
}

void vaul_error_source::print_err(const char *msg)
{
    print_err(stderr, msg);
}

void vaul_error_source::set_error_desc(const char *s)
{
    free((char*)error_desc);
    error_desc = s;
}

void vaul_error_source::more_error_desc(const char *s)
{
    if(error_desc == NULL)
	set_error_desc(s);
    else
	set_error_desc (vaul_aprintf ("%s%s", error_desc, s));
}