Skip to content

Commit

Permalink
Convert file path to wide char to handle unicode file path on Windows (
Browse files Browse the repository at this point in the history
  • Loading branch information
Watson1978 authored Jun 12, 2022
1 parent cdbed34 commit a8900b0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
12 changes: 11 additions & 1 deletion ext/oj/oj.c
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,17 @@ static VALUE load_file(int argc, VALUE *argv, VALUE self) {
}
}
path = StringValuePtr(*argv);
if (0 == (fd = open(path, O_RDONLY))) {
#ifdef _WIN32
{
WCHAR *wide_path;
wide_path = rb_w32_mbstr_to_wstr(CP_UTF8, path, -1, NULL);
fd = rb_w32_wopen(wide_path, O_RDONLY);
free(wide_path);
}
#else
fd = open(path, O_RDONLY);
#endif
if (0 == fd) {
rb_raise(rb_eIOError, "%s", strerror(errno));
}
switch (mode) {
Expand Down
4 changes: 4 additions & 0 deletions ext/oj/sparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,11 @@ oj_pi_sparse(int argc, VALUE *argv, ParseInfo pi, int fd) {
}
stack_cleanup(&pi->stack);
if (0 != fd) {
#ifdef _WIN32
rb_w32_close(fd);
#else
close(fd);
#endif
}
if (err_has(&pi->err)) {
rb_set_errinfo(Qnil);
Expand Down
18 changes: 18 additions & 0 deletions test/test_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ def test_datetime_object
dump_and_load(DateTime.new(2012, 6, 19), false)
end

def test_load_unicode_path
json =<<~JSON
{
"x":true,
"y":58,
"z": [1,2,3]
}
JSON

Tempfile.create('file_test_conceição1.json') do |f|
f.write(json)
f.close

objects = Oj.load_file(f.path)
assert_equal(Oj.load(json), objects)
end
end

def dump_and_load(obj, trace=false)
filename = File.join(File.dirname(__FILE__), 'file_test.json')
File.open(filename, "w") { |f|
Expand Down

0 comments on commit a8900b0

Please sign in to comment.