Просмотр исходного кода

Merge pull request #20 from viest/dev

 FIX(memory): Process end, but not output file, memory leak.
王杰新 7 лет назад
Родитель
Сommit
e29472452c
6 измененных файлов с 94 добавлено и 69 удалено
  1. 1 1
      README.md
  2. 4 5
      excel_writer.c
  3. 24 58
      kernel/excel.c
  4. 59 5
      kernel/format.c
  5. 4 0
      kernel/include.h
  6. 2 0
      php_excel_writer.h

+ 1 - 1
README.md

@@ -1,6 +1,6 @@
 ![php-excel](https://github.com/viest/php-excel-writer/blob/master/resource/logo.png)
 
-[![Build Status](https://travis-ci.org/viest/php-excel-writer.svg?branch=master)](https://travis-ci.org/viest/php-excel-writer)
+[![Build Status](https://travis-ci.org/viest/php-ext-excel-export.svg?branch=master)](https://travis-ci.org/viest/php-ext-excel-export)
 
 #### 1、Install the dependencies
 

+ 4 - 5
excel_writer.c

@@ -15,13 +15,10 @@
 #endif
 
 #include "php.h"
-#include "php_ini.h"
-#include "php_excel_writer.h"
 #include "ext/standard/info.h"
+#include "kernel/include.h"
 
-#include "kernel/excel.h"
-#include "kernel/format.h"
-#include "kernel/exception.h"
+excel_resource_t *excel_res;
 
 int le_excel_writer;
 
@@ -33,6 +30,8 @@ PHP_MINIT_FUNCTION(excel_writer)
 	VTIFUL_STARTUP_MODULE(excel);
 	VTIFUL_STARTUP_MODULE(format);
 
+	excel_res = emalloc(sizeof(excel_resource_t));
+
     le_excel_writer = zend_register_list_destructors_ex(_php_vtiful_excel_close, NULL, VTIFUL_RESOURCE_NAME, module_number);
 
 	return SUCCESS;

+ 24 - 58
kernel/excel.c

@@ -95,7 +95,6 @@ PHP_METHOD(vtiful_excel, fileName)
 {
     zval rv, file_path, handle, *config, *tmp_path;
     zend_string *file_name, *key, *full_path;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
             Z_PARAM_STR(file_name)
@@ -122,11 +121,10 @@ PHP_METHOD(vtiful_excel, fileName)
     full_path = zend_string_init(tmp_dir, strlen(tmp_dir), 0);
     ZVAL_STR(&file_path, full_path);
 
-    res = emalloc(sizeof(excel_resource_t));
-    res->workbook  = workbook_new(tmp_dir);
-    res->worksheet = workbook_add_worksheet(res->workbook, NULL);
+    excel_res->workbook  = workbook_new(tmp_dir);
+    excel_res->worksheet = workbook_add_worksheet(excel_res->workbook, NULL);
 
-    ZVAL_RES(&handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&handle, zend_register_resource(excel_res, le_excel_writer));
 
     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);
@@ -140,9 +138,8 @@ PHP_METHOD(vtiful_excel, fileName)
  */
 PHP_METHOD(vtiful_excel, header)
 {
-    zval rv, res_handle, *attr_handle, *header, *header_value;
+    zval rv, res_handle, *header, *header_value;
     zend_long header_l_key;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
             Z_PARAM_ARRAY(header)
@@ -150,15 +147,12 @@ PHP_METHOD(vtiful_excel, header)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
-
     ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value) {
-         type_writer(header_value, 0, header_l_key, res, NULL);
+         type_writer(header_value, 0, header_l_key, excel_res, NULL);
          zval_ptr_dtor(header_value);
     } ZEND_HASH_FOREACH_END();
 
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */
@@ -167,9 +161,8 @@ PHP_METHOD(vtiful_excel, header)
  */
 PHP_METHOD(vtiful_excel, data)
 {
-    zval rv, *data, *attr_handle, res_handle, *data_r_value, *data_l_value;
+    zval rv, *data, res_handle, *data_r_value, *data_l_value;
     zend_long data_r_key, data_l_key;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
             Z_PARAM_ARRAY(data)
@@ -177,19 +170,16 @@ PHP_METHOD(vtiful_excel, data)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
-
     ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data), data_r_key, data_r_value) {
         if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
             ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(data_r_value), data_l_key, data_l_value) {
-                type_writer(data_l_value, data_r_key+1, data_l_key, res, NULL);
+                type_writer(data_l_value, data_r_key+1, data_l_key, excel_res, NULL);
                 zval_ptr_dtor(data_l_value);
             } ZEND_HASH_FOREACH_END();
         }
     } ZEND_HASH_FOREACH_END();
 
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */
@@ -199,15 +189,13 @@ PHP_METHOD(vtiful_excel, data)
 PHP_METHOD(vtiful_excel, output)
 {
     zval rv, null_handle, *handle, *file_path;
-    excel_resource_t *res;
 
     handle    = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
     file_path = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_FIL), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(handle);
 
-    workbook_file(res, handle);
+    workbook_file(excel_res, handle);
 
-    efree(res);
+    efree(excel_res);
 
     ZVAL_NULL(&null_handle);
     zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), &null_handle);
