read.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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, const size_t string_value_length, const zend_ulong type, zval *zv_result_t, const zend_ulong zv_hashtable_index)
  74. {
  75. if (type == 0) {
  76. goto STRING;
  77. }
  78. if (!is_number(string_value)) {
  79. goto STRING;
  80. }
  81. if (type & READ_TYPE_DATETIME) {
  82. if (string_value_length == 0) {
  83. data_to_null(zv_result_t);
  84. return;
  85. }
  86. double value = strtod(string_value, NULL);
  87. if (value != 0) {
  88. value = (value - 25569) * 86400;
  89. }
  90. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  91. add_index_long(zv_result_t, zv_hashtable_index, (zend_long)(value + 0.5));
  92. } else {
  93. ZVAL_LONG(zv_result_t, (zend_long)(value + 0.5));
  94. }
  95. return;
  96. }
  97. if (type & READ_TYPE_DOUBLE) {
  98. if (string_value_length == 0) {
  99. data_to_null(zv_result_t);
  100. return;
  101. }
  102. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  103. add_index_double(zv_result_t, zv_hashtable_index,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 (string_value_length == 0) {
  111. data_to_null(zv_result_t);
  112. return;
  113. }
  114. zend_long _long_value;
  115. sscanf(string_value, ZEND_LONG_FMT, &_long_value);
  116. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  117. add_index_long(zv_result_t, zv_hashtable_index, _long_value);
  118. } else {
  119. ZVAL_LONG(zv_result_t, _long_value);
  120. }
  121. return;
  122. }
  123. STRING:
  124. {
  125. zend_long _long = 0; double _double = 0;
  126. if (!(type & READ_TYPE_STRING)) {
  127. is_numeric_string(string_value, string_value_length, &_long, &_double, 0);
  128. }
  129. if (Z_TYPE_P(zv_result_t) == IS_ARRAY) {
  130. if (_double > 0) {
  131. add_index_double(zv_result_t, zv_hashtable_index, _double);
  132. return;
  133. } else if (_long > 0) {
  134. add_index_long(zv_result_t, zv_hashtable_index, _long);
  135. return;
  136. }
  137. add_index_stringl(zv_result_t, zv_hashtable_index, string_value, string_value_length);
  138. return;
  139. }
  140. if (_double > 0) {
  141. ZVAL_DOUBLE(zv_result_t, _double);
  142. return;
  143. } else if (_long > 0) {
  144. ZVAL_LONG(zv_result_t, _long);
  145. return;
  146. }
  147. ZVAL_STRINGL(zv_result_t, string_value, string_value_length);
  148. }
  149. }
  150. /* }}} */
  151. /* {{{ */
  152. int sheet_read_row(xlsxioreadersheet sheet_t)
  153. {
  154. return xlsxioread_sheet_next_row(sheet_t);
  155. }
  156. /* }}} */
  157. /* {{{ */
  158. unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_result_t, zval *zv_type_arr_t, unsigned int flag)
  159. {
  160. zend_long _type, _cell_index = 0, _last_cell_index = 0;
  161. zend_bool _skip_empty_value_cell = 0;
  162. zend_array *_za_type_t = NULL;
  163. char *_string_value = NULL;
  164. zval *_current_type = NULL;
  165. if (flag && !sheet_read_row(sheet_t)) {
  166. return XLSWRITER_FALSE;
  167. }
  168. if (xlsxioread_sheet_flags(sheet_t) & SKIP_EMPTY_VALUE) {
  169. _skip_empty_value_cell = 1;
  170. }
  171. if (Z_TYPE_P(zv_result_t) != IS_ARRAY) {
  172. array_init(zv_result_t);
  173. }
  174. if (zv_type_arr_t != NULL && Z_TYPE_P(zv_type_arr_t) == IS_ARRAY) {
  175. _za_type_t = Z_ARR_P(zv_type_arr_t);
  176. }
  177. while ((_string_value = xlsxioread_sheet_next_cell(sheet_t)) != NULL)
  178. {
  179. size_t _string_value_length = strlen(_string_value);
  180. _type = READ_TYPE_EMPTY;
  181. _last_cell_index = xlsxioread_sheet_last_column_index(sheet_t) - 1;
  182. if (_last_cell_index < 0 || (_skip_empty_value_cell && _string_value_length == 0)) {
  183. goto FREE_TMP_VALUE;
  184. }
  185. if (_last_cell_index > _cell_index) {
  186. _cell_index = _last_cell_index;
  187. }
  188. if (_za_type_t != NULL) {
  189. _current_type = zend_hash_index_find(_za_type_t, _cell_index);
  190. if (_current_type != NULL && Z_TYPE_P(_current_type) == IS_LONG) {
  191. _type = Z_LVAL_P(_current_type);
  192. }
  193. }
  194. data_to_custom_type(_string_value, _string_value_length, _type, zv_result_t, _cell_index);
  195. FREE_TMP_VALUE:
  196. ++_cell_index;
  197. free(_string_value);
  198. }
  199. return XLSWRITER_TRUE;
  200. }
  201. /* }}} */
  202. /* {{{ */
  203. int sheet_row_callback (size_t row, size_t max_col, void* callback_data)
  204. {
  205. if (callback_data == NULL) {
  206. return FAILURE;
  207. }
  208. xls_read_callback_data *_callback_data = (xls_read_callback_data *)callback_data;
  209. zval args[3], retval;
  210. _callback_data->fci->retval = &retval;
  211. _callback_data->fci->params = args;
  212. _callback_data->fci->param_count = 3;
  213. ZVAL_LONG(&args[0], (row - 1));
  214. ZVAL_LONG(&args[1], (max_col - 1));
  215. ZVAL_STRING(&args[2], "XLSX_ROW_END");
  216. zend_call_function(_callback_data->fci, _callback_data->fci_cache);
  217. zval_ptr_dtor(&args[2]);
  218. zval_ptr_dtor(&retval);
  219. return SUCCESS;
  220. }
  221. /* }}} */
  222. /* {{{ */
  223. int sheet_cell_callback (size_t row, size_t col, const char *value, void *callback_data)
  224. {
  225. size_t _value_length = strlen(value);
  226. if (callback_data == NULL) {
  227. return FAILURE;
  228. }
  229. xls_read_callback_data *_callback_data = (xls_read_callback_data *)callback_data;
  230. if (_callback_data->fci == NULL || _callback_data->fci_cache == NULL) {
  231. return FAILURE;
  232. }
  233. zval args[3], retval;
  234. _callback_data->fci->retval = &retval;
  235. _callback_data->fci->params = args;
  236. _callback_data->fci->param_count = 3;
  237. ZVAL_LONG(&args[0], (row - 1));
  238. ZVAL_LONG(&args[1], (col - 1));
  239. ZVAL_NULL(&args[2]);
  240. if (value == NULL) {
  241. goto CALL_USER_FUNCTION;
  242. }
  243. if (Z_TYPE_P(_callback_data->zv_type_t) != IS_ARRAY) {
  244. zend_long _long = 0; double _double = 0;
  245. if (is_numeric_string(value, _value_length, &_long, &_double, 0)) {
  246. if (_double > 0) {
  247. ZVAL_DOUBLE(&args[2], _double);
  248. } else {
  249. ZVAL_LONG(&args[2], _long);
  250. }
  251. } else {
  252. ZVAL_STRINGL(&args[2], value, _value_length);
  253. }
  254. }
  255. if (Z_TYPE_P(_callback_data->zv_type_t) == IS_ARRAY) {
  256. zval *_current_type = NULL;
  257. zend_ulong _type = READ_TYPE_EMPTY;
  258. if ((_current_type = zend_hash_index_find(Z_ARR_P(_callback_data->zv_type_t), (col - 1))) != NULL) {
  259. if (Z_TYPE_P(_current_type) == IS_LONG) {
  260. _type = Z_LVAL_P(_current_type);
  261. }
  262. }
  263. data_to_custom_type(value, _value_length, _type, &args[2], 0);
  264. }
  265. CALL_USER_FUNCTION:
  266. zend_call_function(_callback_data->fci, _callback_data->fci_cache);
  267. zval_ptr_dtor(&args[2]);
  268. zval_ptr_dtor(&retval);
  269. return SUCCESS;
  270. }
  271. /* }}} */
  272. /* {{{ */
  273. unsigned int load_sheet_current_row_data_callback (zend_string *zs_sheet_name_t, xlsxioreader file_t, void *callback_data)
  274. {
  275. if (zs_sheet_name_t == NULL) {
  276. return xlsxioread_process(file_t, NULL, XLSXIOREAD_SKIP_NONE, sheet_cell_callback, sheet_row_callback, callback_data);
  277. }
  278. return xlsxioread_process(file_t, ZSTR_VAL(zs_sheet_name_t), XLSXIOREAD_SKIP_NONE, sheet_cell_callback, sheet_row_callback, callback_data);
  279. }
  280. /* }}} */
  281. /* {{{ */
  282. void load_sheet_all_data (xlsxioreadersheet sheet_t, zval *zv_type_t, zval *zv_result_t)
  283. {
  284. if (Z_TYPE_P(zv_result_t) != IS_ARRAY) {
  285. array_init(zv_result_t);
  286. }
  287. while (sheet_read_row(sheet_t))
  288. {
  289. zval _zv_tmp_row;
  290. ZVAL_NULL(&_zv_tmp_row);
  291. load_sheet_current_row_data(sheet_t, &_zv_tmp_row, zv_type_t, READ_SKIP_ROW);
  292. add_next_index_zval(zv_result_t, &_zv_tmp_row);
  293. }
  294. }
  295. /* }}} */