/usr/share/perl5/EB/Format.pm is in eekboek 2.00.03-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | #! perl
package main;
our $cfg;
our $dbh;
package EB::Format;
use strict;
our $VERSION = sprintf "%d.%03d", q$Revision: 1.41 $ =~ /(\d+)/g;
use EB;
use base qw(Exporter);
my $stdfmt0;
my $stdfmtw;
my $btwfmt0;
my $btwfmtw;
my $numpat;
my $btwpat;
my $decimalpt;
my $thousandsep;
our @EXPORT;
our $amount_width;
our $date_width;
sub numround_ieee {
# This somethimes does odd things.
# E.g. 892,5 -> 892 and 891,5 -> 892.
0 + sprintf("%.0f", $_[0]);
}
use POSIX qw(floor ceil);
sub numround_posix {
my ($val) = @_;
if ( $val < 0 ) {
ceil($val - 0.5);
}
else {
floor($val + 0.5);
}
}
use POSIX qw(floor);
my $_half;
sub numround_bankers {
# Based on Math::Round::round_even.
my $x = shift;
return 0 unless $x;
my $sign = ($x >= 0) ? 1 : -1;
$x = abs($x);
my $in = int($x);
# Round to next even if exactly 0.5.
if ( ($x - $in) == 0.5 ) {
return $sign * (($in % 2 == 0) ? $in : $in + 1);
}
unless ( defined($_half) ) {
# Determine what value to use for "one-half". Because of the
# perversities of floating-point hardware, we must use a value
# slightly larger than 1/2. We accomplish this by determining
# the bit value of 0.5 and increasing it by a small amount in
# a lower-order byte. Since the lowest-order bits are still
# zero, the number is mathematically exact.
my $halfhex = unpack('H*', pack('d', 0.5));
if ( substr($halfhex,0,2) ne '00' && substr($halfhex, -2) eq '00' ) {
# Big-endian.
substr($halfhex, -4) = '1000';
} else {
# Little-endian.
substr($halfhex, 0, 4) = '0010';
}
$_half = unpack('d', pack('H*', $halfhex));
}
$sign * POSIX::floor($x + $_half);
}
sub _setup {
assert( NUMGROUPS != AMTPRECISION, "NUMGROUPS != AMTPRECISION" );
################ BTW display format ################
$btwfmt0 = '%.' . (BTWPRECISION-2) . 'f';
$btwfmtw = '%' . BTWWIDTH . "." . (BTWPRECISION-2) . 'f';
$btwpat = qr/^([-+])?(\d+)?(?:[.,])?(\d{1,@{[BTWPRECISION-2]}})?$/;
################ Amount display format ################
$amount_width = $cfg->val(qw(text numwidth), AMTWIDTH);
if ( $amount_width =~ /^\+(\d+)$/ ) {
$amount_width = AMTWIDTH + $1;
}
elsif ( $amount_width =~ /^\-(\d+)$/ ) {
$amount_width = AMTWIDTH - $1;
}
elsif ( $amount_width =~ /^(\d+)%$/ ) {
$amount_width = int((AMTWIDTH * $1) / 100);
}
elsif ( $amount_width !~ /^\d+$/ ) {
warn("?"._T("Configuratiefout: [format]numwidth moet een getal zijn")."\n");
$amount_width = AMTWIDTH;
}
$decimalpt = $cfg->val(qw(locale decimalpt), undef);
$thousandsep = $cfg->val(qw(locale thousandsep), undef);
my $fmt = $cfg->val(qw(format amount), undef);
if ( $fmt || !defined($decimalpt) ) {
$fmt = _T("1.234,56") unless defined $fmt;
Carp::croak(__x("Configuratiefout: ongeldige waarde voor {item}",
item => "format".':'."amount")."\n")
unless $fmt =~ /^\d+([.,])\d\d$/
|| $fmt =~ /^\d+(\.\d\d\d)*(\,)\d\d$/
|| $fmt =~ /^\d+(\,\d\d\d)*(\.)\d\d$/;
if ( defined $2 ) {
$decimalpt = $2;
$thousandsep = substr($1, 0, 1);
}
else {
$decimalpt = $1;
$thousandsep = "";
}
$amount_width = length($fmt) if length($fmt) > $amount_width;
}
else {
$amount_width += int(($amount_width - AMTPRECISION - 2) / 3) if $thousandsep;
}
$stdfmt0 = '%.' . AMTPRECISION . 'f';
$stdfmtw = '%' . $amount_width . "." . AMTPRECISION . 'f';
my $sub = "";
$sub .= <<EOD;
my \$v = shift;
if ( \$v == int(\$v) && \$v >= 0 ) {
\$v = ("0" x (@{[AMTPRECISION + 1]} - length(\$v))) . \$v if length(\$v) <= @{[AMTPRECISION]};
substr(\$v, length(\$v) - @{[AMTPRECISION]}, 0) = q\000$decimalpt\000;
}
else {
\$v = sprintf("$stdfmt0", \$v/@{[AMTSCALE]});
EOD
$sub .= <<EOD if $decimalpt ne '.';
\$v =~ s/\\./$decimalpt/;
EOD
$sub .= <<EOD;
}
\$v =~ s/^\\+//;
EOD
eval("sub numfmt_plain { $sub; \$v }");
die($@) if $@;
$sub .= <<EOD if $thousandsep;
\$v = reverse(\$v);
\$v =~ s/(\\d\\d\\d)(?=\\d)(?!\\d*@{[quotemeta($decimalpt)]})/\${1}$thousandsep/g;
\$v = scalar(reverse(\$v));
EOD
eval("sub numfmt { $sub; \$v }");
die($@) if $@;
$numpat = qr/^([-+])?(\d+)?(?:[.,])?(\d{1,@{[AMTPRECISION]}})?$/;
################ Rounding Algorithms ################
my $numround = lc($cfg->val(qw(strategy round), "ieee"));
unless ( defined &{"numround_$numround"} ) {
die("?".__x("Onbekende afrondingsmethode: {meth}",
meth => $numround)."\n");
}
*numround = \&{"numround_$numround"};
################ Date display format ################
$fmt = $cfg->val(qw(format date), "YYYY-MM-DD");
$sub = "sub datefmt { \$_[0] }";
my $sub_full = "sub datefmt_full { \$_[0] }";
my $sub_plain = "sub datefmt_plain { \$_[0] }";
if ( lc($fmt) eq "dd-mm-yyyy" ) {
$sub = q<sub datefmt { join("-", reverse(split(/-/, $_[0]))) }>;
$sub_full = q<sub datefmt_full { join("-", reverse(split(/-/, $_[0]))) }>;
}
elsif ( lc($fmt) eq "dd-mm" ) {
$sub = q<sub datefmt { $_[0] =~ /(\d+)-(\d+)-(\d+)/; "$3-$2" }>;
$sub_full = q<sub datefmt_full { join("-", reverse(split(/-/, $_[0]))) }>;
}
elsif ( lc($fmt) ne "yyyy-mm-dd" ) {
die("?".__x("Ongeldige datumformaatspecificatie: {fmt}",
fmt => $fmt)."\n");
}
for ( $sub, $sub_full, $sub_plain ) {
eval($_);
die($_."\n".$@) if $@;
}
$date_width = length(datefmt("2006-01-01"));
}
sub numxform_strict {
$_ = shift;
my $err = __x("Ongeldig bedrag: {num}", num => $_);
my $sign = "";
$sign = $1 if s/^([-+])// && $1 eq '-';
# NNNN -> NNNN.00
if ( /^\d+$/ ) {
s/^0+(\d)$/$1/;
return $sign . $_ . "." . ("0" x AMTPRECISION);
}
# N,NNN -> NNNN.00
if ( /^(\d{1,@{[NUMGROUPS]}})(\,\d{@{[NUMGROUPS]}})*$/ && $1 ) {
s/\,//g;
s/^0+(\d)$/$1/;
return $sign . $_ . "." . ("0" x AMTPRECISION);
}
# N.NNN -> NNNN.00
if ( /^(\d{1,@{[NUMGROUPS]}})(\.\d{@{[NUMGROUPS]}})*$/ && $1 ) {
s/\.//g;
s/^0+(\d)$/$1/;
return $sign . $_ . "." . ("0" x AMTPRECISION);
}
# N.NNN,NN or N,NNN.NN
return $err
unless /^([\d.]+)(\,)(\d{@{[AMTPRECISION]}})$/
|| /^([\d,]+)(\.)(\d{@{[AMTPRECISION]}})$/;
my ($mant, $sep, $frac) = ( $1, $2, $3 );
# N.NNN , NN -> NNNN NN
if ( $sep eq "," ) {
$mant =~ s/\.//g
if $mant =~ /^\d{1,@{[NUMGROUPS]}}(\.\d{@{[NUMGROUPS]}})*$/;
}
# N,NNN . NN -> NNNN NN
else {
$mant =~ s/\,//g
if $mant =~ /^\d{1,@{[NUMGROUPS]}}(\,\d{@{[NUMGROUPS]}})*$/;
}
# NNNN NN -> NNNN.NN
$mant =~ s/^0+(\d)$/$1/;
return $sign . $mant . "." . $frac if $mant =~ /^\d+$/;
die("?$err\n"); # not well-formed
}
sub numxform_loose {
$_ = shift;
my $err = __x("Ongeldig getal: {num}", num => $_);
# If there's a single comma, make decimal point.
s/,/./ if /^.*,.*$/;
return $_ if /^[-+]*\d+(\.\d+)?$/;
die("?$err\n"); # not well-formed
}
sub numxform {
my ($n) = @_;
my $res = numxform_strict($n);
return $res if defined $res;
# return $n if $n =~ /^[-a+]?\d+[.,]\d+$/; # a ?
return $n if $n =~ /^[-+]?\d+[.,]\d+$/;
return undef;
}
sub amount($) {
my $val = shift;
my $debug = $cfg->val(__PACKAGE__, "debugexpr", 0);
if ( $val =~ /.[-+*\/\(\)]/ ) {
print STDERR ("val \"$val\" -> ") if $debug;
$val =~ s/([.,\d]+)/numxform_loose($1)/ge;
print STDERR ("\"$val\" -> ") if $debug;
my $res = eval($val);
warn("$val: $@"), return undef if $debug && $@;
return undef if $@;
$val = sprintf($stdfmt0, $res);
print STDERR ("$val\n") if $debug;
}
else {
return undef
unless $val = numxform_strict($val); # fortunately, 0.00 is true
}
return undef unless $val =~ $numpat;
my ($s, $w, $f) = ($1 || "", $2 || 0, $3 || 0);
$f .= "0" x (AMTPRECISION - length($f));
return 0 + ($s.$w.$f);
}
sub numfmtw {
my $v = shift;
if ( $v == int($v) && $v >= 0 ) {
$v = ("0" x (AMTPRECISION - length($v) + 1)) . $v if length($v) <= AMTPRECISION;
$v = (" " x (AMTWIDTH - length($v))) . $v if length($v) < AMTWIDTH;
substr($v, length($v) - AMTPRECISION, 0) = $decimalpt;
}
else {
$v = sprintf($stdfmtw, $v/AMTSCALE);
$v =~ s/\./$decimalpt/;
}
$v;
}
#### UNUSED
sub numfmtv {
my $v = shift;
if ( $v == int($v) && $v >= 0 ) {
$v = ("0" x (AMTPRECISION - length($v) + 1)) . $v if length($v) <= AMTPRECISION;
$v = (" " x ($_[0] - length($v))) . $v if length($v) < $_[0];
substr($v, length($v) - AMTPRECISION, 0) = $decimalpt;
}
else {
$v = sprintf('%'.$_[0].'.'.AMTPRECISION.'f', $v/AMTSCALE);
$v =~ s/\./$decimalpt/;
}
$v;
}
sub btwfmt {
my $v = sprintf($btwfmt0, 100*$_[0]/BTWSCALE);
$v =~ s/\./$decimalpt/;
$v;
}
sub btwpat { $btwpat }
################ Code ################
push( @EXPORT,
qw(amount numround btwfmt),
qw($amount_width numfmt numfmt_plain),
qw($date_width datefmt datefmt_full datefmt_plain),
);
_setup();
1;
|