Using the tinycdb index files.
#!/usr/local/bin/perl ############################################################################## # file: tools_tinycdb.pl # # Mascot Parser toolkit example code # ############################################################################## # COPYRIGHT NOTICE # # Copyright 1998-2010 Matrix Science Limited All Rights Reserved. # # # ############################################################################## # $Source: parser/examples/test_perl/tools_tinycdb.pl $ # $Author: villek@matrixscience.com $ # $Date: 2018-07-30 16:23:53 +0100 $ # $Revision: 1b450440f9c97e1e41d0fc6016a27d68951d4532 | MSPARSER_REL_2_8_1-0-gea32989045 $ ############################################################################## use strict; ############################################################################## use msparser; # This next line just creates the object. It doesn't open a file # The 'source file' is (unusually!) this perl script ($0), meaning that # if this file is changed, the index will be re-created. my $version = "1.1"; my $sourceFile = $0; my $cdbname = $0.'.cdb'; my $cdb = new msparser::ms_tinycdb($cdbname, $version, $sourceFile); if (!$cdb->isValid()) { showErrors($cdb); exit; } if (!$cdb->openIndexFile(1)) { if ($cdb->isPossibleToCreate) { print "Creating file because: " . $cdb->getLastErrorString . "\n"; $cdb->prepareToCreate(); $cdb->saveValueForKey("Hello", "world"); # It is unusual to use the same key for 2 values, but it can be done: $cdb->saveValueForKey("HELP", "me"); $cdb->saveValueForKey("HELP", "me too"); # Example of saving a file offset. Can use a binary offset into the file # with the ms_tincycdb::saveFileOffsetForKey() function, but this example # uses saveIntForKey and a line offset. This facile example uses this # perl source file as the input! # and saves an offset to THIS line! open(my $fh, '<', $0) or die $!; my @list = <$fh>; close $fh; my $line=0; while ($list[$line] !~ m/# and saves an/) { $line++; } $cdb->saveIntForKey("Line", $line); $cdb->finishCreate(); } } if ($cdb->isValid) { print "Retrieving value for Hello : ", $cdb->getValueFromKey("Hello"), "\n"; print "Retrieving 1st value for HELP : ", $cdb->getValueFromKey("HELP", 0), "\n"; print "Retrieving 2nd value for HELP : ", $cdb->getValueFromKey("HELP", 1), "\n"; # Check saved line index open(my $fh, '<', $0) or die $!; my @list = <$fh>; close $fh; my $line = $cdb->getIntFromKey("Line"); print "Testing line index. Line ", ($line + 1), ": ", $list[$line], "\n"; } else { showErrors($cdb); } sub showErrors { my ($obj) = @_; my $err = $obj->getErrorHandler(); my $i; for my $i (1 .. $err->getNumberOfErrors) { print "Error number: "; print $err->getErrorNumber($i); print " ("; print $err->getErrorRepeats($i)+1 . " times) : "; print $err->getErrorString($i) . "\n"; } print "\n"; $obj->clearAllErrors(); }
Copyright © 2022 Matrix Science Ltd. All Rights Reserved. Generated on Thu Mar 31 2022 01:12:29 |