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