Skip to content

Commit

Permalink
fix: format memory, close #455
Browse files Browse the repository at this point in the history
  • Loading branch information
viest committed Aug 27, 2024
1 parent 54c3e07 commit 81de626
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 31 deletions.
21 changes: 16 additions & 5 deletions include/xlswriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ typedef struct {
lxw_format *format;
} xls_resource_format_t;

typedef struct {
HashTable *maps;
} xls_resource_formats_cache_t;

typedef struct {
lxw_data_validation *validation;
} xls_resource_validation_t;
Expand All @@ -102,11 +106,12 @@ typedef struct {
} xls_resource_rich_string_t;

typedef struct _vtiful_xls_object {
xls_resource_read_t read_ptr;
xls_resource_write_t write_ptr;
zend_long write_line;
xls_resource_format_t format_ptr;
zend_object zo;
xls_resource_read_t read_ptr;
xls_resource_write_t write_ptr;
zend_long write_line;
xls_resource_format_t format_ptr;
xls_resource_formats_cache_t formats_cache_ptr;
zend_object zo;
} xls_object;

typedef struct _vtiful_format_object {
Expand Down Expand Up @@ -294,6 +299,10 @@ static inline void php_vtiful_close_resource(zend_object *obj) {
intern->format_ptr.format = NULL;
}

if (intern->formats_cache_ptr.maps != NULL) {
zend_hash_destroy(intern->formats_cache_ptr.maps);
}

#ifdef ENABLE_READER
if (intern->read_ptr.sheet_t != NULL) {
xlsxioread_sheet_close(intern->read_ptr.sheet_t);
Expand Down Expand Up @@ -361,4 +370,6 @@ lxw_datetime timestamp_to_datetime(zend_long timestamp);
zend_string* char_join_to_zend_str(const char *left, const char *right);
zend_string* str_pick_up(zend_string *left, const char *right, size_t len);

lxw_format* object_format(xls_object *obj, zend_string *format, lxw_format *format_handle);

#endif
50 changes: 49 additions & 1 deletion kernel/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void call_object_method(zval *object, const char *function_name, uint32_t param_
}
/* }}} */

/* {{{ */
lxw_datetime timestamp_to_datetime(zend_long timestamp)
{
int yearLocal = php_idate('Y', timestamp, 0);
Expand All @@ -102,4 +103,51 @@ lxw_datetime timestamp_to_datetime(zend_long timestamp)
};

return datetime;
}
}
/* }}} */

/* {{{ */
lxw_format* object_format(xls_object *obj, zend_string *format, lxw_format *format_handle)
{
if (format == NULL && format_handle == NULL) {
return NULL;
}

if (format != NULL && format_handle != NULL) {
zend_string *_format_key = strpprintf(0, "%p|%s", format_handle, format->val);

void *exit_format = zend_hash_str_find_ptr(obj->formats_cache_ptr.maps, ZEND_STRL(_format_key->val));

if (exit_format != NULL) {
return (lxw_format *)exit_format;
}

lxw_format *new_format = workbook_add_format((&obj->write_ptr)->workbook);
format_copy(new_format, format_handle);
format_set_num_format(new_format, ZSTR_VAL(format));

zend_hash_str_add_ptr(obj->formats_cache_ptr.maps, ZEND_STRL(_format_key->val), new_format);

zend_string_release(_format_key);

return new_format;
}

if (format != NULL) {
void *exit_format = zend_hash_str_find_ptr(obj->formats_cache_ptr.maps, ZEND_STRL(format->val));

if (exit_format != NULL) {
return (lxw_format *)exit_format;
}

lxw_format *new_format = workbook_add_format((&obj->write_ptr)->workbook);
format_set_num_format(new_format, ZSTR_VAL(format));

zend_hash_str_add_ptr(obj->formats_cache_ptr.maps, ZEND_STRL(format->val), new_format);

return new_format;
}

return format_handle;
}
/* }}} */
25 changes: 13 additions & 12 deletions kernel/excel.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ PHP_VTIFUL_API zend_object *vtiful_xls_objects_new(zend_class_entry *ce)

intern->zo.handlers = &vtiful_xls_handlers;

intern->read_ptr.file_t = NULL;
intern->read_ptr.sheet_t = NULL;
intern->read_ptr.file_t = NULL;
intern->read_ptr.sheet_t = NULL;

