basename.c

Go to the documentation of this file.
00001 #include <ctype.h>
00002 #include <string.h>
00003 
00022 char * G_basename(char *filename, const char *desired_ext)
00023 {
00024     /* Find the last . in the filename */
00025     char *dot = strrchr(filename, '.');
00026 
00027     /* Check there is a . and it's not the last character
00028      * in the string, i.e. there is an extension */
00029     if(dot && ((dot - filename) < strlen(filename)) )
00030     {
00031         char *ext = dot + 1;
00032         int i, match = 1;
00033 
00034         /* if the extension matches (case insensitive)
00035          * then truncate the filename to the basename */
00036         for( i = 0; i < strlen(desired_ext); i++ )
00037         {
00038             if( (ext[i] == '\0') || (tolower(ext[i]) != tolower(desired_ext[i])) )
00039             {
00040                 match = 0;
00041                 break;
00042             }
00043         }
00044         
00045         if( match )
00046             *dot = '\0';
00047 
00048     }
00049 
00050     return filename;
00051 }

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