瀏覽代碼

Merge pull request #34 from viest/dev

 FEAT(const_memory): Large file export
王杰新 7 年之前
父節點
當前提交
19f7f132ef
共有 5 個文件被更改,包括 71 次插入24 次删除
  1. 1 0
      config.m4
  2. 1 1
      config.w32
  3. 19 0
      kernel/common.c
  4. 45 22
      kernel/excel.c
  5. 5 1
      kernel/include.h

+ 1 - 0
config.m4

@@ -5,6 +5,7 @@ if test "$PHP_EXCEL_WRITER" != "no"; then
     excel_writer_sources="excel_writer.c \
     kernel/exception.c \
     kernel/resource.c \
+    kernel/common.c \
     "
 
     AC_MSG_CHECKING([Check libxlsxwriter support])

+ 1 - 1
config.w32

@@ -10,7 +10,7 @@ if (PHP_EXCEL_WRITER != "no") {
         CHECK_HEADER_ADD_INCLUDE("packager.h", "CFLAGS_EXCEL_WRITER", PHP_PHP_BUILD + "\\MSVCLibXlsxWriter\\libxlsxwriter\\include\\xlsxwriter;" + PHP_EXCEL_WRITER) &&
         CHECK_HEADER_ADD_INCLUDE("format.h", "CFLAGS_EXCEL_WRITER", PHP_PHP_BUILD + "\\MSVCLibXlsxWriter\\libxlsxwriter\\include\\xlsxwriter;" + PHP_EXCEL_WRITER)) {
         EXTENSION("excel_writer", "excel_writer.c")
-        ADD_SOURCES(configure_module_dirname + "\\kernel", "resource.c exception.c excel.c write.c format.c", "excel_writer");
+        ADD_SOURCES(configure_module_dirname + "\\kernel", "common.c resource.c exception.c excel.c write.c format.c", "excel_writer");
     } else {
         WARNING("excel_writer not enabled, LibXlsxWriter.lib or headers not found");
     }

+ 19 - 0
kernel/common.c

@@ -0,0 +1,19 @@
+#include "include.h"
+
+void excel_file_path(zend_string *file_name, zval *dir_path, zval *file_path)
+{
+    zend_string *full_path, *zstr_path;
+
+    zstr_path = zval_get_string(dir_path);
+
+    if (Z_STRVAL_P(dir_path)[Z_STRLEN_P(dir_path)-1] == '/') {
+        full_path = zend_string_extend(zstr_path, ZSTR_LEN(zstr_path) + ZSTR_LEN(file_name), 0);
+        memcpy(ZSTR_VAL(full_path)+ZSTR_LEN(zstr_path), ZSTR_VAL(file_name), ZSTR_LEN(file_name)+1);
+    } else {
+        full_path = zend_string_extend(zstr_path, ZSTR_LEN(zstr_path) + ZSTR_LEN(file_name) + 1, 0);
+        ZSTR_VAL(full_path)[ZSTR_LEN(zstr_path)] ='/';
+        memcpy(ZSTR_VAL(full_path)+ZSTR_LEN(zstr_path)+1, ZSTR_VAL(file_name), ZSTR_LEN(file_name)+1);
+    }
+
+    ZVAL_STR(file_path, full_path);
+}

+ 45 - 22
kernel/excel.c

@@ -108,7 +108,7 @@ PHP_METHOD(vtiful_excel, __construct)
 PHP_METHOD(vtiful_excel, fileName)
 {
     zval rv, file_path, handle, *config, *tmp_path;
-    zend_string *file_name, *full_path, *zstr_path;
+    zend_string *file_name;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
             Z_PARAM_STR(file_name)
@@ -119,20 +119,41 @@ PHP_METHOD(vtiful_excel, fileName)
     config   = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_COF), 0, &rv TSRMLS_DC);
     tmp_path = zend_hash_str_find(Z_ARRVAL_P(config), V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1);
 
-    zstr_path = zval_get_string(tmp_path);
+    excel_file_path(file_name, tmp_path, &file_path);
 
-    if (Z_STRVAL_P(tmp_path)[Z_STRLEN_P(tmp_path)-1] == '/') {
-        full_path = zend_string_extend(zstr_path, ZSTR_LEN(zstr_path) + ZSTR_LEN(file_name), 0);
-        memcpy(ZSTR_VAL(full_path)+ZSTR_LEN(zstr_path), ZSTR_VAL(file_name), ZSTR_LEN(file_name)+1);
-    } else {
-        full_path = zend_string_extend(zstr_path, ZSTR_LEN(zstr_path) + ZSTR_LEN(file_name) + 1, 0);
-        ZSTR_VAL(full_path)[ZSTR_LEN(zstr_path)] ='/';
-        memcpy(ZSTR_VAL(full_path)+ZSTR_LEN(zstr_path)+1, ZSTR_VAL(file_name), ZSTR_LEN(file_name)+1);
-    }
+    excel_res->workbook  = workbook_new(Z_STRVAL(file_path));
+    excel_res->worksheet = workbook_add_worksheet(excel_res->workbook, NULL);
 
-    ZVAL_STR(&file_path, full_path);
+    ZVAL_RES(&handle, zend_register_resource(excel_res, le_excel_writer));
 
