File Coverage

blib/lib/Geo/GDAL/FFI/VSI/File.pm
Criterion Covered Total %
statement 23 52 44.2
branch 0 6 0.0
condition 0 9 0.0
subroutine 8 14 57.1
pod 4 5 80.0
total 35 86 40.7


line stmt bran cond sub pod time code
1             package Geo::GDAL::FFI::VSI::File;
2 5     5   59 use v5.10;
  5         16  
3 5     5   24 use strict;
  5         10  
  5         95  
4 5     5   21 use warnings;
  5         10  
  5         138  
5 5     5   35 use Encode qw(decode encode);
  5         16  
  5         231  
6 5     5   30 use Carp;
  5         9  
  5         387  
7 5     5   36 use FFI::Platypus::Buffer;
  5         8  
  5         284  
8 5     5   2551 use FFI::Platypus::Declare;
  5         6758  
  5         30  
9              
10             our $VERSION = 0.0900;
11              
12             sub Open {
13 0     0 1   my ($class, $path, $access) = @_;
14 0   0       $access //= 'r';
15 0           my $self = {};
16 0           $self->{handle} = Geo::GDAL::FFI::VSIFOpenExL(encode(utf8 => $path), $access, 1);
17 0 0         unless ($self->{handle}) {
18 0   0       confess Geo::GDAL::FFI::error_msg() // "Failed to open '$path' with access '$access'.";
19             }
20 0           return bless $self, $class;
21             }
22              
23             sub DESTROY {
24 0     0     my ($self) = @_;
25 0           $self->Close;
26             }
27              
28             sub Close {
29 0     0 1   my ($self) = @_;
30 0 0         return unless $self->{handle};
31 0           my $e = Geo::GDAL::FFI::VSIFCloseL($self->{handle});
32 0 0 0       confess Geo::GDAL::FFI::error_msg() // "Failed to close a VSIFILE." if $e == -1;
33 0           delete $self->{handle};
34             }
35              
36             sub Read {
37 0     0 1   my ($self, $len) = @_;
38 0   0       $len //= 1;
39 0           my $buf = ' ' x $len;
40 0           my ($pointer, $size) = scalar_to_buffer $buf;
41 0           my $n = Geo::GDAL::FFI::VSIFReadL($pointer, 1, $len, $self->{handle});
42 0           return substr $buf, 0, $n;
43             }
44              
45             sub Write {
46 0     0 1   my ($self, $buf) = @_;
47 5     5   2067 my $len = do {use bytes; length($buf)};
  5         11  
  5         28  
  0            
  0            
48 0           my $address = cast 'string' => 'opaque', $buf;
49 0           return Geo::GDAL::FFI::VSIFWriteL($address, 1, $len, $self->{handle});
50             }
51              
52             sub Ingest {
53 0     0 0   my ($self) = @_;
54 0           my $s;
55 0           my $e = Geo::GDAL::FFI::VSIIngestFile($self->{handle}, '', \$s, 0, -1);
56 0           return $s;
57             }
58              
59             1;
60              
61             =pod
62              
63             =encoding UTF-8
64              
65             =head1 NAME
66              
67             Geo::GDAL::FFI::VSI::File - A GDAL virtual file
68              
69             =head1 SYNOPSIS
70              
71             =head1 DESCRIPTION
72              
73             =head1 METHODS
74              
75             =head2 Open
76              
77             my $vsifile = Geo::GDAL::FFI::VSI::File->Open($name, $access);
78              
79             Open a virtual file. $name is the name of the file to open. $access is
80             'r', 'r+', 'a', or 'w'. 'r' is the default.
81              
82             Returns a Geo::GDAL::FFI::VSI::File object.
83              
84             =head2 Close
85              
86             Closes the file handle. Is done automatically when the object is
87             destroyed.
88              
89             =head2 Read($len)
90              
91             Read $len bytes from the file. Returns the bytes in a Perl
92             string. $len is optional and by default 1.
93              
94             =head2 Write($buf)
95              
96             Write the Perl string $buf into the file. Returns the number of
97             succesfully written bytes.
98              
99             =head1 LICENSE
100              
101             This software is released under the Artistic License. See
102             L.
103              
104             =head1 AUTHOR
105              
106             Ari Jolma - Ari.Jolma at gmail.com
107              
108             =head1 SEE ALSO
109              
110             L
111              
112             L, L, L
113              
114             =cut
115              
116             __END__;