Matrix Science header

tools_tinycdb.pl

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: /vol/cvsroot/parser/examples/test_perl/tools_tinycdb.pl,v $
#     $Author: villek $
#       $Date: 2010/08/31 08:48:07 $
#   $Revision: 1.3 $ 
##############################################################################
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 © 2016 Matrix Science Ltd.  All Rights Reserved. Generated on Fri Jun 2 2017 01:44:50