Browse Source

Merge pull request #363 from viest/dev

Feat(read): add file path in open file exception message
viest 4 năm trước cách đây
mục cha
commit
afb5dab177
4 tập tin đã thay đổi với 29 bổ sung3 xóa
  1. 1 0
      include/xlswriter.h
  2. 17 0
      kernel/common.c
  3. 10 2
      kernel/read.c
  4. 1 1
      tests/open_xlsx_file_not_found.phpt

+ 1 - 0
include/xlswriter.h

@@ -268,6 +268,7 @@ void url_writer(zend_long row, zend_long columns, xls_resource_write_t *res, zen
 lxw_error workbook_file(xls_resource_write_t *self);
 
 lxw_datetime timestamp_to_datetime(zend_long timestamp);
+zend_string* char_join_to_zend_str(const char *left, const char *right);
 zend_string* str_pick_up(zend_string *left, const char *right, size_t len);
 
 #endif

+ 17 - 0
kernel/common.c

@@ -50,6 +50,23 @@ zend_string* str_pick_up(zend_string *left, const char *right, size_t len)
 }
 /* }}} */
 
+/* {{{ */
+zend_string* char_join_to_zend_str(const char *left, const char *right)
+{
+    size_t _new_len = strlen(left) + strlen(right);
+
+    zend_string *str = zend_string_alloc(_new_len, 0);
+
+    memcpy(ZSTR_VAL(str), left, strlen(left));
+    memcpy(ZSTR_VAL(str) + strlen(left), right, strlen(right) + 1);
+
+    ZSTR_VAL(str)[_new_len] = '\0';
+
+    return str;
+}
+
+/* }}} */
+
 /* {{{ */
 void call_object_method(zval *object, const char *function_name, uint32_t param_count, zval *params, zval *ret_val)
 {

+ 10 - 2
kernel/read.c

@@ -23,14 +23,22 @@ xlsxioreader file_open(const char *directory, const char *file_name) {
     strcat(path, file_name);
 
     if (file_exists(path) == XLSWRITER_FALSE) {
+        zend_string *message = char_join_to_zend_str("File not found, file path:", path);
+        zend_throw_exception(vtiful_exception_ce, ZSTR_VAL(message), 121);
+
+        zend_string_free(message);
         efree(path);
-        zend_throw_exception(vtiful_exception_ce, "File not found, please check the path in the config or file name", 121);
+
         return NULL;
     }
 
     if ((file = xlsxioread_open(path)) == NULL) {
+        zend_string *message = char_join_to_zend_str("Failed to open file, file path:", path);
+        zend_throw_exception(vtiful_exception_ce, ZSTR_VAL(message), 100);
+
+        zend_string_free(message);
         efree(path);
-        zend_throw_exception(vtiful_exception_ce, "Failed to open file", 100);
+
         return NULL;
     }
 

+ 1 - 1
tests/open_xlsx_file_not_found.phpt

@@ -21,4 +21,4 @@ try {
 //
 ?>
 --EXPECT--
-string(64) "File not found, please check the path in the config or file name"
+string(57) "File not found, file path:./tests/tutorial_not_found.xlsx"