bugfix and improvements:
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 1 Feb 2004 23:28:27 +0000 (23:28 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 1 Feb 2004 23:28:27 +0000 (23:28 +0000)
- debug and quiet switches
- skip fields which are too short (just subfield without data)
- calculate directory address
- calculate base address in leader

git-svn-id: file:///home/dpavlin/private/svn/webpac/trunk@217 13eb9ef6-21d5-0310-b721-a9d68796d827

tools/phpmylib2marc.pl

index 83e02f8..eeb5e80 100755 (executable)
@@ -25,6 +25,7 @@ my $passwd = "";
 
 my $usage = 0;
 my $debug = 0;
+my $quiet = 0;
 
 my $result = GetOptions(
        "database=s" => \$database,
@@ -32,6 +33,8 @@ my $result = GetOptions(
        "user=s" => \$user,
        "password=s" => \$passwd,
        "debug!" => \$debug,
+       "verbose!" => \$debug,
+       "quiet!" => \$quiet,
        "help!" => \$usage,
        );
 
@@ -110,8 +113,6 @@ while (my $row = $sth->fetchrow_hashref()) {
        while (!$skip && $directory =~ s/(\d{3})(\d{4})(\d{5})//) {
                my ($tag,$len,$addr) = ($1,$2,$3);
 
-               print STDERR "tag/len/addr: $tag $len $addr\n" if ($debug);
-
                sub check_field($) {
                        my $f = shift;
                        my $del = substr($f,0,1);
@@ -120,43 +121,49 @@ while (my $row = $sth->fetchrow_hashref()) {
                }
 
                if (($addr+$len) > length($fields)) {
-                       print STDERR "WARNING: error in dictionary on record $rec_nr skipping...\n" if ($debug);
+                       print STDERR "WARNING: error in dictionary on record $rec_nr skipping...\n" if (! $quiet);
                        $skip = 1;
                        next;
                }
 
                # take field
                my $f = substr($fields,$addr,$len);
-               print STDERR "data $tag [$len] $addr: '$f'\n" if ($debug);
+               print STDERR "tag/len/addr $tag [$len] $addr: '$f'\n" if ($debug);
 
                my $del = substr($fields,$addr+$len,1);
 
                # check field delimiters...
                if ($del ne chr(30)) {
-                       print STDERR "WARNING: skipping record $rec_nr, can't find delimiters got: '$del'\n" if ($debug);
+                       print STDERR "WARNING: skipping record $rec_nr, can't find delimiters got: '$del'\n" if (! $quiet);
                        $skip = 1;
                        next;
                }
 
                check_field($f);
-                       
+
+               if (length($f) < 2) {
+                       print STDERR "WARNING: skipping field $tag from record $rec_nr because it's too short!\n" if (! $quiet);
+                       next;
+               }
+
+
                if ($tag =~ m/^00/) {
                        # fields 001-008 doesn't have indicators
-                       $new_dictionary .= sprintf("%03d%04d%05d",$tag,$len,$addr);
+                       $new_dictionary .= sprintf("%03d%04d%05d",$tag,$len,length($new_fields || ''));
                        $new_fields.=$f;
                } else {
-                       $new_dictionary .= sprintf("%03d%04d%05d",$tag,($len+2),($addr+$o));
+                       $new_dictionary .= sprintf("%03d%04d%05d",$tag,($len+2),length($new_fields || ''));
                        $new_fields.=chr(30)."  ".substr($f,1);
                        $o += 2;
                }
        }
 
        if (! $skip) {
-               my $new_leader = sprintf($leader_fmt,24+length($new_dictionary.$new_fields)+2,$base_addr);
+               my $new_leader = sprintf($leader_fmt,24+length($new_dictionary.$new_fields)+2,length($new_dictionary)+25);
                my $new_marc = $new_leader . $new_dictionary . $new_fields . chr(30);
                $new_marc .= chr(29);   # end of record
 
-               print STDERR "original and new marc: [$rec_nr]\n$marc\n$new_marc\n\n" if ($debug);
+               print STDERR "original and new marc: [$rec_nr/$count]\n$marc\n$new_marc\n\n" if ($debug);
                print "$new_marc";
                $count++;
        }