read.c 13 KB

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