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

ruby-changes:5867

From: naruse <ko1@a...>
Date: Tue, 17 Jun 2008 07:31:32 +0900 (JST)
Subject: [ruby-changes:5867] Ruby:r17374 (trunk): * dir.c (dir_data): add intenc and extenc.

naruse	2008-06-17 07:29:05 +0900 (Tue, 17 Jun 2008)

  New Revision: 17374

  Modified files:
    trunk/ChangeLog
    trunk/dir.c

  Log:
    * dir.c (dir_data): add intenc and extenc.
    
    * dir.c (dir_s_alloc): ditto.
    
    * dir.c (dir_initialize): now accept internal_encoding and
      exernal_encoding.
    
    * dir.c (dir_s_open): changed for dir_initialize.
    
    * dir.c (dir_open_dir): ditto.
    
    * dir.c (dir_foreach): changed for dir_oepn_dir.
    
    * dir.c (dir_entries): changed for dir_oepn_dir.
    
    * dir.c (dir_enc_str): defined.
    
    * dir.c (dir_path): use dir_enc_str.
    
    * dir.c (dir_read): ditto.
    
    * dir.c (dir_each): ditto.

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=17374&r2=17373&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/dir.c?r1=17374&r2=17373&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 17373)
+++ ChangeLog	(revision 17374)
@@ -1,3 +1,28 @@
+Tue Jun 17 06:32:55 2008  NARUSE, Yui  <naruse@r...>
+
+	* dir.c (dir_data): add intenc and extenc.
+
+	* dir.c (dir_s_alloc): ditto.
+
+	* dir.c (dir_initialize): now accept internal_encoding and
+	  exernal_encoding.
+
+	* dir.c (dir_s_open): changed for dir_initialize.
+
+	* dir.c (dir_open_dir): ditto.
+	
+	* dir.c (dir_foreach): changed for dir_oepn_dir.
+
+	* dir.c (dir_entries): changed for dir_oepn_dir.
+
+	* dir.c (dir_enc_str): defined.
+
+	* dir.c (dir_path): use dir_enc_str.
+
+	* dir.c (dir_read): ditto.
+
+	* dir.c (dir_each): ditto.
+
 Tue Jun 17 06:28:57 2008  NARUSE, Yui  <naruse@r...>
 
 	* io.c (io_set_encoding): defined.
Index: dir.c
===================================================================
--- dir.c	(revision 17373)
+++ dir.c	(revision 17374)
@@ -12,6 +12,7 @@
 **********************************************************************/
 
 #include "ruby/ruby.h"
+#include "ruby/encoding.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -342,6 +343,8 @@
 struct dir_data {
     DIR *dir;
     char *path;
+    rb_encoding *intenc;
+    rb_encoding *extenc;
 };
 
 static void
@@ -364,6 +367,8 @@
 
     dirp->dir = NULL;
     dirp->path = NULL;
+    dirp->intenc = NULL;
+    dirp->extenc = NULL;
 
     return obj;
 }
@@ -375,16 +380,71 @@
  *  Returns a new directory object for the named directory.
  */
 static VALUE
