mf_fn_ext.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License, version 2.0, for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  22. /**
  23. @file mysys/mf_fn_ext.cc
  24. */
  25. #include <string.h>
  26. #include "m_string.h"
  27. #include "my_dbug.h"
  28. #include "my_io.h"
  29. #if defined(FN_DEVCHAR) || defined(_WIN32)
  30. #include "my_sys.h"
  31. #include "mysys/mysys_priv.h"
  32. #endif
  33. /*
  34. Return a pointer to the extension of the filename.
  35. SYNOPSIS
  36. fn_ext()
  37. name Name of file
  38. DESCRIPTION
  39. The extension is defined as everything after the last extension character
  40. (normally '.') after the directory name.
  41. RETURN VALUES
  42. Pointer to to the extension character. If there isn't any extension,
  43. points at the end ASCII(0) of the filename.
  44. */
  45. const char *fn_ext(const char *name) {
  46. #if defined(FN_DEVCHAR) || defined(_WIN32)
  47. char buff[FN_REFLEN];
  48. size_t res_length;
  49. const char *gpos = name + dirname_part(buff, name, &res_length);
  50. #else
  51. const char *gpos = strrchr(name, FN_LIBCHAR);
  52. if (gpos == nullptr) gpos = name;
  53. #endif
  54. const char *pos = strrchr(gpos, FN_EXTCHAR);
  55. return pos ? pos : strend(gpos);
  56. }
  57. char *fn_ext(char *name) {
  58. return const_cast<char *>(fn_ext(static_cast<const char *>(name)));
  59. }