Skip to content

Commit 3949323

Browse files
committed
add tmpfile ctor
1 parent dd83444 commit 3949323

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Revision history for {{$dist->name}}},
22

33
{{$NEXT}}
4+
- Add tmpfile constructor for FFI::C::File (gh#41)
45

56
0.09 2020-09-22 14:28:36 -0600
67
- Move to PerlFFI org (gh#37)

lib/FFI/C/File.pm

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ $ffi->attach( fopen => [ 'string', 'string' ] => 'opaque' => sub {
125125
bless \$ptr, $class;
126126
});
127127

128+
=head2 tmpfile
129+
130+
my $file = FFI::C::File->tmpfile;
131+
132+
Creates and opens a temporary file. The file is opened as binary file for update. On
133+
Windows this may require administrator privileges.
134+
135+
=cut
136+
137+
$ffi->attach( tmpfile => [] => 'opaque' => sub {
138+
my($xsub, $class) = @_;
139+
if(my $ptr = $xsub->())
140+
{
141+
return bless \$ptr, $class;
142+
}
143+
else
144+
{
145+
croak "Error opening temp file: $!"
146+
}
147+
});
148+
128149
=head2 new
129150
130151
my $file = FFI::C::File->new($ptr);

lib/FFI/C/PosixFile.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Returns the POSIX file descriptor for the file pointer.
7979
else
8080
{
8181
require Sub::Install;
82-
foreach my $ctor (qw( fopen freopen new fdopen ))
82+
foreach my $ctor (qw( fopen freopen new fdopen tmpfile ))
8383
{
8484
Sub::Install::install_sub({
8585
code => sub { croak "FFI::C::PosixFile not supported on this platform"; },

t/ffi_c_file.t

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ subtest 'basic read' => sub {
5656

5757
};
5858

59+
subtest 'tmpfile' => sub {
60+
skip_all 'tricky permissions on windows' if $^O eq 'MSWin32';
61+
62+
my $file = FFI::C::File->tmpfile;
63+
isa_ok $file, 'FFI::C::File';
64+
};
65+
5966
subtest 'basic write' => sub {
6067

6168
my $path = tempfile;

0 commit comments

Comments
 (0)