Skip to content

Commit 9f70406

Browse files
authored
Merge pull request #402 from viest/dev
Feat: rich string
2 parents afbcdae + fd6dfb7 commit 9f70406

File tree

13 files changed

+341
-20
lines changed

13 files changed

+341
-20
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ script:
4141
- ./travis/run-test.sh
4242
- make clean && phpize --clean
4343
- phpize && ./configure --enable-reader && make clean && make
44-
- if [ -n "$USE_VALGRIND" ]; then REPORT_EXIT_STATUS=1 php -n run-tests.php -m -n -d extension_dir=./modules/ -d extension=xlswriter.so -P --show-diff --set-timeout 120; fi
44+
- if [ -n "$USE_VALGRIND" ]; then REPORT_EXIT_STATUS=1 php -n run-tests.php -m -n -d extension_dir=./modules/ -d extension=xlswriter.so -P --show-all --set-timeout 120; fi

config.m4

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ PHP_ARG_ENABLE(reader, enable xlsx reader support,
1313
if test "$PHP_XLSWRITER" != "no"; then
1414
xls_writer_sources="
1515
xlswriter.c \
16-
kernel/exception.c \
17-
kernel/resource.c \
16+
kernel/chart.c \
1817
kernel/common.c \
1918
kernel/excel.c \
20-
kernel/write.c \
19+
kernel/exception.c \
2120
kernel/format.c \
22-
kernel/chart.c \
2321
kernel/help.c \
22+
kernel/resource.c \
23+
kernel/rich_string.c \
2424
kernel/validation.c \
25+
kernel/write.c \
2526
"
2627

2728
xls_read_sources="

config.w32

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ if (PHP_XLSWRITER != "no") {
1818
AC_DEFINE("HAVE_LXW_VERSION", 1, "lxw_version available in 0.7.7");
1919

2020
ADD_SOURCES(configure_module_dirname + "\\kernel", "\
21-
exception.c \
22-
resource.c \
21+
chart.c \
2322
common.c \
23+
csv.c \
2424
excel.c \
25-
write.c \
25+
exception.c \
2626
format.c \
27-
chart.c \
28-
read.c \
29-
csv.c \
3027
help.c \
28+
read.c \
29+
resource.c \
30+
rich_string.c \
3131
validation.c \
32+
write.c \
3233
", "xlswriter");
3334

3435
ADD_SOURCES(configure_module_dirname + "\\library\\libxlsxwriter\\third_party\\minizip", "\

include/rich_string.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| XlsWriter Extension |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 2017-2018 The Viest |
6+
+----------------------------------------------------------------------+
7+
| http://www.viest.me |
8+
+----------------------------------------------------------------------+
9+
| Author: viest <[email protected]> |
10+
+----------------------------------------------------------------------+
11+
*/
12+
13+
#ifndef PHP_EXT_XLS_EXPORT_RICH_STRING_H
14+
#define PHP_EXT_XLS_EXPORT_RICH_STRING_H
15+
16+
extern zend_class_entry *vtiful_rich_string_ce;
17+
18+
VTIFUL_STARTUP_FUNCTION(rich_string);
19+
20+
#endif

include/xlswriter.h

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "exception.h"
3737
#include "format.h"
3838
#include "chart.h"
39+
#include "rich_string.h"
3940
#include "help.h"
4041

4142
#ifdef ENABLE_READER
@@ -95,6 +96,10 @@ typedef struct {
9596
lxw_chart_series *series;
9697
} xls_resource_chart_t;
9798

99+
typedef struct {
100+
lxw_rich_string_tuple *tuple;
101+
} xls_resource_rich_string_t;
102+
98103
typedef struct _vtiful_xls_object {
99104
xls_resource_read_t read_ptr;
100105
xls_resource_write_t write_ptr;
@@ -118,16 +123,22 @@ typedef struct _vtiful_validation_object {
118123
zend_object zo;
119124
} validation_object;
120125

126+
typedef struct _vtiful_rich_string_object {
127+
xls_resource_rich_string_t ptr;
128+
zend_object zo;
129+
} rich_string_object;
130+
121131
#define REGISTER_CLASS_CONST_LONG(class_name, const_name, value) \
122132
zend_declare_class_constant_long(class_name, const_name, sizeof(const_name)-1, (zend_long)value);
123133

124134
#define REGISTER_CLASS_PROPERTY_NULL(class_name, property_name, acc) \
125135
zend_declare_property_null(class_name, ZEND_STRL(property_name), acc);
126136

127-
#define Z_XLS_P(zv) php_vtiful_xls_fetch_object(Z_OBJ_P(zv));
128-
#define Z_CHART_P(zv) php_vtiful_chart_fetch_object(Z_OBJ_P(zv));
129-
#define Z_FORMAT_P(zv) php_vtiful_format_fetch_object(Z_OBJ_P(zv));
130-
#define Z_VALIDATION_P(zv) php_vtiful_validation_fetch_object(Z_OBJ_P(zv));
137+
#define Z_XLS_P(zv) php_vtiful_xls_fetch_object(Z_OBJ_P(zv));
138+
#define Z_CHART_P(zv) php_vtiful_chart_fetch_object(Z_OBJ_P(zv));
139+
#define Z_FORMAT_P(zv) php_vtiful_format_fetch_object(Z_OBJ_P(zv));
140+
#define Z_VALIDATION_P(zv) php_vtiful_validation_fetch_object(Z_OBJ_P(zv));
141+
#define Z_RICH_STR_P(zv) php_vtiful_rich_string_fetch_object(Z_OBJ_P(zv));
131142

132143
#define WORKBOOK_NOT_INITIALIZED(xls_object_t) \
133144
do { \
@@ -254,6 +265,14 @@ static inline validation_object *php_vtiful_validation_fetch_object(zend_object
254265
return (validation_object *)((char *)(obj) - XtOffsetOf(validation_object, zo));
255266
}
256267

268+
static inline rich_string_object *php_vtiful_rich_string_fetch_object(zend_object *obj) {
269+
if (obj == NULL) {
270+
return NULL;
271+
}
272+
273+
return (rich_string_object *)((char *)(obj) - XtOffsetOf(validation_object, zo));
274+
}
275+
257276
static inline void php_vtiful_close_resource(zend_object *obj) {
258277
if (obj == NULL) {
259278
return;
@@ -287,10 +306,11 @@ static inline void php_vtiful_close_resource(zend_object *obj) {
287306
intern->read_ptr.data_type_default = READ_TYPE_EMPTY;
288307
}
289308

290-
lxw_format * zval_get_format(zval *handle);
291-
lxw_data_validation * zval_get_validation(zval *resource);
292-
xls_resource_write_t * zval_get_resource(zval *handle);
293-
xls_resource_chart_t * zval_get_chart(zval *resource);
309+
lxw_format * zval_get_format(zval *handle);
310+
lxw_data_validation * zval_get_validation(zval *resource);
311+
lxw_rich_string_tuple * zval_get_rich_string(zval *resource);
312+
xls_resource_write_t * zval_get_resource(zval *handle);
313+
xls_resource_chart_t * zval_get_chart(zval *resource);
294314

295315
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);
296316

@@ -327,6 +347,7 @@ void worksheet_set_rows(lxw_row_t start, lxw_row_t end, double height, xls_resou
327347
void image_writer(zval *value, zend_long row, zend_long columns, double width, double height, xls_resource_write_t *res);
328348
void formula_writer(zend_string *value, zend_long row, zend_long columns, xls_resource_write_t *res, lxw_format *format);
329349
void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_write_t *res, zend_string *format, lxw_format *format_handle);
350+
void rich_string_writer(zend_long row, zend_long columns, xls_resource_write_t *res, zval *rich_strings, lxw_format *format);
330351
void datetime_writer(lxw_datetime *datetime, zend_long row, zend_long columns, zend_string *format, xls_resource_write_t *res, lxw_format *format_handle);
331352
void url_writer(zend_long row, zend_long columns, xls_resource_write_t *res, zend_string *url, zend_string *text, zend_string *tool_tip, lxw_format *format);
332353

kernel/excel.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ ZEND_BEGIN_ARG_INFO_EX(xls_insert_text_arginfo, 0, 0, 3)
117117
ZEND_ARG_INFO(0, format_handle)
118118
ZEND_END_ARG_INFO()
119119

120+
ZEND_BEGIN_ARG_INFO_EX(xls_insert_rtext_arginfo, 0, 0, 3)
121+
ZEND_ARG_INFO(0, row)
122+
ZEND_ARG_INFO(0, column)
123+
ZEND_ARG_INFO(0, rich_strings)
124+
ZEND_ARG_INFO(0, format_handle)
125+
ZEND_END_ARG_INFO()
126+
120127
ZEND_BEGIN_ARG_INFO_EX(xls_insert_date_arginfo, 0, 0, 3)
121128
ZEND_ARG_INFO(0, row)
122129
ZEND_ARG_INFO(0, column)
@@ -700,6 +707,37 @@ PHP_METHOD(vtiful_xls, insertText)
700707
}
701708
/* }}} */
702709

710+
/** {{{ \Vtiful\Kernel\Excel::insertRichText(int $row, int $column, array $richString[, resource $formatHandle])
711+
*/
712+
PHP_METHOD(vtiful_xls, insertRichText)
713+
{
714+
zend_long row = 0, column = 0;
715+
zval *rich_strings = NULL, *format_handle = NULL;
716+
717+
ZEND_PARSE_PARAMETERS_START(3, 4)
718+
Z_PARAM_LONG(row)
719+
Z_PARAM_LONG(column)
720+
Z_PARAM_ARRAY(rich_strings)
721+
Z_PARAM_OPTIONAL
722+
Z_PARAM_RESOURCE_OR_NULL(format_handle)
723+
ZEND_PARSE_PARAMETERS_END();
724+
725+
ZVAL_COPY(return_value, getThis());
726+
727+
xls_object *obj = Z_XLS_P(getThis());
728+
729+
WORKBOOK_NOT_INITIALIZED(obj);
730+
731+
SHEET_LINE_SET(obj, row);
732+
733+
if (format_handle != NULL) {
734+
rich_string_writer(row, column, &obj->write_ptr, rich_strings, zval_get_format(format_handle));
735+
} else {
736+
rich_string_writer(row, column, &obj->write_ptr, rich_strings, obj->format_ptr.format);
737+
}
738+
}
739+
/* }}} */
740+
703741
/** {{{ \Vtiful\Kernel\Excel::insertDate(int $row, int $column, int $timestamp[, string $format, resource $formatHandle])
704742
*/
705743
PHP_METHOD(vtiful_xls, insertDate)
@@ -1632,6 +1670,7 @@ zend_function_entry xls_methods[] = {
16321670
PHP_ME(vtiful_xls, getHandle, xls_get_handle_arginfo, ZEND_ACC_PUBLIC)
16331671
PHP_ME(vtiful_xls, autoFilter, xls_auto_filter_arginfo, ZEND_ACC_PUBLIC)
16341672
PHP_ME(vtiful_xls, insertText, xls_insert_text_arginfo, ZEND_ACC_PUBLIC)
1673+
PHP_ME(vtiful_xls, insertRichText, xls_insert_rtext_arginfo, ZEND_ACC_PUBLIC)
16351674
PHP_ME(vtiful_xls, insertDate, xls_insert_date_arginfo, ZEND_ACC_PUBLIC)
16361675
PHP_ME(vtiful_xls, insertChart, xls_insert_chart_arginfo, ZEND_ACC_PUBLIC)
16371676
PHP_ME(vtiful_xls, insertUrl, xls_insert_url_arginfo, ZEND_ACC_PUBLIC)

kernel/format.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@ PHP_METHOD(vtiful_format, toResource)
386386
}
387387
/* }}} */
388388

389-
390389
/** {{{ format_methods
391390
*/
392391
zend_function_entry format_methods[] = {

kernel/resource.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ xls_resource_chart_t *zval_get_chart(zval *resource)
5959
}
6060
/* }}} */
6161

62+
/* {{{ */
63+
lxw_rich_string_tuple *zval_get_rich_string(zval *resource)
64+
{
65+
lxw_rich_string_tuple *res;
66+
67+
if((res = (lxw_rich_string_tuple *)zend_fetch_resource(Z_RES_P(resource), VTIFUL_RESOURCE_NAME, le_xls_writer)) == NULL) {
68+
zend_throw_exception(vtiful_exception_ce, "rich string resources resolution fail", 210);
69+
}
70+
71+
return res;
72+
}
73+
/* }}} */
74+
6275
/* {{{ */
6376
lxw_data_validation *zval_get_validation(zval *resource)
6477
{

kernel/rich_string.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| XlsWriter Extension |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 2017-2018 The Viest |
6+
+----------------------------------------------------------------------+
7+
| http://www.viest.me |
8+
+----------------------------------------------------------------------+
9+
| Author: viest <[email protected]> |
10+
+----------------------------------------------------------------------+
11+
*/
12+
13+
#include "xlswriter.h"
14+
15+
zend_class_entry *vtiful_rich_string_ce;
16+
17+
/* {{{ rich_string_objects_new
18+
*/
19+
static zend_object_handlers rich_string_handlers;
20+
21+
static zend_always_inline void *vtiful_rich_string_object_alloc(size_t obj_size, zend_class_entry *ce) {
22+
void *obj = emalloc(obj_size);
23+
memset(obj, 0, obj_size);
24+
return obj;
25+
}
26+
27+
PHP_VTIFUL_API zend_object *rich_string_objects_new(zend_class_entry *ce)
28+
{
29+
rich_string_object *rich_string = vtiful_rich_string_object_alloc(sizeof(rich_string_object), ce);
30+
31+
zend_object_std_init(&rich_string->zo, ce);
32+
object_properties_init(&rich_string->zo, ce);
33+
34+
rich_string->ptr.tuple = NULL;
35+
rich_string->zo.handlers = &rich_string_handlers;
36+
37+
return &rich_string->zo;
38+
}
39+
/* }}} */
40+
41+
/* {{{ rich_string_objects_free
42+
*/
43+
static void rich_string_objects_free(zend_object *object)
44+
{
45+
rich_string_object *intern = php_vtiful_rich_string_fetch_object(object);
46+
47+
if (intern->ptr.tuple != NULL) {
48+
efree(intern->ptr.tuple);
49+
intern->ptr.tuple = NULL;
50+
}
51+
52+
zend_object_std_dtor(&intern->zo);
53+
}
54+
/* }}} */
55+
56+
/* {{{ ARG_INFO
57+
*/
58+
ZEND_BEGIN_ARG_INFO_EX(rich_string_construct_arginfo, 0, 0, 1)
59+
ZEND_ARG_INFO(0, text)
60+
ZEND_ARG_INFO(0, format_handle)
61+
ZEND_END_ARG_INFO()
62+
/* }}} */
63+
64+
/** {{{ \Vtiful\Kernel\RichString::__construct(string $text, resource $format)
65+
*/
66+
PHP_METHOD(vtiful_rich_string, __construct)
67+
{
68+
zend_string *text = NULL;
69+
zval *format_handle = NULL;
70+
rich_string_object *obj = NULL;
71+
72+
ZEND_PARSE_PARAMETERS_START(1, 2)
73+
Z_PARAM_STR(text)
74+
Z_PARAM_OPTIONAL
75+
Z_PARAM_RESOURCE_OR_NULL(format_handle)
76+
ZEND_PARSE_PARAMETERS_END();
77+
78+
ZVAL_COPY(return_value, getThis());
79+
80+
obj = Z_RICH_STR_P(getThis());
81+
82+
if (obj->ptr.tuple != NULL) {
83+
return;
84+
}
85+
86+
lxw_rich_string_tuple *instance = (lxw_rich_string_tuple *)ecalloc(1, sizeof(lxw_rich_string_tuple));
87+
88+
zend_string *zstr = zend_string_copy(text);
89+
90+
if (format_handle == NULL) {
91+
instance->format = NULL;
92+
instance->string = ZSTR_VAL(zstr);
93+
} else {
94+
instance->format = zval_get_format(format_handle);
95+
instance->string = ZSTR_VAL(zstr);
96+
}
97+
98+
obj->ptr.tuple = instance;
99+
}
100+
/* }}} */
101+
102+
/** {{{ rich_string_methods
103+
*/
104+
zend_function_entry rich_string_methods[] = {
105+
PHP_ME(vtiful_rich_string, __construct, rich_string_construct_arginfo, ZEND_ACC_PUBLIC)
106+
PHP_FE_END
107+
};
108+
/* }}} */
109+
110+
/** {{{ VTIFUL_STARTUP_FUNCTION
111+
*/
112+
VTIFUL_STARTUP_FUNCTION(rich_string) {
113+
zend_class_entry ce;
114+
115+
INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "RichString", rich_string_methods);
116+
ce.create_object = rich_string_objects_new;
117+
vtiful_rich_string_ce = zend_register_internal_class(&ce);
118+
119+
memcpy(&rich_string_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
120+
rich_string_handlers.offset = XtOffsetOf(rich_string_object, zo);
121+
rich_string_handlers.free_obj = rich_string_objects_free;
122+
123+
return SUCCESS;
124+
}
125+
/* }}} */

0 commit comments

Comments
 (0)