1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* win32/{dir.h, win32.c} (rb_w32_readdir_with_enc): new function to

read entry with specified enc.

	* win32/win32.c (readdir_internal): extract from rb_w32_opendir().

	* win32/win32.c (opendir_internal): extract from rb_w32_readdir().

	* dir.c (dir_read, dir_each): use new READDIR macro instead of readdir()
	  to pass enc for special version of readdir, such as above.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22087 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
usa 2009-02-06 08:23:00 +00:00
parent 03c7332241
commit 6343c99a2e
4 changed files with 115 additions and 37 deletions

View file

@ -1,3 +1,15 @@
Fri Feb 6 17:19:23 2009 NAKAMURA Usaku <usa@ruby-lang.org>
* win32/{dir.h, win32.c} (rb_w32_readdir_with_enc): new function to
read entry with specified enc.
* win32/win32.c (readdir_internal): extract from rb_w32_opendir().
* win32/win32.c (opendir_internal): extract from rb_w32_readdir().
* dir.c (dir_read, dir_each): use new READDIR macro instead of readdir()
to pass enc for special version of readdir, such as above.
Fri Feb 6 12:11:24 2009 NAKAMURA Usaku <usa@ruby-lang.org>
* ruby.c (process_options): set initial default_external before -r.

9
dir.c
View file

