3 # Copyright 2009-2012, Equinox Software, Inc.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # Parses Bibliofile files.
20 # Usage: parse_db.pl TITLE.DB [--ignore-indexes]
21 # Choosing --ignore-indexes will find data you'd otherwise miss, but also grabs a lot of junk you'll need to filter out.
27 my $ignoreIndexes = '';
29 my $opts = GetOptions('ignore-indexes' => \$ignoreIndexes);
33 my $startOfColumnTypes = 8;
34 my $startOfRealData = 4096;
36 my $initialOffset = 6;
57 $rowLength = ord(substr($data, 0, 1)) + (256 * (ord(substr($data, 1, 1))));
58 #print STDERR "Row length: $rowLength\n";
60 my $numColumns = ord substr($data, 2, 1);
61 #print STDERR "Columns: $numColumns\n";
63 my $namedata = substr($data, $startOfColumnTypes + ($numColumns * 7) - 2);
64 @fieldNames = split(/\x00/, $namedata);
66 for (my $i = 0; $i < $numColumns; $i++) {
67 $fieldTypes[$i] = substr($data, ($i * 7) + $startOfColumnTypes, 1);
68 $fieldLengths[$i] = ord substr($data, ($i * 7) + $startOfColumnTypes + 1, 1);
75 print join("\t", @fieldNames) . "\n";
81 while (read DB, my $data, $blockSize) {
83 next if ($blocks == 1);
84 my $maxRecords = POSIX::floor($blockSize / $rowLength);
85 unless $ignoreIndexes {
86 my $indexIndicator1 = ord substr($data, 1, 1);
87 next if ($indexIndicator1 != 0);
88 my $indexIndicator2 = ord substr($data, 7, 1);
89 next if ($indexIndicator2 == 0);
92 for (my $r = 0; $r < $maxRecords; $r++) {
97 #print STDERR "Record " . ($r+1) . " of $maxRecords\n";
99 for (my $f = 0; $f < scalar(@fieldLengths); $f++) {
100 $field[$f] = substr($data, $initialOffset + ($r * $rowLength) + $pos, $fieldLengths[$f]);
101 if ($fieldTypes[$f] eq 'S') { $field[$f] = ord $field[$f]; }
102 $pos += $fieldLengths[$f];
105 if ($field[0] =~ m/[^\x00]/) {
106 print join("\t", @field) . "\n";