read.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #include "xlswriter.h"
  13. #include "ext/date/php_date.h"
  14. /* {{{ */
  15. xlsxioreader file_open(const char *directory, const char *file_name) {
  16. char *path = (char *)emalloc(strlen(directory) + strlen(file_name) + 2);
  17. xlsxioreader file;
  18. strcpy(path, directory);
  19. strcat(path, "/");
  20. strcat(path, file_name);
  21. if (file_exists(path) == XLSWRITER_FALSE) {
  22. zend_string *message = char_join_to_zend_str("File not found, file path:", path);
  23. zend_throw_exception(vtiful_exception_ce, ZSTR_VAL(message), 121);
  24. zend_string_free(message);
  25. efree(path);
  26. return NULL;
  27. }
  28. if ((file = xlsxioread_open(path)) == NULL) {
  29. zend_string *message = char_join_to_zend_str("Failed to open file, file path:", path);
  30. zend_throw_exception(vtiful_exception_ce, ZSTR_VAL(message), 100);
  31. zend_string_free(message);
  32. efree(path);
  33. return NULL;
  34. }
  35. efree(path);
  36. return file;
  37. }
  38. /* }}} */
  39. /* {{{ */
  40. xlsxioreadersheet sheet_open(xlsxioreader file_t, const zend_string *zs_sheet_name_t, const zend_long zl_flag)
  41. {
  42. if (zs_sheet_name_t == NULL) {
  43. return xlsxioread_sheet_open(file_t, NULL, zl_flag);
  44. }
  45. return xlsxioread_sheet_open(file_t, ZSTR_VAL(zs_sheet_name_t), zl_flag);
  46. }
  47. /* }}} */
  48. /* {{{ */
  49. void sheet_list(xlsxioreader file_t, zval *zv_result_t)
  50. {
  51. const char *sheet_name = NULL;
  52. xlsxioreadersheetlist sheet_list = NULL;
  53. if (Z_TYPE_P(zv_result_t) != IS_ARRAY) {
  54. array_init(zv_result_t);
  55. }
  56. if ((sheet_list = xlsxioread_sheetlist_open(file_t)) == NULL) {
  57. return;
  58. }
  59. while ((sheet_name = xlsxioread_sheetlist_next(sheet_list)) != NULL) {
  60. add_next_index_stringl(zv_result_t, sheet_name, strlen(sheet_name));
  61. }
  62. xlsxioread_sheetlist_close(sheet_list);
  63. }
  64. /* }}} */
  65. /* {{{ */
  66. int is_number(const char *value)
  67. {
  68. if (strspn(value, ".0123456789") == strlen(value)) {
  69. return XLSWRITER_TRUE;
  70. }
  71. return XLSWRITER_FALSE;
  72. }
  73. /* }}} */
  74. /* {{{ */
  75. void data_to_null(zval *zv_result_t)
  76. {
  77. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  78. add_next_index_null(zv_result_t);
  79. } else {
  80. ZVAL_NULL(zv_result_t);
  81. }
  82. }
  83. /* }}} */
  84. /* {{{ */
  85. void data_to_custom_type(const char *string_value, const size_t string_value_length, const zend_ulong type, zval *zv_result_t, const zend_ulong zv_hashtable_index)
  86. {
  87. if (type == 0) {
  88. goto STRING;
  89. }
  90. if (!is_number(string_value)) {
  91. goto STRING;
  92. }
  93. if (type & READ_TYPE_DATETIME) {
  94. if (string_value_length == 0) {
  95. data_to_null(zv_result_t);
  96. return;
  97. }
  98. zend_long timestamp = date_double_to_timestamp(zend_strtod(string_value, NULL));
  99. // GMT
  100. // if (value != 0) {
  101. // timestamp = (value - 25569) * 86400;
  102. // }
  103. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  104. add_index_long(zv_result_t, zv_hashtable_index, timestamp);
  105. } else {
  106. ZVAL_LONG(zv_result_t, timestamp);
  107. }
  108. return;
  109. }
  110. if (type & READ_TYPE_DOUBLE) {
  111. if (string_value_length == 0) {
  112. data_to_null(zv_result_t);
  113. return;
  114. }
  115. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  116. add_index_double(zv_result_t, zv_hashtable_index,strtod(string_value, NULL));
  117. } else {
  118. ZVAL_DOUBLE(zv_result_t, strtod(string_value, NULL));
  119. }
  120. return;
  121. }
  122. if (type & READ_TYPE_INT) {
  123. if (string_value_length == 0) {
  124. data_to_null(zv_result_t);
  125. return;
  126. }
  127. zend_long _long_value;
  128. sscanf(string_value, ZEND_LONG_FMT, &_long_value);
  129. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  130. add_index_long(zv_result_t, zv_hashtable_index, _long_value);
  131. } else {
  132. ZVAL_LONG(zv_result_t, _long_value);
  133. }
  134. return;
  135. }
  136. STRING:
  137. {
  138. if (!(type & READ_TYPE_STRING)) {
  139. zend_long _long = 0; double _double = 0;
  140. is_numeric_string(string_value, string_value_length, &_long, &_double, 0);
  141. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  142. if (_double > 0) {
  143. add_index_double(zv_result_t, zv_hashtable_index, _double);
  144. return;
  145. }
  146. if (_long > 0) {
  147. add_index_long(zv_result_t, zv_hashtable_index, _long);
  148. return;
  149. }
  150. } else {
  151. if (_double > 0) {
  152. ZVAL_DOUBLE(zv_result_t, _double);
  153. return;
  154. }
  155. if (_long > 0) {
  156. ZVAL_LONG(zv_result_t, _long);
  157. return;
  158. }
  159. }
  160. }
  161. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  162. add_index_stringl(zv_result_t, zv_hashtable_index, string_value, string_value_length);
  163. return;
  164. }
  165. ZVAL_STRINGL(zv_result_t, string_value, string_value_length);
  166. }
  167. }
  168. /* }}} */
  169. /* {{{ */
  170. int sheet_read_row(xlsxioreadersheet sheet_t)
  171. {
  172. return xlsxioread_sheet_next_row(sheet_t);
  173. }
  174. /* }}} */
  175. /* {{{ */
  176. unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_result_t, zval *zv_type_arr_t, zend_long data_type_default, unsigned int flag)
  177. {
  178. zend_long _type, _cell_index = 0, _last_cell_index = 0;
  179. zend_bool _skip_empty_value_cell = 0;
  180. zend_array *_za_type_t = NULL;
  181. char *_string_value = NULL;
  182. zval *_current_type = NULL;
  183. if (flag && !sheet_read_row(sheet_t)) {
  184. return XLSWRITER_FALSE;
  185. }
  186. if (xlsxioread_sheet_flags(sheet_t) & SKIP_EMPTY_VALUE) {
  187. _skip_empty_value_cell = 1;
  188. }
  189. if (Z_TYPE_P(zv_result_t) != IS_ARRAY) {
  190. array_init(zv_result_t);
  191. }
  192. if (zv_type_arr_t != NULL && Z_TYPE_P(zv_type_arr_t) == IS_ARRAY) {
  193. _za_type_t = Z_ARR_P(zv_type_arr_t);
  194. }
  195. while ((_string_value = xlsxioread_sheet_next_cell(sheet_t)) != NULL)
  196. {
  197. size_t _string_value_length = strlen(_string_value);
  198. _type = READ_TYPE_EMPTY;
  199. _last_cell_index = xlsxioread_sheet_last_column_index(sheet_t) - 1;
  200. if (_last_cell_index < 0 || (_skip_empty_value_cell && _string_value_length == 0)) {
  201. goto FREE_TMP_VALUE;
  202. }
  203. if (_last_cell_index > _cell_index) {
  204. _cell_index = _last_cell_index;
  205. }
  206. if (_za_type_t != NULL) {
  207. _current_type = zend_hash_index_find(_za_type_t, _cell_index);
  208. if (_current_type != NULL && Z_TYPE_P(_current_type) == IS_LONG) {
  209. _type = Z_LVAL_P(_current_type);
  210. } else {
  211. _type = data_type_default;
  212. }
  213. } else {
  214. _type = data_type_default;
  215. }
  216. data_to_custom_type(_string_value, _string_value_length, _type, zv_result_t, _cell_index);
  217. FREE_TMP_VALUE:
  218. ++_cell_index;
  219. free(_string_value);
  220. }
  221. return XLSWRITER_TRUE;
  222. }
  223. /* }}} */
  224. /* {{{ */
  225. int sheet_row_callback (size_t row, size_t max_col, void* callback_data)
  226. {
  227. if (callback_data == NULL) {
  228. return FAILURE;
  229. }
  230. xls_read_callback_data *_callback_data = (xls_read_callback_data *)callback_data;
  231. zval args[3], retval;
  232. _callback_data->fci->retval = &retval;
  233. _callback_data->fci->params = args;
  234. _callback_data->fci->param_count = 3;
  235. ZVAL_LONG(&args[0], (row - 1));
  236. ZVAL_LONG(&args[1], (max_col - 1));
  237. ZVAL_STRING(&args[2], "XLSX_ROW_END");
  238. zend_call_function(_callback_data->fci, _callback_data->fci_cache);
  239. zval_ptr_dtor(&args[2]);
  240. zval_ptr_dtor(&retval);
  241. return SUCCESS;
  242. }
  243. /* }}} */
  244. /* {{{ */
  245. int sheet_cell_callback (size_t row, size_t col, const char *value, void *callback_data)
  246. {
  247. size_t _value_length = 0;
  248. if (value != NULL) {
  249. _value_length = strlen(value);
  250. }
  251. if (callback_data == NULL) {
  252. return FAILURE;
  253. }
  254. xls_read_callback_data *_callback_data = (xls_read_callback_data *)callback_data;
  255. if (_callback_data->fci == NULL || _callback_data->fci_cache == NULL) {
  256. return FAILURE;
  257. }
  258. zval args[3], retval;
  259. _callback_data->fci->retval = &retval;
  260. _callback_data->fci->params = args;
  261. _callback_data->fci->param_count = 3;
  262. ZVAL_LONG(&args[0], (row - 1));
  263. ZVAL_LONG(&args[1], (col - 1));
  264. ZVAL_NULL(&args[2]);
  265. if (value == NULL) {
  266. goto CALL_USER_FUNCTION;
  267. }
  268. if (Z_TYPE_P(_callback_data->zv_type_t) != IS_ARRAY && _callback_data->data_type_default == READ_TYPE_EMPTY) {
  269. zend_long _long = 0; double _double = 0;
  270. if (is_numeric_string(value, _value_length, &_long, &_double, 0)) {
  271. if (_double > 0) {
  272. ZVAL_DOUBLE(&args[2], _double);
  273. } else {
  274. ZVAL_LONG(&args[2], _long);
  275. }
  276. } else {
  277. ZVAL_STRINGL(&args[2], value, _value_length);
  278. }
  279. }
  280. if (Z_TYPE_P(_callback_data->zv_type_t) != IS_ARRAY && _callback_data->data_type_default != READ_TYPE_EMPTY) {
  281. data_to_custom_type(value, _value_length, _callback_data->data_type_default, &args[2], 0);
  282. }
  283. if (Z_TYPE_P(_callback_data->zv_type_t) == IS_ARRAY) {
  284. zval *_current_type = NULL;
  285. zend_ulong _type = READ_TYPE_EMPTY;
  286. if ((_current_type = zend_hash_index_find(Z_ARR_P(_callback_data->zv_type_t), (col - 1))) != NULL) {
  287. if (Z_TYPE_P(_current_type) == IS_LONG) {
  288. _type = Z_LVAL_P(_current_type);
  289. }
  290. }
  291. data_to_custom_type(value, _value_length, _type, &args[2], 0);
  292. }
  293. CALL_USER_FUNCTION:
  294. zend_call_function(_callback_data->fci, _callback_data->fci_cache);
  295. zval_ptr_dtor(&args[2]);
  296. zval_ptr_dtor(&retval);
  297. return SUCCESS;
  298. }
  299. /* }}} */
  300. /* {{{ */
  301. unsigned int load_sheet_current_row_data_callback (zend_string *zs_sheet_name_t, xlsxioreader file_t, void *callback_data)
  302. {
  303. if (zs_sheet_name_t == NULL) {
  304. return xlsxioread_process(file_t, NULL, XLSXIOREAD_SKIP_NONE, sheet_cell_callback, sheet_row_callback, callback_data);
  305. }
  306. return xlsxioread_process(file_t, ZSTR_VAL(zs_sheet_name_t), XLSXIOREAD_SKIP_NONE, sheet_cell_callback, sheet_row_callback, callback_data);
  307. }
  308. /* }}} */
  309. /* {{{ */
  310. void load_sheet_row_data (xlsxioreadersheet sheet_t, zend_long sheet_flag, zval *zv_type_t, zend_long data_type_default, zval *zv_result_t)
  311. {
  312. size_t row_index = 0;
  313. do {
  314. load_sheet_current_row_data(sheet_t, zv_result_t, zv_type_t, data_type_default, READ_ROW);
  315. if (row_index == xlsxioread_sheet_last_row_index(sheet_t)) {
  316. return;
  317. }
  318. row_index = xlsxioread_sheet_last_row_index(sheet_t);
  319. if (sheet_flag & XLSXIOREAD_SKIP_EMPTY_ROWS
  320. && Z_TYPE_P(zv_result_t) == IS_ARRAY
  321. && zend_hash_num_elements(Z_ARR_P(zv_result_t)) == 0) {
  322. continue;
  323. }
  324. return;
  325. } while (1);
  326. }
  327. /* }}} */
  328. /* {{{ */
  329. void load_sheet_all_data (xlsxioreadersheet sheet_t, zend_long sheet_flag, zval *zv_type_t, zend_long data_type_default, zval *zv_result_t)
  330. {
  331. if (Z_TYPE_P(zv_result_t) != IS_ARRAY) {
  332. array_init(zv_result_t);
  333. }
  334. while (sheet_read_row(sheet_t))
  335. {
  336. zval _zv_tmp_row;
  337. ZVAL_NULL(&_zv_tmp_row);
  338. load_sheet_current_row_data(sheet_t, &_zv_tmp_row, zv_type_t, data_type_default, READ_SKIP_ROW);
  339. if (sheet_flag & XLSXIOREAD_SKIP_EMPTY_ROWS
  340. && Z_TYPE(_zv_tmp_row) == IS_ARRAY
  341. && zend_hash_num_elements(Z_ARR(_zv_tmp_row)) == 0) {
  342. zval_ptr_dtor(&_zv_tmp_row);
  343. continue;
  344. }
  345. add_next_index_zval(zv_result_t, &_zv_tmp_row);
  346. }
  347. }
  348. /* }}} */
  349. void skip_rows(xlsxioreadersheet sheet_t, zval *zv_type_t, zend_long data_type_default, zend_long zl_skip_row)
  350. {
  351. while (sheet_read_row(sheet_t))
  352. {
  353. zval _zv_tmp_row;
  354. ZVAL_NULL(&_zv_tmp_row);
  355. if (xlsxioread_sheet_last_row_index(sheet_t) < zl_skip_row) {
  356. sheet_read_row(sheet_t);
  357. }
  358. load_sheet_current_row_data(sheet_t, &_zv_tmp_row, zv_type_t, data_type_default, READ_SKIP_ROW);
  359. zval_ptr_dtor(&_zv_tmp_row);
  360. if (xlsxioread_sheet_last_row_index(sheet_t) >= zl_skip_row) {
  361. break;
  362. }
  363. }
  364. }