csv.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include "php_streams.h"
  14. #include "ext/standard/file.h"
  15. /* {{{ */
  16. unsigned int xlsx_to_csv(
  17. zval *stream_resource,
  18. const char *delimiter_str, int delimiter_str_len,
  19. const char *enclosure_str, int enclosure_str_len,
  20. const char *escape_str, int escape_str_len,
  21. xlsxioreadersheet sheet_t, zval *zv_type_arr_t, unsigned int flag, zend_fcall_info *fci, zend_fcall_info_cache *fci_cache
  22. )
  23. {
  24. ssize_t ret = 0;
  25. zval *_zv_type_arr_t = NULL;
  26. php_stream *_stream_t = NULL;
  27. char delimiter = ',', enclosure = '"', escape_char = '\\';
  28. ZEND_ASSERT(Z_TYPE_P(stream_resource) == IS_RESOURCE);
  29. if (((_stream_t) = (php_stream *)zend_fetch_resource2((Z_RES_P(stream_resource)),
  30. "stream", php_file_le_stream(), php_file_le_pstream())) == NULL) {
  31. return XLSWRITER_FALSE;
  32. }
  33. if (delimiter_str != NULL) {
  34. if (delimiter_str_len < 1) {
  35. zend_throw_exception(vtiful_exception_ce, "delimiter must be a character", 190);
  36. return XLSWRITER_FALSE;
  37. } else if (delimiter_str_len > 1) {
  38. zend_throw_exception(vtiful_exception_ce, "delimiter must be a single character", 191);
  39. return XLSWRITER_FALSE;
  40. }
  41. delimiter = *delimiter_str;
  42. }
  43. if (enclosure_str != NULL) {
  44. if (enclosure_str_len < 1) {
  45. zend_throw_exception(vtiful_exception_ce, "enclosure must be a character", 192);
  46. return XLSWRITER_FALSE;
  47. } else if (enclosure_str_len > 1) {
  48. zend_throw_exception(vtiful_exception_ce, "enclosure must be a single character", 193);
  49. return XLSWRITER_FALSE;
  50. }
  51. enclosure = *enclosure_str;
  52. }
  53. if (escape_str != NULL) {
  54. if (escape_str_len < 1) {
  55. zend_throw_exception(vtiful_exception_ce, "escape must be a character", 194);
  56. return XLSWRITER_FALSE;
  57. } else if (escape_str_len > 1) {
  58. zend_throw_exception(vtiful_exception_ce, "escape must be a single character", 195);
  59. return XLSWRITER_FALSE;
  60. }
  61. escape_char = *escape_str;
  62. }
  63. if (Z_TYPE_P(zv_type_arr_t) == IS_ARRAY) {
  64. _zv_type_arr_t = zv_type_arr_t;
  65. }
  66. zval _zv_tmp_row;
  67. ZVAL_NULL(&_zv_tmp_row);
  68. while (sheet_read_row(sheet_t))
  69. {
  70. load_sheet_current_row_data(sheet_t, &_zv_tmp_row, _zv_type_arr_t, flag);
  71. if (fci != NULL && fci_cache != NULL) {
  72. zval retval;
  73. fci->retval = &retval;
  74. fci->params = &_zv_tmp_row;
  75. fci->param_count = 1;
  76. zend_call_function(fci, fci_cache);
  77. if (Z_TYPE(retval) == IS_ARRAY) {
  78. ret = php_fputcsv(_stream_t, &retval, delimiter, enclosure, escape_char);
  79. }
  80. zval_ptr_dtor(&retval);
  81. goto CLEAN_UP_SCENE;
  82. }
  83. ret = php_fputcsv(_stream_t, &_zv_tmp_row, delimiter, enclosure, escape_char);
  84. CLEAN_UP_SCENE:
  85. zend_hash_clean(Z_ARRVAL(_zv_tmp_row));
  86. if (ret < 0) {
  87. return XLSWRITER_FALSE;
  88. }
  89. }
  90. zval_dtor(&_zv_tmp_row);
  91. return XLSWRITER_TRUE;
  92. }
  93. /* }}} */