[GRASS-dev] [bug #4628] (grass) r.out.tiff: check for .tif.tif

Paul Kelly paul-grass at stjohnspoint.co.uk
Mon Jun 19 06:49:42 EDT 2006


On Mon, 19 Jun 2006, Request Tracker wrote:

> anyone have a simple C replacement for `basename $file .tif`?

Sounds like it would be useful as a library function. I wrote the 
following which seems to work, but could do with more testing:

/**
  * \brief Checks if a filename matches a certain file extension
  * (case insensitive) and if so, truncates the string to the
  * base file name (cf. basename Unix command)
  *
  * Truncates filename to the base part (before the last .)
  * if it matches the extension, otherwise leaves it unchanged.
  *
  * \param filename String containing filename
  *
  * \param ext String containing extension to look for (case
  * insensitive and  as long as needs to be, e.g. tif will
  * match .tif and .tiff, sh will match .shp and .shx, htm will
  * match .htm and .html)
  *
  * \return Pointer to filename
  **/

char * G_basename(char *filename, char *desired_ext)
{
     /* Find the last . in the filename */
     char *dot = strrchr(filename, '.');

     /* Check there is a dot and its not the last character
      * in the string, i.e. there is an extension */
     if(dot && ((dot - filename) < strlen(filename)) )
     {
         char *ext = dot + 1;

         /* if the extension matches (case insensitive)
          * then truncate the filename to the basename */
         if( strncasecmp(ext, desired_ext, strlen(desired_ext)) == 0 )
             *dot = '\0';

     }

     return filename;
}




More information about the grass-dev mailing list