8000 * file.c (rb_str_encode_ospath): use UTF-8 instead of UTF-16LE as · documenting-ruby/ruby@80a2cf3 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 80a2cf3

Browse files
committed
* file.c (rb_str_encode_ospath): use UTF-8 instead of UTF-16LE as
internal path encoding on Windows. result of Ruby Developers' Meeting 20091013. * win32.c, include/ruby/win32.h (rb_w32_ulink, rb_w32_urename, rb_w32_ustati64, rb_w32_uopen, rb_w32_uutime, rb_w32_uchdir, rb_w32_umkdir, rb_w32_urmdir, rb_w32_uunlink): new functions to accept UTF-8 path. * dir.c, file.c, io.c: follow above changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/win32-unicode-test@25344 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent ef695a0 commit 80a2cf3

File tree

6 files changed

+317
-105
lines changed

6 files changed

+317
-105
lines changed

ChangeLog

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
Thu Oct 15 14:16:04 2009 NAKAMURA Usaku <usa@ruby-lang.org>
2+
3+
* file.c (rb_str_encode_ospath): use UTF-8 instead of UTF-16LE as
4+
internal path encoding on Windows. result of Ruby Developers' Meeting
5+
20091013.
6+
7+
* win32.c, include/ruby/win32.h (rb_w32_ulink, rb_w32_urename,
8+
rb_w32_ustati64, rb_w32_uopen, rb_w32_uutime, rb_w32_uchdir,
9+
rb_w32_umkdir, rb_w32_urmdir, rb_w32_uunlink): new functions to
10+
accept UTF-8 path.
11+
12+
* dir.c, file.c, io.c: follow above changes.
13+
114
Wed Oct 14 17:30:26 2009 NAKAMU 10000 RA Usaku <usa@ruby-lang.org>
215

316
* include/ruby/intern.h, dir.c, file.c, io.c (rb_str_encode_ospath):

dir.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ char *strchr(char*,char);
7070
/* define system APIs */
7171
#ifdef _WIN32
7272
#undef chdir
73-
#define chdir(p) _wchdir((WCHAR *)(p))
73+
#define chdir(p) rb_w32_uchdir(p)
7474
#undef mkdir
75-
#define mkdir(p, m) rb_w32_wmkdir((WCHAR *)(p), m)
75+
#define mkdir(p, m) rb_w32_umkdir(p, m)
7676
#undef rmdir
77-
#define rmdir(p) _wrmdir((WCHAR *)(p))
77+
#define rmdir(p) rb_w32_urmdir(p)
7878
#endif
7979

8080
#define FNM_NOESCAPE 0x01

file.c

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,23 @@ int flock(int, int);
7272

