GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71933
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 }
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:46
#define GMAPSET_MAX
Definition: gis.h:149
#define G_FATAL_EXIT
Definition: gis.h:353
#define NULL
Definition: ccmath.h:32
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
void output(const char *fmt,...)
#define GNAME_MAX
Definition: gis.h:148
#define _(str)
Definition: glocale.h:13
int G_name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:36
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
const char * G_find_vector2(const char *name, const char *mapset)
Find a vector map (look but don&#39;t touch)
Definition: find_vect.c:62
#define G_FATAL_PRINT
Definition: gis.h:354
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203