filename.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Basic filename support macros.
  2. Copyright (C) 2001-2004, 2007-2020 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* From Paul Eggert and Jim Meyering. */
  14. #ifndef _FILENAME_H
  15. #define _FILENAME_H
  16. #include <string.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /* Filename support.
  21. ISSLASH(C) tests whether C is a directory separator
  22. character.
  23. HAS_DEVICE(Filename) tests whether Filename contains a device
  24. specification.
  25. FILE_SYSTEM_PREFIX_LEN(Filename) length of the device specification
  26. at the beginning of Filename,
  27. index of the part consisting of
  28. alternating components and slashes.
  29. FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
  30. 1 when a non-empty device specification
  31. can be followed by an empty or relative
  32. part,
  33. 0 when a non-empty device specification
  34. must be followed by a slash,
  35. 0 when device specification don't exist.
  36. IS_ABSOLUTE_FILE_NAME(Filename)
  37. tests whether Filename is independent of
  38. any notion of "current directory".
  39. IS_RELATIVE_FILE_NAME(Filename)
  40. tests whether Filename may be concatenated
  41. to a directory filename.
  42. Note: On native Windows, OS/2, DOS, "c:" is neither an absolute nor a
  43. relative file name!
  44. IS_FILE_NAME_WITH_DIR(Filename) tests whether Filename contains a device
  45. or directory specification.
  46. */
  47. #if defined _WIN32 || defined __CYGWIN__ \
  48. || defined __EMX__ || defined __MSDOS__ || defined __DJGPP__
  49. /* Native Windows, Cygwin, OS/2, DOS */
  50. # define ISSLASH(C) ((C) == '/' || (C) == '\\')
  51. /* Internal macro: Tests whether a character is a drive letter. */
  52. # define _IS_DRIVE_LETTER(C) \
  53. (((C) >= 'A' && (C) <= 'Z') || ((C) >= 'a' && (C) <= 'z'))
  54. /* Help the compiler optimizing it. This assumes ASCII. */
  55. # undef _IS_DRIVE_LETTER
  56. # define _IS_DRIVE_LETTER(C) \
  57. (((unsigned int) (C) | ('a' - 'A')) - 'a' <= 'z' - 'a')
  58. # define HAS_DEVICE(Filename) \
  59. (_IS_DRIVE_LETTER ((Filename)[0]) && (Filename)[1] == ':')
  60. # define FILE_SYSTEM_PREFIX_LEN(Filename) (HAS_DEVICE (Filename) ? 2 : 0)
  61. # ifdef __CYGWIN__
  62. # define FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE 0
  63. # else
  64. /* On native Windows, OS/2, DOS, the system has the notion of a
  65. "current directory" on each drive. */
  66. # define FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE 1
  67. # endif
  68. # if FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
  69. # define IS_ABSOLUTE_FILE_NAME(Filename) \
  70. ISSLASH ((Filename)[FILE_SYSTEM_PREFIX_LEN (Filename)])
  71. # else
  72. # define IS_ABSOLUTE_FILE_NAME(Filename) \
  73. (ISSLASH ((Filename)[0]) || HAS_DEVICE (Filename))
  74. # endif
  75. # define IS_RELATIVE_FILE_NAME(Filename) \
  76. (! (ISSLASH ((Filename)[0]) || HAS_DEVICE (Filename)))
  77. # define IS_FILE_NAME_WITH_DIR(Filename) \
  78. (strchr ((Filename), '/') != NULL || strchr ((Filename), '\\') != NULL \
  79. || HAS_DEVICE (Filename))
  80. #else
  81. /* Unix */
  82. # define ISSLASH(C) ((C) == '/')
  83. # define HAS_DEVICE(Filename) ((void) (Filename), 0)
  84. # define FILE_SYSTEM_PREFIX_LEN(Filename) ((void) (Filename), 0)
  85. # define FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE 0
  86. # define IS_ABSOLUTE_FILE_NAME(Filename) ISSLASH ((Filename)[0])
  87. # define IS_RELATIVE_FILE_NAME(Filename) (! ISSLASH ((Filename)[0]))
  88. # define IS_FILE_NAME_WITH_DIR(Filename) (strchr ((Filename), '/') != NULL)
  89. #endif
  90. /* Deprecated macros. For backward compatibility with old users of the
  91. 'filename' module. */
  92. #define IS_ABSOLUTE_PATH IS_ABSOLUTE_FILE_NAME
  93. #define IS_PATH_WITH_DIR IS_FILE_NAME_WITH_DIR
  94. #ifdef __cplusplus
  95. }
  96. #endif
  97. #endif /* _FILENAME_H */