// // 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 #define Z_ISTREAM_P(zval_p, name) \ __gnu_cxx::stdio_filebuf _filebuf(zval_to_fd(zval_p), std::ios::in); \ std::istream name(&_filebuf) #define Z_OSTREAM_P(zval_p, name) \ __gnu_cxx::stdio_filebuf _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(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(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