Browse Source

Feat: exception message and reader

1. default enable reader
2. exception message
viest 3 years ago
parent
commit
fdcd294f52
6 changed files with 90 additions and 8 deletions
  1. 1 1
      config.m4
  2. 2 0
      include/exception.h
  3. 6 6
      include/xlswriter.h
  4. 5 1
      kernel/excel.c
  5. 50 0
      kernel/exception.c
  6. 26 0
      tests/writer_exception.phpt

+ 1 - 1
config.m4

@@ -8,7 +8,7 @@ PHP_ARG_WITH(libxlsxio, system libxlsxio,
 [  --with-libxlsxio=DIR     Use system libxlsxio], no, no)
 
 PHP_ARG_ENABLE(reader, enable xlsx reader support,
-[  --enable-reader          Enable xlsx reader?], no, no)
+[  --enable-reader          Enable xlsx reader?], yes, yes)
 
 if test "$PHP_XLSWRITER" != "no"; then
     xls_writer_sources="

+ 2 - 0
include/exception.h

@@ -17,4 +17,6 @@ extern zend_class_entry *vtiful_exception_ce;
 
 VTIFUL_STARTUP_FUNCTION(exception);
 
+char* exception_message_map(int code);
+
 #endif

+ 6 - 6
include/xlswriter.h

@@ -161,12 +161,12 @@ typedef struct _vtiful_validation_object {
         }                                                                                                 \
     } while(0);
 
-#define WORKSHEET_WRITER_EXCEPTION(error)                                                  \
-    do {                                                                                   \
-        if(error > LXW_NO_ERROR) {                                                         \
-            zend_throw_exception(vtiful_exception_ce, "Worksheet write exception", error); \
-            return;                                                                        \
-        }                                                                                  \
+#define WORKSHEET_WRITER_EXCEPTION(error)                                                   \
+    do {                                                                                    \
+        if(error > LXW_NO_ERROR) {                                                          \
+            zend_throw_exception(vtiful_exception_ce, exception_message_map(error), error); \
+            return;                                                                         \
+        }                                                                                   \
     } while(0)
 
 #define FCALL_TWO_ARGS(bucket)                   \

+ 5 - 1
kernel/excel.c

@@ -470,7 +470,11 @@ PHP_METHOD(vtiful_xls, constMemory)
     if(obj->write_ptr.workbook == NULL) {
         xls_file_path(zs_file_name, dir_path, &file_path);
 
-        lxw_workbook_options options = {.constant_memory = LXW_TRUE, .tmpdir = NULL};
+        lxw_workbook_options options = {
+            .constant_memory = LXW_TRUE,
+            .tmpdir = NULL,
+            .use_zip64 = LXW_TRUE
+        };
 
         if(zs_sheet_name != NULL) {
             sheet_name = ZSTR_VAL(zs_sheet_name);

+ 50 - 0
kernel/exception.c

@@ -33,3 +33,53 @@ VTIFUL_STARTUP_FUNCTION(exception) {
     return SUCCESS;
 }
 /* }}} */
+
+/** {{{ exception_message_map
+*/
+char* exception_message_map(int code) {
+    switch (code) {
+        case LXW_ERROR_MEMORY_MALLOC_FAILED:
+            return "Memory error, failed to malloc() required memory.";
+        case LXW_ERROR_CREATING_XLSX_FILE:
+            return "Error creating output xlsx file. Usually a permissions error.";
+        case LXW_ERROR_CREATING_TMPFILE:
+            return "Error encountered when creating a tmpfile during file assembly.";
+        case LXW_ERROR_READING_TMPFILE:
+            return "Error reading a tmpfile.";
+        case LXW_ERROR_ZIP_FILE_OPERATION:
+            return "Zlib error with a file operation while creating xlsx file.";
+        case LXW_ERROR_ZIP_FILE_ADD:
+            return "Zlib error when adding sub file to xlsx file.";
+        case LXW_ERROR_ZIP_CLOSE:
+            return "Zlib error when closing xlsx file.";
+        case LXW_ERROR_NULL_PARAMETER_IGNORED:
+            return "NULL function parameter ignored.";
+        case LXW_ERROR_PARAMETER_VALIDATION:
+            return "Function parameter validation error.";
+        case LXW_ERROR_SHEETNAME_LENGTH_EXCEEDED:
+            return "Worksheet name exceeds Excel's limit of 31 characters.";
+        case LXW_ERROR_INVALID_SHEETNAME_CHARACTER:
+            return "Worksheet name contains invalid.";
+        case LXW_ERROR_SHEETNAME_ALREADY_USED:
+            return "Worksheet name is already in use.";
+        case LXW_ERROR_32_STRING_LENGTH_EXCEEDED:
+            return "Parameter exceeds Excel's limit of 32 characters.";
+        case LXW_ERROR_128_STRING_LENGTH_EXCEEDED:
+            return "Parameter exceeds Excel's limit of 128 characters.";
+        case LXW_ERROR_255_STRING_LENGTH_EXCEEDED:
+            return "Parameter exceeds Excel's limit of 255 characters.";
+        case LXW_ERROR_MAX_STRING_LENGTH_EXCEEDED:
+            return "String exceeds Excel's limit of 32:767 characters.";
+        case LXW_ERROR_SHARED_STRING_INDEX_NOT_FOUND:
+            return "Error finding internal string index.";
+        case LXW_ERROR_WORKSHEET_INDEX_OUT_OF_RANGE:
+            return "Worksheet row or column index out of range.";
+        case LXW_ERROR_WORKSHEET_MAX_NUMBER_URLS_EXCEEDED:
+            return "Maximum number of worksheet URLs (65530) exceeded.";
+        case LXW_ERROR_IMAGE_DIMENSIONS:
+            return "Couldn't read image dimensions or DPI.";
+        default:
+            return "Unknown error";
+    }
+}
+/* }}} */

+ 26 - 0
tests/writer_exception.phpt

@@ -0,0 +1,26 @@
+--TEST--
+Check for vtiful presence
+--SKIPIF--
+<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
+--FILE--
+<?php
+    try {
+        $config = ['path' => './tests'];
+
+        $fileObject = new \Vtiful\Kernel\Excel($config);
+
+        $fileObject->constMemory('tutorial.xlsx', 'DemoSheet')
+            ->insertText(1, 0, 'viest')
+            ->insertText(0, 0, 'viest');
+    } catch (\Vtiful\Kernel\Exception $exception) {
+          echo $exception->getCode() . PHP_EOL;
+          echo $exception->getMessage() . PHP_EOL;
+    }
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/tutorial.xlsx');
+?>
+--EXPECT--
+23
+Worksheet row or column index out of range.