read.c 12 KB

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