legal_vname.c

Go to the documentation of this file.
00001 
00018 #include <string.h>
00019 #include <grass/gis.h>
00020 #include <grass/Vect.h>
00021 #include <grass/glocale.h>
00022 
00036 int Vect_legal_filename(const char *s)
00037 {
00038     /* full list of SQL keywords available at
00039        http://www.postgresql.org/docs/8.2/static/sql-keywords-appendix.html
00040      */
00041     static const char *keywords[] = { "and", "or", "not", NULL };
00042     char buf[GNAME_MAX];
00043     int i;
00044 
00045     sprintf(buf, "%s", s);
00046 
00047     if (*s == '.' || *s == 0) {
00048         G_warning(_("Illegal vector map name <%s>. May not contain '.' or 'NULL'."),
00049                   buf);
00050         return -1;
00051     }
00052 
00053     /* file name must start with letter */
00054     if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z'))) {
00055         G_warning(_("Illegal vector map name <%s>. Must start with a letter."),
00056                   buf);
00057         return -1;
00058     }
00059 
00060     for (s++; *s; s++)
00061         if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z') ||
00062               (*s >= '0' && *s <= '9') || *s == '_' || *s == '@')) {
00063             G_warning(_("Illegal vector map name <%s>. Character '%c' not allowed."),
00064                       buf, *s);
00065             return -1;
00066         }
00067 
00068     for (i = 0; keywords[i]; i++)
00069         if (G_strcasecmp(buf, keywords[i]) == 0) {
00070             G_warning(_("Illegal vector map name <%s>. SQL keyword cannot be used as vector map name."),
00071                       buf);
00072             return -1;
00073         }
00074 
00075     return 1;
00076 }
00077 
00094 int Vect_check_input_output_name(const char *input, const char *output,
00095                                  int error)
00096 {
00097     const char *mapset;
00098 
00099     if (Vect_legal_filename(output) == -1) {
00100         if (error == GV_FATAL_EXIT) {
00101             G_fatal_error(_("Output vector map name <%s> is not valid map name"),
00102                           output);
00103         }
00104         else if (error == GV_FATAL_PRINT) {
00105             G_warning(_("Output vector map name <%s> is not valid map name"),
00106                       output);
00107             return 1;
00108         }
00109         else {                  /* GV_FATAL_RETURN */
00110             return 1;
00111         }
00112     }
00113 
00114     mapset = G_find_vector2(input, "");
00115 
00116     if (mapset == NULL) {
00117         if (error == GV_FATAL_EXIT) {
00118             G_fatal_error(_("Vector map <%s> not found"), input);
00119         }
00120         else if (error == GV_FATAL_PRINT) {
00121             G_warning(_("Vector map <%s> not found"), input);
00122             return 1;
00123         }
00124         else {                  /* GV_FATAL_RETURN */
00125             return 1;
00126         }
00127     }
00128 
00129     if (strcmp(mapset, G_mapset()) == 0) {
00130         const char *in;
00131         char nm[GNAME_MAX], ms[GMAPSET_MAX];
00132 
00133         if (G__name_is_fully_qualified(input, nm, ms)) {
00134             in = nm;
00135         }
00136         else {
00137             in = input;
00138         }
00139 
00140         if (strcmp(in, output) == 0) {
00141             if (error == GV_FATAL_EXIT) {
00142                 G_fatal_error(_("Output vector map <%s> is used as input"),
00143                               output);
00144             }
00145             else if (error == GV_FATAL_PRINT) {
00146                 G_warning(_("Output vector map <%s> is used as input"),
00147                           output);
00148                 return 1;
00149             }
00150             else {              /* GV_FATAL_RETURN */
00151                 return 1;
00152             }
00153         }
00154     }
00155 
00156     return 0;
00157 }
Generated on Tue Apr 6 13:28:10 2010 for GRASS Programmer's Manual by  doxygen 1.6.3