refactor filebuf

This commit is contained in:
CismonX 2019-06-08 01:56:22 +08:00
parent 533935f649
commit 047d0fed7d
3 changed files with 43 additions and 41 deletions

View File

@ -144,13 +144,10 @@ namespace php_arma
if (stream == nullptr) {
native->print();
} else {
#ifdef __GNUC__
__gnu_cxx::stdio_filebuf<char> filebuf(zval_to_fd(stream), std::ios::out);
std::ostream os(&filebuf);
native->print(os);
#else
filebuf_unsupported();
#endif // __GNUC__
Z_OSTREAM_P(stream, os);
if constexpr (ZVAL_TO_IOSTREAM_SUPPORTED) {
native->print(os);
}
}
}
@ -167,13 +164,10 @@ namespace php_arma
if (stream == nullptr) {
native->raw_print();
} else {
#ifdef __GNUC__
__gnu_cxx::stdio_filebuf<char> filebuf(zval_to_fd(stream), std::ios::out);
std::ostream os(&filebuf);
native->raw_print(os);
#else
filebuf_unsupported();
#endif // __GNUC__
Z_OSTREAM_P(stream, os);
if constexpr (ZVAL_TO_IOSTREAM_SUPPORTED) {
native->raw_print(os);
}
}
}

View File

@ -11,23 +11,19 @@
#include <armadillo>
#if PHP_VERSION_ID < 70200
#define zend_hash_str_find_deref zend_hash_str_find
#endif
namespace php_arma
{
#ifdef ARMA_USE_HDF5
zend_always_inline
arma::hdf5_name zend_array_to_hdf5_name(zend_array *ht)
{
auto fname_z = zend_hash_str_find_deref(ht, "file_name", sizeof("file_name") - 1);
auto fname_z = zend_hash_str_find(ht, "file_name", sizeof("file_name") - 1);
std::string fname(fname_z && Z_TYPE_P(fname_z) == IS_STRING ? Z_STRVAL_P(fname_z) : "");
auto dsname_z = zend_hash_str_find_deref(ht, "dataset_name", sizeof("dataset_name") - 1);
auto dsname_z = zend_hash_str_find(ht, "dataset_name", sizeof("dataset_name") - 1);
std::string dsname(dsname_z && Z_TYPE_P(dsname_z) == IS_STRING ? Z_STRVAL_P(dsname_z) : "");
auto opts_z = zend_hash_str_find_deref(ht, "options", sizeof("options") - 1);
auto opts_z = zend_hash_str_find(ht, "options", sizeof("options") - 1);
arma::hdf5_opts::opts opts(opts_z && Z_TYPE_P(opts_z) == IS_LONG ? Z_LVAL_P(opts_z) : hdf5_opts::none);
return { fname, dsname, opts };
@ -78,14 +74,12 @@ namespace php_arma
RETURN_BOOL(native->save(Z_STRVAL_P(dest), ftype, false));
}
if (Z_TYPE_P(dest) == IS_RESOURCE) {
#ifdef __GNUC__
__gnu_cxx::stdio_filebuf<char> filebuf(zval_to_fd(dest), std::ios::out);
std::ostream os(&filebuf);
RETURN_BOOL(native->save(os, ftype, false));
#else
filebuf_unsupported();
RETURN_FALSE;
#endif // __GNUC__
Z_OSTREAM_P(dest, os);
if constexpr (ZVAL_TO_IOSTREAM_SUPPORTED) {
RETURN_BOOL(native->save(os, ftype, false));
} else {
RETURN_FALSE;
}
}
#ifdef ARMA_USE_HDF5
if (Z_TYPE_P(dest) == IS_ARRAY) {
@ -121,14 +115,12 @@ namespace php_arma
RETURN_BOOL(native->load(Z_STRVAL_P(from), ftype, false));
}
if (Z_TYPE_P(from) == IS_RESOURCE) {
#ifdef __GNUC__
__gnu_cxx::stdio_filebuf<char> filebuf(zval_to_fd(from), std::ios::in);
std::istream is(&filebuf);
RETURN_BOOL(native->load(is, ftype, false));
#else
filebuf_unsupported();
RETURN_FALSE;
#endif // __GNUC__
Z_ISTREAM_P(from, is);
if constexpr (ZVAL_TO_IOSTREAM_SUPPORTED) {
RETURN_BOOL(native->load(is, ftype, false));
} else {
RETURN_FALSE;
}
}
#ifdef ARMA_USE_HDF5
if (Z_TYPE_P(from) == IS_ARRAY) {

View File

@ -10,12 +10,29 @@
#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
{
#ifdef __GNUC__
zend_always_inline
int zval_to_fd(zval *zv)
{
@ -30,14 +47,13 @@ namespace php_arma
} *stream_data = reinterpret_cast<decltype(stream_data)>(stream->abstract);
return stream_data->fd;
}
#else
zend_always_inline
void filebuf_unsupported()
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 // __GNUC__
}
#endif // !PHP_ARMA_STREAM_UTILS_HH