Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:05:27

0001 //  boost/filesystem/cstdio.hpp  ------------------------------------------------------//
0002 
0003 //  Copyright Andrey Semashev 2023
0004 
0005 //  Distributed under the Boost Software License, Version 1.0.
0006 //  See http://www.boost.org/LICENSE_1_0.txt
0007 
0008 //  Library home page: http://www.boost.org/libs/filesystem
0009 
0010 //--------------------------------------------------------------------------------------//
0011 
0012 #ifndef BOOST_FILESYSTEM_CSTDIO_HPP
0013 #define BOOST_FILESYSTEM_CSTDIO_HPP
0014 
0015 #include <boost/filesystem/config.hpp>
0016 #include <boost/filesystem/path.hpp>
0017 #include <cstdio>
0018 #if defined(BOOST_WINDOWS_API)
0019 #include <wchar.h>
0020 #include <cstddef>
0021 #include <cstring>
0022 #include <cstdlib>
0023 #endif
0024 
0025 #include <boost/filesystem/detail/header.hpp> // must be the last #include
0026 
0027 namespace boost {
0028 namespace filesystem {
0029 
0030 #if defined(BOOST_WINDOWS_API)
0031 
0032 inline std::FILE* fopen(filesystem::path const& p, const char* mode)
0033 {
0034 #if defined(__CYGWIN__) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && defined(__STRICT_ANSI__))
0035     // Cygwin and MinGW32 in strict ANSI mode do not declare _wfopen
0036     return std::fopen(p.string().c_str(), mode);
0037 #else
0038     // mode should only contain ASCII characters and is likely short
0039     struct small_array
0040     {
0041         wchar_t buf[128u];
0042         wchar_t* p;
0043 
0044         small_array() BOOST_NOEXCEPT : p(buf) {}
0045         ~small_array() BOOST_NOEXCEPT
0046         {
0047             if (BOOST_UNLIKELY(p != buf))
0048                 std::free(p);
0049         }
0050     }
0051     wmode;
0052     std::size_t wmode_len = std::mbstowcs(wmode.p, mode, sizeof(wmode.buf) / sizeof(wchar_t));
0053     if (BOOST_UNLIKELY(wmode_len >= (sizeof(wmode.buf) / sizeof(wchar_t))))
0054     {
0055         wmode_len = std::mbstowcs(NULL, mode, 0u);
0056         // Check for size overflow, including (size_t)-1, which indicates mbstowcs error
0057         if (BOOST_UNLIKELY(wmode_len >= (static_cast< std::size_t >(-1) / sizeof(wchar_t))))
0058             return NULL;
0059 
0060         wmode.p = static_cast< wchar_t* >(std::malloc((wmode_len + 1u) * sizeof(wchar_t)));
0061         if (BOOST_UNLIKELY(!wmode.p))
0062             return NULL;
0063 
0064         std::size_t wmode_len2 = std::mbstowcs(wmode.p, mode, wmode_len + 1u);
0065         if (BOOST_UNLIKELY(wmode_len2 > wmode_len))
0066             return NULL;
0067     }
0068 
0069     return ::_wfopen(p.c_str(), wmode.p);
0070 #endif
0071 }
0072 
0073 #else // defined(BOOST_WINDOWS_API)
0074 
0075 inline std::FILE* fopen(filesystem::path const& p, const char* mode)
0076 {
0077     return std::fopen(p.c_str(), mode);
0078 }
0079 
0080 #endif // defined(BOOST_WINDOWS_API)
0081 
0082 } // namespace filesystem
0083 } // namespace boost
0084 
0085 #include <boost/filesystem/detail/footer.hpp>
0086 
0087 #endif // BOOST_FILESYSTEM_CSTDIO_HPP