| 1 | # Copyright 2009, 2010, 2011, 2014 Kevin Ryde | ||||||||||
| 2 | |||||||||||
| 3 | # This file is part of POSIX-Wide. | ||||||||||
| 4 | # | ||||||||||
| 5 | # POSIX-Wide is free software; you can redistribute it and/or modify | ||||||||||
| 6 | # it under the terms of the GNU General Public License as published by the | ||||||||||
| 7 | # Free Software Foundation; either version 3, or (at your option) any later | ||||||||||
| 8 | # version. | ||||||||||
| 9 | # | ||||||||||
| 10 | # POSIX-Wide is distributed in the hope that it will be useful, but | ||||||||||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||||||||||
| 13 | # for more details. | ||||||||||
| 14 | # | ||||||||||
| 15 | # You should have received a copy of the GNU General Public License along | ||||||||||
| 16 | # with POSIX-Wide. If not, see <http://www.gnu.org/licenses/>. | ||||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | # Possible funcs: | ||||||||||
| 20 | # asctime() | ||||||||||
| 21 | # ctime() | ||||||||||
| 22 | # Believe always ascii day/month, or at least that's what glibc gives. | ||||||||||
| 23 | # | ||||||||||
| 24 | # Different: | ||||||||||
| 25 | # strcoll() | ||||||||||
| 26 | # strxfrm() | ||||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | package POSIX::Wide; | ||||||||||
| 30 | 1 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 1 | ▶ | use POSIX (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 1 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 1 | ▶ | use Encode::Locale; # has 'locale' from its initial 0.01 release | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | 1 | our $VERSION = 10; | |||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 1 | our @ISA = ('Exporter'); | |||||||||
| 41 | 1 | our @EXPORT_OK = qw(localeconv perror strerror strftime tzname | |||||||||
| 42 | $ERRNO $EXTENDED_OS_ERROR); | ||||||||||
| 43 | # not yet ... | ||||||||||
| 44 | # our %EXPORT_TAGS = (all => \@EXPORT_OK); | ||||||||||
| 45 | |||||||||||
| 46 | 1 | ▶ | use POSIX::Wide::ERRNO; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 1 | tie (our $ERRNO, 'POSIX::Wide::ERRNO'); | |||||||||
| 48 | |||||||||||
| 49 | 1 | ▶ | use POSIX::Wide::EXTENDED_OS_ERROR; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 1 | tie (our $EXTENDED_OS_ERROR, 'POSIX::Wide::EXTENDED_OS_ERROR'); | |||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | 1 | our @LOCALECONV_STRING_FIELDS = (qw(decimal_point | |||||||||
| 54 | thousands_sep | ||||||||||
| 55 | int_curr_symbol | ||||||||||
| 56 | currency_symbol | ||||||||||
| 57 | mon_decimal_point | ||||||||||
| 58 | mon_thousands_sep | ||||||||||
| 59 | positive_sign | ||||||||||
| 60 | negative_sign)); | ||||||||||
| 61 | |||||||||||
| 62 | # POSIX.xs of perl 5.10.1 has mon_thousands_sep conditionalized, so allow | ||||||||||
| 63 | # for it and maybe other fields to not exist. | ||||||||||
| 64 | # | ||||||||||
| 65 | # POSIX.xs omits fields which are empty strings "", so for example when | ||||||||||
| 66 | # positive_sign is an empty string (which is usual in an English locale) | ||||||||||
| 67 | # there's no such field in the POSIX::localeconv() return. | ||||||||||
| 68 | # | ||||||||||
| 69 | sub localeconv { | ||||||||||
| 70 | 1 | ▶ | my $l = POSIX::localeconv(); | ||||||||
|
Subroutine
localeconv: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 71 | 1 | foreach my $key (@LOCALECONV_STRING_FIELDS) { | |||||||||
| 72 | 8 | ▶ | if (exists $l->{$key}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 8 | $l->{$key} = _to_wide($l->{$key}); | |||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | 1 | return $l; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | # STDERR like POSIX/perror.al | ||||||||||
| 80 | sub perror { | ||||||||||
| 81 | 0 | ▶ | if (@_) { print STDERR @_,': '; } | ||||||||
|
Subroutine
perror: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 82 | 0 | print STDERR strerror($!),"\n"; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | sub strerror { | ||||||||||
| 86 | 1 | ▶ | return _to_wide (POSIX::strerror ($_[0])); | ||||||||
|
Subroutine
strerror: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | # \020-\176 is printable ascii | ||||||||||
| 90 | # only basic control chars are allows through to strftime, in particular Esc | ||||||||||
| 91 | # is excluded in case the locale is shift-jis etc and it means something | ||||||||||
| 92 | sub strftime { | ||||||||||
| 93 | 14 | ▶ | (my $fmt = shift) =~ s{(%[\020-\176\t\n\r\f\a]*)} | ||||||||
|
Subroutine
strftime: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 94 | 16 | { _to_wide(POSIX::strftime($1,@_)) }ge; | |||||||||
| 95 | 14 | return $fmt; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | sub tzname { | ||||||||||
| 99 | 1 | ▶ | return map {_to_wide($_)} POSIX::tzname(); | ||||||||
|
Subroutine
tzname: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | sub _to_wide { | ||||||||||
| 103 | 35 | ▶ | my ($str) = @_; | ||||||||
|
Subroutine
_to_wide: called CC=2 SCAR=7.6
| |||||||||||
| 104 | 35 | ▶ | if (utf8::is_utf8($str)) { return $str; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | |||||||||||
| 106 | # netbsd langinfo(CODESET) returns "646" meaning ISO-646, ie. ASCII. Must | ||||||||||
| 107 | # put that through resolve_alias() to turn it into "ascii". | ||||||||||
| 108 | # | ||||||||||
| 109 | 35 | return Encode::decode ('locale', $str, Encode::FB_CROAK()); | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 1 | 1; | |||||||||
| 113 | __END__ | ||||||||||