/* * Copyright (c) 1995, 1996 Gunther Schadow. All rights reserved. * * 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. */ #include "pg_config.h" IDENT("@(#) odbm-filter.cc (Gunther Schadow) 12/19/96"); #include "odbm.h" #include #include #include #include /****************************************************************** * Filter support * */ /* * */ odbm::filter::filter(int s, const char *v, operator_t o) { str = s; value = strdup(v); opt = o; opf = NULL; next = NULL; } odbm::filter::filter(int s, const char *v, operator_f o) { str = s; value = strdup(v); opt = eq; opf = o; next = NULL; } odbm::filter::~filter() { delete [] value; if(next != NULL) delete next; // this is a recursion! } /* * Default copy constructor (reference) copies bitwise (as well as * the default assignment operator. CAVE: we rely on this! * odbm::filter::filter(const odbm::filter &x); // default */ /* * Copy pointer constructor copies the whole filter list plus every * value string */ odbm::filter::filter(const odbm::filter *x) { if(x != NULL) { *this = *x; // bitwise assignment this->value = strdup(value); // now save the string filter **p = &next; while(x->next != NULL) { x = x->next; *p = new filter(*x); // bitwise copy constructor (*p)->value = strdup(value); // now save the string p = &(*p)->next; } } } void odbm::filter::add(int s, const char *v, operator_t o) { filter *p = new filter(*this); str = s; value = strdup(v); opt = o; opf = NULL; next = p; } void odbm::filter::add(int s, const char *v, operator_f o) { filter *p = new filter(*this); str = s; value = strdup(v); opt = eq; opf = o; next = p; } bool odbm::filter::test(const odbm *c) { for(filter *p = this; p != NULL; p = p->next) { const char *field; if(p->str == 0) field = *c->keyp; else field = c->strings[p->str - 1]; if(p->opf != NULL) { if(!(*p->opf)(field, p->value)) return FALSE; } else switch(p->opt) { case eq: if(strcmp(field, p->value) != 0) return FALSE; else break; case ne: if(strcmp(field, p->value) == 0) return FALSE; else break; } } return TRUE; } const char *odbm::filter::operator [] (int i) const { for(const filter *p = this; p != NULL; p = p->next) { if(p->str == i) return p->value; } return NULL; } void odbm::addfilt(const char * const *variable, const char *value, operator_t op) { if(fltp == NULL) fltp = new filter(stringno(variable), value, op); else fltp->add(stringno(variable), value, op); } /* * The strno here is the real string number, i.e. starting with 0 for * the first string since it makes no sense to put the key on the * filter list. */ void odbm::addfilt(int strno, const char *value, operator_t op) { if(fltp == NULL) fltp = new filter(strno + 1, value, op); else fltp->add(strno + 1, value, op); } void odbm::addfilt(const char * const *variable, const char *value, operator_f op) { if(fltp == NULL) fltp = new filter(stringno(variable), value, op); else fltp->add(stringno(variable), value, op); } void odbm::remfilt() { if(fltp != NULL) delete fltp; }