read.c 9.3 KB

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