read.c 11 KB

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