1 /*****************************************************************************
2 * Copyright (c) 2019 FrontISTR Commons
3 * This software is released under the MIT License, see LICENSE.txt
4 *****************************************************************************/
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <string.h>
10 #include "hecmw_util.h"
11 #include "hecmw_error.h"
12
13 static int hecmw_errno;
14
15 static char hecmw_errmsg[HECMW_MSG_LEN + 1];
16
HECMW_set_verror(int errorno,const char * fmt,va_list ap)17 int HECMW_set_verror(int errorno, const char *fmt, va_list ap) {
18 char errmsg[HECMW_MSG_LEN + 1];
19
20 hecmw_errno = errorno;
21
22 HECMW_snprintf(hecmw_errmsg, sizeof(hecmw_errmsg), "%s",
23 HECMW_strmsg(errorno));
24 HECMW_vsnprintf(errmsg, sizeof(errmsg), fmt, ap);
25
26 if (strlen(errmsg) > 0) {
27 HECMW_snprintf(hecmw_errmsg + strlen(hecmw_errmsg),
28 sizeof(hecmw_errmsg) - strlen(hecmw_errmsg), " (%s)",
29 errmsg);
30 }
31
32 HECMW_print_error();
33
34 return 0;
35 }
36
HECMW_set_error(int errorno,const char * fmt,...)37 int HECMW_set_error(int errorno, const char *fmt, ...) {
38 int rc;
39 va_list ap;
40
41 rc = 0;
42 va_start(ap, fmt);
43 rc = HECMW_set_verror(errorno, fmt, ap);
44 va_end(ap);
45
46 return rc;
47 }
48
HECMW_get_error(char ** errmsg)49 int HECMW_get_error(char **errmsg) {
50 if (errmsg) {
51 *errmsg = hecmw_errmsg;
52 }
53 return hecmw_errno;
54 }
55
HECMW_get_errno(void)56 int HECMW_get_errno(void) { return hecmw_errno; }
57
HECMW_get_errmsg(void)58 char *HECMW_get_errmsg(void) { return hecmw_errmsg; }
59