Skip to content

Commit 545d323

Browse files
committed
Feat: file and directory helper function
1 parent 719dc8e commit 545d323

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

include/help.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#ifndef PHP_EXT_XLS_EXPORT_HELP_H
1414
#define PHP_EXT_XLS_EXPORT_HELP_H
1515

16+
unsigned int file_exists(const char *path);
17+
unsigned int directory_exists(const char *path);
1618
zend_long date_double_to_timestamp(double value);
1719

1820
#endif //PHP_EXT_XLS_EXPORT_HELP_H

kernel/excel.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#include "xlswriter.h"
1414
#include "ext/date/php_date.h"
15-
#include "ext/standard/php_filestat.h"
1615

1716
zend_class_entry *vtiful_xls_ce;
1817

@@ -278,7 +277,7 @@ PHP_METHOD(vtiful_xls, __construct)
278277
PHP_METHOD(vtiful_xls, fileName)
279278
{
280279
char *sheet_name = NULL;
281-
zval file_path, dir_exists, *dir_path = NULL;
280+
zval file_path, *dir_path = NULL;
282281
zend_string *zs_file_name = NULL, *zs_sheet_name = NULL;
283282

284283
ZEND_PARSE_PARAMETERS_START(1, 2)
@@ -287,16 +286,13 @@ PHP_METHOD(vtiful_xls, fileName)
287286
Z_PARAM_STR(zs_sheet_name)
288287
ZEND_PARSE_PARAMETERS_END();
289288

290-
ZVAL_NULL(&dir_exists);
291289
ZVAL_COPY(return_value, getThis());
292290

293291
GET_CONFIG_PATH(dir_path, vtiful_xls_ce, return_value);
294292

295-
php_stat(ZSTR_VAL(Z_STR_P(dir_path)), strlen(ZSTR_VAL(Z_STR_P(dir_path))), FS_IS_DIR, &dir_exists);
296-
297-
if (Z_TYPE(dir_exists) == IS_FALSE) {
298-
zval_ptr_dtor(&dir_exists);
293+
if(directory_exists(ZSTR_VAL(Z_STR_P(dir_path))) == XLSWRITER_FALSE) {
299294
zend_throw_exception(vtiful_exception_ce, "Configure 'path' directory does not exist", 121);
295+
return;
300296
}
301297

302298
xls_object *obj = Z_XLS_P(getThis());
@@ -315,8 +311,6 @@ PHP_METHOD(vtiful_xls, fileName)
315311

316312
zval_ptr_dtor(&file_path);
317313
}
318-
319-
zval_ptr_dtor(&dir_exists);
320314
}
321315
/* }}} */
322316

kernel/help.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#include "xlswriter.h"
1414
#include "ext/date/php_date.h"
1515
#include "ext/standard/php_math.h"
16+
#include "ext/standard/php_filestat.h"
1617

18+
/* {{{ */
1719
zend_long date_double_to_timestamp(double value) {
1820
double days, partDay, hours, minutes, seconds;
1921

@@ -56,4 +58,35 @@ zend_long date_double_to_timestamp(double value) {
5658
zval_ptr_dtor(&_format_result);
5759

5860
return timestamp;
59-
}
61+
}
62+
/* }}} */
63+
64+
/* {{{ */
65+
unsigned int directory_exists(const char *path) {
66+
zval dir_exists;
67+
php_stat(path, strlen(path), FS_IS_DIR, &dir_exists);
68+
69+
if (Z_TYPE(dir_exists) == IS_FALSE) {
70+
zval_ptr_dtor(&dir_exists);
71+
return XLSWRITER_FALSE;
72+
}
73+
74+
zval_ptr_dtor(&dir_exists);
75+
return XLSWRITER_TRUE;
76+
}
77+
/* }}} */
78+
79+
/* {{{ */
80+
unsigned int file_exists(const char *path) {
81+
zval file_exists;
82+
php_stat(path, strlen(path), FS_IS_FILE, &file_exists);
83+
84+
if (Z_TYPE(file_exists) == IS_FALSE) {
85+
zval_ptr_dtor(&file_exists);
86+
return XLSWRITER_FALSE;
87+
}
88+
89+
zval_ptr_dtor(&file_exists);
90+
return XLSWRITER_TRUE;
91+
}
92+
/* }}} */

kernel/read.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
#include "xlswriter.h"
1414
#include "ext/date/php_date.h"
15-
#include "ext/standard/php_math.h"
16-
#include "ext/standard/php_filestat.h"
1715

1816
/* {{{ */
1917
xlsxioreader file_open(const char *directory, const char *file_name) {
@@ -24,25 +22,19 @@ xlsxioreader file_open(const char *directory, const char *file_name) {
2422
strcat(path, "/");
2523
strcat(path, file_name);
2624

27-
zval file_exists;
28-
php_stat(path, strlen(path), FS_IS_FILE, &file_exists);
29-
30-
if (Z_TYPE(file_exists) == IS_FALSE) {
25+
if (file_exists(path) == XLSWRITER_FALSE) {
3126
efree(path);
32-
zval_ptr_dtor(&file_exists);
3327
zend_throw_exception(vtiful_exception_ce, "File not found, please check the path in the config or file name", 121);
3428
return NULL;
3529
}
3630

3731
if ((file = xlsxioread_open(path)) == NULL) {
3832
efree(path);
39-
zval_ptr_dtor(&file_exists);
4033
zend_throw_exception(vtiful_exception_ce, "Failed to open file", 100);
4134
return NULL;
4235
}
4336

4437
efree(path);
45-
zval_ptr_dtor(&file_exists);
4638
return file;
4739
}
4840
/* }}} */

0 commit comments

Comments
 (0)