GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
legal_name.c
Go to the documentation of this file.
1 
17 #include <stdio.h>
18 #include <string.h>
19 #include <grass/gis.h>
20 #include <grass/glocale.h>
21 
22 
36 int G_legal_filename(const char *s)
37 {
38  if (*s == '.' || *s == 0) {
39  fprintf(stderr, _("Illegal filename. Cannot be '.' or 'NULL'\n"));
40  return -1;
41  }
42 
43  for (; *s; s++)
44  if (*s == '/' || *s == '"' || *s == '\'' || *s <= ' ' ||
45  *s == '@' || *s == ',' || *s == '=' || *s == '*' || *s > 0176) {
46  fprintf(stderr,
47  _("Illegal filename. Character <%c> not allowed.\n"), *s);
48  return -1;
49  }
50 
51  return 1;
52 }
53 
54 
68 int G_check_input_output_name(const char *input, const char *output,
69  int error)
70 {
71  char *mapset;
72 
73  if (output == NULL)
74  return 0; /* don't die on undefined parameters */
75  if (G_legal_filename(output) == -1) {
76  if (error == GR_FATAL_EXIT) {
77  G_fatal_error(_("Output raster map name <%s> is not valid map name"),
78  output);
79  }
80  else if (error == GR_FATAL_PRINT) {
81  G_warning(_("Output raster map name <%s> is not valid map name"),
82  output);
83  return 1;
84  }
85  else { /* GR_FATAL_RETURN */
86  return 1;
87  }
88  }
89 
90  mapset = G_find_cell2(input, "");
91 
92  if (mapset == NULL) {
93  if (error == GR_FATAL_EXIT) {
94  G_fatal_error(_("Raster map <%s> not found"), input);
95  }
96  else if (error == GR_FATAL_PRINT) {
97  G_warning(_("Raster map <%s> not found"), input);
98  return 1;
99  }
100  else { /* GR_FATAL_RETURN */
101  return 1;
102  }
103  }
104 
105  if (strcmp(mapset, G_mapset()) == 0) {
106  char nm[1000], ms[1000];
107  const char *in;
108 
109  if (G__name_is_fully_qualified(input, nm, ms)) {
110  in = nm;
111  }
112  else {
113  in = input;
114  }
115 
116  if (strcmp(in, output) == 0) {
117  if (error == GR_FATAL_EXIT) {
118  G_fatal_error(_("Output raster map <%s> is used as input"),
119  output);
120  }
121  else if (error == GR_FATAL_PRINT) {
122  G_warning(_("Output raster map <%s> is used as input"),
123  output);
124  return 1;
125  }
126  else { /* GR_FATAL_RETURN */
127  return 1;
128  }
129  }
130  }
131 
132  return 0;
133 }