GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
legal_vname.c
Go to the documentation of this file.
1 
18 #include <string.h>
19 #include <grass/gis.h>
20 #include <grass/Vect.h>
21 #include <grass/glocale.h>
22 
36 int Vect_legal_filename(const char *s)
37 {
38  /* full list of SQL keywords available at
39  http://www.postgresql.org/docs/8.2/static/sql-keywords-appendix.html
40  */
41  static const char *keywords[] = { "and", "or", "not", NULL };
42  char buf[GNAME_MAX];
43  int i;
44 
45  sprintf(buf, "%s", s);
46 
47  if (*s == '.' || *s == 0) {
48  G_warning(_("Illegal vector map name <%s>. May not contain '.' or 'NULL'."),
49  buf);
50  return -1;
51  }
52 
53  /* file name must start with letter */
54  if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z'))) {
55  G_warning(_("Illegal vector map name <%s>. Must start with a letter."),
56  buf);
57  return -1;
58  }
59 
60  for (s++; *s; s++)
61  if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z') ||
62  (*s >= '0' && *s <= '9') || *s == '_' || *s == '@')) {
63  G_warning(_("Illegal vector map name <%s>. Character '%c' not allowed."),
64  buf, *s);
65  return -1;
66  }
67 
68  for (i = 0; keywords[i]; i++)
69  if (G_strcasecmp(buf, keywords[i]) == 0) {
70  G_warning(_("Illegal vector map name <%s>. SQL keyword cannot be used as vector map name."),
71  buf);
72  return -1;
73  }
74 
75  return 1;
76 }
77 
94 int Vect_check_input_output_name(const char *input, const char *output,
95  int error)
96 {
97  const char *mapset;
98 
99  if (Vect_legal_filename(output) == -1) {
100  if (error == GV_FATAL_EXIT) {
101  G_fatal_error(_("Output vector map name <%s> is not valid map name"),
102  output);
103  }
104  else if (error == GV_FATAL_PRINT) {
105  G_warning(_("Output vector map name <%s> is not valid map name"),
106  output);
107  return 1;
108  }
109  else { /* GV_FATAL_RETURN */
110  return 1;
111  }
112  }
113 
114  mapset = G_find_vector2(input, "");
115 
116  if (mapset == NULL) {
117  if (error == GV_FATAL_EXIT) {
118  G_fatal_error(_("Vector map <%s> not found"), input);
119  }
120  else if (error == GV_FATAL_PRINT) {
121  G_warning(_("Vector map <%s> not found"), input);
122  return 1;
123  }
124  else { /* GV_FATAL_RETURN */
125  return 1;
126  }
127  }
128 
129  if (strcmp(mapset, G_mapset()) == 0) {
130  const char *in;
131  char nm[GNAME_MAX], ms[GMAPSET_MAX];
132 
133  if (G__name_is_fully_qualified(input, nm, ms)) {
134  in = nm;
135  }
136  else {
137  in = input;
138  }
139 
140  if (strcmp(in, output) == 0) {
141  if (error == GV_FATAL_EXIT) {
142  G_fatal_error(_("Output vector map <%s> is used as input"),
143  output);
144  }
145  else if (error == GV_FATAL_PRINT) {
146  G_warning(_("Output vector map <%s> is used as input"),
147  output);
148  return 1;
149  }
150  else { /* GV_FATAL_RETURN */
151  return 1;
152  }
153  }
154  }
155 
156  return 0;
157 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:192
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
char * G_find_vector2(const char *name, const char *mapset)
find a vector map (look but don&#39;t touch)
Definition: find_vect.c:75
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
void output(const char *fmt,...)
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:57