Browse Source

Feat: file and directory helper function

viest 4 years ago
parent
commit
545d323e49
4 changed files with 40 additions and 19 deletions
  1. 2 0
      include/help.h
  2. 3 9
      kernel/excel.c
  3. 34 1
      kernel/help.c
  4. 1 9
      kernel/read.c

+ 2 - 0
include/help.h

@@ -13,6 +13,8 @@
 #ifndef PHP_EXT_XLS_EXPORT_HELP_H
 #ifndef PHP_EXT_XLS_EXPORT_HELP_H
 #define PHP_EXT_XLS_EXPORT_HELP_H
 #define PHP_EXT_XLS_EXPORT_HELP_H
 
 
+unsigned int file_exists(const char *path);
+unsigned int directory_exists(const char *path);
 zend_long date_double_to_timestamp(double value);
 zend_long date_double_to_timestamp(double value);
 
 
 #endif //PHP_EXT_XLS_EXPORT_HELP_H
 #endif //PHP_EXT_XLS_EXPORT_HELP_H

+ 3 - 9
kernel/excel.c

@@ -12,7 +12,6 @@
 
 
 #include "xlswriter.h"
 #include "xlswriter.h"
 #include "ext/date/php_date.h"
 #include "ext/date/php_date.h"
-#include "ext/standard/php_filestat.h"
 
 
 zend_class_entry *vtiful_xls_ce;
 zend_class_entry *vtiful_xls_ce;
 
 
@@ -278,7 +277,7 @@ PHP_METHOD(vtiful_xls, __construct)
 PHP_METHOD(vtiful_xls, fileName)
 PHP_METHOD(vtiful_xls, fileName)
 {
 {
     char *sheet_name = NULL;
     char *sheet_name = NULL;
-    zval file_path, dir_exists, *dir_path = NULL;
+    zval file_path, *dir_path = NULL;
     zend_string *zs_file_name = NULL, *zs_sheet_name = NULL;
     zend_string *zs_file_name = NULL, *zs_sheet_name = NULL;
 
 
     ZEND_PARSE_PARAMETERS_START(1, 2)
     ZEND_PARSE_PARAMETERS_START(1, 2)
@@ -287,16 +286,13 @@ PHP_METHOD(vtiful_xls, fileName)
             Z_PARAM_STR(zs_sheet_name)
             Z_PARAM_STR(zs_sheet_name)
     ZEND_PARSE_PARAMETERS_END();
     ZEND_PARSE_PARAMETERS_END();
 
 
-    ZVAL_NULL(&dir_exists);
     ZVAL_COPY(return_value, getThis());
     ZVAL_COPY(return_value, getThis());
 
 
     GET_CONFIG_PATH(dir_path, vtiful_xls_ce, return_value);
     GET_CONFIG_PATH(dir_path, vtiful_xls_ce, return_value);
 
 
-    php_stat(ZSTR_VAL(Z_STR_P(dir_path)), strlen(ZSTR_VAL(Z_STR_P(dir_path))), FS_IS_DIR, &dir_exists);
-
-    if (Z_TYPE(dir_exists) == IS_FALSE) {
-        zval_ptr_dtor(&dir_exists);
+    if(directory_exists(ZSTR_VAL(Z_STR_P(dir_path))) == XLSWRITER_FALSE) {
         zend_throw_exception(vtiful_exception_ce, "Configure 'path' directory does not exist", 121);
         zend_throw_exception(vtiful_exception_ce, "Configure 'path' directory does not exist", 121);
+        return;
     }
     }
 
 
     xls_object *obj = Z_XLS_P(getThis());
     xls_object *obj = Z_XLS_P(getThis());
@@ -315,8 +311,6 @@ PHP_METHOD(vtiful_xls, fileName)
 
 
         zval_ptr_dtor(&file_path);
         zval_ptr_dtor(&file_path);
     }
     }