-    excel_res->workbook  = workbook_new(Z_STRVAL(file_path));
+    zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
+    zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &handle);
+
+    zval_ptr_dtor(&file_path);
+}
+/* }}} */
+
+/** {{{ \Vtiful\Kernel\Excel::constMemory(string $fileName)
+ */
+PHP_METHOD(vtiful_excel, constMemory)
+{
+    zval rv, file_path, handle, *config, *tmp_path;
+    zend_string *file_name;
+
+    ZEND_PARSE_PARAMETERS_START(1, 1)
+            Z_PARAM_STR(file_name)
+    ZEND_PARSE_PARAMETERS_END();
+
+    ZVAL_COPY(return_value, getThis());
+
+    config   = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_COF), 0, &rv TSRMLS_DC);
+    tmp_path = zend_hash_str_find(Z_ARRVAL_P(config), V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1);
+
+    excel_file_path(file_name, tmp_path, &file_path);
+
+    lxw_workbook_options options = {.constant_memory = LXW_TRUE, .tmpdir = NULL};
+
+    excel_res->workbook  = workbook_new_opt(Z_STRVAL(file_path), &options);
     excel_res->worksheet = workbook_add_worksheet(excel_res->workbook, NULL);
 
     ZVAL_RES(&handle, zend_register_resource(excel_res, le_excel_writer));
@@ -144,11 +165,12 @@ PHP_METHOD(vtiful_excel, fileName)
 }
 /* }}} */
 
+
 /** {{{ \Vtiful\Kernel\Excel::header(array $header)
  */
 PHP_METHOD(vtiful_excel, header)
 {
-    zval rv, res_handle, *header, *header_value;
+    zval res_handle, *header, *header_value;
     zend_long header_l_key;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -171,7 +193,7 @@ PHP_METHOD(vtiful_excel, header)
  */
 PHP_METHOD(vtiful_excel, data)
 {
-    zval rv, *data, res_handle, *data_r_value, *data_l_value;
+    zval *data, res_handle, *data_r_value, *data_l_value;
     zend_long data_r_key, data_l_key;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -231,7 +253,7 @@ PHP_METHOD(vtiful_excel, getHandle)
  */
 PHP_METHOD(vtiful_excel, insertText)
 {
-    zval rv, res_handle;
+    zval res_handle;
     zval *data;
     zend_long row, column;
     zend_string *format = NULL;
@@ -257,7 +279,7 @@ PHP_METHOD(vtiful_excel, insertText)
  */
 PHP_METHOD(vtiful_excel, insertImage)
 {
-    zval rv, res_handle;
+    zval res_handle;
     zval *image;
     zend_long row, column;
 
@@ -280,7 +302,7 @@ PHP_METHOD(vtiful_excel, insertImage)
  */
 PHP_METHOD(vtiful_excel, insertFormula)
 {
-    zval rv, res_handle;
+    zval res_handle;
     zval *formula;
     zend_long row, column;
 
@@ -303,7 +325,7 @@ PHP_METHOD(vtiful_excel, insertFormula)
  */
 PHP_METHOD(vtiful_excel, autoFilter)
 {
-    zval rv, res_handle;
+    zval res_handle;
     zend_string *range;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -323,7 +345,7 @@ PHP_METHOD(vtiful_excel, autoFilter)
  */
 PHP_METHOD(vtiful_excel, mergeCells)
 {
-    zval rv, res_handle;
+    zval res_handle;
     zend_string *range, *data;
 
     ZEND_PARSE_PARAMETERS_START(2, 2)
@@ -405,6 +427,7 @@ PHP_METHOD(vtiful_excel, setRow)
 zend_function_entry excel_methods[] = {
         PHP_ME(vtiful_excel, __construct,   excel_construct_arginfo,      ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
         PHP_ME(vtiful_excel, fileName,      excel_file_name_arginfo,      ZEND_ACC_PUBLIC)
+        PHP_ME(vtiful_excel, constMemory,   excel_file_name_arginfo,      ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_excel, header,        excel_header_arginfo,         ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_excel, data,          excel_data_arginfo,           ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_excel, output,        NULL,                         ZEND_ACC_PUBLIC)
@@ -429,9 +452,9 @@ VTIFUL_STARTUP_FUNCTION(excel) {
 
     vtiful_excel_ce = zend_register_internal_class(&ce);
 
-    zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_COF), ZEND_ACC_PRIVATE);
-    zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_FIL), ZEND_ACC_PRIVATE);
-    zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_HANDLE), ZEND_ACC_PRIVATE);
+    REGISTER_CLASS_PROPERTY_NULL(vtiful_excel_ce, V_EXCEL_COF,    ZEND_ACC_PRIVATE);
+    REGISTER_CLASS_PROPERTY_NULL(vtiful_excel_ce, V_EXCEL_FIL,    ZEND_ACC_PRIVATE);
+    REGISTER_CLASS_PROPERTY_NULL(vtiful_excel_ce, V_EXCEL_HANDLE, ZEND_ACC_PRIVATE);
 
     return SUCCESS;
 }

+ 5 - 1
kernel/include.h

@@ -20,6 +20,9 @@
 #define REGISTER_CLASS_CONST_LONG(class_name, const_name, value) \
     zend_declare_class_constant_long(class_name, const_name, sizeof(const_name)-1, (zend_long)value);
 
+#define REGISTER_CLASS_PROPERTY_NULL(class_name, property_name, acc) \
+    zend_declare_property_null(class_name, ZEND_STRL(property_name), acc);
+
 #define ROW(range) \
     lxw_name_to_row(range)
 
@@ -30,7 +33,6 @@ typedef struct {
 
 extern excel_resource_t *excel_res;
 
-
 excel_resource_t * zval_get_resource(zval *handle);
 lxw_format       * zval_get_format(zval *handle);
 
@@ -52,4 +54,6 @@ void set_column(zend_string *range, double width, excel_resource_t *res, lxw_for
 void set_row(zend_string *range, double height, excel_resource_t *res, lxw_format *format);
 lxw_error workbook_file(excel_resource_t *self, zval *handle);
 
+void excel_file_path(zend_string *file_name, zval *dir_path, zval *file_path);
+
 #endif