read.c 8.1 KB

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