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

this bug's URL: http://intevation.de/rt/webrt?serial_num=4628
-------------------------------------------------------------------------

Subject: r.out.tiff: check for .tif.tif

Hi,

r.out.tiff automatically appends a ".tif" to the end of the output filename.
It should check to see if the string ends in upper/lower case .tif or .tiff
before doing this. A lot of folks assume filename extensions will be added
when needed, so I think we should continue to append.

probably r.out.png and friends as well.

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

thanks,
Hamish

-------------------------------------------- Managed by Request Tracker

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;
}

H:

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

P:

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

..

char * G_basename(char *filename, char *desired_ext)

..

         if( strncasecmp(ext, desired_ext, strlen(desired_ext)) == 0 )

Is strncasecmp() portable? man page says "only" BSD 4.4 compliant.

Is it better to use G_tolcase() or G_str_to_lower() first? [I just
noticed those were duplicates. We should consolidate. G_str_to_lower()
is only 2 months old so the obvious choice for removal, but it does
have a better name.]

lib/gis/mapcase.c
lib/gis/strings.c

oh, I see in lib/gis/strings.c we have a G_strcasecmp().

We still should merge G_tolcase() and G_str_to_lower() though.

I see lib/gis/mapcase.c defines tolower() and toupper(). As those are
both standard ANSI C fns, they are redundant and should be removed?

Hamish

On Tue, 2006-06-20 at 12:18 +1200, Hamish wrote:

H:
> > anyone have a simple C replacement for `basename $file .tif`?
P:
> Sounds like it would be useful as a library function. I wrote the
> following which seems to work, but could do with more testing:
..
> char * G_basename(char *filename, char *desired_ext)
..
> if( strncasecmp(ext, desired_ext, strlen(desired_ext)) == 0 )

Is strncasecmp() portable? man page says "only" BSD 4.4 compliant.

Yes, it's BSD and not to our definition of portable. Please use
G_strcasecmp().

Is it better to use G_tolcase() or G_str_to_lower() first? [I just
noticed those were duplicates. We should consolidate. G_str_to_lower()
is only 2 months old so the obvious choice for removal, but it does
have a better name.]

lib/gis/mapcase.c
lib/gis/strings.c

oh, I see in lib/gis/strings.c we have a G_strcasecmp().

:slight_smile:

We still should merge G_tolcase() and G_str_to_lower() though.

I see lib/gis/mapcase.c defines tolower() and toupper(). As those are
both standard ANSI C fns, they are redundant and should be removed?

Yeah, it appears that G_to[u|l]case() are redundant. swig uses
G_tolcase(), but I don't see why it couldn't be replaced with
G_str_to_lower().

--
Brad Douglas <rez touchofmadness com> KB8UYR
Address: 37.493,-121.924 / WGS84 National Map Corps #TNMC-3785

Brad Douglas wrote:

On Tue, 2006-06-20 at 12:18 +1200, Hamish wrote:

H:

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

P:

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

..

char * G_basename(char *filename, char *desired_ext)

..

        if( strncasecmp(ext, desired_ext, strlen(desired_ext)) == 0 )

Is strncasecmp() portable? man page says "only" BSD 4.4 compliant.

Yes, it's BSD and not to our definition of portable. Please use
G_strcasecmp().

Hmmm but it needs to be strncasecmp so "tif" will also match "tiff" etc. I guess I could implement it as a for loop matching character by character. Will change that.

On Tue, 2006-06-20 at 09:26 +0100, Paul Kelly wrote:

>>> if( strncasecmp(ext, desired_ext, strlen(desired_ext)) == 0 )
>>
>>Is strncasecmp() portable? man page says "only" BSD 4.4 compliant.
>
>
> Yes, it's BSD and not to our definition of portable. Please use
> G_strcasecmp().

Hmmm but it needs to be strncasecmp so "tif" will also match "tiff" etc.
I guess I could implement it as a for loop matching character by
character. Will change that.

I can add G_strncasecmp() if it's absolutely necessary (and nobody
objects).

--
Brad Douglas <rez touchofmadness com> KB8UYR
Address: 37.493,-121.924 / WGS84 National Map Corps #TNMC-3785

On Tue, 20 Jun 2006, Brad Douglas wrote:

Hmmm but it needs to be strncasecmp so "tif" will also match "tiff" etc.
I guess I could implement it as a for loop matching character by
character. Will change that.

I can add G_strncasecmp() if it's absolutely necessary (and nobody
objects).

Well, I have put in a portable version (into basename.c) now and it works so OK for now I think. But if we come across somewhere else that it's needed then would be worthwhile.

Paul