Explorar o código

Feat: xlsx to csv

viest %!s(int64=5) %!d(string=hai) anos
pai
achega
540e46379e
Modificáronse 5 ficheiros con 109 adicións e 0 borrados
  1. 1 0
      config.m4
  2. 18 0
      include/csv.h
  3. 1 0
      include/xlswriter.h
  4. 58 0
      kernel/csv.c
  5. 31 0
      kernel/excel.c

+ 1 - 0
config.m4

@@ -24,6 +24,7 @@ if test "$PHP_XLSWRITER" != "no"; then
 
     xls_read_sources="
     kernel/read.c \
+    kernel/csv.c \
     "
 
     minizip_sources="

+ 18 - 0
include/csv.h

@@ -0,0 +1,18 @@
+/*
+  +----------------------------------------------------------------------+
+  | XlsWriter Extension                                                  |
+  +----------------------------------------------------------------------+
+  | Copyright (c) 2017-2018 The Viest                                    |
+  +----------------------------------------------------------------------+
+  | http://www.viest.me                                                  |
+  +----------------------------------------------------------------------+
+  | Author: viest <[email protected]>                                 |
+  +----------------------------------------------------------------------+
+*/
+
+#ifndef PHP_EXT_XLS_WRITER_CSV_H
+#define PHP_EXT_XLS_WRITER_CSV_H
+
+unsigned int xlsx_to_csv(zval *stream_resource, xlsxioreadersheet sheet_t, zval *zv_type_arr_t, unsigned int flag);
+
+#endif // PHP_EXT_XLS_WRITER_CSV_H

+ 1 - 0
include/xlswriter.h

@@ -37,6 +37,7 @@
 #ifdef ENABLE_READER
 #include "xlsxio_read.h"
 #include "read.h"
+#include "csv.h"
 
 typedef struct {
     xlsxioreader      file_t;

+ 58 - 0
kernel/csv.c

@@ -0,0 +1,58 @@
+/*
+  +----------------------------------------------------------------------+
+  | XlsWriter Extension                                                  |
+  +----------------------------------------------------------------------+
+  | Copyright (c) 2017-2018 The Viest                                    |
+  +----------------------------------------------------------------------+
+  | http://www.viest.me                                                  |
+  +----------------------------------------------------------------------+
+  | Author: viest <[email protected]>                                 |
+  +----------------------------------------------------------------------+
+*/
+
+#include "xlswriter.h"
+#include "php_streams.h"
+#include "ext/standard/file.h"
+
+/* {{{ */
+unsigned int xlsx_to_csv(zval *stream_resource, xlsxioreadersheet sheet_t, zval *zv_type_arr_t, unsigned int flag)
+{
+    zval *_zv_type_arr_t = NULL;
+    php_stream *_stream_t;
+
+    ssize_t ret;
+    char delimiter = ',';
+    char enclosure = '"';
+    int escape_char = (unsigned char) '\\';
+
+    ZEND_ASSERT(Z_TYPE_P(stream_resource) == IS_RESOURCE);
+
+    if (((_stream_t) = (php_stream *)zend_fetch_resource2((Z_RES_P(stream_resource)),
+            "stream", php_file_le_stream(), php_file_le_pstream())) == NULL) {
+        return XLSWRITER_FALSE;
+	}
+
+    if (Z_TYPE_P(zv_type_arr_t) == IS_ARRAY) {
+        _zv_type_arr_t = zv_type_arr_t;
+    }
+
+    zval _zv_tmp_row;
+    ZVAL_NULL(&_zv_tmp_row);
+
+    while (sheet_read_row(sheet_t))
+    {
+        load_sheet_current_row_data(sheet_t, &_zv_tmp_row, _zv_type_arr_t, flag);
+        ret = php_fputcsv(_stream_t, &_zv_tmp_row, delimiter, enclosure, escape_char);
+
+        zend_hash_clean(Z_ARRVAL(_zv_tmp_row));
+
+        if (ret < 0) {
+            return XLSWRITER_FALSE;
+        }
+    }
+
+    zval_dtor(&_zv_tmp_row);
+
+    return XLSWRITER_TRUE;
+}
+/* }}} */

+ 31 - 0
kernel/excel.c

@@ -163,6 +163,10 @@ ZEND_BEGIN_ARG_INFO_EX(xls_open_sheet_arginfo, 0, 0, 1)
                 ZEND_ARG_INFO(0, zs_sheet_name)
 ZEND_END_ARG_INFO()
 
+ZEND_BEGIN_ARG_INFO_EX(xls_put_csv_arginfo, 0, 0, 1)
+                ZEND_ARG_INFO(0, fp)
+ZEND_END_ARG_INFO()
+
 ZEND_BEGIN_ARG_INFO_EX(xls_set_type_arginfo, 0, 0, 1)
                 ZEND_ARG_INFO(0, zv_type_t)
 ZEND_END_ARG_INFO()
@@ -924,6 +928,32 @@ PHP_METHOD(vtiful_xls, setType)
 }
 /* }}} */
 
+/** {{{ \Vtiful\Kernel\Excel::putCSV()
+ */
+PHP_METHOD(vtiful_xls, putCSV)
+{
+    zval *fp = NULL, *zv_type = NULL;;
+
+    ZEND_PARSE_PARAMETERS_START(1, 1)
+            Z_PARAM_RESOURCE(fp)
+    ZEND_PARSE_PARAMETERS_END();
+
+    xls_object *obj = Z_XLS_P(getThis());
+
+    if (!obj->read_ptr.sheet_t) {
+        RETURN_FALSE;
+    }
+
+    zv_type = zend_read_property(vtiful_xls_ce, getThis(), ZEND_STRL(V_XLS_TYPE), 0, NULL);
+
+    if (xlsx_to_csv(fp, obj->read_ptr.sheet_t, zv_type, READ_SKIP_ROW) == XLSWRITER_TRUE) {
+        RETURN_TRUE;
+    }
+
+    RETURN_FALSE;
+}
+/* }}} */
+
 /** {{{ \Vtiful\Kernel\Excel::getSheetData()
  */
 PHP_METHOD(vtiful_xls, getSheetData)
@@ -1037,6 +1067,7 @@ zend_function_entry xls_methods[] = {
 #ifdef ENABLE_READER
         PHP_ME(vtiful_xls, openFile,         xls_open_file_arginfo,          ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, openSheet,        xls_open_sheet_arginfo,         ZEND_ACC_PUBLIC)
+        PHP_ME(vtiful_xls, putCSV,           xls_put_csv_arginfo,            ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, sheetList,        NULL,                           ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, setType,          xls_set_type_arginfo,           ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, getSheetData,     NULL,                           ZEND_ACC_PUBLIC)