This repository has been archived on 2020-06-07. You can view files and clone it, but cannot push or open issues or pull requests.
php-armadillo/src/stream_utils.hh
2019-08-25 22:18:46 +08:00

67 lines
1.9 KiB
C++

//
// php-armadillo/stream_utils.hh
//
// @Author CismonX
//
#ifndef PHP_ARMA_STREAM_UTILS_HH
#define PHP_ARMA_STREAM_UTILS_HH
#include "php_arma.hh"
#ifdef __GLIBCXX__
#include <ext/stdio_filebuf.h>
#define Z_ISTREAM_P(zval_p, name) \
__gnu_cxx::stdio_filebuf<char> _filebuf(zval_to_fd(zval_p), std::ios::in); \
std::istream name(&_filebuf)
#define Z_OSTREAM_P(zval_p, name) \
__gnu_cxx::stdio_filebuf<char> _filebuf(zval_to_fd(zval_p), std::ios::out); \
std::ostream name(&_filebuf)
#else
#define Z_ISTREAM_P(zval_p, name) \
extern std::istream name; \
zval_to_iostream_unsupported()
#define Z_OSTREAM_P(zval_p, name) \
extern std::ostream name; \
zval_to_iostream_unsupported()
#endif // __GLIBCXX__
namespace php_arma
{
#ifdef __GLIBCXX__
zend_always_inline
int zval_to_fd(zval *zv)
{
auto stream = reinterpret_cast<php_stream*>(zend_fetch_resource2_ex(
zv, "stream", php_file_le_stream(), php_file_le_pstream()));
if (stream == nullptr) {
return -1;
}
struct {
FILE *file;
int fd;
} *stream_data = reinterpret_cast<decltype(stream_data)>(stream->abstract);
return stream_data->fd;
}
constexpr auto zval_to_iostream_supported = true;
#else
zend_always_inline
void zval_to_iostream_unsupported()
{
throw_exception_ex(zend_ce_type_error, "resource is not yet supported, use file name instead, "
"or re-compile php-armadillo and link with libstdc++.");
}
constexpr auto zval_to_iostream_supported = false;
#endif // __GLIBCXX__
}
#endif // !PHP_ARMA_STREAM_UTILS_HH