legal_name.c

Go to the documentation of this file.
00001 #include <string.h>
00002 #include <grass/gis.h>
00003 #include <grass/glocale.h>
00004 /**********************************************************************
00005  *
00006  *   char *
00007  *   G_legal_filename (name)
00008  *      char *name           filename to be checked
00009  *
00010  *   returns:    1  if name is OK
00011  *              -1  if name begins with ".", if name contains a "/",
00012  *                  if name contains a quote character,
00013  *                  or if name contains a non-printing character.
00014  **********************************************************************/
00015 
00016 
00031 int G_legal_filename (char *s)
00032 {
00033     if (*s == '.' || *s == 0) {
00034         fprintf(stderr, _("Illegal filename.  Cannot be '.' or 'NULL'\n"));
00035         return -1;
00036     }
00037 
00038     for (; *s; s++)
00039         if (*s == '/' || *s == '"' || *s == '\'' || *s <= ' ' || 
00040             *s == '@' || *s == ',' || *s == '=' || *s == '*' || *s > 0176) {
00041                 fprintf(stderr, _("Illegal filename. character <%c> not allowed.\n"), *s);
00042             return -1;
00043         }
00044 
00045     return 1;
00046 }
00047 
00060 int G_check_input_output_name ( char * input, char * output, int error )
00061 {
00062     char *mapset;
00063 
00064     if ( output == NULL) return 0; /* don't die on undefined parameters */
00065     if ( G_legal_filename(output) == -1 ) {
00066         if ( error == GR_FATAL_EXIT ) {
00067             G_fatal_error ( _("Output name '%s' is not valid rast name."), output );  
00068         } else if ( error == GR_FATAL_PRINT ) {
00069             G_warning ( _("Output name '%s' is not valid rast name."), output );
00070             return 1;
00071         } else { /* GR_FATAL_RETURN */
00072             return 1;
00073         }
00074     }
00075 
00076     mapset = G_find_cell2 (input, "");
00077     
00078     if ( mapset == NULL ) {
00079         if ( error == GR_FATAL_EXIT ) {
00080             G_fatal_error ( _("Cannot find input map '%s'"), input );  
00081         } else if ( error == GR_FATAL_PRINT ) {
00082             G_warning ( _("Cannot find input map '%s'"), input );
00083             return 1;
00084         } else { /* GR_FATAL_RETURN */
00085             return 1;
00086         }
00087     }
00088 
00089     if ( strcmp(mapset,G_mapset()) == 0 ) {
00090         char *in, nm[1000], ms[1000];
00091         
00092         if ( G__name_is_fully_qualified(input,nm,ms) ) {
00093             in = nm;
00094         } else {
00095             in = input;
00096         }
00097         
00098         if ( strcmp(in,output) == 0 ) {
00099             if ( error == GR_FATAL_EXIT ) {
00100                 G_fatal_error ( _("Output map '%s' is used as input"), output );  
00101             } else if ( error == GR_FATAL_PRINT ) {
00102                 G_warning ( _("Output map '%s' is used as input"), output );
00103                 return 1;
00104             } else { /* GR_FATAL_RETURN */
00105                 return 1;
00106             }
00107         }
00108     }
00109 
00110     return 0;
00111 }
00112 

Generated on Wed Dec 19 14:59:06 2007 for GRASS by  doxygen 1.5.4