GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-835afb4352
legal_vname.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/legal_vname.c
3 
4  \brief Vector library - Check if map name is legal vector map name
5 
6  (C) 2001-2009 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Radim Blazek
12  */
13 
14 #include <string.h>
15 #include <grass/vector.h>
16 #include <grass/glocale.h>
17 
18 /*!
19  \brief Check if output is legal vector name.
20 
21  Rule: [A-Za-z][A-Za-z0-9_]*
22 
23  Check also for SQL keywords.
24 
25  \param s filename 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 not
29  continue with A..Za..z0..9_
30  */
31 
32 int Vect_legal_filename(const char *s)
33 {
34  /* full list of SQL keywords available at
35  http://www.postgresql.org/docs/8.2/static/sql-keywords-appendix.html
36  */
37  static const char *keywords[] = {"and", "or", "not", NULL};
38  char buf[GNAME_MAX];
39  int i;
40 
41  sprintf(buf, "%s", s);
42 
43  if (*s == '.' || *s == 0) {
44  G_warning(
45  _("Illegal vector map name <%s>. May not contain '.' or 'NULL'."),
46  buf);
47  return -1;
48  }
49 
50  /* file name must start with letter */
51  if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z'))) {
52  G_warning(_("Illegal vector map name <%s>. Must start with a letter."),
53  buf);
54  return -1;
55  }
56 
57  for (s++; *s; s++)
58  if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z') ||
59  (*s >= '0' && *s <= '9') || *s == '_')) {
60  G_warning(
61  _("Illegal vector map name <%s>. Character '%c' not allowed."),
62  buf, *s);
63  return -1;
64  }
65 
66  for (i = 0; keywords[i]; i++)
67  if (G_strcasecmp(buf, keywords[i]) == 0) {
68  G_warning(_("Illegal vector map name <%s>. SQL keyword cannot be "
69  "used as vector map name."),
70  buf);
71  return -1;
72  }
73 
74  return 1;
75 }
76 
77 /*!
78  \brief Check for input and output vector map name.
79 
80  Check
81  - output is legal vector name
82  - if can find input map
83  - if input was found in current mapset, check if input != output
84 
85  \param input input name
86  \param output output name
87  \param error error type G_FATAL_EXIT, G_FATAL_PRINT, G_FATAL_RETURN
88 
89  \return 0 OK
90  \return 1 error
91  */
92 
93 int Vect_check_input_output_name(const char *input, const char *output,
94  int error)
95 {
96  const char *mapset;
97  char inm[GNAME_MAX], ims[GMAPSET_MAX];
98  char onm[GNAME_MAX], oms[GMAPSET_MAX];
99 
100  /* check for fully-qualified map name */
101  if (G_name_is_fully_qualified(output, onm, oms)) {
102  if (strcmp(oms, G_mapset()) != 0) {
103  if (error == G_FATAL_EXIT) {
104  G_fatal_error(_("Output vector map name <%s> is not in the "
105  "current mapset (%s)"),
106  output, G_mapset());
107  }
108  else if (error == G_FATAL_PRINT) {
109  G_warning(_("Output vector map name <%s> is not in the current "
110  "mapset (%s)"),
111  output, G_mapset());
112  return 1;
113  }
114  else { /* GV_FATAL_RETURN */
115  return 1;
116  }
117  }
118  output = onm;
119  }
120 
121  if (Vect_legal_filename(output) == -1) {
122  if (error == G_FATAL_EXIT) {
123  G_fatal_error(_("Output vector map name <%s> is not SQL compliant"),
124  output);
125  }
126  else if (error == G_FATAL_PRINT) {
127  G_warning(_("Output vector map name <%s> is not SQL compliant"),
128  output);
129  return 1;
130  }
131  else { /* GV_FATAL_RETURN */
132  return 1;
133  }
134  }
135 
136  if (G_name_is_fully_qualified(input, inm, ims)) {
137  if (strcasecmp(ims, "ogr") != 0)
138  mapset = G_find_vector2(input, "");
139  else
140  mapset = ims;
141  }
142  else
143  mapset = G_find_vector2(input, "");
144 
145  if (mapset == NULL) {
146  if (error == G_FATAL_EXIT) {
147  G_fatal_error(_("Vector map <%s> not found"), input);
148  }
149  else if (error == G_FATAL_PRINT) {
150  G_warning(_("Vector map <%s> not found"), input);
151  return 1;
152  }
153  else { /* GV_FATAL_RETURN */
154  return 1;
155  }
156  }
157 
158  if (strcmp(mapset, G_mapset()) == 0) {
159  if (G_name_is_fully_qualified(input, inm, ims)) {
160  input = inm;
161  }
162 
163  if (strcmp(input, output) == 0) {
164  if (error == G_FATAL_EXIT) {
165  G_fatal_error(_("Output vector map <%s> is used as input"),
166  output);
167  }
168  else if (error == G_FATAL_PRINT) {
169  G_warning(_("Output vector map <%s> is used as input"), output);
170  return 1;
171  }
172  else { /* GV_FATAL_RETURN */
173  return 1;
174  }
175  }
176  }
177 
178  return 0;
179 }
#define NULL
Definition: ccmath.h:32
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:36
const char * G_find_vector2(const char *, const char *)
Find a vector map (look but don't touch)
Definition: find_vect.c:62
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition: strings.c:47
#define G_FATAL_PRINT
Definition: gis.h:402
#define GMAPSET_MAX
Definition: gis.h:192
#define G_FATAL_EXIT
Definition: gis.h:401
#define GNAME_MAX
Definition: gis.h:191
#define _(str)
Definition: glocale.h:10
int Vect_check_input_output_name(const char *input, const char *output, int error)
Check for input and output vector map name.
Definition: legal_vname.c:93
int Vect_legal_filename(const char *s)
Check if output is legal vector name.
Definition: legal_vname.c:32
void output(const char *fmt,...)