GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Radim Blazek
10 */
11
12#include <string.h>
13#include <grass/vector.h>
14#include <grass/glocale.h>
15
16/*!
17 \brief Check if output is legal vector name.
18
19 Rule: [A-Za-z][A-Za-z0-9_]*
20
21 Check also for SQL keywords.
22
23 \param s filename to be checked
24
25 \return 1 OK
26 \return -1 if name does not start with letter A..Za..z or if name does not
27 continue with A..Za..z0..9_
28 */
29int Vect_legal_filename(const char *s)
30{
31 /* full list of SQL keywords available at
32 https://www.postgresql.org/docs/8.2/static/sql-keywords-appendix.html
33 */
34 static const char *keywords[] = {"and", "or", "not", NULL};
35 char buf[GNAME_MAX];
36 int i;
37
38 snprintf(buf, sizeof(buf), "%s", s);
39
40 if (*s == '.' || *s == 0) {
42 _("Illegal vector map name <%s>. May not contain '.' or 'NULL'."),
43 buf);
44 return -1;
45 }
46
47 /* file name must start with letter */
48 if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z'))) {
49 G_warning(_("Illegal vector map name <%s>. Must start with a letter."),
50 buf);
51 return -1;
52 }
53
54 for (s++; *s; s++)
55 if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z') ||
56 (*s >= '0' && *s <= '9') || *s == '_')) {
58 _("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 "
66 "used as vector map name."),
67 buf);
68 return -1;
69 }
70
71 return 1;
72}
73
74/*!
75 \brief Check for input and output vector map name.
76
77 Check
78 - output is legal vector name
79 - if can find input map
80 - if input was found in current mapset, check if input != output
81
82 \param input input name
83 \param output output name
84 \param error error type G_FATAL_EXIT, G_FATAL_PRINT, G_FATAL_RETURN
85
86 \return 0 OK
87 \return 1 error
88 */
89int Vect_check_input_output_name(const char *input, const char *output,
90 int error)
91{
92 const char *mapset;
95
96 /* check for fully-qualified map name */
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 "
101 "current mapset (%s)"),
102 output, G_mapset());
103 }
104 else if (error == G_FATAL_PRINT) {
105 G_warning(_("Output vector map name <%s> is not in the current "
106 "mapset (%s)"),
107 output, G_mapset());
108 return 1;
109 }
110 else { /* GV_FATAL_RETURN */
111 return 1;
112 }
113 }
114 output = onm;
115 }
116
117 if (Vect_legal_filename(output) == -1) {
118 if (error == G_FATAL_EXIT) {
119 G_fatal_error(_("Output vector map name <%s> is not SQL compliant"),
120 output);
121 }
122 else if (error == G_FATAL_PRINT) {
123 G_warning(_("Output vector map name <%s> is not SQL compliant"),
124 output);
125 return 1;
126 }
127 else { /* GV_FATAL_RETURN */
128 return 1;
129 }
130 }
131
132 if (G_name_is_fully_qualified(input, inm, ims)) {
133 if (strcasecmp(ims, "ogr") != 0)
134 mapset = G_find_vector2(input, "");
135 else
136 mapset = ims;
137 }
138 else
139 mapset = G_find_vector2(input, "");
140
141 if (mapset == NULL) {
142 if (error == G_FATAL_EXIT) {
143 G_fatal_error(_("Vector map <%s> not found"), input);
144 }
145 else if (error == G_FATAL_PRINT) {
146 G_warning(_("Vector map <%s> not found"), input);
147 return 1;
148 }
149 else { /* GV_FATAL_RETURN */
150 return 1;
151 }
152 }
153
154 if (strcmp(mapset, G_mapset()) == 0) {
155 if (G_name_is_fully_qualified(input, inm, ims)) {
156 input = inm;
157 }
158
159 if (strcmp(input, output) == 0) {
160 if (error == G_FATAL_EXIT) {
161 G_fatal_error(_("Output vector map <%s> is used as input"),
162 output);
163 }
164 else if (error == G_FATAL_PRINT) {
165 G_warning(_("Output vector map <%s> is used as input"), output);
166 return 1;
167 }
168 else { /* GV_FATAL_RETURN */
169 return 1;
170 }
171 }
172 }
173
174 return 0;
175}
#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:34
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition strings.c:45
const char * G_find_vector2(const char *, const char *)
Find a vector map (look but don't touch)
Definition find_vect.c:60
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
#define G_FATAL_PRINT
Definition gis.h:408
#define GMAPSET_MAX
Definition gis.h:194
#define G_FATAL_EXIT
Definition gis.h:407
#define GNAME_MAX
Definition gis.h:193
#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:89
int Vect_legal_filename(const char *s)
Check if output is legal vector name.
Definition legal_vname.c:29
void output(const char *fmt,...)
#define strcasecmp
Definition strings.h:8