From 97685355825fc169440cd44d3c24f040559a757c Mon Sep 17 00:00:00 2001 From: Paolo Bosetti Date: Tue, 10 Dec 2013 14:33:36 +0100 Subject: [PATCH] Made compatible with VisualStudio --- mrbgem.rake | 7 +++ src/Win/dirent.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++ src/Win/dirent.h | 50 ++++++++++++++++ src/dir.c | 28 ++++++++- 4 files changed, 230 insertions(+), 3 deletions(-) create mode 100644 src/Win/dirent.c create mode 100644 src/Win/dirent.h diff --git a/mrbgem.rake b/mrbgem.rake index 670298049..fd5fe3781 100644 --- a/mrbgem.rake +++ b/mrbgem.rake @@ -3,4 +3,11 @@ MRuby::Gem::Specification.new('mruby-dir') do |spec| spec.authors = 'Internet Initiative Japan Inc.' spec.cc.include_paths << "#{build.root}/src" + + case RUBY_PLATFORM + when /mingw|mswin/ + spec.objs += Dir.glob("#{dir}/src/Win/*.{c,cpp,m,asm,S}").map { |f| + objfile(f.relative_path_from(dir).pathmap("#{build_dir}/%X")) + } + end end diff --git a/src/Win/dirent.c b/src/Win/dirent.c new file mode 100644 index 000000000..12a2547bf --- /dev/null +++ b/src/Win/dirent.c @@ -0,0 +1,148 @@ +/* + + Implementation of POSIX directory browsing functions and types for Win32. + + Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com) + History: Created March 1997. Updated June 2003 and July 2012. + Rights: See end of file. + +*/ + +#include "dirent.h" +#include +#include /* _findfirst and _findnext set errno iff they return -1 */ +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef ptrdiff_t handle_type; /* C99's intptr_t not sufficiently portable */ + +struct DIR +{ + handle_type handle; /* -1 for failed rewind */ + struct _finddata_t info; + struct dirent result; /* d_name null iff first time */ + char *name; /* null-terminated char string */ +}; + +DIR *opendir(const char *name) +{ + DIR *dir = 0; + + if(name && name[0]) + { + size_t base_length = strlen(name); + const char *all = /* search pattern must end with suitable wildcard */ + strchr("/\\", name[base_length - 1]) ? "*" : "/*"; + + if((dir = (DIR *) malloc(sizeof *dir)) != 0 && + (dir->name = (char *) malloc(base_length + strlen(all) + 1)) != 0) + { + strcat(strcpy(dir->name, name), all); + + if((dir->handle = + (handle_type) _findfirst(dir->name, &dir->info)) != -1) + { + dir->result.d_name = 0; + } + else /* rollback */ + { + free(dir->name); + free(dir); + dir = 0; + } + } + else /* rollback */ + { + free(dir); + dir = 0; + errno = ENOMEM; + } + } + else + { + errno = EINVAL; + } + + return dir; +} + +int closedir(DIR *dir) +{ + int result = -1; + + if(dir) + { + if(dir->handle != -1) + { + result = _findclose(dir->handle); + } + + free(dir->name); + free(dir); + } + + if(result == -1) /* map all errors to EBADF */ + { + errno = EBADF; + } + + return result; +} + +struct dirent *readdir(DIR *dir) +{ + struct dirent *result = 0; + + if(dir && dir->handle != -1) + { + if(!dir->result.d_name || _findnext(dir->handle, &dir->info) != -1) + { + result = &dir->result; + result->d_name = dir->info.name; + } + } + else + { + errno = EBADF; + } + + return result; +} + +void rewinddir(DIR *dir) +{ + if(dir && dir->handle != -1) + { + _findclose(dir->handle); + dir->handle = (handle_type) _findfirst(dir->name, &dir->info); + dir->result.d_name = 0; + } + else + { + errno = EBADF; + } +} + +#ifdef __cplusplus +} +#endif + +/* + + Copyright Kevlin Henney, 1997, 2003, 2012. All rights reserved. + + Permission to use, copy, modify, and distribute this software and its + documentation for any purpose is hereby granted without fee, provided + that this copyright and permissions notice appear in all copies and + derivatives. + + This software is supplied "as is" without express or implied warranty. + + But that said, if there are any problems please get in touch. + +*/ diff --git a/src/Win/dirent.h b/src/Win/dirent.h new file mode 100644 index 000000000..a02a0d828 --- /dev/null +++ b/src/Win/dirent.h @@ -0,0 +1,50 @@ +#ifndef DIRENT_INCLUDED +#define DIRENT_INCLUDED + +/* + + Declaration of POSIX directory browsing functions and types for Win32. + + Author: Kevlin Henney (kevlin@acm.org, kevlin@curbralan.com) + History: Created March 1997. Updated June 2003. + Rights: See end of file. + +*/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef struct DIR DIR; + +struct dirent +{ + char *d_name; +}; + +DIR *opendir(const char *); +int closedir(DIR *); +struct dirent *readdir(DIR *); +void rewinddir(DIR *); + +/* + + Copyright Kevlin Henney, 1997, 2003. All rights reserved. + + Permission to use, copy, modify, and distribute this software and its + documentation for any purpose is hereby granted without fee, provided + that this copyright and permissions notice appear in all copies and + derivatives. + + This software is supplied "as is" without express or implied warranty. + + But that said, if there are any problems please get in touch. + +*/ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/dir.c b/src/dir.c index 19bb5834f..e80a5886e 100644 --- a/src/dir.c +++ b/src/dir.c @@ -10,14 +10,26 @@ #include "mruby/string.h" #include "error.h" #include -#include +#if defined(_WIN32) || defined(_WIN64) + #define MAXPATHLEN 1024 + #define PATH_MAX MAX_PATH + #define S_ISDIR(B) ((B)&_S_IFDIR) + #include "Win/dirent.h" + #include + #define rmdir _rmdir + #define getcwd _getcwd + #define mkdir _mkdir + #define chdir _chdir +#else + #include + #include + #include +#endif #include -#include #include #include #include #include -#include #include /* with/without IO module */ @@ -202,6 +214,10 @@ mrb_dir_rewind(mrb_state *mrb, mrb_value self) mrb_value mrb_dir_seek(mrb_state *mrb, mrb_value self) { + #if defined(_WIN32) || (_WIN64) + mrb_raise(mrb, E_RUNTIME_ERROR, "dirseek() unreliable on Win platforms"); + return self; + #else struct mrb_dir *mdir; mrb_int pos; @@ -213,11 +229,16 @@ mrb_dir_seek(mrb_state *mrb, mrb_value self) mrb_get_args(mrb, "i", &pos); seekdir(mdir->dir, (long)pos); return self; + #endif } mrb_value mrb_dir_tell(mrb_state *mrb, mrb_value self) { + #if defined(_WIN32) || (_WIN64) + mrb_raise(mrb, E_RUNTIME_ERROR, "dirtell() unreliable on Win platforms"); + return mrb_fixnum_value(0); + #else struct mrb_dir *mdir; mrb_int pos; @@ -228,6 +249,7 @@ mrb_dir_tell(mrb_state *mrb, mrb_value self) } pos = (mrb_int)telldir(mdir->dir); return mrb_fixnum_value(pos); + #endif } void