intern->format_ptr.format = NULL;
intern->write_ptr.workbook = NULL;
intern->format_ptr.format = NULL;
intern->write_ptr.workbook = NULL;
intern->formats_cache_ptr.maps = zend_new_array(0);

intern->read_ptr.data_type_default = READ_TYPE_EMPTY;

Expand Down Expand Up @@ -612,7 +613,7 @@ PHP_METHOD(vtiful_xls, header)
}

ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value)
type_writer(header_value, 0, header_l_key, &obj->write_ptr, NULL, format_handle);
type_writer(header_value, 0, header_l_key, &obj->write_ptr, NULL, object_format(obj, NULL, format_handle));
ZEND_HASH_FOREACH_END();

// When inserting the header for the first time, the row number is incremented by one,
Expand Down Expand Up @@ -657,7 +658,7 @@ PHP_METHOD(vtiful_xls, data)
if (key == NULL) {
column_index = index;
}
type_writer(data, SHEET_CURRENT_LINE(obj), column_index, &obj->write_ptr, NULL, obj->format_ptr.format);
type_writer(data, SHEET_CURRENT_LINE(obj), column_index, &obj->write_ptr, NULL, object_format(obj, NULL, obj->format_ptr.format));

// next number index
++column_index;
Expand Down Expand Up @@ -724,9 +725,9 @@ PHP_METHOD(vtiful_xls, insertText)
SHEET_LINE_SET(obj, row);

if (format_handle != NULL) {
type_writer(data, row, column, &obj->write_ptr, format, zval_get_format(format_handle));
type_writer(data, row, column, &obj->write_ptr, format, object_format(obj, format, zval_get_format(format_handle)));
} else {
type_writer(data, row, column, &obj->write_ptr, format, obj->format_ptr.format);
type_writer(data, row, column, &obj->write_ptr, format, object_format(obj, format, obj->format_ptr.format));
}
}
/* }}} */
Expand Down Expand Up @@ -799,9 +800,9 @@ PHP_METHOD(vtiful_xls, insertDate)
lxw_datetime datetime = timestamp_to_datetime(data->value.lval);

if (format_handle != NULL) {
datetime_writer(&datetime, row, column, format, &obj->write_ptr, zval_get_format(format_handle));
datetime_writer(&datetime, row, column, format, &obj->write_ptr, object_format(obj, format, zval_get_format(format_handle)));
} else {
datetime_writer(&datetime, row, column, format, &obj->write_ptr, obj->format_ptr.format);
datetime_writer(&datetime, row, column, format, &obj->write_ptr, object_format(obj, format, obj->format_ptr.format));
}

// Release default format
Expand Down Expand Up @@ -1007,9 +1008,9 @@ PHP_METHOD(vtiful_xls, mergeCells)
WORKBOOK_NOT_INITIALIZED(obj);

if (argc == 3 && format_handle != NULL) {
merge_cells(range, data, &obj->write_ptr, zval_get_format(format_handle));
merge_cells(range, data, &obj->write_ptr, object_format(obj, NULL, zval_get_format(format_handle)));
} else {
merge_cells(range, data, &obj->write_ptr, obj->format_ptr.format);
merge_cells(range, data, &obj->write_ptr, object_format(obj, NULL, obj->format_ptr.format));
}
}
/* }}} */
Expand Down
9 changes: 1 addition & 8 deletions kernel/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,7 @@ void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_
*/
void datetime_writer(lxw_datetime *datetime, zend_long row, zend_long columns, zend_string *format, xls_resource_write_t *res, lxw_format *format_handle)
{
lxw_format *value_format = workbook_add_format(res->workbook);

if (format_handle != NULL) {
format_copy(value_format, format_handle);
}

format_set_num_format(value_format, ZSTR_VAL(format));
worksheet_write_datetime(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, datetime, value_format);
worksheet_write_datetime(res->worksheet, (lxw_row_t)row, (lxw_col_t)columns, datetime, format_handle);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ array(2) {
[1]=>
string(4) "Cost"
}
array(2) {
[0]=>
string(0) ""
[1]=>
string(0) ""
array(0) {
}
array(5) {
[0]=>
Expand Down

0 comments on commit 81de626

Please sign in to comment.