@ -474,6 +474,11 @@ dir_path(VALUE dir)
* d.read #=> ".."
* d.read #=> "config.h"
*/
#ifdef _WIN32
# define READDIR(dir, enc) rb_w32_readdir_with_enc(dir, enc)
#else
# define READDIR(dir, enc) readdir(dir)
#endif
static VALUE
dir_read(VALUE dir)
{
@ -482,7 +487,7 @@ dir_read(VALUE dir)
GetDIR(dir, dirp);
errno = 0;
dp = readdir(dirp->dir);
dp = READDIR(dirp->dir, dirp->enc);
if (dp) {
return rb_external_str_new_with_enc(dp->d_name, NAMLEN(dp), dirp->enc);
}
@ -521,7 +526,7 @@ dir_each(VALUE dir)
RETURN_ENUMERATOR(dir, 0, 0);
GetDIR(dir, dirp);
rewinddir(dirp->dir);
for (dp = readdir(dirp->dir); dp != NULL; dp = readdir(dirp->dir)) {
for (dp = READDIR(dirp->dir, dirp->enc); dp != NULL; dp = READDIR(dirp->dir, dirp->enc)) {
rb_yield(rb_external_str_new_with_enc(dp->d_name, NAMLEN(dp), dirp->enc));
if (dirp->dir == NULL) dir_closed();
}

View file

@ -29,6 +29,7 @@ typedef struct {
DIR* rb_w32_opendir(const char*);
struct direct* rb_w32_readdir(DIR *);
struct direct* rb_w32_readdir_with_enc(DIR *, rb_encoding *);
off_t rb_w32_telldir(DIR *);
void rb_w32_seekdir(DIR *, off_t);
void rb_w32_rewinddir(DIR *);

View file

@ -11,6 +11,7 @@
*/
#include "ruby/ruby.h"
#include "ruby/encoding.h"
#include "dln.h"
#include <fcntl.h>
#include <process.h>
@ -1471,31 +1472,15 @@ open_dir_handle(const char *filename, WIN32_FIND_DATAW *fd)
return fh;
}
DIR *
rb_w32_opendir(const char *filename)
static DIR *
opendir_internal(HANDLE fh, WIN32_FIND_DATAW *fd)
{
DIR *p;
long len;
long idx;
DIR *p;
long len;
long idx;
WCHAR *tmpW;
char *tmp;
struct stati64 sbuf;
WIN32_FIND_DATAW fd;
HANDLE fh;
//
// check to see if we've got a directory
//
if (rb_w32_stati64(filename, &sbuf) < 0)
return NULL;
if (!(sbuf.st_mode & S_IFDIR) &&
(!ISALPHA(filename[0]) || filename[1] != ':' || filename[2] != '\0' ||
((1 << (filename[0] & 0x5f) - 'A') & GetLogicalDrives()) == 0)) {
errno = ENOTDIR;
return NULL;
}
fh = open_dir_handle(filename, &fd);
if (fh == INVALID_HANDLE_VALUE) {
return NULL;
}
@ -1516,7 +1501,7 @@ rb_w32_opendir(const char *filename)
// of the previous string found.
//
do {
len = lstrlenW(fd.cFileName) + 1;
len = lstrlenW(fd->cFileName) + 1;
//
// bump the string table size by enough for the
@ -1532,7 +1517,7 @@ rb_w32_opendir(const char *filename)
}
p->start = tmpW;
memcpy(&p->start[idx], fd.cFileName, len * sizeof(WCHAR));
memcpy(&p->start[idx], fd->cFileName, len * sizeof(WCHAR));
if (p->nfiles % DIRENT_PER_CHAR == 0) {
tmp = realloc(p->bits, p->nfiles / DIRENT_PER_CHAR + 1);
@ -1541,20 +1526,43 @@ rb_w32_opendir(const char *filename)
p->bits = tmp;
p->bits[p->nfiles / DIRENT_PER_CHAR] = 0;
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
if (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
SetBit(p->bits, BitOfIsDir(p->nfiles));
if (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
if (fd->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
SetBit(p->bits, BitOfIsRep(p->nfiles));
p->nfiles++;
idx += len;
} while (FindNextFileW(fh, &fd));
} while (FindNextFileW(fh, fd));
FindClose(fh);
p->size = idx;
p->curr = p->start;
return p;
}
DIR *
rb_w32_opendir(const char *filename)
{
struct stati64 sbuf;
WIN32_FIND_DATAW fd;
HANDLE fh;
//
// check to see if we've got a directory
//
if (rb_w32_stati64(filename, &sbuf) < 0)
return NULL;
if (!(sbuf.st_mode & S_IFDIR) &&
(!ISALPHA(filename[0]) || filename[1] != ':' || filename[2] != '\0' ||
((1 << (filename[0] & 0x5f) - 'A') & GetLogicalDrives()) == 0)) {
errno = ENOTDIR;
return NULL;
}
fh = open_dir_handle(filename, &fd);
return opendir_internal(fh, &fd);
}
//
// Move to next entry
//
@ -1575,24 +1583,64 @@ move_to_next_entry(DIR *dirp)
// Readdir just returns the current string pointer and bumps the
// string pointer to the next entry.
//
static BOOL
win32_direct_conv(const WCHAR *file, struct direct *entry, rb_encoding *dummy)
{
UINT cp = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
entry->d_namlen =
WideCharToMultiByte(cp, 0, file, -1, NULL, 0, NULL, NULL) - 1;
if (!(entry->d_name = malloc(entry->d_namlen + 1)))
return FALSE;
WideCharToMultiByte(cp, 0, file, -1, entry->d_name, entry->d_namlen + 1,
NULL, NULL);
return TRUE;
}
struct direct *
rb_w32_readdir(DIR *dirp)
static BOOL
ruby_direct_conv(const WCHAR *file, struct direct *entry, rb_encoding *enc)
{
static rb_encoding *utf16 = (rb_encoding *)-1;
VALUE src;
VALUE dst;
VALUE opthash;
int ecflags;
VALUE ecopts;
if (utf16 == (rb_encoding *)-1) {
utf16 = rb_enc_find("UTF-16LE");
if (utf16 == rb_ascii8bit_encoding())
utf16 = NULL;
}
if (!utf16)
/* maybe miniruby */
return win32_direct_conv(file, entry, NULL);
src = rb_enc_str_new((char *)file, lstrlenW(file) * sizeof(WCHAR), utf16);
opthash = rb_hash_new();
rb_hash_aset(opthash, ID2SYM(rb_intern("undef")),
ID2SYM(rb_intern("replace")));
ecflags = rb_econv_prepare_opts(opthash, &ecopts);
dst = rb_str_encode(src, rb_enc_from_encoding(enc), ecflags, ecopts);
entry->d_namlen = RSTRING_LEN(dst);
if (!(entry->d_name = malloc(entry->d_namlen + 1)))
return FALSE;
memcpy(entry->d_name, RSTRING_PTR(dst), entry->d_namlen);
entry->d_name[entry->d_namlen] = '\0';
return TRUE;
}
static struct direct *
readdir_internal(DIR *dirp, BOOL (*conv)(const WCHAR *, struct direct *, rb_encoding *), rb_encoding *enc)
{
static int dummy = 0;
UINT cp = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
if (dirp->curr) {
//
// first set up the structure to return
//
dirp->dirstr.d_namlen =
WideCharToMultiByte(cp, 0, dirp->curr, -1, NULL, 0, NULL, NULL) - 1;
if (!(dirp->dirstr.d_name = malloc(dirp->dirstr.d_namlen + 1)))
return NULL;
WideCharToMultiByte(cp, 0, dirp->curr, -1, dirp->dirstr.d_name,
dirp->dirstr.d_namlen + 1, NULL, NULL);
conv(dirp->curr, &dirp->dirstr, enc);
//
// Fake inode
@ -1617,6 +1665,18 @@ rb_w32_readdir(DIR *dirp)
return NULL;
}
struct direct *
rb_w32_readdir(DIR *dirp)
{
return readdir_internal(dirp, win32_direct_conv, NULL);
}
struct direct *
rb_w32_readdir_with_enc(DIR *dirp, rb_encoding *enc)
{
return readdir_internal(dirp, ruby_direct_conv, enc);
}
//
// Telldir returns the current string pointer position
//