Skip to content

Commit fe5d9a6

Browse files
authored
Merge pull request #167 from viest/dev
Feat: nextCellCallback read
2 parents 7adc6d8 + 0e408db commit fe5d9a6

File tree

11 files changed

+428
-23
lines changed

11 files changed

+428
-23
lines changed

.travis.yml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,37 @@ compiler:
77
os:
88
- linux
99

10-
php:
11-
- 7.0
12-
- 7.1
13-
- 7.2
14-
- 7.3
15-
- 7.4snapshot
16-
- nightly
10+
matrix:
11+
include:
12+
- php: 7.0
13+
env: USE_VALGRIND=1
14+
- php: 7.1
15+
env: USE_VALGRIND=1
16+
- php: 7.2
17+
env: USE_VALGRIND=1
18+
- php: 7.3
19+
- php: 7.4snapshot
20+
- php: nightly
21+
22+
addons:
23+
apt:
24+
packages: valgrind
1725

1826
notifications:
1927
2028

2129
before_script:
2230
- git submodule update --init
2331
- sudo apt-get install zlib1g-dev -y
24-
- phpize && ./configure && make clean && make
32+
- phpize && ./configure --enable-reader && make clean && make
2533

2634
branches:
2735
only:
2836
- master
2937
- dev
30-
- dev-read
38+
3139
script:
3240
- ./travis/run-test.sh
41+
- make clean && phpize --clean
42+
- phpize && ./configure --enable-reader && make clean && make
43+
- 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

ide-helper/helper.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class Excel
1616
const TYPE_DOUBLE = 0x04;
1717
const TYPE_TIMESTAMP = 0x08;
1818

19+
const SKIP_NONE = 0x00;
20+
const SKIP_EMPTY_ROW = 0x01;
21+
const SKIP_EMPTY_CELLS = 0x02;
22+
1923
/**
2024
* Excel constructor.
2125
*
@@ -328,12 +332,13 @@ public function openFile(string $fileName): self
328332
* default open first sheet
329333
*
330334
* @param string|NULL $sheetName
335+
* @param int skipFlag
331336
*
332337
* @return Excel
333338
*
334339
* @author viest
335340
*/
336-
public function openSheet(string $sheetName = NULL): self
341+
public function openSheet(string $sheetName = NULL, int $skipFlag = 0x00): self
337342
{
338343
return $this;
339344
}
@@ -361,6 +366,19 @@ public function nextRow(): array
361366
{
362367
return [];
363368
}
369+
370+
/**
371+
* Next Cell In Callback
372+
*
373+
* @param callable $callback function(int $row, int $cell, string $data)
374+
* @param string|NULL $sheetName sheet name
375+
*
376+
* @author viest
377+
*/
378+
public function nextCellCallback(callable $callback, string $sheetName = NULL): void
379+
{
380+
//
381+
}
364382
}
365383

