GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-b4187339ee
d_error.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_driver/d_error.c
3 
4  \brief DBMI Library (driver) - error reporting
5 
6  Taken from DB drivers.
7 
8  (C) 1999-2008, 2012 by the GRASS Development Team
9 
10  This program is free software under the GNU General Public
11  License (>=v2). Read the file COPYING that comes with GRASS
12  for details.
13 
14  \author Joel Jones (CERL/UIUC)
15  \author Radim Blazek
16  \author Adopted for DBMI by Martin Landa <landa.martin@gmail.com>
17  */
18 
19 #include <string.h>
20 #include <errno.h>
21 #include <grass/dbmi.h>
22 #include <grass/glocale.h>
23 
24 /* initialize the global struct */
25 struct error_state {
26  char *driver_name;
27  dbString *errMsg;
28 };
29 
30 static struct error_state state;
31 static struct error_state *st = &state;
32 
33 static void init(void)
34 {
35  db_set_string(st->errMsg, "");
36  db_d_append_error(_("DBMI-%s driver error:"), st->driver_name);
37  db_append_string(st->errMsg, "\n");
38 }
39 
40 /*!
41  \brief Init error message for DB driver
42 
43  Initialize prefix
44 
45  \param name driver name (eg. "SQLite"))
46  */
47 void db_d_init_error(const char *name)
48 {
49  if (!st->errMsg) {
50  st->errMsg = (dbString *)G_malloc(sizeof(dbString));
51  db_init_string(st->errMsg);
52  }
53 
54  G_debug(1, "db_d_init_error(): %s", name);
55 
56  st->driver_name = G_malloc(strlen(name) + 1);
57  strcpy(st->driver_name, name);
58  init();
59 }
60 
61 /*!
62  \brief Append error message for DB driver
63 
64  \param fmt formatted message
65  */
66 void db_d_append_error(const char *fmt, ...)
67 {
68  FILE *fp = NULL;
69  char *work = NULL;
70  int count = 0;
71  va_list ap;
72 
73  va_start(ap, fmt);
74  if ((fp = tmpfile())) {
75  count = vfprintf(fp, fmt, ap);
76  if (count >= 0 && (work = G_calloc(count + 1, 1))) {
77  rewind(fp);
78  if (fread(work, 1, count, fp) != (size_t)count) {
79  if (ferror(fp))
80  G_fatal_error(_("DBMI-%s driver file reading error: %s"),
81  st->driver_name, strerror(errno));
82  }
83  db_append_string(st->errMsg, work);
84  G_free(work);
85  }
86  fclose(fp);
87  }
88  va_end(ap);
89 }
90 
91 /*!
92  \brief Report error message for DB driver
93  */
95 {
96  db_append_string(st->errMsg, "\n");
97  db_error(db_get_string(st->errMsg));
98 
99  init();
100 }
void init(double work[])
Definition: as177.c:61
#define NULL
Definition: ccmath.h:32
void db_d_append_error(const char *fmt,...)
Append error message for DB driver.
Definition: d_error.c:66
void db_d_report_error(void)
Report error message for DB driver.
Definition: d_error.c:94
void db_d_init_error(const char *name)
Init error message for DB driver.
Definition: d_error.c:47
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition: string.c:41
void db_init_string(dbString *)
Initialize dbString.
Definition: string.c:25
void db_error(const char *)
Report error message.
int db_append_string(dbString *, const char *)
Append string to dbString.
Definition: string.c:205
char * db_get_string(const dbString *)
Get string.
Definition: string.c:140
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_calloc(m, n)
Definition: defs/gis.h:95
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition: defs/gis.h:94
int G_debug(int, const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
int count
const char * name
Definition: named_colr.c:6
struct state state
Definition: parser.c:103
struct state * st
Definition: parser.c:104
#define strcpy
Definition: parson.c:62