read.c 5.5 KB

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