366384
/**

include/excel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#define V_XLS_CONST_READ_TYPE_STRING "TYPE_STRING"
2424
#define V_XLS_CONST_READ_TYPE_DATETIME "TYPE_TIMESTAMP"
2525

26+
#define V_XLS_CONST_READ_SKIP_NONE "SKIP_NONE"
27+
#define V_XLS_CONST_READ_SKIP_EMPTY_ROW "SKIP_EMPTY_ROW"
28+
#define V_XLS_CONST_READ_SKIP_EMPTY_CELLS "SKIP_EMPTY_CELLS"
29+
2630
#define READ_TYPE_EMPTY 0x00
2731
#define READ_TYPE_STRING 0x01
2832
#define READ_TYPE_INT 0x02

include/read.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
#define READ_SKIP_ROW 0
1717
#define READ_ROW 0x01
1818

19-
int sheet_read_row(xlsxioreadersheet sheet);
19+
int sheet_read_row(xlsxioreadersheet sheet_t);
2020
xlsxioreader file_open(const char *directory, const char *file_name);
2121
void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_result_t);
22-
xlsxioreadersheet sheet_open(xlsxioreader file, const zend_string *zs_sheet_name_t);
22+
xlsxioreadersheet sheet_open(xlsxioreader file_t, const zend_string *zs_sheet_name_t, const zend_long zl_flag);
2323
unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_result_t, zval *zv_type, unsigned int flag);
24+
unsigned int load_sheet_current_row_data_callback(zend_string *zs_sheet_name_t, xlsxioreader file_t, void *callback_data);
2425

2526
#endif //PHP_READ_INCLUDE_H

include/xlswriter.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ typedef struct {
4242
xlsxioreader file_t;
4343
xlsxioreadersheet sheet_t;
4444
} xls_resource_read_t;
45+
46+
typedef struct {
47+
zend_fcall_info *fci;
48+
zend_fcall_info_cache *fci_cache;
49+
} xls_read_callback_data;
4550
#endif
4651

4752
#ifndef ENABLE_READER
@@ -117,6 +122,15 @@ static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
117122
} \
118123
} while(0);
119124

125+
#define FCALL_TWO_ARGS(bucket) \
126+
ZVAL_COPY_VALUE(&args[0], &bucket->val); \
127+
if (bucket->key) { \
128+
ZVAL_STR(&args[1], bucket->key); \
129+
} else { \
130+
ZVAL_LONG(&args[1], bucket->h); \
131+
} \
132+
zend_call_function(&fci, &fci_cache);
133+
120134
#define ROW(range) \
121135
lxw_name_to_row(range)
122136

kernel/excel.c

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ ZEND_END_ARG_INFO()
162162
ZEND_BEGIN_ARG_INFO_EX(xls_open_sheet_arginfo, 0, 0, 1)
163163
ZEND_ARG_INFO(0, zs_sheet_name)
164164
ZEND_END_ARG_INFO()
165+
166+
ZEND_BEGIN_ARG_INFO_EX(xls_next_cell_callback_arginfo, 0, 0, 2)
167+
ZEND_ARG_INFO(0, fci)
168+
ZEND_ARG_INFO(0, sheet_name)
169+
ZEND_END_ARG_INFO()
165170
/* }}} */
166171

167172
/** {{{ \Vtiful\Kernel\xls::__construct(array $config)
@@ -728,11 +733,13 @@ PHP_METHOD(vtiful_xls, openFile)
728733
*/
729734
PHP_METHOD(vtiful_xls, openSheet)
730735
{
736+
zend_long zl_flag = XLSXIOREAD_SKIP_NONE;
731737
zend_string *zs_sheet_name = NULL;
732738

733-
ZEND_PARSE_PARAMETERS_START(0, 1)
739+
ZEND_PARSE_PARAMETERS_START(0, 2)
734740
Z_PARAM_OPTIONAL
735741
Z_PARAM_STR(zs_sheet_name)
742+
Z_PARAM_LONG(zl_flag)
736743
ZEND_PARSE_PARAMETERS_END();
737744

738745
ZVAL_COPY(return_value, getThis());
@@ -743,7 +750,7 @@ PHP_METHOD(vtiful_xls, openSheet)
743750
RETURN_NULL();
744751
}
745752

746-
obj->read_ptr.sheet_t = sheet_open(obj->read_ptr.file_t, zs_sheet_name);
753+
obj->read_ptr.sheet_t = sheet_open(obj->read_ptr.file_t, zs_sheet_name, zl_flag);
747754
}
748755
/* }}} */
749756

@@ -782,6 +789,35 @@ PHP_METHOD(vtiful_xls, nextRow)
782789
}
783790
/* }}} */
784791

