| 1 | package MARC::Charset::Table; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | MARC::Charset::Table - character mapping db | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | use MARC::Charset::Table; | ||||||||||||||||||||
| 10 | use MARC::Charset::Constants qw(:all); | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | # create the table object | ||||||||||||||||||||
| 13 | my $table = MARC::Charset::Table->new(); | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # get a code using the marc8 character set code and the character | ||||||||||||||||||||
| 16 | my $code = $table->lookup_by_marc8(CYRILLIC_BASIC, 'K'); | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | # get a code using the utf8 value | ||||||||||||||||||||
| 19 | $code = $table->lookup_by_utf8(chr(0x043A)); | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | MARC::Charset::Table is a wrapper around the character mapping database, | ||||||||||||||||||||
| 24 | which is implemented as a tied hash on disk. This database gets generated | ||||||||||||||||||||
| 25 | by Makefile.PL on installation of MARC::Charset using | ||||||||||||||||||||
| 26 | MARC::Charset::Compiler. | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | The database is essentially a key/value mapping where a key is a | ||||||||||||||||||||
| 29 | MARC-8 character set code + a MARC-8 character, or an integer representing the | ||||||||||||||||||||
| 30 | UCS code point. These keys map to a serialized MARC::Charset::Code object. | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 20 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 20 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 20 | ▶ | use POSIX; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 20 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 20 | @AnyDBM_File::ISA = qw(GDBM_File DB_File NDBM_File ODBM_File SDBM_File); | |||||||||||||||||||
| 39 | # SDBM_File is last on the list because it produces the largest database | ||||||||||||||||||||
| 40 | # on disk. | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 20 | $Storable::canonical = 1; | |||||||||||||||||||
| 43 | # try to keep the generated database reproducible | ||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | 20 | ▶ | use AnyDBM_File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 46 | 20 | ▶ | use MARC::Charset::Code; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 47 | 20 | ▶ | use MARC::Charset::Constants qw(:all); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 48 | 20 | ▶ | use Storable qw(nfreeze thaw); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =head2 new() | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | The consturctor. | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | sub new | ||||||||||||||||||||
| 57 | 19 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||
| 58 | 19 | my $class = shift; | |||||||||||||||||||
| 59 | 19 | ▶ | my $self = bless {}, ref($class) || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 60 | 19 | $self->_init(O_RDONLY); | |||||||||||||||||||
| 61 | 19 | return $self; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =head2 add_code() | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | Add a MARC::Charset::Code to the table. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | sub add_code | ||||||||||||||||||||
| 73 | 16408 | ▶ | { | ||||||||||||||||||
|
Subroutine
add_code: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 74 | 16408 | my ($self, $code) = @_; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | # the Code object is serialized | ||||||||||||||||||||
| 77 | 16408 | my $frozen = nfreeze($code); | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | # to support lookup by marc8 and utf8 values we | ||||||||||||||||||||
| 80 | # stash away the rule in the db using two keys | ||||||||||||||||||||
| 81 | 16408 | my $marc8_key = $code->marc8_hash_code(); | |||||||||||||||||||
| 82 | 16408 | my $utf8_key = $code->utf8_hash_code(); | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # stash away the marc8 lookup key | ||||||||||||||||||||
| 85 | 16408 | $self->{db}->{$marc8_key} = $frozen; | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | # stash away the utf8 lookup key (only if it's not already there!) | ||||||||||||||||||||
| 88 | # this means that the sets that appear in the xml file will have | ||||||||||||||||||||
| 89 | # precedence ascii/ansel. Note that we're using 'defined' instead of | ||||||||||||||||||||
| 90 | # 'exists' because NDBM_File and ODBM_File don't support 'exists'. | ||||||||||||||||||||
| 91 | 16408 | ▶ | $self->{db}->{$utf8_key} = $frozen unless defined $self->{db}->{$utf8_key}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =head2 get_code() | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | Retrieve a code using a hash key. | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | sub get_code | ||||||||||||||||||||
| 102 | 541 | ▶ | { | ||||||||||||||||||
|
Subroutine
get_code: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 103 | 541 | my ($self, $key) = @_; | |||||||||||||||||||
| 104 | 541 | my $db = $self->db(); | |||||||||||||||||||
| 105 | 541 | my $frozen = $db->{$key}; | |||||||||||||||||||
| 106 | 541 | ▶ | return thaw($frozen) if $frozen; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 9 | return; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =head2 lookup_by_marc8() | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | Looks up MARC::Charset::Code entry using a character set code and a MARC-8 | ||||||||||||||||||||
| 114 | value. | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | use MARC::Charset::Constants qw(HEBREW); | ||||||||||||||||||||
| 117 | $code = $table->lookup_by_marc8(HEBREW, chr(0x60)); | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | sub lookup_by_marc8 | ||||||||||||||||||||
| 122 | 401 | ▶ | { | ||||||||||||||||||
|
Subroutine
lookup_by_marc8: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 123 | 401 | my ($self, $charset, $marc8) = @_; | |||||||||||||||||||
| 124 | 401 | ▶ | $charset = BASIC_LATIN if $charset eq ASCII_DEFAULT; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 401 | return $self->get_code(sprintf('%s:%s', $charset, $marc8)); | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =head2 lookup_by_utf8() | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | Looks up a MARC::Charset::Code object using a utf8 value. | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | sub lookup_by_utf8 | ||||||||||||||||||||
| 136 | 138 | ▶ | { | ||||||||||||||||||
|
Subroutine
lookup_by_utf8: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 137 | 138 | my ($self, $value) = @_; | |||||||||||||||||||
| 138 | 138 | return $self->get_code(ord($value)); | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | =head2 db() | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | Returns a reference to a tied character database. MARC::Charset::Table | ||||||||||||||||||||
| 147 | wraps access to the db, but you can get at it if you want. | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | sub db | ||||||||||||||||||||
| 152 | 542 | ▶ | { | ||||||||||||||||||
|
Subroutine
db: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 153 | 542 | return shift->{db}; | |||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =head2 db_path() | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | Returns the path to the character encoding database. Can be called | ||||||||||||||||||||
| 160 | statically too: | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | print MARC::Charset::Table->db_path(); | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | sub db_path | ||||||||||||||||||||
| 167 | 21 | ▶ | { | ||||||||||||||||||
|
Subroutine
db_path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 168 | 21 | my $path = $INC{'MARC/Charset/Table.pm'}; | |||||||||||||||||||
| 169 | 21 | $path =~ s/\.pm$//; | |||||||||||||||||||
| 170 | 21 | return $path; | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =head2 brand_new() | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | An alternate constructor which removes the existing database and starts | ||||||||||||||||||||
| 177 | afresh. Be careful with this one, it's really only used on MARC::Charset | ||||||||||||||||||||
| 178 | installation. | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | sub brand_new | ||||||||||||||||||||
| 183 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
brand_new: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||
| 184 | 1 | my $class = shift; | |||||||||||||||||||
| 185 | 1 | ▶ | my $self = bless {}, ref($class) || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 186 | 1 | $self->_init(O_CREAT|O_RDWR); | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 1 | return $self; | |||||||||||||||||||
| 189 | } | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | # helper function for initializing table internals | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | sub _init | ||||||||||||||||||||
| 195 | 20 | ▶ | { | ||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 196 | 20 | my ($self, $dbm_opts) = @_; | |||||||||||||||||||
| 197 | 20 | tie my %db, 'AnyDBM_File', db_path(), $dbm_opts, 0644; ## no critic (ValuesAndExpressions::ProhibitLeadingZeros) | |||||||||||||||||||
| 198 | 20 | $self->{db} = \%db; | |||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 20 | 1; | |||||||||||||||||||