Debian Codegraph @

tools/dbfdump

raw
1 #!/usr/bin/perl
2 #
3 # Seven Kingdoms: Ancient Adversaries
4 #
5 # Copyright 1997,1998 Enlight Software Ltd.
6 # Copyright 2017 Jesse Allen
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #
21 #
22
23 use warnings;
24 use strict;
25
26 use FindBin;
27 use lib $FindBin::Bin;
28
29 use List::Util qw(min max);
30 use dbf;
31
32 if (!@ARGV) {
33 print "Usage: $0 input.dbf\n";
34 exit 0;
35 }
36 my ($dbf_file) = @ARGV;
37
38 my ($dbf) = dbf->read_file($dbf_file);
39 if (!defined($dbf)) {
40 printf("Error: Can't read $dbf_file\n");
41 exit 0;
42 }
43
44 # used for write testing
45 #$dbf->write_file("$dbf_file.new");
46
47 my $records = $dbf->get_records();
48 my $fields = $dbf->get_field_names();
49 my @ptr_fields;
50 my $format = "%6s:";
51 for (my $i = 0; $i < @$fields; $i++) {
52 my $len = max($dbf->get_field_len($i), length($fields->[$i]));
53 if ($fields->[$i] =~ /PTR/) {
54 $format .= " %-${len}s";
55 push(@ptr_fields, $i);
56 } else {
57 $format .= " %-${len}s";
58 }
59 }
60 $format .= "\n";
61 printf($format, "RECORD", @$fields);
62 #print "Format string: $format\n";
63 for (my $i = 0; $i < $records; $i++) {
64 my $record = $dbf->get_record($i);
65 for (my $j = 0; $j < @ptr_fields; $j++) {
66 my $val = unpack('L', $record->[$ptr_fields[$j]]);
67 $record->[$ptr_fields[$j]] = $val;
68 }
69 printf($format, $i, @$record);
70 }
71
72 exit 0;