792+
/** {{{ \Vtiful\Kernel\xls::nextCellCallback()
793+
*/
794+
PHP_METHOD(vtiful_xls, nextCellCallback)
795+
{
796+
zend_string *zs_sheet_name = NULL;
797+
zend_fcall_info fci = empty_fcall_info;
798+
zend_fcall_info_cache fci_cache = empty_fcall_info_cache;
799+
800+
ZEND_PARSE_PARAMETERS_START(1, 2)
801+
Z_PARAM_FUNC(fci, fci_cache)
802+
Z_PARAM_OPTIONAL
803+
Z_PARAM_STR(zs_sheet_name)
804+
ZEND_PARSE_PARAMETERS_END();
805+
806+
xls_object *obj = Z_XLS_P(getThis());
807+
808+
if (!obj->read_ptr.file_t) {
809+
RETURN_FALSE;
810+
}
811+
812+
xls_read_callback_data callback_data;
813+
814+
callback_data.fci = &fci;
815+
callback_data.fci_cache = &fci_cache;
816+
817+
load_sheet_current_row_data_callback(zs_sheet_name, obj->read_ptr.file_t, &callback_data);
818+
}
819+
/* }}} */
820+
785821
#endif
786822

787823
/** {{{ xls_methods
@@ -808,10 +844,11 @@ zend_function_entry xls_methods[] = {
808844
PHP_ME(vtiful_xls, setRow, xls_set_row_arginfo, ZEND_ACC_PUBLIC)
809845

810846
#ifdef ENABLE_READER
811-
PHP_ME(vtiful_xls, openFile, xls_open_file_arginfo, ZEND_ACC_PUBLIC)
812-
PHP_ME(vtiful_xls, openSheet, xls_open_sheet_arginfo, ZEND_ACC_PUBLIC)
813-
PHP_ME(vtiful_xls, getSheetData, NULL, ZEND_ACC_PUBLIC)
814-
PHP_ME(vtiful_xls, nextRow, NULL, ZEND_ACC_PUBLIC)
847+
PHP_ME(vtiful_xls, openFile, xls_open_file_arginfo, ZEND_ACC_PUBLIC)
848+
PHP_ME(vtiful_xls, openSheet, xls_open_sheet_arginfo, ZEND_ACC_PUBLIC)
849+
PHP_ME(vtiful_xls, getSheetData, NULL, ZEND_ACC_PUBLIC)
850+
PHP_ME(vtiful_xls, nextRow, NULL, ZEND_ACC_PUBLIC)
851+
PHP_ME(vtiful_xls, nextCellCallback, xls_next_cell_callback_arginfo, ZEND_ACC_PUBLIC)
815852
#endif
816853

817854
PHP_FE_END
@@ -834,6 +871,12 @@ VTIFUL_STARTUP_FUNCTION(excel) {
834871
REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_COF, ZEND_ACC_PRIVATE);
835872
REGISTER_CLASS_PROPERTY_NULL(vtiful_xls_ce, V_XLS_FIL, ZEND_ACC_PRIVATE);
836873

874+
#ifdef ENABLE_READER
875+
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_SKIP_NONE, XLSXIOREAD_SKIP_NONE);
876+
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_SKIP_EMPTY_ROW, XLSXIOREAD_SKIP_EMPTY_ROWS);
877+
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_SKIP_EMPTY_CELLS, XLSXIOREAD_SKIP_EMPTY_CELLS);
878+
#endif
879+
837880
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_INT, READ_TYPE_INT);
838881
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_DOUBLE, READ_TYPE_DOUBLE);
839882
REGISTER_CLASS_CONST_LONG(vtiful_xls_ce, V_XLS_CONST_READ_TYPE_STRING, READ_TYPE_STRING);

kernel/read.c

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ xlsxioreader file_open(const char *directory, const char *file_name) {
3131
/* }}} */
3232

