basename.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * \file basename.c
  3. *
  4. * \brief GIS Library - Program basename routines.
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 1999-2007
  14. */
  15. #include <grass/gis.h>
  16. #include <ctype.h>
  17. #include <string.h>
  18. /**
  19. * \brief Truncates filename to the base part (before the last '.')
  20. * if it matches the extension, otherwise leaves it unchanged.
  21. *
  22. * Checks if a filename matches a certain file extension
  23. * (case insensitive) and if so, truncates the string to the
  24. * base file name (cf. basename Unix command)
  25. *
  26. * \param[in] filename string containing filename
  27. * \param[in] desired_ext string containing extension to look for (case
  28. * insensitive)
  29. * \return Pointer to filename
  30. */
  31. char *G_basename(char *filename, const char *desired_ext)
  32. {
  33. /* Find the last . in the filename */
  34. char *dot = strrchr(filename, '.');
  35. if (dot && G_strcasecmp(dot + 1, desired_ext) == 0)
  36. *dot = '\0';
  37. return filename;
  38. }