read.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, const zend_string *zs_sheet_name_t)
  29. {
  30. if (zs_sheet_name_t == NULL) {
  31. return xlsxioread_sheet_open(file, NULL, XLSXIOREAD_SKIP_EMPTY_ROWS);
  32. }
  33. return xlsxioread_sheet_open(file, ZSTR_VAL(zs_sheet_name_t), XLSXIOREAD_SKIP_EMPTY_ROWS);
  34. }
  35. /* }}} */
  36. /* {{{ */
  37. int sheet_read_row(xlsxioreadersheet sheet)
  38. {
  39. return xlsxioread_sheet_next_row(sheet);
  40. }
  41. /* }}} */
  42. /* {{{ */
  43. char* sheet_read_column(xlsxioreadersheet sheet)
  44. {
  45. return xlsxioread_sheet_next_cell(sheet);
  46. }
  47. /* }}} */
  48. /* {{{ */
  49. unsigned int load_sheet_current_row_data(xlsxioreadersheet sheet_t, zval *zv_result_t, unsigned int flag)
  50. {
  51. char *_string_value = NULL;
  52. if (flag && !sheet_read_row(sheet_t)) {
  53. return XLSWRITER_FALSE;
  54. }
  55. if (Z_TYPE_P(zv_result_t) != IS_ARRAY) {
  56. array_init(zv_result_t);
  57. }
  58. while ((_string_value = sheet_read_column(sheet_t)) != NULL)
  59. {
  60. add_next_index_stringl(zv_result_t, _string_value, strlen(_string_value));
  61. }
  62. return XLSWRITER_TRUE;
  63. }
  64. /* }}} */
  65. /* {{{ */
  66. void load_sheet_all_data(xlsxioreadersheet sheet_t, zval *zv_result_t)
  67. {
  68. if (Z_TYPE_P(zv_result_t) != IS_ARRAY) {
  69. array_init(zv_result_t);
  70. }
  71. while (sheet_read_row(sheet_t))
  72. {
  73. zval _zv_tmp_row;
  74. load_sheet_current_row_data(sheet_t, &_zv_tmp_row, READ_SKIP_ROW);
  75. add_next_index_zval(zv_result_t, &_zv_tmp_row);
  76. }
  77. }
  78. /* }}} */