Skip to content

Commit

Permalink
Use wide character functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
djberg96 committed Apr 19, 2012
1 parent e571217 commit c0fec12
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/file/temp.rb
Expand Up @@ -30,14 +30,14 @@ class File::Temp < File
ffi_lib :kernel32

attach_function :CloseHandle, [:long], :bool
attach_function :CreateFileA, [:string, :ulong, :ulong, :pointer, :ulong, :ulong, :ulong], :long
attach_function :DeleteFileA, [:string], :bool
attach_function :GetTempPathA, [:long, :pointer], :long
attach_function :GetTempFileNameA, [:string, :string, :uint, :pointer], :uint
attach_function :CreateFileW, [:buffer_in, :ulong, :ulong, :pointer, :ulong, :ulong, :ulong], :long
attach_function :DeleteFileW, [:string], :bool
attach_function :GetTempPathW, [:ulong, :buffer_out], :ulong
attach_function :GetTempFileNameW, [:buffer_in, :string, :uint, :buffer_out], :uint

private_class_method :_close, :_fdopen, :_open, :_open_osfhandle
private_class_method :CloseHandle, :CreateFileA, :DeleteFileA
private_class_method :GetTempPathA, :GetTempFileNameA
private_class_method :CloseHandle, :CreateFileW, :DeleteFileW
private_class_method :GetTempPathW, :GetTempFileNameW

S_IWRITE = 128
S_IREAD = 256
Expand Down Expand Up @@ -190,13 +190,14 @@ def get_posix_errno
# Simple wrapper around the GetTempPath function.
#
def get_temp_path
buf = FFI::MemoryPointer.new(:char, 1024)
buf = 0.chr * 1024
buf.encode!("UTF-16LE")

if GetTempPathA(buf.size, buf) == 0
raise SystemCallError, FFI.errno, 'GetTempPathA'
if GetTempPathW(buf.size, buf) == 0
raise SystemCallError, FFI.errno, 'GetTempPathW'
end

buf.read_string.chop # remove trailing slash
buf.strip.chop # remove trailing slash
end

# The version of tmpfile() implemented by Microsoft is unacceptable.
Expand All @@ -209,15 +210,16 @@ def get_temp_path
#
def tmpfile
file_name = get_temp_path()
buf = FFI::MemoryPointer.new(:char, 1024)
buf = 0.chr * 1024
buf.encode!("UTF-16LE")

if GetTempFileNameA(file_name, 'rb_', 0, buf) == 0
raise SystemCallError, FFI.errno, 'GetTempFileNameA'
if GetTempFileNameW(file_name, 'rb_', 0, buf) == 0
raise SystemCallError, FFI.errno, 'GetTempFileNameW'
end

file_name = buf.read_string
file_name = buf.strip

handle = CreateFileA(
handle = CreateFileW(
file_name,
GENERIC_READ | GENERIC_WRITE,
0,
Expand All @@ -228,8 +230,8 @@ def tmpfile
)

if handle == INVALID_HANDLE_VALUE
DeleteFileA(file_name)
raise SystemCallError, FFI.errno, 'CreateFileA'
DeleteFileW(file_name)
raise SystemCallError, FFI.errno, 'CreateFileW'
end

fd = _open_osfhandle(handle, 0)
Expand Down

0 comments on commit c0fec12

Please sign in to comment.