#!/usr/local/bin/perl # memorized.pl - functions to implement memorized transactions # # Written by Curtis Olson. Started October 13, 1994. # # Copyright (C) 1994 - 1999 Curtis L. Olson - curt@me.umn.edu # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # $Id: memorized.pl,v 1.1.1.1 1999/12/18 02:05:22 curt Exp $ package CBB; use strict; # don't take no guff # WARNING: This file must be "required" after the main transaction file. # %CBB::MEMS - an associative array of transactions and description keys # @CBB::MEMKEYS - a sorted list of description keys (for memorized transactions) # initialize the memorized transaction list sub init_mems { # out: result %CBB::MEMS = (); $CBB::sorted_memkeys = 1; return "ok"; } # insert a memorized transaction into the MEMS list sub insert_mem { # in: trans # out: trans my($trans) = @_; my($date, $check, $desc, $debit, $credit, $cat, $com, $cleared, $total) = split(/\t/, $trans); $CBB::MEMS{$desc} = $trans; print DEBUG "insert_mem: $desc -> $trans\n" if $CBB::debug; return "$trans"; } # insert a memorized transaction into the MEMS list *AND* rebuild the MEMKEYS sub insert_and_update_mem { # in: trans # out: trans my($trans) = @_; my($date, $check, $desc, $debit, $credit, $cat, $com, $cleared, $total) = split(/\t/, $trans); $CBB::MEMS{$desc} = $trans; @CBB::MEMKEYS = sort(keys %CBB::MEMS); print DEBUG "insert_mem: $desc -> $trans\n" if $CBB::debug; return "$trans"; } # build the MEMS list sub rehash_mems { # out: result my($key); &init_mems(); if ($CBB::calced == 0) { &calc_trans(); } if ($CBB::sorted_keys == 0) { &sort_keys(); } foreach $key (@CBB::KEYS) { &insert_mem($CBB::TRANS{$key}); } @CBB::MEMKEYS = sort(keys %CBB::MEMS); return "ok"; } # attempt to find a transaction matching the description # incomplete descriptions are allowed sub find_mem { # in: desc # out: trans my($desc) = @_; my($result, $count, $i, $match, $memkey); if ( $desc ne "" ) { $count = 0; $match = 0; foreach $memkey (@CBB::MEMKEYS) { if ( $memkey =~ m/^\Q$desc/i ) { print DEBUG "$memkey <=> $desc\n" if $CBB::debug; $count++; if ( $memkey =~ m/^\Q$desc$/i ) { print DEBUG "exact match $memkey <=> $desc\n" if $CBB::debug; $match = 1; } if ( length($result) ) { $i = 0; while ( $i < length($result) && substr("\U$result", $i, 1) eq substr("\U$memkey", $i, 1) ) { $i++; } $result = substr($result, 0, $i); } else { $result = $memkey } } } if ( length($result) && ($count == 1) ) { return "$desc\t$CBB::MEMS{$result}"; } elsif ( $match ) { return "$desc\t$CBB::MEMS{$result}"; } elsif ( length($result) ) { return "partial_match:$result"; } } return "none"; } &init_mems(); 1; # need to return a true value