From 5ed0ea0310714590080fef75b39ccb002a60223e Mon Sep 17 00:00:00 2001 From: Charlie Somerville Date: Mon, 28 Jan 2013 00:40:22 +1100 Subject: [PATCH] add sl_file_type --- inc/slash/platform.h | 10 ++++++++++ platform/posix.c | 14 ++++++++++++++ platform/win32.c | 16 +++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/inc/slash/platform.h b/inc/slash/platform.h index 2ef56b5f..26bdf558 100644 --- a/inc/slash/platform.h +++ b/inc/slash/platform.h @@ -8,9 +8,19 @@ struct sl_vm; char* sl_realpath(struct sl_vm* vm, char* path); +typedef enum sl_file_type { + SL_FT_NO_EXIST, + SL_FT_FILE, + SL_FT_DIR +} +sl_file_type_t; + int sl_file_exists(struct sl_vm* vm, char* path); +sl_file_type_t +sl_file_type(struct sl_vm* vm, char* path); + int sl_abs_file_exists(char* path); diff --git a/platform/posix.c b/platform/posix.c index f1ca36bf..3ecfaded 100644 --- a/platform/posix.c +++ b/platform/posix.c @@ -39,6 +39,20 @@ sl_file_exists(sl_vm_t* vm, char* path) return !stat(sl_realpath(vm, path), &s); } +sl_file_type_t +sl_file_type(struct sl_vm* vm, char* path) +{ + struct stat s; + if(stat(sl_realpath(vm, path), &s)) { + return SL_FT_NO_EXIST; + } + if(S_ISDIR(s.st_mode)) { + return SL_FT_DIR; + } else { + return SL_FT_FILE; + } +} + int sl_abs_file_exists(char* path) { struct stat s; diff --git a/platform/win32.c b/platform/win32.c index 0d2c2094..48000c11 100644 --- a/platform/win32.c +++ b/platform/win32.c @@ -34,7 +34,21 @@ int sl_file_exists(sl_vm_t* vm, char* path) { DWORD dwAttrib = GetFileAttributes(sl_realpath(vm, path)); - return (dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); + return dwAttrib != INVALID_FILE_ATTRIBUTES; +} + +sl_file_type_t +sl_file_type(struct sl_vm* vm, char* path +{ + DWORD attrib = GetFileAttributes(sl_realpath(vm, path)); + if(attrib == INVALID_FILE_ATTRIBUTES) { + return SL_FT_NO_EXIST; + } + if(attrib & FILE_ATTRIBUTE_DIRECTORY) { + return SL_FT_DIR; + } else { + return SL_FT_FILE; + } } int