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

67 lines
1.9 KiB
C++
Raw Normal View History

2019-05-30 14:02:11 +00:00
//
// php-armadillo/stream_utils.hh
//
// @Author CismonX
//
#ifndef PHP_ARMA_STREAM_UTILS_HH
#define PHP_ARMA_STREAM_UTILS_HH
#include "php_arma.hh"
2019-07-16 14:16:01 +00:00
#ifdef __GLIBCXX__
2019-06-07 17:56:22 +00:00
2019-05-30 14:02:11 +00:00
#include <ext/stdio_filebuf.h>
2019-06-07 17:56:22 +00:00
#define Z_ISTREAM_P(zval_p, name) \
__gnu_cxx::stdio_filebuf<char> _filebuf(zval_to_fd(zval_p), std::ios::in); \
2019-06-07 18:08:49 +00:00
std::istream name(&_filebuf)
2019-06-07 17:56:22 +00:00
#define Z_OSTREAM_P(zval_p, name) \
__gnu_cxx::stdio_filebuf<char> _filebuf(zval_to_fd(zval_p), std::ios::out); \
2019-06-07 18:08:49 +00:00
std::ostream name(&_filebuf)
2019-06-07 17:56:22 +00:00
#else
2019-07-12 16:29:03 +00:00
#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()
2019-06-07 17:56:22 +00:00
2019-07-16 14:16:01 +00:00
#endif // __GLIBCXX__
2019-05-30 14:02:11 +00:00
namespace php_arma
{
2019-07-16 14:16:01 +00:00
#ifdef __GLIBCXX__
2019-05-30 14:02:11 +00:00
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;
}
2019-06-11 10:18:40 +00:00
constexpr auto zval_to_iostream_supported = true;
2019-06-09 14:47:33 +00:00
#else
2019-05-30 14:02:11 +00:00
zend_always_inline
2019-06-07 17:56:22 +00:00
void zval_to_iostream_unsupported()
2019-05-30 14:02:11 +00:00
{
2019-06-11 10:18:40 +00:00
throw_exception_ex(zend_ce_type_error, "resource is not yet supported, use file name instead, "
2019-08-25 14:15:33 +00:00
"or re-compile php-armadillo and link with libstdc++.");
2019-05-30 14:02:11 +00:00
}
2019-06-11 10:18:40 +00:00
constexpr auto zval_to_iostream_supported = false;
2019-07-16 14:16:01 +00:00
#endif // __GLIBCXX__
2019-05-30 14:02:11 +00:00
}
#endif // !PHP_ARMA_STREAM_UTILS_HH