Commit b1604e89
b1604e893600Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
Merge pull request #569 from ydah/add-file-directory-predicates
feat(runtime): add File.directory? and File.file? predicates
Details
- Commit
- b1604e893600
- Parent
- 122dce635444
- Merge parent
- 38eba378250b
lib/sp_runtime.hmodifiedView file
@@ -25,6 +25,7 @@
2525
#include <time.h>2626
#include <setjmp.h>2727
#include <errno.h>28+
#include <sys/stat.h>2829
#ifdef _WIN322930
#include <windows.h>3031
#include <process.h>@@ -52,6 +53,12 @@
5253
#ifndef MAP_ANONYMOUS5354
#define MAP_ANONYMOUS MAP_ANON5455
#endif56+
#ifndef S_ISDIR57+
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)58+
#endif59+
#ifndef S_ISREG60+
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)61+
#endif5562
5663
typedef int64_t mrb_int;5764
typedef double mrb_float;@@ -1692,6 +1699,43 @@
16921699
static volatile int sp_catch_top = 0;16931700
static void sp_throw(const char *tag, mrb_int val) { int i = sp_catch_top - 1; while (i >= 0) { if (strcmp(sp_catch_tag[i], tag) == 0) { sp_catch_val[i] = val; sp_catch_top = i + 1; longjmp(sp_catch_stack[i], 1); } i--; } fprintf(stderr, "uncaught throw: %s\n", tag); exit(1); }16941701
1702+
#ifdef _WIN321703+
static DWORD sp_file_attributes(const char *path) {1704+
if (!path) return INVALID_FILE_ATTRIBUTES;1705+
int len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, NULL, 0);1706+
if (len <= 0) return INVALID_FILE_ATTRIBUTES;1707+
wchar_t *wpath = (wchar_t *)malloc(sizeof(wchar_t) * (size_t)len);1708+
if (!wpath) return INVALID_FILE_ATTRIBUTES;1709+
if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, wpath, len) <= 0) {1710+
free(wpath);1711+
return INVALID_FILE_ATTRIBUTES;1712+
}1713+
DWORD attrs = GetFileAttributesW(wpath);1714+
free(wpath);1715+
return attrs;1716+
}1717+
1718+
static mrb_bool sp_file_directory(const char *path) {1719+
DWORD attrs = sp_file_attributes(path);1720+
return attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY);1721+
}1722+
1723+
static mrb_bool sp_file_file(const char *path) {1724+
DWORD attrs = sp_file_attributes(path);1725+
return attrs != INVALID_FILE_ATTRIBUTES && !(attrs & FILE_ATTRIBUTE_DIRECTORY);1726+
}1727+
#else1728+
static mrb_bool sp_file_directory(const char *path) {1729+
struct stat st;1730+
return path && stat(path, &st) == 0 && S_ISDIR(st.st_mode);1731+
}1732+
1733+
static mrb_bool sp_file_file(const char *path) {1734+
struct stat st;1735+
return path && stat(path, &st) == 0 && S_ISREG(st.st_mode);1736+
}1737+
#endif1738+
16951739
/* Text mode ("r") matches CRuby's File.read: on Windows, CRLF is16961740
normalized to LF on read, which cancels out fopen("w")'s16971741
LF→CRLF on write. Without this, content from File.read passed tospinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/file_directory_predicates.rbaddedView file
Loading diff…
test/file_directory_predicates.rb.expectedaddedView file
Loading diff…