-dir_initialize(VALUE dir, VALUE dirname)
+dir_initialize(int argc, VALUE *argv, VALUE dir)
 {
     struct dir_data *dp;
+    static rb_encoding *fs_encoding;
+    rb_encoding  *intencoding, *extencoding;
+    VALUE dirname, opt;
+    static VALUE sym_intenc, sym_extenc;
 
+    if (!sym_intenc) {
+	sym_intenc = ID2SYM(rb_intern("internal_encoding"));
+	sym_extenc = ID2SYM(rb_intern("external_encoding"));
+	fs_encoding = rb_filesystem_encoding();
+    }
+
+    intencoding = NULL;
+    extencoding = fs_encoding;
+    rb_scan_args(argc, argv, "11", &dirname, &opt);
+
+    if (!NIL_P(opt)) {
+        VALUE v, extenc=Qnil, intenc=Qnil;
+        opt = rb_check_convert_type(opt, T_HASH, "Hash", "to_hash");
+
+        v = rb_hash_aref(opt, sym_intenc);
+        if (!NIL_P(v)) intenc = v;
+        v = rb_hash_aref(opt, sym_extenc);
+        if (!NIL_P(v)) extenc = v;
+
+	if (!NIL_P(extenc)) {
+	    extencoding = rb_to_encoding(extenc);
+	    if (!NIL_P(intenc)) {
+		intencoding = rb_to_encoding(intenc);
+		if (extencoding == intencoding) {
+		    rb_warn("Ignoring internal encoding '%s': it is identical to external encoding '%s'",
+			    RSTRING_PTR(rb_inspect(intenc)),
+			    RSTRING_PTR(rb_inspect(extenc)));
+		    intencoding = NULL;
+		}
+	    }
+	}
+	else if (!NIL_P(intenc)) {
+	    rb_raise(rb_eArgError, "External encoding must be specified when internal encoding is given");
+	}
+    }
+
+    {
+	rb_encoding  *dirname_encoding = rb_enc_get(dirname);
+	if (rb_usascii_encoding() != dirname_encoding
+	    && rb_ascii8bit_encoding() != dirname_encoding
+#if defined __APPLE__
+	    && rb_utf8_encoding() != dirname_encoding
+#endif
+	    && extencoding != dirname_encoding) {
+	    if (!intencoding) intencoding = dirname_encoding;
+	    dirname = rb_str_transcode(dirname, rb_enc_from_encoding(extencoding));
+	}
+    }
     FilePathValue(dirname);
+
     Data_Get_Struct(dir, struct dir_data, dp);
     if (dp->dir) closedir(dp->dir);
     if (dp->path) xfree(dp->path);
     dp->dir = NULL;
     dp->path = NULL;
+    dp->intenc = intencoding;
+    dp->extenc = extencoding;
     dp->dir = opendir(RSTRING_PTR(dirname));
     if (dp->dir == NULL) {
 	if (errno == EMFILE || errno == ENFILE) {
@@ -412,12 +472,12 @@
  *  block.
  */
 static VALUE
-dir_s_open(VALUE klass, VALUE dirname)
+dir_s_open(int argc, VALUE *argv, VALUE klass)
 {
     struct dir_data *dp;
     VALUE dir = Data_Make_Struct(klass, struct dir_data, 0, free_dir, dp);
 
-    dir_initialize(dir, dirname);
+    dir_initialize(argc, argv, dir);
     if (rb_block_given_p()) {
 	return rb_ensure(rb_yield, dir, dir_close, dir);
     }
@@ -445,6 +505,16 @@
     if (dirp->dir == NULL) dir_closed();\
 } while (0)
 
+static VALUE
+dir_enc_str(VALUE str, struct dir_data *dirp)
+{
+    rb_enc_associate(str, dirp->extenc);
+    if (dirp->intenc) {
+        str = rb_str_transcode(str, rb_enc_from_encoding(dirp->intenc));
+    }
+    return str;
+}
+
 /*
  *  call-seq:
  *     dir.inspect => string
@@ -483,7 +553,7 @@
 
     Data_Get_Struct(dir, struct dir_data, dirp);
     if (!dirp->path) return Qnil;
-    return rb_str_new2(dirp->path);
+    return dir_enc_str(rb_str_new2(dirp->path), dirp);
 }
 
 /*
@@ -508,7 +578,7 @@
     errno = 0;
     dp = readdir(dirp->dir);
     if (dp) {
-	return rb_tainted_str_new(dp->d_name, NAMLEN(dp));
+	return dir_enc_str(rb_tainted_str_new(dp->d_name, NAMLEN(dp)), dirp);
     }
     else if (errno == 0) {	/* end of stream */
 	return Qnil;
@@ -546,7 +616,7 @@
     GetDIR(dir, dirp);
     rewinddir(dirp->dir);
     for (dp = readdir(dirp->dir); dp != NULL; dp = readdir(dirp->dir)) {
-	rb_yield(rb_tainted_str_new(dp->d_name, NAMLEN(dp)));
+	rb_yield(dir_enc_str(rb_tainted_str_new(dp->d_name, NAMLEN(dp)), dirp));
 	if (dirp->dir == NULL) dir_closed();
     }
     return dir;
@@ -1686,9 +1756,9 @@
 }
 
 static VALUE
-dir_open_dir(VALUE path)
+dir_open_dir(int argc, VALUE *argv)
 {
-    VALUE dir = rb_funcall(rb_cDir, rb_intern("open"), 1, path);
+    VALUE dir = rb_funcall2(rb_cDir, rb_intern("open"), argc, argv);
 
     if (TYPE(dir) != T_DATA ||
 	RDATA(dir)->dfree != (RUBY_DATA_FUNC)free_dir) {
@@ -1717,12 +1787,12 @@
  *
  */
 static VALUE
-dir_foreach(VALUE io, VALUE dirname)
+dir_foreach(int argc, VALUE *argv, VALUE io)
 {
     VALUE dir;
 
-    RETURN_ENUMERATOR(io, 1, &dirname);
-    dir = dir_open_dir(dirname);
+    RETURN_ENUMERATOR(io, argc, argv);
+    dir = dir_open_dir(argc, argv);
     rb_ensure(dir_each, dir, dir_close, dir);
     return Qnil;
 }
@@ -1739,11 +1809,11 @@
  *
  */
 static VALUE
-dir_entries(VALUE io, VALUE dirname)
+dir_entries(int argc, VALUE *argv, VALUE io)
 {
     VALUE dir;
 
-    dir = dir_open_dir(dirname);
+    dir = dir_open_dir(argc, argv);
     return rb_ensure(rb_Array, dir, dir_close, dir);
 }
 
@@ -1868,11 +1938,11 @@
     rb_include_module(rb_cDir, rb_mEnumerable);
 
     rb_define_alloc_func(rb_cDir, dir_s_alloc);
-    rb_define_singleton_method(rb_cDir, "open", dir_s_open, 1);
+    rb_define_singleton_method(rb_cDir, "open", dir_s_open, -1);
     rb_define_singleton_method(rb_cDir, "foreach", dir_foreach, 1);
-    rb_define_singleton_method(rb_cDir, "entries", dir_entries, 1);
+    rb_define_singleton_method(rb_cDir, "entries", dir_entries, -1);
 
-    rb_define_method(rb_cDir,"initialize", dir_initialize, 1);
+    rb_define_method(rb_cDir,"initialize", dir_initialize, -1);
     rb_define_method(rb_cDir,"path", dir_path, 0);
     rb_define_method(rb_cDir,"inspect", dir_inspect, 0);
     rb_define_method(rb_cDir,"read", dir_read, 0);

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

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