@@ -234,10 +222,9 @@ PHP_METHOD(vtiful_excel, getHandle)
 PHP_METHOD(vtiful_excel, insertText)
 {
     zval rv, res_handle;
-    zval *attr_handle, *data;
+    zval *data;
     zend_long row, column;
     zend_string *format = NULL;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(3, 4)
             Z_PARAM_LONG(row)
@@ -249,12 +236,9 @@ PHP_METHOD(vtiful_excel, insertText)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
+    type_writer(data, row, column, excel_res, format);
 
-    type_writer(data, row, column, res, format);
-
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */
@@ -264,9 +248,8 @@ PHP_METHOD(vtiful_excel, insertText)
 PHP_METHOD(vtiful_excel, insertImage)
 {
     zval rv, res_handle;
-    zval *attr_handle, *image;
+    zval *image;
     zend_long row, column;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(3, 3)
             Z_PARAM_LONG(row)
@@ -276,12 +259,9 @@ PHP_METHOD(vtiful_excel, insertImage)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
-
-    image_writer(image, row, column, res);
+    image_writer(image, row, column, excel_res);
 
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */
@@ -291,9 +271,8 @@ PHP_METHOD(vtiful_excel, insertImage)
 PHP_METHOD(vtiful_excel, insertFormula)
 {
     zval rv, res_handle;
-    zval *attr_handle, *formula;
+    zval *formula;
     zend_long row, column;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(3, 3)
             Z_PARAM_LONG(row)
@@ -303,12 +282,9 @@ PHP_METHOD(vtiful_excel, insertFormula)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
+    formula_writer(formula, row, column, excel_res);
 
-    formula_writer(formula, row, column, res);
-
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */
@@ -318,9 +294,7 @@ PHP_METHOD(vtiful_excel, insertFormula)
 PHP_METHOD(vtiful_excel, autoFilter)
 {
     zval rv, res_handle;
-    zval *attr_handle;
     zend_string *range;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
             Z_PARAM_STR(range)
@@ -328,12 +302,9 @@ PHP_METHOD(vtiful_excel, autoFilter)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
-
-    auto_filter(range, res);
+    auto_filter(range, excel_res);
 
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */
@@ -343,9 +314,7 @@ PHP_METHOD(vtiful_excel, autoFilter)
 PHP_METHOD(vtiful_excel, mergeCells)
 {
     zval rv, res_handle;
-    zval *attr_handle;
     zend_string *range, *data;
-    excel_resource_t *res;
 
     ZEND_PARSE_PARAMETERS_START(2, 2)
             Z_PARAM_STR(range)
@@ -354,12 +323,9 @@ PHP_METHOD(vtiful_excel, mergeCells)
 
     ZVAL_COPY(return_value, getThis());
 
-    attr_handle = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
-    res = zval_get_resource(attr_handle);
-
-    merge_cells(range, data, res);
+    merge_cells(range, data, excel_res);
 
-    ZVAL_RES(&res_handle, zend_register_resource(res, le_excel_writer));
+    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
     zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
 }
 /* }}} */

+ 59 - 5
kernel/format.c

@@ -23,11 +23,16 @@ zend_class_entry *vtiful_format_ce;
 ZEND_BEGIN_ARG_INFO_EX(format_style_arginfo, 0, 0, 1)
                 ZEND_ARG_INFO(0, handle)
 ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(format_underline_arginfo, 0, 0, 2)
+                ZEND_ARG_INFO(0, handle)
+                ZEND_ARG_INFO(0, style)
+ZEND_END_ARG_INFO()
 /* }}} */
 
-/** {{{ \Vtiful\Kernel\Format::boldStype()
+/** {{{ \Vtiful\Kernel\Format::bold()
  */
-PHP_METHOD(vtiful_format, boldStype)
+PHP_METHOD(vtiful_format, bold)
 {
     zval *handle;
     lxw_format *bold_format;
@@ -46,10 +51,57 @@ PHP_METHOD(vtiful_format, boldStype)
 }
 /* }}} */
 
+/** {{{ \Vtiful\Kernel\Format::italic()
+ */
+PHP_METHOD(vtiful_format, italic)
+{
+    zval *handle;
+    lxw_format *italic_format;
+    excel_resource_t *excel_res;
+
+    ZEND_PARSE_PARAMETERS_START(1, 1)
+            Z_PARAM_RESOURCE(handle)
+    ZEND_PARSE_PARAMETERS_END();
+
+    excel_res   = zval_get_resource(handle);
+    italic_format = workbook_add_format(excel_res->workbook);
+
+    format_set_italic(italic_format);
+
+    RETURN_RES(zend_register_resource(italic_format, le_excel_writer));
+}
+/* }}} */
+
+/** {{{ \Vtiful\Kernel\Format::underline()
+ */
+PHP_METHOD(vtiful_format, underline)
+{
+    zval *handle;
+    zend_long style;
+    lxw_format *underline_format;
+    excel_resource_t *excel_res;
+
+    ZEND_PARSE_PARAMETERS_START(2, 2)
+            Z_PARAM_RESOURCE(handle)
+            Z_PARAM_LONG(style)
+    ZEND_PARSE_PARAMETERS_END();
+
+    excel_res = zval_get_resource(handle);
+    underline_format = workbook_add_format(excel_res->workbook);
+
+    format_set_underline(underline_format, style);
+
+    RETURN_RES(zend_register_resource(underline_format, le_excel_writer));
+}
+/* }}} */
+
+
 /** {{{ excel_methods
 */
-zend_function_entry formac_methods[] = {
-        PHP_ME(vtiful_format, boldStype, format_style_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+zend_function_entry format_methods[] = {
+        PHP_ME(vtiful_format, bold,      format_style_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+        PHP_ME(vtiful_format, italic,    format_style_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+        PHP_ME(vtiful_format, underline, format_style_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
         PHP_FE_END
 };
 /* }}} */
@@ -59,7 +111,9 @@ zend_function_entry formac_methods[] = {
 VTIFUL_STARTUP_FUNCTION(format) {
     zend_class_entry ce;
 
-    INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Format", formac_methods);
+    INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Format", format_methods);
+
+//    zend_declare_class_constant_long(&ce, "UNDERLINE_SINGLE", sizeof("UNDERLINE_SINGLE")-1, (zend_long)LXW_UNDERLINE_SINGLE);
 
     vtiful_format_ce = zend_register_internal_class(&ce);
 

+ 4 - 0
kernel/include.h

@@ -10,6 +10,7 @@
 
 #include "xlsxwriter.h"
 #include "xlsxwriter/packager.h"
+#include "xlsxwriter/format.h"
 
 #include "php_excel_writer.h"
 #include "excel.h"
@@ -21,6 +22,9 @@ typedef struct {
     lxw_worksheet *worksheet;
 } excel_resource_t;
 
+extern excel_resource_t *excel_res;
+
+
 excel_resource_t * zval_get_resource(zval *handle);
 
 STATIC lxw_error _store_defined_name(lxw_workbook *self, const char *name, const char *app_name, const char *formula, int16_t index, uint8_t hidden);

+ 2 - 0
php_excel_writer.h

@@ -13,6 +13,8 @@
 #ifndef PHP_VTIFUL_H
 #define PHP_VTIFUL_H
 
+#include "kernel/include.h"
+
 extern zend_module_entry excel_writer_module_entry;
 #define phpext_excel_writer_ptr &excel_writer_module_entry