-
-    zval_ptr_dtor(&dir_exists);
 }
 }
 /* }}} */
 /* }}} */
 
 

+ 34 - 1
kernel/help.c

@@ -13,7 +13,9 @@
 #include "xlswriter.h"
 #include "xlswriter.h"
 #include "ext/date/php_date.h"
 #include "ext/date/php_date.h"
 #include "ext/standard/php_math.h"
 #include "ext/standard/php_math.h"
+#include "ext/standard/php_filestat.h"
 
 
+/* {{{ */
 zend_long date_double_to_timestamp(double value) {
 zend_long date_double_to_timestamp(double value) {
     double days, partDay, hours, minutes, seconds;
     double days, partDay, hours, minutes, seconds;
 
 
@@ -56,4 +58,35 @@ zend_long date_double_to_timestamp(double value) {
     zval_ptr_dtor(&_format_result);
     zval_ptr_dtor(&_format_result);
 
 
     return timestamp;
     return timestamp;
-}
+}
+/* }}} */
+
+/* {{{ */
+unsigned int directory_exists(const char *path) {
+    zval dir_exists;
+    php_stat(path, strlen(path), FS_IS_DIR, &dir_exists);
+
+    if (Z_TYPE(dir_exists) == IS_FALSE) {
+        zval_ptr_dtor(&dir_exists);
+        return XLSWRITER_FALSE;
+    }
+
+    zval_ptr_dtor(&dir_exists);
+    return XLSWRITER_TRUE;
+}
+/* }}} */
+
+/* {{{ */
+unsigned int file_exists(const char *path) {
+    zval file_exists;
+    php_stat(path, strlen(path), FS_IS_FILE, &file_exists);
+
+    if (Z_TYPE(file_exists) == IS_FALSE) {
+        zval_ptr_dtor(&file_exists);
+        return XLSWRITER_FALSE;
+    }
+
+    zval_ptr_dtor(&file_exists);
+    return XLSWRITER_TRUE;
+}
+/* }}} */

+ 1 - 9
kernel/read.c

@@ -12,8 +12,6 @@
 
 
 #include "xlswriter.h"
 #include "xlswriter.h"
 #include "ext/date/php_date.h"
 #include "ext/date/php_date.h"
-#include "ext/standard/php_math.h"
-#include "ext/standard/php_filestat.h"
 
 
 /* {{{ */
 /* {{{ */
 xlsxioreader file_open(const char *directory, const char *file_name) {
 xlsxioreader file_open(const char *directory, const char *file_name) {
@@ -24,25 +22,19 @@ xlsxioreader file_open(const char *directory, const char *file_name) {
     strcat(path, "/");
     strcat(path, "/");
     strcat(path, file_name);
     strcat(path, file_name);
 
 
-    zval file_exists;
-    php_stat(path, strlen(path), FS_IS_FILE, &file_exists);
-
-    if (Z_TYPE(file_exists) == IS_FALSE) {
+    if (file_exists(path) == XLSWRITER_FALSE) {
         efree(path);
         efree(path);
-        zval_ptr_dtor(&file_exists);
         zend_throw_exception(vtiful_exception_ce, "File not found, please check the path in the config or file name", 121);
         zend_throw_exception(vtiful_exception_ce, "File not found, please check the path in the config or file name", 121);
         return NULL;
         return NULL;
     }
     }
 
 
     if ((file = xlsxioread_open(path)) == NULL) {
     if ((file = xlsxioread_open(path)) == NULL) {
         efree(path);
         efree(path);
-        zval_ptr_dtor(&file_exists);
         zend_throw_exception(vtiful_exception_ce, "Failed to open file", 100);
         zend_throw_exception(vtiful_exception_ce, "Failed to open file", 100);
         return NULL;
         return NULL;
     }
     }
 
 
     efree(path);
     efree(path);
-    zval_ptr_dtor(&file_exists);
     return file;
     return file;
 }
 }
 /* }}} */
 /* }}} */