GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-835afb4352
legal_dbname.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_base/legal_dbname.c
3 
4  \brief DBMI Library (base) - validate DB names
5 
6  \todo Are we as restrictive here as for vector names?
7 
8  (C) 1999-2009, 2011 by the GRASS Development Team
9 
10  This program is free software under the GNU General Public License
11  (>=v2). Read the file COPYING that comes with GRASS for details.
12 
13  \author Joel Jones (CERL/UIUC), Radim Blazek
14  \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
15  */
16 
17 #include <grass/gis.h>
18 #include <grass/dbmi.h>
19 #include <grass/glocale.h>
20 
21 /*!
22  \brief Check if output is legal table name
23 
24  Rule: [A-Za-z][A-Za-z0-9_@]*
25  \param s table name to be checked
26 
27  \return 1 OK
28  \return -1 if name does not start with letter A..Za..z or if name does
29  not continue with A..Za..z0..9_@
30  */
31 int db_legal_tablename(const char *s)
32 {
33  char buf[GNAME_MAX];
34 
35  sprintf(buf, "%s", s);
36 
37  if (*s == '.' || *s == 0) {
38  G_warning(
39  _("Illegal table map name <%s>. May not contain '.' or 'NULL'."),
40  buf);
41  return DB_FAILED;
42  }
43 
44  /* file name must start with letter */
45  if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z'))) {
46  G_warning(_("Illegal table map name <%s>. Must start with a letter."),
47  buf);
48  return DB_FAILED;
49  }
50 
51  for (s++; *s; s++)
52  if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z') ||
53  (*s >= '0' && *s <= '9') || *s == '_' || *s == '@')) {
54  G_warning(
55  _("Illegal table map name <%s>. Character <%c> not allowed."),
56  buf, *s);
57  return DB_FAILED;
58  }
59 
60  return DB_OK;
61 }
#define DB_FAILED
Definition: dbmi.h:72
#define DB_OK
Definition: dbmi.h:71
void G_warning(const char *,...) __attribute__((format(printf
#define GNAME_MAX
Definition: gis.h:191
#define _(str)
Definition: glocale.h:10
int db_legal_tablename(const char *s)
Check if output is legal table name.
Definition: legal_dbname.c:31