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

ruby-changes:46630

From: nobu <ko1@a...>
Date: Tue, 16 May 2017 19:26:03 +0900 (JST)
Subject: [ruby-changes:46630] nobu:r58745 (trunk): rb_w32_ugetcwd: UTF-8 version getcwd

nobu	2017-05-16 19:25:56 +0900 (Tue, 16 May 2017)

  New Revision: 58745

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58745

  Log:
    rb_w32_ugetcwd: UTF-8 version getcwd
    
    * dir.c (rb_dir_getwd): convert from UTF-8.
    
    * win32/win32.c (w32_getcwd): codepage aware getcwd using
      GetCurrentDirectoryW.

  Modified files:
    trunk/dir.c
    trunk/win32/dir.h
    trunk/win32/win32.c
Index: dir.c
===================================================================
--- dir.c	(revision 58744)
+++ dir.c	(revision 58745)
@@ -74,6 +74,7 @@ char *strchr(char*,char); https://github.com/ruby/ruby/blob/trunk/dir.c#L74
 #define rmdir(p) rb_w32_urmdir(p)
 #undef opendir
 #define opendir(p) rb_w32_uopendir(p)
+#define ruby_getcwd() rb_w32_ugetcwd(NULL, 0)
 #define IS_WIN32 1
 #else
 #define IS_WIN32 0
@@ -1051,10 +1052,14 @@ rb_dir_getwd(void) https://github.com/ruby/ruby/blob/trunk/dir.c#L1052
 {
     char *path;
     VALUE cwd;
-    int fsenc = rb_enc_to_index(rb_filesystem_encoding());
+    rb_encoding *fs = rb_filesystem_encoding();
+    int fsenc = rb_enc_to_index(fs);
 
     if (fsenc == ENCINDEX_US_ASCII) fsenc = ENCINDEX_ASCII;
     path = my_getcwd();
+#ifdef _WIN32
+    cwd = rb_str_conv_enc(rb_utf8_str_new_cstr(path), NULL, fs);
+#else
 #ifdef __APPLE__
     cwd = rb_str_normalize_ospath(path, strlen(path));
     OBJ_TAINT(cwd);
@@ -1062,6 +1067,7 @@ rb_dir_getwd(void) https://github.com/ruby/ruby/blob/trunk/dir.c#L1067
     cwd = rb_tainted_str_new2(path);
 #endif
     rb_enc_associate_index(cwd, fsenc);
+#endif
 
     xfree(path);
     return cwd;
Index: win32/win32.c
===================================================================
--- win32/win32.c	(revision 58744)
+++ win32/win32.c	(revision 58745)
@@ -4655,43 +4655,61 @@ clock_getres(clockid_t clock_id, struct https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L4655
 }
 
 /* License: Ruby's */
-char *
-rb_w32_getcwd(char *buffer, int size)
+static char *
+w32_getcwd(char *buffer, int size, UINT cp)
 {
-    char *p = buffer;
-    int len;
+    WCHAR *p;
+    int wlen, len;
 
-    len = GetCurrentDirectory(0, NULL);
+    len = GetCurrentDirectoryW(0, NULL);
     if (!len) {
 	errno = map_errno(GetLastError());
 	return NULL;
     }
 
-    if (p) {
+    if (buffer && size < len) {
+	errno = ERANGE;
+	return NULL;
+    }
+
+    p = ALLOCA_N(WCHAR, len);
+    if (!GetCurrentDirectoryW(len, p)) {
+	errno = map_errno(GetLastError());
+        return NULL;
+    }
+
+    wlen = translate_wchar(p, L'\\', L'/') - p + 1;
+    len = WideCharToMultiByte(cp, 0, p, wlen, NULL, 0, NULL, NULL);
+    if (buffer) {
 	if (size < len) {
 	    errno = ERANGE;
 	    return NULL;
 	}
     }
     else {
-	p = malloc(len);
-	size = len;
-	if (!p) {
+	buffer = malloc(len);
+	if (!buffer) {
 	    errno = ENOMEM;
 	    return NULL;
 	}
     }
+    WideCharToMultiByte(cp, 0, p, wlen, buffer, len, NULL, NULL);
 
-    if (!GetCurrentDirectory(size, p)) {
-	errno = map_errno(GetLastError());
-	if (!buffer)
-	    free(p);
-        return NULL;
-    }
+    return buffer;
+}
 
-    translate_char(p, '\\', '/', filecp());
+/* License: Ruby's */
+char *
+rb_w32_getcwd(char *buffer, int size)
+{
+    return w32_getcwd(buffer, size, filecp());
+}
 
-    return p;
+/* License: Ruby's */
+char *
+rb_w32_ugetcwd(char *buffer, int size)
+{
+    return w32_getcwd(buffer, size, CP_UTF8);
 }
 
 /* License: Artistic or GPL */
Index: win32/dir.h
===================================================================
--- win32/dir.h	(revision 58744)
+++ win32/dir.h	(revision 58745)
@@ -33,6 +33,7 @@ long           rb_w32_telldir(DIR *); https://github.com/ruby/ruby/blob/trunk/win32/dir.h#L33
 void           rb_w32_seekdir(DIR *, long);
 void           rb_w32_rewinddir(DIR *);
 void           rb_w32_closedir(DIR *);
+char          *rb_w32_ugetcwd(char *, int);
 
 #define opendir(s)   rb_w32_opendir((s))
 #define readdir(d)   rb_w32_readdir((d), 0)

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

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