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

ruby-changes:7205

From: akr <ko1@a...>
Date: Wed, 20 Aug 2008 19:24:52 +0900 (JST)
Subject: [ruby-changes:7205] Ruby:r18724 (trunk): * io.c (convconfig_t): new type.

akr	2008-08-20 19:24:37 +0900 (Wed, 20 Aug 2008)

  New Revision: 18724

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

  Log:
    * io.c (convconfig_t): new type.
      (rb_io_extract_modeenc): new function.
      (rb_file_open_generic): new function.
      (rb_file_open_internal): use rb_file_open_generic.
      (rb_file_sysopen_internal): use rb_file_open_generic.
      (rb_open_file): use rb_io_extract_modeenc and rb_file_open_generic.
      (rb_io_open): call rb_file_open_internal instead of rb_file_open.

  Modified files:
    trunk/ChangeLog
    trunk/io.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18723)
+++ ChangeLog	(revision 18724)
@@ -1,3 +1,13 @@
+Wed Aug 20 19:22:32 2008  Tanaka Akira  <akr@f...>
+
+	* io.c (convconfig_t): new type.
+	  (rb_io_extract_modeenc): new function.
+	  (rb_file_open_generic): new function.
+	  (rb_file_open_internal): use rb_file_open_generic.
+	  (rb_file_sysopen_internal): use rb_file_open_generic.
+	  (rb_open_file): use rb_io_extract_modeenc and rb_file_open_generic.
+	  (rb_io_open): call rb_file_open_internal instead of rb_file_open.
+
 Wed Aug 20 19:15:35 2008  Tanaka Akira  <akr@f...>
 
 	* io.c (Init_IO): new constants: File::DSYNC, File::RSYNC and
Index: io.c
===================================================================
--- io.c	(revision 18723)
+++ io.c	(revision 18724)
@@ -3798,6 +3798,57 @@
     return extracted;
 }
 
+typedef struct convconfig_t {
+  rb_encoding *enc;
+  rb_encoding *enc2;
+} convconfig_t;
+
+static void
+rb_io_extract_modeenc(VALUE mode, VALUE opthash,
+        int *modenum_p, int *flags_p, convconfig_t *convconfig_p)
+{
+    int modenum, flags;
+    rb_encoding *enc, *enc2;
+    int has_enc = 0;
+
+    enc = NULL;
+    enc2 = NULL;
+
+    if (NIL_P(mode)) {
+        flags = FMODE_READABLE;
+        modenum = O_RDONLY;
+    }
+    else if (FIXNUM_P(mode)) {
+        modenum = FIX2INT(mode);
+        flags = rb_io_modenum_flags(modenum);
+    }
+    else { /* xxx: Bignum, to_int */
+        const char *p;
+        SafeStringValue(mode);
+        p = StringValueCStr(mode);
+        flags = rb_io_mode_flags(p);
+        modenum = rb_io_flags_modenum(flags);
+        p = strchr(p, ':');
+        if (p) {
+            has_enc = 1;
+            parse_mode_enc(p+1, &enc, &enc2);
+        }
+    }
+
+    if (!NIL_P(opthash)) {
+        if (io_extract_encoding_option(opthash, &enc, &enc2)) {
+            if (has_enc) {
+                rb_raise(rb_eArgError, "encoding sepecified twice");
+            }
+        }
+    }
+
+    *modenum_p = modenum;
+    *flags_p = flags;
+    convconfig_p->enc = enc;
+    convconfig_p->enc2 = enc2;
+}
+
 struct sysopen_struct {
     char *fname;
     int flag;
@@ -3887,20 +3938,50 @@
 }
 
 static VALUE
-rb_file_open_internal(VALUE io, const char *fname, const char *mode)
+rb_file_open_generic(VALUE io, const char *fname, int modenum, int flags, convconfig_t *convconfig, int perm)
 {
     rb_io_t *fptr;
 
     MakeOpenFile(io, fptr);
-    fptr->mode = rb_io_mode_flags(mode);
-    rb_io_mode_enc(fptr, mode);
+    fptr->mode = flags;
+    if (convconfig) {
+        fptr->enc = convconfig->enc;
+        fptr->enc2 = convconfig->enc2;
+    }
+    else {
+        fptr->enc = NULL;
+        fptr->enc2 = NULL;
+    }
     fptr->path = strdup(fname);
-    fptr->fd = rb_sysopen(fptr->path, rb_io_mode_modenum(rb_io_flags_mode(fptr->mode)), 0666);
+    fptr->fd = rb_sysopen(fptr->path, modenum, perm);
     io_check_tty(fptr);
 
     return io;
 }
 
