| 1 | |||
| 2 | =head1 Name | ||
| 3 | |||
| 4 | qbit::File - Functions to manipulate files. | ||
| 5 | |||
| 6 | =cut | ||
| 7 | |||
| 8 | package qbit::File; | ||
| 9 | $qbit::File::VERSION = '2.7'; | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | use utf8; | ||
| 13 | |||
| 14 | use base qw(Exporter); | ||
| 15 | |||
| 16 | BEGIN { | ||
| 17 | our (@EXPORT, @EXPORT_OK); | ||
| 18 | |||
| 19 | @EXPORT = qw( | ||
| 20 | readfile | ||
| 21 | writefile | ||
| 22 | ); | ||
| 23 | @EXPORT_OK = @EXPORT; | ||
| 24 | } | ||
| 25 | |||
| 26 | use Fcntl qw(O_RDONLY O_WRONLY O_CREAT O_APPEND); | ||
| 27 | |||
| 28 | use qbit::StringUtils qw(fix_utf); | ||
| 29 | use qbit::Exceptions; | ||
| 30 | use qbit::GetText qw(gettext); | ||
| 31 | |||
| 32 | =head1 Functions | ||
| 33 | |||
| 34 | =head2 readfile | ||
| 35 | |||
| 36 | B<Arguments:> | ||
| 37 | |||
| 38 | =over | ||
| 39 | |||
| 40 | =item | ||
| 41 | |||
| 42 | B<$filename> - string, file name; | ||
| 43 | |||
| 44 | =item | ||
| 45 | |||
| 46 | B<%opts> - hash, additional arguments: | ||
| 47 | |||
| 48 | =over | ||
| 49 | |||
| 50 | =item | ||
| 51 | |||
| 52 | B<binary> - boolean, binary ? C<binmode($fh)> : C<binmode($fh, ':utf8')>. | ||
| 53 | |||
| 54 | =back | ||
| 55 | |||
| 56 | =back | ||
| 57 | |||
| 58 | B<Return value:> string, file content. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub readfile($;%) { | ||
| 63 | my ($filename, %opts) = @_; | ||
| 64 | |||
| 65 | my $fh = local *FH; | ||
| 66 | unless (sysopen($fh, $filename, O_RDONLY)) { | ||
| 67 | throw Exception gettext('Cannot open file "%s": %s', $filename, fix_utf($!)); | ||
| 68 | } | ||
| 69 | |||
| 70 | binmode $fh; | ||
| 71 | |||
| 72 | my $size_left = -s $filename; | ||
| 73 | |||
| 74 | my $content = ''; | ||
| 75 | while (1) { | ||
| 76 | my $read_cnt = sysread($fh, $content, $size_left, length $content); | ||
| 77 | |||
| 78 | unless (defined $read_cnt) { | ||
| 79 | throw Exception gettext('Cannot read file "%s": %s', $filename, fix_utf($!)); | ||
| 80 | } | ||
| 81 | |||
| 82 | last if $read_cnt == 0; | ||
| 83 | |||
| 84 | $size_left -= $read_cnt; | ||
| 85 | last if $size_left <= 0; | ||
| 86 | } | ||
| 87 | |||
| 88 | utf8::decode($content) unless $opts{'binary'}; | ||
| 89 | |||
| 90 | return $content; | ||
| 91 | } | ||
| 92 | |||
| 93 | =head2 writefile | ||
| 94 | |||
| 95 | B<Arguments:> | ||
| 96 | |||
| 97 | =over | ||
| 98 | |||
| 99 | =item | ||
| 100 | |||
| 101 | B<$filename> - string, file name; | ||
| 102 | |||
| 103 | =item | ||
| 104 | |||
| 105 | B<$data> - string, file content; | ||
| 106 | |||
| 107 | =item | ||
| 108 | |||
| 109 | B<%opts> - hash, additional arguments: | ||
| 110 | |||
| 111 | =over | ||
| 112 | |||
| 113 | =item | ||
| 114 | |||
| 115 | B<binary> - boolean, binary ? C<binmode($fh)> : C<binmode($fh, ':utf8')>. | ||
| 116 | |||
| 117 | B<append> - boolean | ||
| 118 | |||
| 119 | =back | ||
| 120 | |||
| 121 | =back | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub writefile($$;%) { | ||
| 126 | my ($filename, $data, %opts) = @_; | ||
| 127 | |||
| 128 | my $fh = local *FH; | ||
| 129 | |||
| 130 | my $mode = O_WRONLY | O_CREAT; | ||
| 131 | $mode |= O_APPEND if $opts{'append'}; | ||
| 132 | |||
| 133 | unless (sysopen($fh, $filename, $mode)) { | ||
| 134 | throw Exception gettext('Cannot open file "%s" for write: %s', $filename, fix_utf($!)); | ||
| 135 | } | ||
| 136 | |||
| 137 | $opts{'binary'} ? binmode $fh : binmode $fh, ':utf8'; | ||
| 138 | |||
| 139 | print $fh $data; | ||
| 140 | |||
| 141 | close($fh); | ||
| 142 | } | ||
| 143 | |||
| 144 | 1; |