7373
/* define system APIs */
7474
#ifdef _WIN32
75-
#define STAT(p, s) rb_w32_wstati64((WCHAR *)(p), s)
75+
#define STAT(p, s) rb_w32_ustati64(p, s)
7676
#undef lstat
77-
#define lstat(p, s) rb_w32_wstati64((WCHAR *)(p), s)
77+
#define lstat(p, s) rb_w32_ustati64(p, s)
7878
#undef access
79-
#define access(p, m) _waccess((WCHAR *)(p), m)
79+
#define access(p, m) rb_w32_uaccess(p, m)
8080
#undef chmod
81-
#define chmod(p, m) _wchmod((WCHAR *)(p), m)
81+
#define chmod(p, m) rb_w32_uchmod(p, m)
8282
#undef chown
83-
#define chown(p, o, g) rb_w32_wchown((WCHAR *)(p), o, g)
83+
#define chown(p, o, g) rb_w32_uchown(p, o, g)
8484
#undef utime
85-
#define utime(p, t) rb_w32_wutime((WCHAR *)(p), t)
85+
#define utime(p, t) rb_w32_uutime(p, t)
8686
#undef link
87-
#define link(f, t) rb_w32_wlink((WCHAR *)(f), (WCHAR *)(t))
87+
#define link(f, t) rb_w32_ulink(f, t)
8888
#undef unlink
89-
#define unlink(p) rb_w32_wunlink((WCHAR *)(p))
89+
#define unlink(p) rb_w32_uunlink(p)
9090
#undef rename
91-
#define rename(f, t) _wrename((WCHAR *)(f), (WCHAR *)(t))
91+
#define rename(f, t) rb_w32_urename(f, t)
9292
#else
9393
#define STAT(p, s) stat(p, s)
9494
#endif
@@ -171,32 +171,14 @@ rb_str_encode_ospath(VALUE path)
171171
{
172172
char *s;
173173
#ifdef _WIN32
174-
static rb_encoding *utf16 = (rb_encoding *)-1;
175-
VALUE wpath;
176-
if (utf16 == (rb_encoding *)-1) {
177-
utf16 = rb_enc_find("UTF-16LE");
178-
if (utf16 == rb_ascii8bit_encoding())
179-
utf16 = NULL;
180-
}
181-
if (utf16 && rb_enc_get(path) != rb_ascii8bit_encoding()) {
182-
wpath = rb_str_encode(path, rb_enc_from_encoding(utf16), 0, Qnil);
183-
rb_enc_str_buf_cat(wpath, "", 1, utf16); /* workaround */
184-
return wpath;
185-
}
186-
else if (RSTRING_LEN(path) > 0) {
187-
UINT cp = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
188-
int len = MultiByteToWideChar(cp, 0, RSTRING_PTR(path), -1, NULL, 0);
189-
WCHAR *tmp;
190-
if (len <= 0)
191-
rb_raise(rb_eArgError, "broken pathname");
192-
tmp = ALLOCA_N(WCHAR, len);
193-
len = MultiByteToWideChar(cp, 0, RSTRING_PTR(path), -1, tmp, len);
194-
if (len <= 0)
195-
rb_raise(rb_eArgError, "broken pathname");
196-
wpath = rb_str_new((char *)tmp, len * sizeof(WCHAR));
197-
OBJ_INFECT(wpath, path);
198-
return wpath;
174+
rb_encoding *enc = rb_enc_get(path);
175+
if (enc != rb_ascii8bit_encoding()) {
176+
rb_encoding *utf8 = rb_utf8_encoding();
177+
if (enc != utf8)
178+
path = rb_str_encode(path, rb_enc_from_encoding(utf8), 0, Qnil);
199179
}
180+
else if (RSTRING_LEN(path) > 0)
181+
path = rb_str_encode(path, rb_enc_from_encoding(rb_filesystem_encoding()), 0, Qnil);
200182
#elif defined __APPLE__
201183
rb_encoding *enc = rb_enc_get(path);
202184
rb_encoding *fenc = rb_filesystem_encoding();

include/ruby/win32.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,15 @@ extern int rb_w32_socketpair(int, int, int, int *);
259259
extern char * rb_w32_getcwd(char *, int);
260260
extern char * rb_w32_getenv(const char *);
261261
extern int rb_w32_rename(const char *, const char *);
262+
extern int rb_w32_urename(const char *, const char *);
262263
extern char **rb_w32_get_environ(void);
263264
extern void rb_w32_free_environ(char **);
264265
extern int rb_w32_map_errno(DWORD);
265266

266267
extern int chown(const char *, int, int);
267-
extern int rb_w32_wchown(const WCHAR *, int, int);
268+
extern int rb_w32_uchown(const char *, int, int);
268269
extern int link(const char *, const char *);
269-
extern int rb_w32_wlink(const WCHAR *, const WCHAR *);
270+
extern int rb_w32_ulink(const char *, const char *);
270271
extern int gettimeofday(struct timeval *, struct timezone *);
271272
extern rb_pid_t waitpid (rb_pid_t, int *, int);
272273
extern rb_pid_t rb_w32_spawn(int, const char *, const char*);
@@ -278,14 +279,17 @@ extern rb_pid_t rb_w32_getppid(void);
278279
#if !defined(__BORLANDC__)
279280
extern int rb_w32_isatty(int);
280281
#endif
281-
extern int rb_w32_wmkdir(const WCHAR *, int);
282282
extern int rb_w32_mkdir(const char *, int);
283+
extern int rb_w32_umkdir(const char *, int);
283284
extern int rb_w32_rmdir(const char *);
285+
extern int rb_w32_urmdir(const char *);
284286
extern int rb_w32_unlink(const char *);
285-
extern int rb_w32_wunlink(const WCHAR *);
287+
extern int rb_w32_uunlink(const char *);
288+
extern int rb_w32_uchmod(const char *, int);
286289
extern int rb_w32_stati64(const char *, struct stati64 *);
287-
extern int rb_w32_wstati64(const WCHAR *, struct stati64 *);
290+
extern int rb_w32_ustati64(const char *, struct stati64 *);
288291
extern int rb_w32_access(const char *, int);
292+
extern int rb_w32_uaccess(const char *, int);
289293

290294
#ifdef __BORLANDC__
291295
extern int rb_w32_fstati64(int, struct stati64 *);
@@ -558,15 +562,16 @@ HANDLE GetCurrentThreadHandle(void);
558562
int rb_w32_sleep(unsigned long msec);
559563
int rb_w32_putc(int, FILE*);
560564
int rb_w32_getc(FILE*);
561-
int rb_w32_wopen(const WCHAR *, int, ...);
562565
int rb_w32_open(const char *, int, ...);
566+
int rb_w32_uopen(const char *, int, ...);
567+
int rb_w32_wopen(const WCHAR *, int, ...);
563568
int rb_w32_close(int);
564569
int rb_w32_fclose(FILE*);
565570
int rb_w32_pipe(int[2]);
566571
size_t rb_w32_read(int, void *, size_t);
567572
size_t rb_w32_write(int, const void *, size_t);
568573
int rb_w32_utime(const char *, const struct utimbuf *);
569-
int rb_w32_wutime(const WCHAR *, const struct utimbuf *);
574+
int rb_w32_uutime(const char *, const struct utimbuf *);
570575
long rb_w32_write_console(unsigned long, int);
571576
int WINAPI rb_w32_Sleep(unsigned long msec);
572577
int rb_w32_wait_events_blocking(HANDLE *events, int num, DWORD timeout);

io.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ extern void Init_File(void);
109109

110110
#define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
111111

112+
/* define system APIs */
113+
#ifdef _WIN32
114+
#undef open
115+
#define open rb_w32_uopen
116+
#endif
117+
112118
VALUE rb_cIO;
113119
VALUE rb_eEOFError;
114120
VALUE rb_eIOError;
@@ -4417,11 +4423,7 @@ sysopen_func(void *ptr)
44174423
{
44184424
const struct sysopen_struct *data = ptr;
44194425
const char *fname = RSTRING_PTR(data->fname);
4420-
#ifdef _WIN32
4421-
return (VALUE)rb_w32_wopen((WCHAR *)fname, data->oflags, data->perm);
4422-
#else
44234426
return (VALUE)open(fname, data->oflags, data->perm);
4424-
#endif
44254427
}
44264428

44274429
static inline int

0 commit comments

Comments
 (0)
0