3333
/* {{{ */
34-
xlsxioreadersheet sheet_open(xlsxioreader file, const zend_string *zs_sheet_name_t)
34+
xlsxioreadersheet sheet_open(xlsxioreader file_t, const zend_string *zs_sheet_name_t, const zend_long zl_flag)
3535
{
3636
if (zs_sheet_name_t == NULL) {
37-
return xlsxioread_sheet_open(file, NULL, XLSXIOREAD_SKIP_EMPTY_ROWS);
37+
return xlsxioread_sheet_open(file_t, NULL, zl_flag);
3838
}
3939

40-
return xlsxioread_sheet_open(file, ZSTR_VAL(zs_sheet_name_t), XLSXIOREAD_SKIP_EMPTY_ROWS);
40+
return xlsxioread_sheet_open(file_t, ZSTR_VAL(zs_sheet_name_t), zl_flag);
4141
}
4242
/* }}} */
4343

4444
/* {{{ */
45-
int sheet_read_row(xlsxioreadersheet sheet)
45+
int sheet_read_row(xlsxioreadersheet sheet_t)
4646
{
47-
return xlsxioread_sheet_next_row(sheet);
47+
return xlsxioread_sheet_next_row(sheet_t);
4848
}
4949
/* }}} */
5050

@@ -111,6 +111,77 @@ unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_res
111111
}
112112
/* }}} */
113113

114+
/* {{{ */
115+
int sheet_row_callback (size_t row, size_t max_col, void* callback_data)
116+
{
117+
if (callback_data == NULL) {
118+
return FAILURE;
119+
}
120+
121+
xls_read_callback_data *_callback_data = (xls_read_callback_data *)callback_data;
122+
123+
zval args[3], retval;
124+
125+
_callback_data->fci->retval = &retval;
126+
_callback_data->fci->params = args;
127+
_callback_data->fci->param_count = 3;
128+
129+
ZVAL_LONG(&args[0], row);
130+
ZVAL_LONG(&args[1], max_col);
131+
ZVAL_STRING(&args[2], "XLSX_ROW_END");
132+
133+
zend_call_function(_callback_data->fci, _callback_data->fci_cache);
134+
135+
zval_ptr_dtor(&args[2]);
136+
zval_ptr_dtor(&retval);
137+
138+
return SUCCESS;
139+
}
140+
/* }}} */
141+
142+
/* {{{ */
143+
int sheet_cell_callback (size_t row, size_t col, const char *value, void *callback_data)
144+
{
145+
if (callback_data == NULL) {
146+
return FAILURE;
147+
}
148+
149+
xls_read_callback_data *_callback_data = (xls_read_callback_data *)callback_data;
150+
151+
if (_callback_data->fci == NULL || _callback_data->fci_cache == NULL) {
152+
return FAILURE;
153+
}
154+
155+
zval args[3], retval;
156+
157+
_callback_data->fci->retval = &retval;
158+
_callback_data->fci->params = args;
159+
_callback_data->fci->param_count = 3;
160+
161+
ZVAL_LONG(&args[0], row);
162+
ZVAL_LONG(&args[1], col);
163+
ZVAL_STRING(&args[2], value);
164+
165+
zend_call_function(_callback_data->fci, _callback_data->fci_cache);
166+
167+
zval_ptr_dtor(&args[2]);
168+
zval_ptr_dtor(&retval);
169+
170+
return SUCCESS;
171+
}
172+
/* }}} */
173+
174+
/* {{{ */
175+
unsigned int load_sheet_current_row_data_callback(zend_string *zs_sheet_name_t, xlsxioreader file_t, void *callback_data)
176+
{
177+
if (zs_sheet_name_t == NULL) {
178+
return xlsxioread_process(file_t, NULL, XLSXIOREAD_SKIP_NONE, sheet_cell_callback, sheet_row_callback, callback_data);
179+
}
180+
181+
return xlsxioread_process(file_t, ZSTR_VAL(zs_sheet_name_t), XLSXIOREAD_SKIP_NONE, sheet_cell_callback, sheet_row_callback, callback_data);
182+
}
183+
/* }}} */
184+
114185
/* {{{ */
115186
void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_result_t)
116187
{

0 commit comments

Comments
 (0)