read.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. /* {{{ */
  14. xlsxioreader file_open(const char *directory, const char *file_name) {
  15. char path[strlen(directory) + strlen(file_name) + 2];
  16. xlsxioreader file;
  17. lxw_strcpy(path, directory);
  18. strcat(path, "/");
  19. strcat(path, file_name);
  20. if ((file = xlsxioread_open(path)) == NULL) {
  21. zend_throw_exception(vtiful_exception_ce, "Failed to open file", 100);
  22. return NULL;
  23. }
  24. return file;
  25. }
  26. /* }}} */
  27. /* {{{ */
  28. xlsxioreadersheet sheet_open(xlsxioreader file_t, const zend_string *zs_sheet_name_t, const zend_long zl_flag)
  29. {
  30. if (zs_sheet_name_t == NULL) {
  31. return xlsxioread_sheet_open(file_t, NULL, zl_flag);
  32. }
  33. return xlsxioread_sheet_open(file_t, ZSTR_VAL(zs_sheet_name_t), zl_flag);
  34. }
  35. /* }}} */
  36. /* {{{ */
  37. int is_number(const char *value)
  38. {
  39. if (strspn(value, ".0123456789") == strlen(value)) {
  40. return XLSWRITER_TRUE;
  41. }
  42. return XLSWRITER_FALSE;
  43. }
  44. /* }}} */
  45. /* {{{ */
  46. void data_to_null(zval *zv_result_t)
  47. {
  48. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  49. add_next_index_null(zv_result_t);
  50. } else {
  51. ZVAL_NULL(zv_result_t);
  52. }
  53. }
  54. /* }}} */
  55. /* {{{ */
  56. void data_to_custom_type(const char *string_value, zend_ulong type, zval *zv_result_t)
  57. {
  58. if (type & READ_TYPE_DATETIME) {
  59. if (!is_number(string_value)) {
  60. goto STRING;
  61. }
  62. if (strlen(string_value) == 0) {
  63. data_to_null(zv_result_t);
  64. return;
  65. }
  66. double value = strtod(string_value, NULL);
  67. if (value != 0) {
  68. value = (value - 25569) * 86400;
  69. }
  70. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  71. add_next_index_long(zv_result_t, (zend_long)(value + 0.5));
  72. } else {
  73. ZVAL_LONG(zv_result_t, (zend_long)(value + 0.5));
  74. }
  75. return;
  76. }
  77. if (type & READ_TYPE_DOUBLE) {
  78. if (!is_number(string_value)) {
  79. goto STRING;
  80. }
  81. if (strlen(string_value) == 0) {
  82. data_to_null(zv_result_t);
  83. return;
  84. }
  85. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  86. add_next_index_double(zv_result_t, strtod(string_value, NULL));
  87. } else {
  88. ZVAL_DOUBLE(zv_result_t, strtod(string_value, NULL));
  89. }
  90. return;
  91. }
  92. if (type & READ_TYPE_INT) {
  93. if (!is_number(string_value)) {
  94. goto STRING;
  95. }
  96. if (strlen(string_value) == 0) {
  97. data_to_null(zv_result_t);
  98. return;
  99. }
  100. zend_long _long_value;
  101. sscanf(string_value, ZEND_LONG_FMT, &_long_value);
  102. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  103. add_next_index_long(zv_result_t, _long_value);
  104. } else {
  105. ZVAL_LONG(zv_result_t, _long_value);
  106. }
  107. return;
  108. }
  109. STRING:
  110. {
  111. zend_long _long = 0; double _double = 0;
  112. is_numeric_string(string_value, strlen(string_value), &_long, &_double, 0);
  113. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  114. if (_double > 0) {
  115. add_next_index_double(zv_result_t, _double);
  116. return;
  117. } else if (_long > 0) {
  118. add_next_index_long(zv_result_t, _long);
  119. return;
  120. }
  121. add_next_index_stringl(zv_result_t, string_value, strlen(string_value));
  122. return;
  123. }
  124. if (_double > 0) {
  125. ZVAL_DOUBLE(zv_result_t, _double);
  126. return;
  127. } else if (_long > 0) {
  128. ZVAL_LONG(zv_result_t, _long);
  129. return;
  130. }
  131. ZVAL_STRINGL(zv_result_t, string_value, strlen(string_value));
  132. }
  133. }
  134. /* }}} */
  135. /* {{{ */
  136. int sheet_read_row(xlsxioreadersheet sheet_t)
  137. {
  138. return xlsxioread_sheet_next_row(sheet_t);
  139. }
  140. /* }}} */
  141. /* {{{ */
  142. unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_result_t, zval *zv_type_arr_t, unsigned int flag)
  143. {
  144. zend_ulong _type, _cell_index = 0;
  145. zend_array *_za_type_t = NULL;
  146. char *_string_value = NULL;
  147. zval *_current_type = NULL;
  148. if (flag && !sheet_read_row(sheet_t)) {
  149. return XLSWRITER_FALSE;
  150. }
  151. if (Z_TYPE_P(zv_result_t) != IS_ARRAY) {
  152. array_init(zv_result_t);
  153. }
  154. if (zv_type_arr_t != NULL && Z_TYPE_P(zv_type_arr_t) == IS_ARRAY) {
  155. _za_type_t = Z_ARR_P(zv_type_arr_t);
  156. }
  157. while ((_string_value = xlsxioread_sheet_next_cell(sheet_t)) != NULL)
  158. {
  159. _type = READ_TYPE_EMPTY;
  160. if (_za_type_t != NULL) {
  161. if ((_current_type = zend_hash_index_find(_za_type_t, _cell_index)) != NULL) {
  162. if (Z_TYPE_P(_current_type) == IS_LONG) {
  163. _type = Z_LVAL_P(_current_type);
  164. }
  165. }
  166. _cell_index++;
  167. }
  168. data_to_custom_type(_string_value, _type, zv_result_t);
  169. }
  170. return XLSWRITER_TRUE;
  171. }
  172. /* }}} */
  173. /* {{{ */
  174. int sheet_row_callback (size_t row, size_t max_col, void* callback_data)
  175. {
  176. if (callback_data == NULL) {
  177. return FAILURE;
  178. }
  179. xls_read_callback_data *_callback_data = (xls_read_callback_data *)callback_data;
  180. zval args[3], retval;
  181. _callback_data->fci->retval = &retval;
  182. _callback_data->fci->params = args;
  183. _callback_data->fci->param_count = 3;
  184. ZVAL_LONG(&args[0], (row - 1));
  185. ZVAL_LONG(&args[1], (max_col - 1));
  186. ZVAL_STRING(&args[2], "XLSX_ROW_END");
  187. zend_call_function(_callback_data->fci, _callback_data->fci_cache);
  188. zval_ptr_dtor(&args[2]);
  189. zval_ptr_dtor(&retval);
  190. return SUCCESS;
  191. }
  192. /* }}} */
  193. /* {{{ */
  194. int sheet_cell_callback (size_t row, size_t col, const char *value, void *callback_data)
  195. {
  196. if (callback_data == NULL) {
  197. return FAILURE;
  198. }
  199. xls_read_callback_data *_callback_data = (xls_read_callback_data *)callback_data;
  200. if (_callback_data->fci == NULL || _callback_data->fci_cache == NULL) {
  201. return FAILURE;
  202. }
  203. zval args[3], retval;
  204. _callback_data->fci->retval = &retval;
  205. _callback_data->fci->params = args;
  206. _callback_data->fci->param_count = 3;
  207. ZVAL_LONG(&args[0], (row - 1));
  208. ZVAL_LONG(&args[1], (col - 1));
  209. if (Z_TYPE_P(_callback_data->zv_type_t) != IS_ARRAY) {
  210. zend_long _long = 0; double _double = 0;
  211. if (is_numeric_string(value, strlen(value), &_long, &_double, 0)) {
  212. if (_double > 0) {
  213. ZVAL_DOUBLE(&args[2], _double);
  214. } else {
  215. ZVAL_LONG(&args[2], _long);
  216. }
  217. } else {
  218. ZVAL_STRINGL(&args[2], value, strlen(value));
  219. }
  220. }
  221. if (Z_TYPE_P(_callback_data->zv_type_t) == IS_ARRAY) {
  222. zval *_current_type = NULL;
  223. zend_ulong _type = READ_TYPE_EMPTY;
  224. if ((_current_type = zend_hash_index_find(Z_ARR_P(_callback_data->zv_type_t), (col - 1))) != NULL) {
  225. if (Z_TYPE_P(_current_type) == IS_LONG) {
  226. _type = Z_LVAL_P(_current_type);
  227. }
  228. }
  229. ZVAL_NULL(&args[2]);
  230. data_to_custom_type(value, _type, &args[2]);
  231. }
  232. zend_call_function(_callback_data->fci, _callback_data->fci_cache);
  233. zval_ptr_dtor(&args[2]);
  234. zval_ptr_dtor(&retval);
  235. return SUCCESS;
  236. }
  237. /* }}} */
  238. /* {{{ */
  239. unsigned int load_sheet_current_row_data_callback(zend_string *zs_sheet_name_t, xlsxioreader file_t, void *callback_data)
  240. {
  241. if (zs_sheet_name_t == NULL) {
  242. return xlsxioread_process(file_t, NULL, XLSXIOREAD_SKIP_NONE, sheet_cell_callback, sheet_row_callback, callback_data);
  243. }
  244. return xlsxioread_process(file_t, ZSTR_VAL(zs_sheet_name_t), XLSXIOREAD_SKIP_NONE, sheet_cell_callback, sheet_row_callback, callback_data);
  245. }
  246. /* }}} */
  247. /* {{{ */
  248. void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_type_t, zval *zv_result_t)
  249. {
  250. if (Z_TYPE_P(zv_result_t) != IS_ARRAY) {
  251. array_init(zv_result_t);
  252. }
  253. while (sheet_read_row(sheet_t))
  254. {
  255. zval _zv_tmp_row;
  256. ZVAL_NULL(&_zv_tmp_row);
  257. load_sheet_current_row_data(sheet_t, &_zv_tmp_row, zv_type_t, READ_SKIP_ROW);
  258. add_next_index_zval(zv_result_t, &_zv_tmp_row);
  259. }
  260. }
  261. /* }}} */