line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
##-*- Mode: CPerl -*- |
2
|
|
|
|
|
|
|
## |
3
|
|
|
|
|
|
|
## File: Tie/File/Indexed/Utf8.pm |
4
|
|
|
|
|
|
|
## Author: Bryan Jurish |
5
|
|
|
|
|
|
|
## Description: tied array access to indexed data files: utf8-encoded strings |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
package Tie::File::Indexed::Utf8; |
9
|
2
|
|
|
2
|
|
14665
|
use Tie::File::Indexed; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
46
|
|
10
|
2
|
|
|
2
|
|
1019
|
use utf8; |
|
2
|
|
|
|
|
28
|
|
|
2
|
|
|
|
|
8
|
|
11
|
2
|
|
|
2
|
|
46
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
255
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
##====================================================================== |
14
|
|
|
|
|
|
|
## Globals |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our @ISA = qw(Tie::File::Indexed); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
##====================================================================== |
19
|
|
|
|
|
|
|
## Subclass API: Data I/O: overides |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
## $bool = $tfi->writeData($utf8_string) |
22
|
|
|
|
|
|
|
## + override transparently encodes $data as utf8 |
23
|
|
|
|
|
|
|
sub writeData { |
24
|
4
|
|
|
4
|
1
|
5
|
my $val = $_[1]; |
25
|
4
|
50
|
|
|
|
8
|
if (defined($val)) { |
26
|
4
|
100
|
|
|
|
9
|
utf8::upgrade($val) if (!utf8::is_utf8($val)); ##-- convert byte-strings to utf8 |
27
|
4
|
|
|
|
|
5
|
utf8::encode($val); ##-- ... but write (encoded) byte-strings |
28
|
|
|
|
|
|
|
} |
29
|
4
|
|
50
|
|
|
15
|
return $_[0]{datfh}->print($val//''); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
## $data_or_undef = $tfi->readData($length) |
33
|
|
|
|
|
|
|
## + read item data from $tfi->{datfh} from its current position |
34
|
|
|
|
|
|
|
## + returned scalars should always have their utf8 flag set |
35
|
|
|
|
|
|
|
sub readData { |
36
|
4
|
50
|
|
4
|
1
|
9
|
defined(my $buf = $_[0]->SUPER::readData($_[1])) or return undef; |
37
|
4
|
|
|
|
|
10
|
utf8::decode($buf); |
38
|
4
|
|
|
|
|
7
|
return $buf; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
1; ##-- be happpy |