[前][次][番号順一覧][スレッド一覧]

ruby-changes:10533

From: usa <ko1@a...>
Date: Fri, 6 Feb 2009 17:25:28 +0900 (JST)
Subject: [ruby-changes:10533] Ruby:r22087 (trunk): * win32/{dir.h, win32.c} (rb_w32_readdir_with_enc): new function to

usa	2009-02-06 17:23:00 +0900 (Fri, 06 Feb 2009)

  New Revision: 22087

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=22087

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/dir.c
    trunk/win32/dir.h
    trunk/win32/win32.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 22086)
+++ ChangeLog	(revision 22087)
@@ -1,3 +1,15 @@
+Fri Feb  6 17:19:23 2009  NAKAMURA Usaku  <usa@r...>
+
+	* 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@r...>
 
 	* ruby.c (process_options): set initial default_external before -r.
Index: dir.c
===================================================================
--- dir.c	(revision 22086)
+++ dir.c	(revision 22087)
@@ -474,6 +474,11 @@
  *     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 @@
 
     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 @@
     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();
     }
Index: win32/win32.c
===================================================================
--- win32/win32.c	(revision 22086)
+++ win32/win32.c	(revision 22087)
@@ -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 @@
     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 @@
     // 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 @@
 	}
 
 	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 @@
 	    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 @@
 // 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 @@
 	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
 //
Index: win32/dir.h
===================================================================
--- win32/dir.h	(revision 22086)
+++ win32/dir.h	(revision 22087)
@@ -29,6 +29,7 @@
 
 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 *);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]