read.c 5.6 KB

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