Commit 9d1dbd95 authored by Dāvis Mosāns's avatar Dāvis Mosāns Committed by Brad King

FStream: Add MinGW support

std::basic_filebuf::open(const wchar_t *) isn't part of C++ standard
and it's only present for MSVC but it isn't present in libstdc++ (MinGW)
so we implement this functionality using GNU libstdc++ stdio_filebuf
extension and _wfopen function.

Change-Id: I7df7a1b22ba272db23c8d962cb66ec71a278ef06
parent d2cdfc6d
......@@ -632,6 +632,11 @@ IF(KWSYS_USE_SystemInformation)
ENDIF()
ENDIF()
IF(KWSYS_USE_FStream)
KWSYS_PLATFORM_CXX_TEST(KWSYS_CXX_HAS_EXT_STDIO_FILEBUF_H
"Checking whether <ext/stdio_filebuf.h> is available" DIRECT)
ENDIF()
#-----------------------------------------------------------------------------
# Choose a directory for the generated headers.
IF(NOT KWSYS_HEADER_ROOT)
......
......@@ -17,6 +17,8 @@
/* Whether wstring is available. */
#define @KWSYS_NAMESPACE@_STL_HAS_WSTRING @KWSYS_STL_HAS_WSTRING@
/* Whether <ext/stdio_filebuf.h> is available. */
#define @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H @KWSYS_CXX_HAS_EXT_STDIO_FILEBUF_H@
/* If building a C++ file in kwsys itself, give the source file
access to the macros without a configured namespace. */
......@@ -24,8 +26,9 @@
# if !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
# define kwsys @KWSYS_NAMESPACE@
# endif
# define KWSYS_NAME_IS_KWSYS @KWSYS_NAMESPACE@_NAME_IS_KWSYS
# define KWSYS_STL_HAS_WSTRING @KWSYS_NAMESPACE@_STL_HAS_WSTRING
# define KWSYS_NAME_IS_KWSYS @KWSYS_NAMESPACE@_NAME_IS_KWSYS
# define KWSYS_STL_HAS_WSTRING @KWSYS_NAMESPACE@_STL_HAS_WSTRING
# define KWSYS_CXX_HAS_EXT_STDIO_FILEBUF_H @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H
#endif
#endif
......@@ -12,20 +12,30 @@
#ifndef @KWSYS_NAMESPACE@_FStream_hxx
#define @KWSYS_NAMESPACE@_FStream_hxx
#include <@KWSYS_NAMESPACE@/Configure.hxx>
#include <@KWSYS_NAMESPACE@/Encoding.hxx>
#include <fstream>
#if defined(_WIN32)
# if !defined(_MSC_VER) && @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H
# include <ext/stdio_filebuf.h>
# endif
#endif
namespace @KWSYS_NAMESPACE@
{
#if defined(_MSC_VER) && _MSC_VER >= 1400
#if defined(_WIN32) && (defined(_MSC_VER) || @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H)
# if defined(_NOEXCEPT)
# define @KWSYS_NAMESPACE@_FStream_NOEXCEPT _NOEXCEPT
# else
# define @KWSYS_NAMESPACE@_FStream_NOEXCEPT
# endif
#if defined(_MSC_VER)
template<typename CharType,typename Traits>
class basic_filebuf : public std::basic_filebuf<CharType,Traits>
{
# if _MSC_VER >= 1400
public:
typedef std::basic_filebuf<CharType,Traits> my_base_type;
basic_filebuf *open(char const *s,std::ios_base::openmode mode)
......@@ -35,25 +45,73 @@ namespace @KWSYS_NAMESPACE@
my_base_type::open(wstr.c_str(), mode)
);
}
# endif
};
#else
inline std::wstring getcmode(const std::ios_base::openmode mode) {
std::wstring cmode;
bool plus = false;
if (mode & std::ios_base::app) {
cmode += L"a";
plus = mode & std::ios_base::in ? true : false;
} else if (mode & std::ios_base::trunc ||
(mode & std::ios_base::out && (mode & std::ios_base::in) == 0)) {
cmode += L"w";
plus = mode & std::ios_base::in ? true : false;
} else {
cmode += L"r";
plus = mode & std::ios_base::out ? true : false;
}
if (plus) {
cmode += L"+";
}
if (mode & std::ios_base::binary) {
cmode += L"b";
} else {
cmode += L"t";
}
return cmode;
};
#endif
template<typename CharType,typename Traits = std::char_traits<CharType> >
class basic_efilebuf
{
public:
#if defined(_MSC_VER)
typedef basic_filebuf<CharType,Traits> internal_buffer_type;
#else
typedef __gnu_cxx::stdio_filebuf<CharType,Traits> internal_buffer_type;
#endif
basic_efilebuf()
basic_efilebuf() : file_(0)
{
buf_ = 0;
}
bool _open(char const *file_name,std::ios_base::openmode mode)
{
if (is_open()) {
if (is_open() || file_) {
return false;
}
#if defined(_MSC_VER)
const bool success = buf_->open(file_name,mode) != 0;
#else
const std::wstring wstr = Encoding::ToWide(file_name);
bool success = false;
std::wstring cmode = getcmode(mode);
file_ = _wfopen(wstr.c_str(), cmode.c_str());
if (file_) {
if (buf_) {
delete buf_;
}
buf_ = new internal_buffer_type(file_, mode);
success = true;
}
#endif
return success;
}
......@@ -78,12 +136,21 @@ namespace @KWSYS_NAMESPACE@
bool success = false;
if (buf_) {
success = buf_->close() != 0;
#if !defined(_MSC_VER)
if (file_) {
success = fclose(file_) == 0 ? success : false;
file_ = 0;
}
#endif
}
return success;
}
static void _set_state(bool success, std::basic_ios<CharType,Traits> *ios)
static void _set_state(bool success, std::basic_ios<CharType,Traits> *ios, basic_efilebuf* efilebuf)
{
#if !defined(_MSC_VER)
ios->rdbuf(efilebuf->buf_);
#endif
if (!success) {
ios->setstate(std::ios_base::failbit);
} else {
......@@ -100,6 +167,7 @@ namespace @KWSYS_NAMESPACE@
protected:
internal_buffer_type* buf_;
FILE *file_;
};
template<typename CharType,typename Traits = std::char_traits<CharType> >
......@@ -127,12 +195,12 @@ class basic_ifstream : public std::basic_istream<CharType,Traits>,
void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::in)
{
mode = mode | std::ios_base::in;
this->_set_state(this->_open(file_name, mode), this);
this->_set_state(this->_open(file_name, mode), this, this);
}
void close()
{
this->_set_state(this->_close(), this);
this->_set_state(this->_close(), this, this);
}
internal_buffer_type *rdbuf() const
......@@ -169,12 +237,12 @@ class basic_ofstream : public std::basic_ostream<CharType,Traits>,
void open(char const *file_name,std::ios_base::openmode mode = std::ios_base::out)
{
mode = mode | std::ios_base::out;
this->_set_state(this->_open(file_name, mode), this);
this->_set_state(this->_open(file_name, mode), this, this);
}
void close()
{
this->_set_state(this->_close(), this);
this->_set_state(this->_close(), this, this);
}
internal_buffer_type *rdbuf() const
......
......@@ -349,3 +349,8 @@ int main()
void f(std ::wstring*) {}
int main() { return 0; }
#endif
#ifdef TEST_KWSYS_CXX_HAS_EXT_STDIO_FILEBUF_H
#include <ext/stdio_filebuf.h>
int main() { return 0; }
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment