Merge pull request #11 from pbosetti/master

Can build on VisualStudio, revised.
This commit is contained in:
Tomoyuki Sahara
2014-03-14 10:41:56 +09:00
6 changed files with 168 additions and 29 deletions
+9 -5
View File
@@ -11,20 +11,24 @@ extern "C" {
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#if defined(_WIN32) || defined(_WIN64)
#include <winsock.h>
#else
#include <unistd.h>
#include <sys/wait.h>
#endif
#include <stdio.h>
#include <string.h>
#include <limits.h>
struct mrb_io {
int fd; /* file descriptor */
int fd2; /* file descriptor */
int pid; /* child's pid (for pipes) */
int fd; /* file descriptor */
int fd2; /* file descriptor */
int pid; /* child's pid (for pipes) */
};
struct mrb_io_type {
+8
View File
@@ -3,4 +3,12 @@ MRuby::Gem::Specification.new('mruby-io') do |spec|
spec.authors = 'Internet Initiative Japan Inc.'
spec.cc.include_paths << "#{build.root}/src"
case RUBY_PLATFORM
when /mingw|mswin/
spec.linker.libraries += ['Ws2_32']
#spec.cc.include_paths += ["C:/Windows/system/include"]
spec.linker.library_paths += ["C:/Windows/system"]
end
end
+19 -1
View File
@@ -12,9 +12,17 @@ class File < IO
if fd_or_path.kind_of? Fixnum
super(fd_or_path, mode)
else
if Object.const_defined? :Errno
eclass = [Errno::ENOENT, Errno::ENFILE]
else
eclass = FileError
end
@path = fd_or_path
begin
fd = IO.sysopen(@path, mode, perm)
rescue RuntimeError => e
raise FileError, "Could not open file (#{e})"
rescue Errno::EMFILE, Errno::ENFILE
GC.start
fd = IO.sysopen(@path, mode, perm)
@@ -56,7 +64,7 @@ class File < IO
def self.expand_path(path, default_dir = '.')
def concat_path(path, base_path)
if path[0] == "/"
if path[0] == "/" || path[1] == ':' # Windows root!
expanded_path = path
elsif path[0] == "~"
if (path[1] == "/" || path[1] == nil)
@@ -119,6 +127,16 @@ class File < IO
end
end
def self.foreach(file)
if block_given?
self.open(file) do |f|
f.each {|l| yield l}
end
else
return self.new(file)
end
end
def self.directory?(file)
FileTest.directory?(file)
end
+70 -16
View File
@@ -15,14 +15,27 @@
#include "mruby/error.h"
#endif
#include <sys/file.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <pwd.h>
#if defined(_WIN32) || defined(_WIN64)
#define UNLINK _unlink
#define GETCWD _getcwd
#define CHMOD(a, b) 0
#define MAXPATHLEN 1024
#define PATH_MAX MAX_PATH
#define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
#include <direct.h>
#else
#define UNLINK unlink
#define GETCWD getcwd
#define CHMOD(a, b) chmod(a,b)
#include <sys/file.h>
#include <libgen.h>
#include <sys/param.h>
#include <pwd.h>
#endif
#define FILE_SEPARATOR "/"
@@ -47,16 +60,21 @@ extern mrb_value mrb_io_fileno(mrb_state *mrb, mrb_value io);
mrb_value
mrb_file_s_umask(mrb_state *mrb, mrb_value klass)
{
int omask = 0;
#if defined(_WIN32) || defined(_WIN64)
/* nothing to do on windows */
#else
mrb_value *argv;
int argc;
mrb_value mask;
mrb_get_args(mrb, "*", &argv, &argc);
int omask = 0;
if (argc == 0) {
omask = umask(0);
umask(omask);
} else if (argc == 1) {
mrb_value mask = argv[0];
mask = argv[0];
if (!mrb_nil_p(mask) && !mrb_fixnum_p(mask)) {
mask = mrb_check_convert_type(mrb, mask, MRB_TT_FIXNUM, "Fixnum", "to_int");
}
@@ -67,6 +85,7 @@ mrb_file_s_umask(mrb_state *mrb, mrb_value klass)
} else {
mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%d for 0..1)", argc);
}
#endif
return mrb_fixnum_value(omask);
}
@@ -75,15 +94,17 @@ mrb_file_s_unlink(mrb_state *mrb, mrb_value obj)
{
mrb_value *argv;
int n, i, argc;
const char *path;
mrb_value pathv;
mrb_get_args(mrb, "*", &argv, &argc);
for (i = 0, n = 0; i < argc; i++) {
mrb_value pathv = argv[i];
pathv = argv[i];
if (mrb_type(pathv) != MRB_TT_STRING) {
mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %s into String", mrb_obj_classname(mrb, pathv));
}
const char *path = mrb_string_value_cstr(mrb, &pathv);;
if (unlink(path) < 0) {
path = mrb_string_value_cstr(mrb, &pathv);;
if (UNLINK(path) < 0) {
mrb_sys_fail(mrb, path);
} else {
n++;
@@ -100,8 +121,8 @@ mrb_file_rename_internal(mrb_state *mrb, mrb_value from, mrb_value to)
dst = mrb_string_value_cstr(mrb, &to);
if (rename(src, dst) < 0) {
if (chmod(dst, 0666) == 0 &&
unlink(dst) == 0 &&
if (CHMOD(dst, 0666) == 0 &&
UNLINK(dst) == 0 &&
rename(src, dst) == 0)
return mrb_fixnum_value(0);
mrb_sys_fail(mrb, "mrb_file_rename_internal failed.");
@@ -137,29 +158,53 @@ mrb_file_s_rename(mrb_state *mrb, mrb_value obj)
static mrb_value
mrb_file_dirname(mrb_state *mrb, mrb_value klass)
{
char *dname, *path;
#if defined(_WIN32) || defined(_WIN64)
char dname[_MAX_DIR], vname[_MAX_DRIVE];
char buffer[_MAX_DRIVE + _MAX_DIR];
char *path;
mrb_value s;
mrb_get_args(mrb, "S", &s);
path = mrb_str_to_cstr(mrb, s);
_splitpath((const char*)path, vname, dname, NULL, NULL);
sprintf_s(buffer, _MAX_DRIVE + _MAX_DIR, "%s%s", vname, dname);
return mrb_str_new_cstr(mrb, buffer);
#else
char *dname, *path;
mrb_value s;
mrb_get_args(mrb, "S", &s);
path = mrb_str_to_cstr(mrb, s);
if ((dname = dirname(path)) == NULL) {
mrb_sys_fail(mrb, "dirname");
}
#endif
return mrb_str_new_cstr(mrb, dname);
}
static mrb_value
mrb_file_basename(mrb_state *mrb, mrb_value klass)
{
#if defined(_WIN32) || defined(_WIN64)
char bname[_MAX_DIR];
char extname[_MAX_EXT];
char *path;
char buffer[_MAX_DIR + _MAX_EXT];
mrb_value s;
mrb_get_args(mrb, "S", &s);
path = mrb_str_to_cstr(mrb, s);
_splitpath((const char*)path, NULL, NULL, bname, extname);
sprintf_s(buffer, _MAX_DIR + _MAX_EXT, "%s%s", bname, extname);
return mrb_str_new_cstr(mrb, buffer);
#else
char *bname, *path;
mrb_value s;
mrb_get_args(mrb, "S", &s);
path = mrb_str_to_cstr(mrb, s);
if ((bname = basename(path)) == NULL) {
mrb_sys_fail(mrb, "basename");
}
return mrb_str_new_cstr(mrb, bname);
#endif
}
static mrb_value
@@ -218,7 +263,7 @@ mrb_file__getwd(mrb_state *mrb, mrb_value klass)
mrb_value path;
path = mrb_str_buf_new(mrb, MAXPATHLEN);
if (getcwd(RSTRING_PTR(path), MAXPATHLEN) == NULL) {
if (GETCWD(RSTRING_PTR(path), MAXPATHLEN) == NULL) {
mrb_sys_fail(mrb, "getcwd(2)");
}
mrb_str_resize(mrb, path, strlen(RSTRING_PTR(path)));
@@ -234,6 +279,7 @@ mrb_file_is_absolute_path(const char *path)
static mrb_value
mrb_file__gethome(mrb_state *mrb, mrb_value klass)
{
#ifndef _WIN32
mrb_value username;
int argc;
char *home;
@@ -259,8 +305,13 @@ mrb_file__gethome(mrb_state *mrb, mrb_value klass)
}
}
return mrb_str_new_cstr(mrb, home);
#else
return mrb_nil_value();
#endif
}
#ifndef _WIN32
mrb_value
mrb_file_flock(mrb_state *mrb, mrb_value self)
{
@@ -290,6 +341,7 @@ mrb_file_flock(mrb_state *mrb, mrb_value self)
}
return mrb_fixnum_value(0);
}
#endif
void
mrb_init_file(mrb_state *mrb)
@@ -311,8 +363,10 @@ mrb_init_file(mrb_state *mrb)
mrb_define_class_method(mrb, file, "_getwd", mrb_file__getwd, MRB_ARGS_NONE());
mrb_define_class_method(mrb, file, "_gethome", mrb_file__gethome, MRB_ARGS_OPT(1));
#ifndef _WIN32
mrb_define_method(mrb, file, "flock", mrb_file_flock, MRB_ARGS_REQ(1));
#endif
cnst = mrb_define_module_under(mrb, file, "Constants");
mrb_define_const(mrb, cnst, "LOCK_SH", mrb_fixnum_value(LOCK_SH));
mrb_define_const(mrb, cnst, "LOCK_EX", mrb_fixnum_value(LOCK_EX));
+10 -5
View File
@@ -15,13 +15,18 @@
#include "mruby/error.h"
#endif
#include <sys/file.h>
#include <libgen.h>
#if defined(_WIN32) || defined(_WIN64)
#define LSTAT stat
#else
#define LSTAT lstat
#include <sys/file.h>
#include <libgen.h>
#include <pwd.h>
#include <sys/param.h>
#endif
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <pwd.h>
extern struct mrb_data_type mrb_io_type;
@@ -50,7 +55,7 @@ mrb_stat0(mrb_state *mrb, mrb_value obj, struct stat *st, int do_lstat)
tmp = mrb_funcall(mrb, obj, "is_a?", 1, str_klass);
if (mrb_test(tmp)) {
if (do_lstat) {
return lstat(mrb_str_to_cstr(mrb, obj), st);
return LSTAT(mrb_str_to_cstr(mrb, obj), st);
} else {
return stat(mrb_str_to_cstr(mrb, obj), st);
}
+52 -2
View File
@@ -17,6 +17,16 @@
#include "mruby/error.h"
#endif
#if defined(_WIN32) || defined(_WIN64)
#include <io.h>
#define open _open
#define close _close
#define read _read
#define write _write
#define lseek _lseek
#else
#endif
static int mrb_io_modestr_to_flags(mrb_state *mrb, const char *modestr);
static int mrb_io_flags_to_modenum(mrb_state *mrb, int flags);
@@ -66,6 +76,41 @@ mrb_io_modestr_to_flags(mrb_state *mrb, const char *mode)
return flags;
}
static int
mrb_io_modenum_to_flags(mrb_state *mrb, int modenum)
{
int flags = 0;
switch (modenum & (O_RDONLY|O_WRONLY|O_RDWR)) {
case O_RDONLY:
flags = FMODE_READABLE;
break;
case O_WRONLY:
flags = FMODE_WRITABLE;
break;
case O_RDWR:
flags = FMODE_READWRITE;
break;
}
if (modenum & O_APPEND) {
flags |= FMODE_APPEND;
}
if (modenum & O_TRUNC) {
flags |= FMODE_TRUNC;
}
if (modenum & O_CREAT) {
flags |= FMODE_CREATE;
}
#ifdef O_BINARY
if (modenum & O_BINARY) {
flags |= FMODE_BINMODE;
}
#endif
return flags;
}
static int
mrb_io_flags_to_modenum(mrb_state *mrb, int flags)
{
@@ -94,13 +139,14 @@ mrb_io_flags_to_modenum(mrb_state *mrb, int flags)
}
#ifdef O_BINARY
if (flags & FMODE_BINMODE) {
modenum |= O_BINARY
modenum |= O_BINARY;
}
#endif
return modenum;
}
#ifndef _WIN32
static int
mrb_proc_exec(const char *pname)
{
@@ -118,6 +164,7 @@ mrb_proc_exec(const char *pname)
execl("/bin/sh", "sh", "-c", pname, (char *)NULL);
return -1;
}
#endif
static void
mrb_io_free(mrb_state *mrb, void *ptr)
@@ -160,6 +207,7 @@ io_open(mrb_state *mrb, mrb_value path, int flags, int perm)
#define NOFILE 64
#endif
#ifndef _WIN32
mrb_value
mrb_io_s_popen(mrb_state *mrb, mrb_value klass)
{
@@ -270,6 +318,7 @@ mrb_io_s_popen(mrb_state *mrb, mrb_value klass)
}
return result;
}
#endif
mrb_value
mrb_io_initialize(mrb_state *mrb, mrb_value io)
@@ -717,9 +766,10 @@ mrb_init_io(mrb_state *mrb)
MRB_SET_INSTANCE_TT(io, MRB_TT_DATA);
mrb_include_module(mrb, io, mrb_module_get(mrb, "Enumerable")); /* 15.2.20.3 */
#ifndef _WIN32
mrb_define_class_method(mrb, io, "_popen", mrb_io_s_popen, MRB_ARGS_ANY());
mrb_define_class_method(mrb, io, "_sysclose", mrb_io_s_sysclose, MRB_ARGS_REQ(1));
#endif
mrb_define_class_method(mrb, io, "for_fd", mrb_io_s_for_fd, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(2));
mrb_define_class_method(mrb, io, "select", mrb_io_s_select, MRB_ARGS_ANY());
mrb_define_class_method(mrb, io, "sysopen", mrb_io_s_sysopen, MRB_ARGS_ANY());