read.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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(char *value)
  38. {
  39. if (strspn(value, ".0123456789") == strlen(value)) {
  40. return XLSWRITER_TRUE;
  41. }
  42. return XLSWRITER_FALSE;
  43. }
  44. /* }}} */
  45. /* {{{ */
  46. int sheet_read_row(xlsxioreadersheet sheet_t)
  47. {
  48. return xlsxioread_sheet_next_row(sheet_t);
  49. }
  50. /* }}} */
  51. /* {{{ */
  52. unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_result_t, zval *zv_type_arr_t, unsigned int flag)
  53. {
  54. zend_ulong _type = READ_TYPE_EMPTY, _cell_index = 0;
  55. zend_array *_za_type_t = NULL;
  56. char *_string_value = NULL;
  57. zval *_current_type = NULL;
  58. if (flag && !sheet_read_row(sheet_t)) {
  59. return XLSWRITER_FALSE;
  60. }
  61. if (Z_TYPE_P(zv_result_t) != IS_ARRAY) {
  62. array_init(zv_result_t);
  63. }
  64. if (zv_type_arr_t != NULL && Z_TYPE_P(zv_type_arr_t) == IS_ARRAY) {
  65. _za_type_t = Z_ARR_P(zv_type_arr_t);
  66. }
  67. while ((_string_value = xlsxioread_sheet_next_cell(sheet_t)) != NULL)
  68. {
  69. _type = READ_TYPE_EMPTY;
  70. if (_za_type_t != NULL) {
  71. if ((_current_type = zend_hash_index_find(_za_type_t, _cell_index)) != NULL) {
  72. if (Z_TYPE_P(_current_type) == IS_LONG) {
  73. _type = Z_LVAL_P(_current_type);
  74. }
  75. }
  76. _cell_index++;
  77. }
  78. if (_type & READ_TYPE_DATETIME) {
  79. if (!is_number(_string_value)) {
  80. goto STRING;
  81. }
  82. double value = strtod(_string_value, NULL);
  83. if (value != 0) {
  84. value = (value - 25569) * 86400;
  85. }
  86. add_next_index_long(zv_result_t, (zend_long)(value + 0.5));
  87. continue;
  88. }
  89. if (_type & READ_TYPE_DOUBLE) {
  90. if (!is_number(_string_value)) {
  91. goto STRING;
  92. }
  93. add_next_index_double(zv_result_t, strtod(_string_value, NULL));
  94. continue;
  95. }
  96. if (_type & READ_TYPE_INT) {
  97. if (!is_number(_string_value)) {
  98. goto STRING;
  99. }
  100. zend_long _long_value;
  101. sscanf(_string_value, "%" PRIi64, &_long_value);
  102. add_next_index_long(zv_result_t, _long_value);
  103. continue;
  104. }
  105. STRING:
  106. add_next_index_stringl(zv_result_t, _string_value, strlen(_string_value));
  107. }
  108. return XLSWRITER_TRUE;
  109. }
  110. /* }}} */
  111. /* {{{ */
  112. int sheet_row_callback (size_t row, size_t max_col, void* callback_data)
  113. {
  114. if (callback_data == NULL) {
  115. return FAILURE;
  116. }
  117. xls_read_callback_data *_callback_data = (xls_read_callback_data *)callback_data;
  118. zval args[3], retval;
  119. _callback_data->fci->retval = &retval;
  120. _callback_data->fci->params = args;
  121. _callback_data->fci->param_count = 3;
  122. ZVAL_LONG(&args[0], row);
  123. ZVAL_LONG(&args[1], max_col);
  124. ZVAL_STRING(&args[2], "XLSX_ROW_END");
  125. zend_call_function(_callback_data->fci, _callback_data->fci_cache);
  126. zval_ptr_dtor(&args[2]);
  127. zval_ptr_dtor(&retval);
  128. return SUCCESS;
  129. }
  130. /* }}} */
  131. /* {{{ */
  132. int sheet_cell_callback (size_t row, size_t col, const char *value, void *callback_data)
  133. {
  134. if (callback_data == NULL) {
  135. return FAILURE;
  136. }
  137. xls_read_callback_data *_callback_data = (xls_read_callback_data *)callback_data;
  138. if (_callback_data->fci == NULL || _callback_data->fci_cache == NULL) {
  139. return FAILURE;
  140. }
  141. zval args[3], retval;
  142. _callback_data->fci->retval = &retval;
  143. _callback_data->fci->params = args;
  144. _callback_data->fci->param_count = 3;
  145. ZVAL_LONG(&args[0], row);
  146. ZVAL_LONG(&args[1], col);
  147. ZVAL_STRING(&args[2], value);
  148. zend_call_function(_callback_data->fci, _callback_data->fci_cache);
  149. zval_ptr_dtor(&args[2]);
  150. zval_ptr_dtor(&retval);
  151. return SUCCESS;
  152. }
  153. /* }}} */
  154. /* {{{ */
  155. unsigned int load_sheet_current_row_data_callback(zend_string *zs_sheet_name_t, xlsxioreader file_t, void *callback_data)
  156. {
  157. if (zs_sheet_name_t == NULL) {
  158. return xlsxioread_process(file_t, NULL, XLSXIOREAD_SKIP_NONE, sheet_cell_callback, sheet_row_callback, callback_data);
  159. }
  160. return xlsxioread_process(file_t, ZSTR_VAL(zs_sheet_name_t), XLSXIOREAD_SKIP_NONE, sheet_cell_callback, sheet_row_callback, callback_data);
  161. }
  162. /* }}} */
  163. /* {{{ */
  164. void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_type_t, zval *zv_result_t)
  165. {
  166. if (Z_TYPE_P(zv_result_t) != IS_ARRAY) {
  167. array_init(zv_result_t);
  168. }
  169. while (sheet_read_row(sheet_t))
  170. {
  171. zval _zv_tmp_row;
  172. ZVAL_NULL(&_zv_tmp_row);
  173. load_sheet_current_row_data(sheet_t, &_zv_tmp_row, zv_type_t, READ_SKIP_ROW);
  174. add_next_index_zval(zv_result_t, &_zv_tmp_row);
  175. }
  176. }
  177. /* }}} */