+static VALUE
+rb_file_open_internal(VALUE io, const char *fname, const char *mode)
+{
+    int flags;
+
+    const char *p = strchr(mode, ':');
+    convconfig_t convconfig;
+    if (p) {
+        parse_mode_enc(p+1, &convconfig.enc, &convconfig.enc2);
+    }
+    else {
+        convconfig.enc = NULL;
+        convconfig.enc2 = NULL;
+    }
+
+    flags = rb_io_mode_flags(mode);
+    return rb_file_open_generic(io, fname,
+            rb_io_mode_modenum(rb_io_flags_mode(flags)),
+            flags,
+            &convconfig,
+            0666);
+}
+
 VALUE
 rb_file_open(const char *fname, const char *mode)
 {
@@ -3908,24 +3989,15 @@
 }
 
 static VALUE
-rb_file_sysopen_internal(VALUE io, const char *fname, int flags, int mode)
+rb_file_sysopen_internal(VALUE io, const char *fname, int modenum, int perm)
 {
-    rb_io_t *fptr;
-
-    MakeOpenFile(io, fptr);
-
-    fptr->path = strdup(fname);
-    fptr->mode = rb_io_modenum_flags(flags);
-    fptr->fd = rb_sysopen(fptr->path, flags, mode);
-    io_check_tty(fptr);
-
-    return io;
+    return rb_file_open_generic(io, fname, modenum, rb_io_modenum_flags(modenum), NULL, perm);
 }
 
 VALUE
-rb_file_sysopen(const char *fname, int flags, int mode)
+rb_file_sysopen(const char *fname, int modenum, int perm)
 {
-    return rb_file_sysopen_internal(io_alloc(rb_cFile), fname, flags, mode);
+    return rb_file_sysopen_internal(io_alloc(rb_cFile), fname, modenum, perm);
 }
 
 #if defined(__CYGWIN__) || !defined(HAVE_FORK)
@@ -4455,9 +4527,9 @@
 rb_open_file(int argc, VALUE *argv, VALUE io)
 {
     VALUE opt=Qnil, fname, vmode, perm;
-    const char *mode;
-    int flags;
+    int modenum, flags;
     unsigned int fmode;
+    convconfig_t convconfig = { NULL, NULL };
 
     if (0 < argc) {
         opt = rb_check_convert_type(argv[argc-1], T_HASH, "Hash", "to_hash");
@@ -4487,31 +4559,13 @@
     }
 #endif
     FilePathValue(fname);
+ 
+    rb_io_extract_modeenc(vmode, opt, &modenum, &flags, &convconfig);
 
-    if (FIXNUM_P(vmode) || !NIL_P(perm)) {
-	if (FIXNUM_P(vmode)) {
-	    flags = FIX2INT(vmode);
-	}
-	else {
-	    SafeStringValue(vmode);
-	    flags = rb_io_mode_modenum(StringValueCStr(vmode));
-	}
-	fmode = NIL_P(perm) ? 0666 :  NUM2UINT(perm);
+    fmode = NIL_P(perm) ? 0666 :  NUM2UINT(perm);
 
-	rb_file_sysopen_internal(io, RSTRING_PTR(fname), flags, fmode);
+    rb_file_open_generic(io, RSTRING_PTR(fname), modenum, flags, &convconfig, fmode);
 
-        if (!FIXNUM_P(vmode)) {
-            rb_io_t *fptr;
-            GetOpenFile(io, fptr);
-            rb_io_mode_enc(fptr, StringValueCStr(vmode));
-        }
-    }
-    else {
-	mode = NIL_P(vmode) ? "r" : StringValueCStr(vmode);
-	rb_file_open_internal(io, RSTRING_PTR(fname), mode);
-    }
-
-    io_set_encoding(io, opt);
     return io;
 }
 
@@ -4730,7 +4784,7 @@
 	return pipe_open_s(cmd, mode);
     }
     else {
-	return rb_file_open(fname, mode);
+        return rb_file_open_internal(io_alloc(rb_cFile), fname, mode);
     }
 }
 

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

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