stat.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // Copyright (c) 2020 Alexander Grund
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #ifndef BOOST_NOWIDE_STAT_HPP_INCLUDED
  7. #define BOOST_NOWIDE_STAT_HPP_INCLUDED
  8. #include <boost/nowide/config.hpp>
  9. #include <sys/types.h>
  10. // Include after sys/types.h
  11. #include <sys/stat.h>
  12. #if defined(__MINGW32__) && defined(__MSVCRT_VERSION__) && __MSVCRT_VERSION__ < 0x0601
  13. /// Forward declaration in case MinGW32 is used and __MSVCRT_VERSION__ is defined lower than 6.1
  14. struct __stat64;
  15. #endif
  16. namespace boost {
  17. namespace nowide {
  18. #if !defined(BOOST_WINDOWS) && !defined(BOOST_NOWIDE_DOXYGEN)
  19. // Note: `using x = struct ::stat` causes a bogus warning in GCC < 11
  20. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66159
  21. typedef struct ::stat stat_t;
  22. typedef struct ::stat posix_stat_t;
  23. using ::stat;
  24. #else
  25. /// \brief Typedef for the file info structure.
  26. /// Able to hold 64 bit file size and timestamps on Windows and usually also on other 64 Bit systems
  27. /// This allows to write portable code with optional LFS support
  28. typedef struct ::__stat64 stat_t;
  29. /// \brief Typedef for the file info structure used in the POSIX stat call
  30. /// Resolves to `struct _stat` on Windows and `struct stat` otherwise
  31. /// This allows to write portable code using the default stat function
  32. typedef struct ::_stat posix_stat_t;
  33. /// \cond INTERNAL
  34. namespace detail {
  35. BOOST_NOWIDE_DECL int stat(const char* path, stat_t* buffer, size_t buffer_size);
  36. BOOST_NOWIDE_DECL int stat(const char* path, posix_stat_t* buffer, size_t buffer_size);
  37. } // namespace detail
  38. /// \endcond
  39. ///
  40. /// \brief UTF-8 aware stat function, returns 0 on success
  41. ///
  42. /// Return information about a file from an UTF-8 encoded path
  43. ///
  44. inline int stat(const char* path, stat_t* buffer)
  45. {
  46. return detail::stat(path, buffer, sizeof(*buffer));
  47. }
  48. ///
  49. /// \brief UTF-8 aware stat function, returns 0 on success
  50. ///
  51. /// Return information about a file from an UTF-8 encoded path
  52. ///
  53. inline int stat(const char* path, posix_stat_t* buffer)
  54. {
  55. return detail::stat(path, buffer, sizeof(*buffer));
  56. }
  57. #endif
  58. } // namespace nowide
  59. } // namespace boost
  60. #endif