Skip to content

Commit

Permalink
add sl_file_type
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Somerville committed Jan 27, 2013
1 parent 4bc894f commit 5ed0ea0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
10 changes: 10 additions & 0 deletions inc/slash/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 14 additions & 0 deletions platform/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 15 additions & 1 deletion platform/win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5ed0ea0

Please sign in to comment.