/usr/share/perl5/XBase.pm is in libdbd-xbase-perl 1:1.05-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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 | use XBase::Memo;
=head1 NAME
XBase - Perl module for reading and writing the dbf files
=cut
# ############
package XBase;
use 5.010;
use strict;
use XBase::Base; # will give us general methods
# ##############
# General things
use vars qw( $VERSION $errstr $CLEARNULLS @ISA );
@ISA = qw( XBase::Base );
$VERSION = '1.02';
$CLEARNULLS = 1; # Cut off white spaces from ends of char fields
*errstr = \$XBase::Base::errstr;
# #########################################
# Open, read_header, init_memo_field, close
# Open the specified file or try to append the .dbf suffix.
sub open {
my ($self) = shift;
my %options;
if (scalar(@_) % 2) { $options{'name'} = shift; }
$self->{'openoptions'} = { %options, @_ };
my %locoptions;
@locoptions{ qw( name readonly ignorememo fh ) }
= @{$self->{'openoptions'}}{ qw( name readonly ignorememo fh ) };
my $filename = $locoptions{'name'};
if ($filename eq '-') {
return $self->SUPER::open(%locoptions);
}
for my $ext ('', '.dbf', '.DBF') {
if (-f $filename.$ext) {
$locoptions{'name'} = $filename.$ext;
$self->NullError();
return $self->SUPER::open(%locoptions);
}
}
$locoptions{'name'} = $filename;
return $self->SUPER::open(%locoptions); # for nice error message
}
# We have to provide way to fill up the object upon open
sub read_header {
my $self = shift;
my $fh = $self->{'fh'};
my $header; # read the header
$self->read($header, 32) == 32 or do {
__PACKAGE__->Error("Error reading header of $self->{'filename'}: $!\n");
return;
};
@{$self}{ qw( version last_update num_rec
header_len record_len encrypted ) }
= unpack 'Ca3Vvv@15a1', $header; # parse the data
### if (0 and $self->{'encrypted'} ne "\000")
### { __PACKAGE__->Error("We don't support encrypted files, sorry.\n"); return; };
my $header_len = $self->{'header_len'};
my ($names, $types, $lengths, $decimals) = ( [], [], [], [] );
my ($unpacks, $readproc, $writeproc) = ( [], [], [] );
my $lastoffset = 1;
while ($self->tell() < $header_len - 1) { # read the field desc's
my $field_def;
$self->read($field_def, 1);
last if $field_def eq "\r"; # we have found the terminator
my $read = $self->read($field_def, 31, 1);
if ($read != 31) {
__PACKAGE__->Error("Error reading field description: $!\n");
return;
}
my ($name, $type, $length, $decimal)
= unpack 'A11a1 @16CC', $field_def;
my ($rproc, $wproc);
if ($type eq 'C') { # char
# fixup for char length > 256
if ($decimal and not $self->{'openoptions'}{'nolongchars'}) {
$length += 256 * $decimal; $decimal = 0;
}
$rproc = sub { my $value = $_[0];
if ($self->{'ChopBlanks'}) {
$value =~ s/\s+$//;
}
return $value;
( $value eq '' ? undef : $value );
};
$wproc = sub { my $value = shift;
sprintf '%-*.*s', $length, $length,
(defined $value ? $value : '');
};
}
elsif ($type eq 'L') { # logical (boolean)
$rproc = sub { my $value = shift;
if ($value =~ /^[YyTt]$/) { return 1; }
if ($value =~ /^[NnFf]$/) { return 0; }
undef;
};
$wproc = sub { my $value = shift;
sprintf '%-*.*s', $length, $length,
(defined $value ? ( $value ? 'T' : 'F') : '?');
};
}
elsif ($type =~ /^[NFD]$/) { # numbers, dates
$rproc = sub { my $value = shift;
($value =~ /\d/) ? $value + 0 : undef;
};
$wproc = sub { my $value = shift;
if (defined $value) {
substr(sprintf('%*.*f', $length, $decimal, ($value + 0)), -$length);
} else {
' ' x $length;
}
};
}
elsif ($type eq 'I') { # Fox integer
$rproc = sub { unpack 'V', shift; };
$wproc = sub { pack 'V', shift; };
}
elsif ($type eq 'B') { # Fox double
$rproc = sub { unpack 'd', reverse scalar shift; };
$wproc = sub { reverse scalar pack 'd', shift; };
}
elsif ($type =~ /^[MGP]$/) { # memo fields
my $memo = $self->{'memo'};
if (not defined $memo and not $self->{'openoptions'}{'ignorememo'}) {
$memo = $self->{'memo'} = $self->init_memo_field() or return;
}
if (defined $memo and $length == 10) {
if (ref $memo eq 'XBase::Memo::Apollo') {
$rproc = sub { $memo->read_record(shift); };
$wproc = sub { $memo->write_record(shift); };
} else {
$rproc = sub {
my $value = shift;
return if not $value =~ /\d/ or $value < 0;
$memo->read_record($value - 1) if defined $memo;
};
$wproc = sub {
my $value = $memo->write_record(-1, $type, $_[0]) if defined $memo and defined $_[0] and $_[0] ne '';
sprintf '%*.*s', $length, $length,
(defined $value ? $value + 1: ''); };
}
}
elsif (defined $memo and $length == 4) {
$rproc = sub {
my $val = unpack('V', $_[0]) - 1;
return if $val < 0;
$memo->read_record($val) if defined $memo;
};
$wproc = sub {
my $value = $memo->write_record(-1, $type, shift) if defined $memo;
pack 'V', (defined $value ? $value + 1: 0); };
} else {
$rproc = sub { undef; };
$wproc = sub { ' ' x $length; };
}
}
elsif ($type eq 'T') { # time fields
# datetime is stored internally as two
# four-byte numbers; the first is the day under
# the Julian Day System (JDS) and the second is
# the number of milliseconds since midnight
$rproc = sub {
my ($day, $time) = unpack 'VV', $_[0];
my $localday = $day - 2440588;
my $localtime = $localday * 24 * 3600;
$localtime += $time / 1000;
### print STDERR "day,time: ($day,$time -> $localtime)\n";
return $localtime;
my $localdata = "[$localday] $localtime: @{[localtime($localtime)]}";
my $usec = $time % 1000;
my $hour = int($time / 3600000);
my $min = int(($time % 3600000) / 60000);
my $sec = int(($time % 60000) / 1000);
return "$day($localdata)-$hour:$min:$sec.$usec";
};
$wproc = sub {
my $localtime = shift;
my $day = int($localtime / (24 * 3600)) + 2440588;
my $time = int(($localtime % (3600 * 24)) * 1000);
### print STDERR "day,time: ($localtime -> $day,$time)\n";
return pack 'VV', $day, $time;
}
}
elsif ($type eq '0') { # SNa : field "_NULLFLAGS"
$rproc = $wproc = sub { '' };
} elsif ($type eq 'Y') { # Fox money
$rproc = sub {
my ($x, $y) = unpack 'VV', scalar shift;
if ($y & 0x80000000) {
- ($y ^ 0xffffffff) * (2**32 / 10**$decimal) - (($x - 1) ^ 0xffffffff) / 10**$decimal;
} else {
$y * (2**32 / 10**$decimal) + $x / 10**$decimal;
}
};
$wproc = sub {
my $value = shift;
if ($value < 0) {
pack 'VV',
(-$value * 10**$decimal + 1) ^ 0xffffffff,
(-$value * 10**$decimal / 2**32) ^ 0xffffffff;
} else {
pack 'VV',
($value * 10**$decimal) % 2**32,
(($value * 10**$decimal) >> 32);
}
};
}
$name =~ s/[\000 ].*$//s;
$name = uc $name; # no locale yet
push @$names, $name;
push @$types, $type;
push @$lengths, $length;
push @$decimals, $decimal;
push @$unpacks, '@' . $lastoffset . 'a' . $length;
push @$readproc, $rproc;
push @$writeproc, $wproc;
$lastoffset += $length;
}
if ($lastoffset > $self->{'record_len'}
and not defined $self->{'openoptions'}{'nolongchars'}) {
$self->seek_to(0);
$self->{'openoptions'}{'nolongchars'} = 1;
return $self->read_header;
}
if ($lastoffset != $self->{'record_len'}
and not defined $self->{'openoptions'}{'ignorebadheader'}) {
__PACKAGE__->Error("Missmatch in header of $self->{'filename'}: record_len $self->{'record_len'} but offset $lastoffset\n");
return;
}
if ($self->{'openoptions'}{'recompute_lastrecno'}) {
$self->{num_rec} = int(((-s $self->{'fh'}) - $self->{header_len})
/ $self->{record_len});
}
my $hashnames = {}; # create name-to-num_of_field hash
@{$hashnames}{ reverse @$names } = reverse ( 0 .. $#$names );
# now it's the time to store the values to the object
@{$self}{ qw( field_names field_types field_lengths field_decimals
hash_names last_field field_unpacks
field_rproc field_wproc ChopBlanks) } =
( $names, $types, $lengths, $decimals,
$hashnames, $#$names, $unpacks,
$readproc, $writeproc, $CLEARNULLS );
1; # return true since everything went fine
}
# When there is a memo field in dbf, try to open the memo file
sub init_memo_field {
my $self = shift;
return $self->{'memo'} if defined $self->{'memo'};
require XBase::Memo;
my %options = ( 'dbf_version' => $self->{'version'},
'memosep' => $self->{'openoptions'}{'memosep'} );
if (defined $self->{'openoptions'}{'memofile'}) {
return XBase::Memo->new($self->{'openoptions'}{'memofile'}, %options);
}
for (qw( dbt DBT fpt FPT smt SMT dbt )) {
my $memo;
my $memoname = $self->{'filename'};
($memoname =~ s/\.dbf$/.$_/i or $memoname =~ s/(\.dbf)?$/.$_/i)
and $memo = XBase::Memo->new($memoname, %options)
and return $memo;
}
return;
}
# Close the file (and memo)
sub close {
my $self = shift;
if (defined $self->{'memo'}) {
$self->{'memo'}->close(); delete $self->{'memo'};
}
$self->SUPER::close();
}
# ###############
# Little decoding
sub version { shift->{'version'}; }
sub last_record { shift->{'num_rec'} - 1; }
sub last_field { shift->{'last_field'}; }
# List of field names, types, lengths and decimals
sub field_names { @{shift->{'field_names'}}; }
sub field_types { @{shift->{'field_types'}}; }
sub field_lengths { @{shift->{'field_lengths'}}; }
sub field_decimals { @{shift->{'field_decimals'}}; }
# Return field number for field name
sub field_name_to_num {
my ($self, $name) = @_; $self->{'hash_names'}{uc $name};
}
sub field_type {
my ($self, $name) = @_;
defined (my $num = $self->field_name_to_num($name)) or return;
($self->field_types)[$num];
}
sub field_length {
my ($self, $name) = @_;
defined (my $num = $self->field_name_to_num($name)) or return;
($self->field_lengths)[$num];
}
sub field_decimal {
my ($self, $name) = @_;
defined (my $num = $self->field_name_to_num($name)) or return;
($self->field_decimals)[$num];
}
# #############################
# Header, field and record info
# Returns (not prints!) the info about the header of the object
*header_info = \&get_header_info;
sub get_header_info {
my $self = shift;
my $hexversion = sprintf '0x%02x', $self->version;
my $longversion = $self->get_version_info()->{'string'};
my $printdate = $self->get_last_change;
my $numfields = $self->last_field() + 1;
my $result = sprintf <<"EOF";
Filename: $self->{'filename'}
Version: $hexversion ($longversion)
Num of records: $self->{'num_rec'}
Header length: $self->{'header_len'}
Record length: $self->{'record_len'}
Last change: $printdate
Num fields: $numfields
Field info:
Num Name Type Len Decimal
EOF
return join '', $result, map { $self->get_field_info($_) }
(0 .. $self->last_field);
}
# Return info about field in dbf file
sub get_field_info {
my ($self, $num) = @_;
sprintf "%d.\t%-16.16s%-8.8s%-8.8s%s\n", $num + 1,
map { $self->{$_}[$num] }
qw( field_names field_types field_lengths field_decimals );
}
# Return last_change item as printable string
sub get_last_change {
my $self = shift;
my $date = $self;
if (ref $self) { $date = $self->{'last_update'}; }
my ($year, $mon, $day) = unpack 'C3', $date;
$year += ($year >= 70) ? 1900 : 2000;
return "$year/$mon/$day";
}
# Return text description of the version value
sub get_version_info {
my $version = shift;
$version = $version->version() if ref $version;
my $result = {};
$result->{'vbits'} = $version & 0x07;
if ($version == 0x30 or $version == 0xf5) {
$result->{'vbits'} = 5; $result->{'foxpro'} = 1;
} elsif ($version & 0x08) {
$result->{'vbits'} = 4; $result->{'memo'} = 1;
} elsif ($version & 0x80) {
$result->{'dbt'} = 1;
}
my $string = "ver. $result->{'vbits'}";
if (exists $result->{'foxpro'}) {
$string .= " (FoxPro)";
}
if (exists $result->{'memo'}) {
$string .= " with memo file";
} elsif (exists $result->{'dbt'}) {
$string .= " with DBT file";
}
$result->{'string'} = $string;
$result;
}
# Print the records as colon separated fields
sub dump_records {
my $self = shift;
my %options = ( 'rs' => "\n", 'fs' => ':', 'undef' => '' );
my %inoptions = @_;
for my $key (keys %inoptions) {
my $value = $inoptions{$key};
my $outkey = lc $key;
$outkey =~ s/[^a-z]//g;
$options{$outkey} = $value;
}
my ($rs, $fs, $undef, $fields, $table)
= @options{ qw( rs fs undef fields table ) };
if (defined $table) {
eval 'use Data::ShowTable';
if ($@) {
warn "You requested table output format but the module Data::ShowTable doesn't\nseem to be installed correctly. Falling back to standard\n";
$table = undef;
} else {
delete $options{'rs'};
delete $options{'fs'};
}
}
my @fields = ();
my @unknown_fields;
if (defined $fields) {
if (ref $fields eq 'ARRAY') {
@fields = @$fields;
} else {
@fields = split /\s*,\s*/, $fields;
my $i = 0;
while ($i < @fields) {
if (defined $self->field_name_to_num($fields[$i])) {
$i++;
} elsif ($fields[$i] =~ /^(.*)-(.*)/) {
local $^W = 0;
my @allfields = $self->field_names;
my ($start, $end) = ($1, $2);
if ($start eq '') {
$start = $allfields[0];
}
if ($end eq '') {
$end = $allfields[$#allfields];
}
my $start_num = $self->field_name_to_num($start);
my $end_num = $self->field_name_to_num($end);
if ($start ne '' and not defined $start_num) {
push @unknown_fields, $start;
}
if ($end ne '' and not defined $end_num) {
push @unknown_fields, $end;
}
unless (defined $start and defined $end) {
$start = 0; $end = -1;
}
splice @fields, $i, 1, @allfields[$start_num .. $end_num];
} else {
push @unknown_fields, $fields[$i];
$i++;
}
}
}
}
if (@unknown_fields) {
$self->Error("There have been unknown fields `@unknown_fields' specified.\n");
return 0;
}
my $cursor = $self->prepare_select(@fields);
my @record;
if (defined $table) {
local $^W = 0;
&ShowBoxTable( $cursor->names(), [], [],
sub {
if ($_[0]) { $cursor->rewind(); }
else { $cursor->fetch() }
});
} else {
while (@record = $cursor->fetch) {
print join($fs, map { defined $_ ? $_ : $undef } @record), $rs;
}
}
1;
}
# ###################
# Reading the records
# Returns fields of the specified record; parameters and number of the
# record (starting from 0) and optionally names of the required
# fields. If no names are specified, all fields are returned. The
# first value in the returned list if always 1/0 deleted flag. Returns
# empty list on error.
sub get_record {
my ($self, $num) = (shift, shift);
$self->NullError();
$self->get_record_nf( $num, map { $self->field_name_to_num($_); } @_);
}
*get_record_as_hash = \&get_record_hash;
sub get_record_hash {
my ($self, $num) = @_;
my @list = $self->get_record($num) or return;
my $hash = {};
@{$hash}{ '_DELETED', $self->field_names() } = @list;
return %$hash if wantarray;
$hash;
}
sub get_record_nf {
my ($self, $num, @fieldnums) = @_;
my $data = $self->read_record($num) or return;
if (not @fieldnums) {
@fieldnums = ( 0 .. $self->last_field );
}
my $unpack = join ' ', '@0a1',
map { my $e;
defined $_ and $e = $self->{'field_unpacks'}[$_];
defined $e ? $e : '@0a0'; } @fieldnums;
my $rproc = $self->{'field_rproc'};
my @fns = (\&_read_deleted, map { (defined $_ and defined $rproc->[$_]) ? $rproc->[$_] : sub { undef; }; } @fieldnums);
my @out = unpack $unpack, $data;
### if ($self->{'encrypted'} ne "\000") {
### for my $data (@out) {
### for (my $i = 0; $i < length($data); $i++) {
### ## my $num = unpack 'C', substr($data, $i, 1);
### ## substr($data, $i, 1) = pack 'C', (($num >> 3) | ($num << 5) ^ 020);
### my $num = unpack 'C', substr($data, $i, 1);
### substr($data, $i, 1) = pack 'C', (($num >> 1) | ($num << 7) ^ 052);
### }
### }
### }
for (@out) { $_ = &{ shift @fns }($_); }
@out;
}
# Processing on read
sub _read_deleted {
my $value = shift;
if ($value eq '*') { return 1; } elsif ($value eq ' ') { return 0; }
undef;
}
sub get_all_records {
my $self = shift;
my $cursor = $self->prepare_select(@_);
my $result = [];
my @record;
while (@record = $cursor->fetch())
{ push @$result, [ @record ]; }
$result;
}
# #############
# Write records
# Write record, values of the fields are in the argument list.
# Record is always undeleted
sub set_record {
my ($self, $num, @data) = @_;
$self->NullError();
my $wproc = $self->{'field_wproc'};
if (defined $self->{'attached_index_columns'}) {
my @nfs = keys %{$self->{'attached_index_columns'}};
my ($del, @old_data) = $self->get_record_nf($num, @nfs);
local $^W = 0;
for my $nf (@nfs) {
if ($old_data[$nf] ne $data[$nf]) {
for my $idx (@{$self->{'attached_index_columns'}{$nf}}) {
$idx->delete($old_data[$nf], $num + 1);
$idx->insert($data[$nf], $num + 1);
}
}
}
}
for (my $i = 0; $i <= $#$wproc; $i++) {
$data[$i] = &{ $wproc->[$i] }($data[$i]);
}
unshift @data, ' ';
### if ($self->{'encrypted'} ne "\000") {
### for my $data (@data) {
### for (my $i = 0; $i < length($data); $i++) {
### my $num = unpack 'C', substr($data, $i, 1);
### substr($data, $i, 1) = pack 'C', (($num << 3) | ($num >> 5) ^ 020);
### }
### }
### }
$self->write_record($num, @data);
}
# Write record, fields are specified as hash, unspecified are set to
# undef/empty
sub set_record_hash {
my ($self, $num, %data) = @_;
$self->NullError();
$self->set_record($num, map { $data{$_} } $self->field_names );
}
# Write record, fields specified as hash, unspecified will be
# unchanged
sub update_record_hash {
my ($self, $num) = ( shift, shift );
$self->NullError();
my %olddata = $self->get_record_hash($num);
return unless %olddata;
$self->set_record_hash($num, %olddata, @_);
}
# Actually write the data (calling XBase::Base::write_record) and keep
# the overall structure of the file correct;
sub write_record {
my ($self, $num) = (shift, shift);
my $ret = $self->SUPER::write_record($num, @_) or return;
if ($num > $self->last_record) {
$self->SUPER::write_record($num + 1, "\x1a"); # add EOF
$self->update_last_record($num) or return;
}
$self->update_last_change or return;
$ret;
}
# Delete and undelete record
sub delete_record {
my ($self, $num) = @_;
$self->NullError();
$self->write_record($num, "*");
}
sub undelete_record {
my ($self, $num) = @_;
$self->NullError();
$self->write_record($num, " ");
}
# Update the last change date
sub update_last_change {
my $self = shift;
return 1 if defined $self->{'updated_today'};
my ($y, $m, $d) = (localtime)[5, 4, 3]; $m++; $y -= 100 if $y >= 100;
$self->write_to(1, pack "C3", ($y, $m, $d)) or return;
$self->{'updated_today'} = 1;
}
# Update the number of records
sub update_last_record {
my ($self, $last) = @_;
$last++;
$self->write_to(4, pack "V", $last);
$self->{'num_rec'} = $last;
}
# Creating new dbf file
sub create {
XBase->NullError();
my $class = shift;
my %options = @_;
if (ref $class) {
%options = ( %$class, %options ); $class = ref $class;
}
my $version = $options{'version'};
if (not defined $version) {
if (defined $options{'memofile'}
and $options{'memofile'} =~ /\.fpt$/i) {
$version = 0xf5;
} else {
$version = 3;
}
}
my $key;
for $key ( qw( field_names field_types field_lengths field_decimals ) ) {
if (not defined $options{$key}) {
__PACKAGE__->Error("Tag $key must be specified when creating new table\n");
return;
}
}
my $needmemo = 0;
my $fieldspack = '';
my $record_len = 1;
my $i;
for $i (0 .. $#{$options{'field_names'}}) {
my $name = uc $options{'field_names'}[$i];
$name = "FIELD$i" unless defined $name;
$name .= "\0";
my $type = $options{'field_types'}[$i];
$type = 'C' unless defined $type;
my $length = $options{'field_lengths'}[$i];
my $decimal = $options{'field_decimals'}[$i];
if (not defined $length) { # defaults
if ($type eq 'C') { $length = 64; }
elsif ($type =~ /^[TD]$/) { $length = 8; }
elsif ($type =~ /^[NF]$/) { $length = 8; }
}
# force correct lengths
if ($type =~ /^[MBGP]$/) { $length = 10; $decimal = 0; }
elsif ($type eq 'L') { $length = 1; $decimal = 0; }
elsif ($type eq 'Y') { $length = 8; $decimal = 4; }
if (not defined $decimal) {
$decimal = 0;
}
$record_len += $length;
my $offset = $record_len;
if ($type eq 'C') {
$decimal = int($length / 256);
$length %= 256;
}
$fieldspack .= pack 'a11a1VCCvCvCa7C', $name, $type, $offset,
$length, $decimal, 0, 0, 0, 0, '', 0;
if ($type eq 'M') {
$needmemo = 1;
if ($version != 0x30) {
$version |= 0x80;
}
}
}
$fieldspack .= "\x0d";
{
local $^W = 0;
$options{'codepage'} += 0;
}
my $header = pack 'C CCC V vvv CC a12 CC v',
$version,
0, 0, 0,
0,
(32 + length $fieldspack), $record_len, 0,
0, 0,
'',
0, $options{'codepage'},
0;
$header .= $fieldspack;
$header .= "\x1a";
my $tmp = $class->new();
my $basename = $options{'name'};
$basename =~ s/\.dbf$//i;
my $newname = $options{'name'};
if (defined $newname and not $newname =~ /\.dbf$/) {
$newname .= '.dbf';
}
$tmp->create_file($newname, 0700) or return;
$tmp->write_to(0, $header) or return;
$tmp->update_last_change();
$tmp->close();
if ($needmemo) {
require XBase::Memo;
my $dbtname = $options{'memofile'};
if (not defined $dbtname) {
$dbtname = $options{'name'};
if ($version == 0x30 or $version == 0xf5) {
$dbtname =~ s/\.DBF$/.FPT/ or $dbtname =~ s/(\.dbf)?$/.fpt/;
} else {
$dbtname =~ s/\.DBF$/.DBT/ or $dbtname =~ s/(\.dbf)?$/.dbt/;
}
}
my $dbttmp = XBase::Memo->new();
my $memoversion = ($version & 15);
$memoversion = 5 if $version == 0x30;
$dbttmp->create('name' => $dbtname,
'version' => $memoversion,
'dbf_filename' => $basename) or return;
}
return $class->new($options{'name'});
}
# Drop the table
sub drop {
my $self = shift;
my $filename = $self;
if (ref $self) {
if (defined $self->{'memo'}) {
$self->{'memo'}->drop();
delete $self->{'memo'};
}
return $self->SUPER::drop();
}
XBase::Base::drop($filename);
}
# Lock and unlock
sub locksh {
my $self = shift;
my $ret = $self->SUPER::locksh or return;
if (defined $self->{'memo'}) {
unless ($self->{'memo'}->locksh()) {
$self->SUPER::unlock;
return;
}
}
$ret;
}
sub lockex {
my $self = shift;
my $ret = $self->SUPER::lockex or return;
if (defined $self->{'memo'}) {
unless ($self->{'memo'}->lockex()) {
$self->SUPER::unlock;
return;
}
}
$ret;
}
sub unlock {
my $self = shift;
$self->{'memo'}->unlock() if defined $self->{'memo'};
$self->SUPER::unlock;
}
#
# Attaching index file
#
sub attach_index {
my ($self, $indexfile) = @_;
require XBase::Index;
my $index = $self->XBase::Index::new($indexfile) or do {
print STDERR XBase->errstr, "\n";
$self->Error(XBase->errstr);
return;
};
print "Got index $index\n" if $XBase::Index::VERBOSE;
my @tags = $index->tags;
my @indexes;
if (@tags) {
for my $tag (@tags) {
my $index = $self->XBase::Index::new($indexfile,
'tag' => $tag)
or do {
print STDERR XBase->errstr, "\n";
$self->Error(XBase->errstr);
return;
};
push @indexes, $index;
}
} else {
@indexes = ( $index );
}
for my $idx (@indexes) {
my $key = $idx->{'key_string'};
my $num = $self->field_name_to_num($key);
print "Got key string $key -> $num\n" if $XBase::Index::VERBOSE;
$self->{'attached_index'} = []
unless defined $self->{'attached_index'};
push @{$self->{'attached_index'}}, $idx;
push @{$self->{'attached_index_columns'}{$num}}, $idx;
}
1;
}
#
# Cursory select
#
sub prepare_select {
my $self = shift;
my $fieldnames = [ @_ ];
if (not @_) { $fieldnames = [ $self->field_names ] };
my $fieldnums = [ map { $self->field_name_to_num($_); } @$fieldnames ];
return bless [ $self, undef, $fieldnums, $fieldnames ], 'XBase::Cursor';
# object, recno, field numbers, field names
}
sub prepare_select_nf {
my $self = shift;
my @fieldnames = $self->field_names;
if (@_) { @fieldnames = @fieldnames[ @_ ] }
return $self->prepare_select(@fieldnames);
}
sub prepare_select_with_index {
my ($self, $file) = ( shift, shift );
my @tagopts = ();
if (ref $file eq 'ARRAY') { ### this is suboptimal
### interface but should suffice for the moment
@tagopts = ('tag' => $file->[1]);
if (defined $file->[2]) {
push @tagopts, ('type' => $file->[2]);
}
$file = $file->[0];
}
my $fieldnames = [ @_ ];
if (not @_) { $fieldnames = [ $self->field_names ] };
my $fieldnums = [ map { $self->field_name_to_num($_); } @$fieldnames ];
require XBase::Index;
my $index = new XBase::Index $file, 'dbf' => $self, @tagopts or
do { $self->Error(XBase->errstr); return; };
$index->prepare_select or
do { $self->Error($index->errstr); return; };
return bless [ $self, undef, $fieldnums, $fieldnames, $index ],
'XBase::IndexCursor';
# object, recno, field numbers, field names, index file
}
package XBase::Cursor;
use vars qw( @ISA );
@ISA = qw( XBase::Base );
sub fetch {
my $self = shift;
my ($xbase, $recno, $fieldnums, $fieldnames) = @$self;
if (defined $recno) { $recno++; }
else { $recno = 0; }
my $lastrec = $xbase->last_record;
while ($recno <= $lastrec) {
my ($del, @result) = $xbase->get_record_nf($recno, @$fieldnums);
if (@result and not $del) {
$self->[1] = $recno;
return @result;
}
$recno++;
}
return;
}
sub fetch_hashref {
my $self = shift;
my @data = $self->fetch;
my $hashref = {};
if (@data) {
@{$hashref}{ @{$self->[3]} } = @data;
return $hashref;
}
return;
}
sub last_fetched {
shift->[1];
}
sub table {
shift->[0];
}
sub names {
shift->[3];
}
sub rewind {
shift->[1] = undef; '0E0';
}
sub attach_index {
my $self = shift;
require XBase::Index;
}
package XBase::IndexCursor;
use vars qw( @ISA );
@ISA = qw( XBase::Cursor );
sub find_eq {
my $self = shift;
$self->[4]->prepare_select_eq(shift);
}
sub fetch {
my $self = shift;
my ($xbase, $recno, $fieldnums, $fieldnames, $index) = @$self;
my ($key, $val);
while (($key, $val) = $index->fetch) {
my ($del, @result) = $xbase->get_record_nf($val - 1, @$fieldnums);
unless ($del) {
$self->[1] = $val;
return @result;
}
}
return;
}
# Indexed number the records starting from one, not zero.
sub last_fetched {
shift->[1] - 1;
}
1;
__END__
=head1 SYNOPSIS
use XBase;
my $table = new XBase "dbase.dbf" or die XBase->errstr;
for (0 .. $table->last_record) {
my ($deleted, $id, $msg)
= $table->get_record($_, "ID", "MSG");
print "$id:\t$msg\n" unless $deleted;
}
=head1 DESCRIPTION
This module can read and write XBase database files, known as dbf in
dBase and FoxPro world. It also reads memo fields from the dbt and fpt
files, if needed. An alpha code of reading index support for ndx, ntx,
mdx, idx and cdx is available for testing -- see the DBD::Index(3) man
page. Module XBase provides simple native interface to XBase files.
For DBI compliant database access, see the DBD::XBase and DBI modules
and their man pages.
The following methods are supported by XBase module:
=head2 General methods
=over 4
=item new
Creates the XBase object, loads the info about the table form the dbf
file. The first parameter should be the name of existing dbf file
(table, in fact) to read. A suffix .dbf will be appended if needed.
This method creates and initializes new object, will also check for
memo file, if needed.
The parameters can also be specified in the form of hash: value of
B<name> is then the name of the table, other flags supported are:
B<memofile> specifies non standard name for the associated memo file.
By default it's the name of the dbf file, with extension dbt or fpt.
B<ignorememo> ignore memo file at all. This is useful if you've lost
the dbt file and you do not need it. Default is false.
B<memosep> separator of memo records in the dBase III dbt files. The
standard says it should be C<"\x1a\x1a">. There are however
implamentations that only put in one C<"\x1a">. XBase.pm tries to
guess which is valid for your dbt but if it fails, you can tell it
yourself.
B<nolongchars> prevents XBase to treat the decimal value of character
fields as high byte of the length -- there are some broken products
around producing character fields with decimal values set.
my $table = new XBase "table.dbf" or die XBase->errstr;
my $table = new XBase "name" => "table.dbf",
"ignorememo" => 1;
B<recompute_lastrecno> forces XBase.pm to disbelieve the information
about the number of records in the header of the dbf file and
recompute the number of records. Use this only if you know that
some other software of yours produces incorrect headers.
=item close
Closes the object/file, no arguments.
=item create
Creates new database file on disk and initializes it with 0 records.
A dbt (memo) file will be also created if the table contains some memo
fields. Parameters to create are passed as hash.
You can call this method as method of another XBase object and then
you only need to pass B<name> value of the hash; the structure
(fields) of the new file will be the same as of the original object.
If you call B<create> using class name (XBase), you have to (besides
B<name>) also specify another four values, each being a reference
to list: B<field_names>, B<field_types>, B<field_lengths> and
B<field_decimals>. The field types are specified by one letter
strings (C, N, L, D, ...). If you set some value as undefined, create
will make it into some reasonable default.
my $newtable = $table->create("name" => "copy.dbf");
my $newtable = XBase->create("name" => "copy.dbf",
"field_names" => [ "ID", "MSG" ],
"field_types" => [ "N", "C" ],
"field_lengths" => [ 6, 40 ],
"field_decimals" => [ 0, undef ]);
Other attributes are B<memofile> for non standard memo file location,
B<codepage> to set the codepage flag in the dbf header (it does not
affect how XBase.pm reads or writes the data though, just to make
FoxPro happy), and B<version> to force different version of the dbt
(dbt) file. The default is the version of the object from which you
create the new one, or 3 if you call this as class method (XBase->create).
The new file mustn't exist yet -- XBase will not allow you to
overwrite existing table. Use B<drop> (or unlink) to delete it first.
=item drop
This method closes the table and deletes it on disk (including
associated memo file, if there is any).
=item last_record
Returns number of the last record in the file. The lines deleted but
present in the file are included in this number.
=item last_field
Returns number of the last field in the file, number of fields minus 1.
=item field_names, field_types, field_lengths, field_decimals
Return list of field names and so on for the dbf file.
=item field_type, field_length, field_decimal
For a field name, returns the appropriate value. Returns undef if the
field doesn't exist in the table.
=back
=head2 Reading the data one by one
When dealing with the records one by one, reading or writing (the
following six methods), you have to specify the number of the record
in the file as the first argument. The range is
C<0 .. $table-E<gt>last_record>.
=over 4
=item get_record
Returns a list of data (field values) from the specified record (line
of the table). The first parameter in the call is the number of the
record. If you do not specify any other parameters, all fields are
returned in the same order as they appear in the file. You can also
put list of field names after the record number and then only those
will be returned. The first value of the returned list is always the
1/0 C<_DELETED> value saying whether the record is deleted or not, so
on success, B<get_record> never returns empty list.
=item get_record_nf
Instead if the names of the fields, you can pass list of numbers of
the fields to read.
=item get_record_as_hash
Returns hash (in list context) or reference to hash (in scalar
context) containing field values indexed by field names. The name of
the deleted flag is C<_DELETED>. The only parameter in the call is
the record number. The field names are returned as uppercase.
=back
=head2 Writing the data
All three writing methods always undelete the record. On success they
return true -- the record number actually written.
=over 4
=item set_record
As parameters, takes the number of the record and the list of values
of the fields. It writes the record to the file. Unspecified fields
(if you pass less than you should) are set to undef/empty.
=item set_record_hash
Takes number of the record and hash as parameters, sets the fields,
unspecified are undefed/emptied.
=item update_record_hash
Like B<set_record_hash> but fields that do not have value specified
in the hash retain their value.
=back
To explicitly delete/undelete a record, use methods B<delete_record>
or B<undelete_record> with record number as a parameter.
Assorted examples of reading and writing:
my @data = $table->get_record(3, "jezek", "krtek");
my $hashref = $table->get_record_as_hash(38);
$table->set_record_hash(8, "jezek" => "jezecek",
"krtek" => 5);
$table->undelete_record(4);
This is a code to update field MSG in record where ID is 123.
use XBase;
my $table = new XBase "test.dbf" or die XBase->errstr;
for (0 .. $table->last_record) {
my ($deleted, $id) = $table->get_record($_, "ID")
die $table->errstr unless defined $deleted;
next if $deleted;
$table->update_record_hash($_, "MSG" => "New message")
if $id == 123;
}
=head2 Sequentially reading the file
If you plan to sequentially walk through the file, you can create
a cursor first and then repeatedly call B<fetch> to get next record.
=over 4
=item prepare_select
As parameters, pass list of field names to return, if no parameters,
the following B<fetch> will return all fields.
=item prepare_select_with_index
The first parameter is the file name of the index file, the rest is
as above. For index types that can hold more index structures in on
file, use arrayref instead of the file name and in that array include
file name and the tag name, and optionaly the index type.
The B<fetch> will then return records in the ascending order,
according to the index.
=back
Prepare will return object cursor, the following method are methods of
the cursor, not of the table.
=over 4
=item fetch
Returns the fields of the next available undeleted record. The list
thus doesn't contain the C<_DELETED> flag since you are guaranteed
that the record is not deleted.
=item fetch_hashref
Returns a hash reference of fields for the next non deleted record.
=item last_fetched
Returns the number of the record last fetched.
=item find_eq
This only works with cursor created via B<prepare_select_with_index>.
Will roll to the first record what is equal to specified argument, or
to the first greater if there is none equal. The following B<fetch>es
then continue normally.
=back
Examples of using cursors:
my $table = new XBase "names.dbf" or die XBase->errstr;
my $cursor = $table->prepare_select("ID", "NAME", "STREET");
while (my @data = $cursor->fetch) {
### do something here, like print "@data\n";
}
my $table = new XBase "employ.dbf";
my $cur = $table->prepare_select_with_index("empid.ndx");
## my $cur = $table->prepare_select_with_index(
["empid.cdx", "ADDRES", "char"], "id", "address");
$cur->find_eq(1097);
while (my $hashref = $cur->fetch_hashref
and $hashref->{"ID"} == 1097) {
### do something here with $hashref
}
The second example shows that after you have done B<find_eq>, the
B<fetch>es continue untill the end of the index, so you have to check
whether you are still on records with given value. And if there is no
record with value 1097 in the indexed field, you will just get the
next record in the order.
The updating example can be rewritten to:
use XBase;
my $table = new XBase "test.dbf" or die XBase->errstr;
my $cursor = $table->prepare_select("ID")
while (my ($id) = $cursor->fetch) {
$table->update_record_hash($cursor->last_fetched,
"MSG" => "New message") if $id == 123
}
=head2 Dumping the content of the file
A method B<get_all_records> returns reference to an array containing
array of values for each undeleted record at once. As parameters,
pass list of fields to return for each record.
To print the content of the file in a readable form, use method
B<dump_records>. It prints all not deleted records from the file. By
default, all fields are printed, separated by colons, one record on
a row. The method can have parameters in a form of a hash with the
following keys:
=over 4
=item rs
Record separator, string, newline by default.
=item fs
Field separator, string, one colon by default.
=item fields
Reference to a list of names of the fields to print. By default it's
undef, meaning all fields.
=item undef
What to print for undefined (NULL) values, empty string by default.
=back
Example of use is
use XBase;
my $table = new XBase "table" or die XBase->errstr;
$table->dump_records("fs" => " | ", "rs" => " <-+\n",
"fields" => [ "id", "msg" ]);'
Also note that there is a script dbf_dump(1) that does the printing.
=head2 Errors and debugging
If the method fails (returns false or null list), the error message
can be retrieved via B<errstr> method. If the B<new> or B<create>
method fails, you have no object so you get the error message using
class syntax C<XBase-E<gt>errstr()>.
The method B<header_info> returns (not prints) string with
information about the file and about the fields.
Module XBase::Base(3) defines some basic functions that are inherited
by both XBase and XBase::Memo(3) module.
=head1 DATA TYPES
The character fields are returned "as is". No charset or other
translation is done. The numbers are converted to Perl numbers. The
date fields are returned as 8 character string of the 'YYYYMMDD' form
and when inserting the date, you again have to provide it in this
form. No checking for the validity of the date is done. The datetime
field is returned in the number of (possibly negative) seconds since
1970/1/1, possibly with decimal part (since it allows precision up to
1/1000 s). To get the fields, use the gmtime (or similar) Perl function.
If there is a memo field in the dbf file, the module tries to open
file with the same name but extension dbt, fpt or smt. It uses module
XBase::Memo(3) for this. It reads and writes this memo field
transparently (you do not know about it) and returns the data as
single scalar.
=head1 INDEX, LOCKS
B<New:> A support for ndx, ntx, mdx, idx and cdx index formats is
available with alpha status for testing. Some of the formats are
already rather stable (ndx). Please read the XBase::Index(3) man page
and the eg/use_index file in the distribution for examples and ideas.
Send me examples of your data files and suggestions for interface if
you need indexes.
General locking methods are B<locksh>, B<lockex> and B<unlock> for
shared lock, exclusive lock and unlock. They call flock but you can
redefine then in XBase::Base package.
=head1 INFORMATION SOURCE
This module is built using information from and article XBase File
Format Description by Erik Bachmann, URL
http://www.clicketyclick.dk/databases/xbase/format/
Thanks a lot.
=head1 VERSION
1.02
=head1 AVAILABLE FROM
http://www.adelton.com/perl/DBD-XBase/
=head1 AUTHOR
(c) 1997--2011 Jan Pazdziora.
All rights reserved. This package is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
Contact the author at jpx dash perl at adelton dot com.
=head1 THANKS
Many people have provided information, test files, test results and
patches. This project would not be so great without them. See the
Changes file for (I hope) complete list. Thank you all, guys!
Special thanks go to Erik Bachmann for his great page about the
file structures; to Frans van Loon, William McKee, Randy Kobes and
Dan Albertsson for longtime cooperation and many emails we've
exchanged when fixing and polishing the modules' behaviour; and to
Dan Albertsson for providing support for the project.
=head1 SEE ALSO
perl(1); XBase::FAQ(3); DBD::XBase(3) and DBI(3) for DBI interface;
dbf_dump(1)
=cut
|