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-06-08 01:56:22 +08:00

60 lines
1.6 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 __GNUC__
#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)
#define ZVAL_TO_IOSTREAM_SUPPORTED true
#else
#define Z_ISTREAM_P(zval_p, name) zval_to_iostream_unsupported()
#define Z_OSTREAM_P(zval_p, name) zval_to_iostream_unsupported()
#define ZVAL_TO_IOSTREAM_SUPPORTED false
#endif // __GNUC__
namespace php_arma
{
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;
}
zend_always_inline
void zval_to_iostream_unsupported()
{
zend_throw_exception(zend_ce_type_error, "Resource is not yet supported. Use file name instead, "
"or re-compile php-armadillo with a compiler supporting GNU extensions.", 0);
}
}
#endif // !PHP_ARMA_STREAM_UTILS_HH