From 50595a34fdbcdcc02b713e0031d1017ba2c3a755 Mon Sep 17 00:00:00 2001 From: Wichert Akkerman Date: Mon, 16 Jul 2001 15:29:03 +0000 Subject: [PATCH] implement checksearch and matchsearch using regular expressions --- dselect/pkglist.cc | 68 ++++++++++++++++++++++++++++++++++++++++++++++ dselect/pkglist.h | 10 +++++++ 2 files changed, 78 insertions(+) diff --git a/dselect/pkglist.cc b/dselect/pkglist.cc index ce48e727..81d3e65a 100644 --- a/dselect/pkglist.cc +++ b/dselect/pkglist.cc @@ -3,6 +3,7 @@ * pkglist.cc - package list administration * * Copyright (C) 1995 Ian Jackson + * Copyright (C) 2001 Wichert Akkerman * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as @@ -367,6 +368,7 @@ void packagelist::initialsetup() { headings= 0; verbose= 0; calcssadone= calcsssdone= 0; + searchdescr= 0; } void packagelist::finalsetup() { @@ -461,6 +463,9 @@ void perpackagestate::free(int recursive) { packagelist::~packagelist() { if (debug) fprintf(debug,"packagelist[%p]::~packagelist()\n",this); + if (searchstring[0]) + regfree(&searchfsm); + discardheadings(); int index; @@ -478,6 +483,66 @@ packagelist::~packagelist() { if (debug) fprintf(debug,"packagelist[%p]::~packagelist() done\n",this); } +int packagelist::checksearch(char* rx) { + int r,opt = REG_NOSUB; + + if (!rx || !*rx) return 0; + + searchdescr=0; + if (searchstring[0]) { + regfree(&searchfsm); + searchstring[0]=0; + } + + /* look for search options */ + for (r=strlen(rx)-1; r>=0; r--) + if ((rx[r]=='/') && ((r==0) || (rx[r-1]!='\\'))) + break; + + if (r>=0) { + rx[r++]='\0'; + if (strcspn(rx+r, "di")!=0) { + displayerror(_("invalid search option given")); + return 0; + } + + while (rx[r]) { + if (rx[r]=='i') + opt|=REG_ICASE; + else if (rx[r]=='d') + searchdescr=1; + r++; + } + } + + if ((r=regcomp(&searchfsm, rx, opt))!=0) { + displayerror(_("error in regular expression")); + return 0; + } + + return 1; +} + +int packagelist::matchsearch(int index) { + const char* thisname; + + thisname=itemname(index); + if (!thisname) return 0; /* Skip things without a name (seperators) */ + + if (regexec(&searchfsm, thisname, 0, NULL, 0)==0) + return 1; + + if (searchdescr) { + const char* descr = table[index]->pkg->available.description; + if (!descr || !*descr) return 0; + + if (regexec(&searchfsm, descr, 0, NULL, 0)==0) + return 1; + } + + return 0; +} + pkginfo **packagelist::display() { // returns list of packages as null-terminated array, which becomes owned // by the caller, if a recursive check is desired. @@ -538,3 +603,6 @@ pkginfo **packagelist::display() { return 0; } } + +/* vi: sw=2 ts=8 + */ diff --git a/dselect/pkglist.h b/dselect/pkglist.h index e57f609c..db17cf60 100644 --- a/dselect/pkglist.h +++ b/dselect/pkglist.h @@ -3,6 +3,7 @@ * pkglist.h - external definitions for package list handling * * Copyright (C) 1994,1995 Ian Jackson + * Copyright (C) 2001 Wichert Akkerman * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as @@ -22,6 +23,8 @@ #ifndef PKGLIST_H #define PKGLIST_H +#include + enum showpriority { dp_none, // has not been involved in any unsatisfied things dp_may, // has been involved in an unsatisfied Suggests @@ -83,6 +86,7 @@ struct perpackagestate { }; class packagelist : public baselist { +protected: int status_width, gap_width, section_width, priority_width; int package_width, versioninstalled_width, versionavailable_width, description_width; int section_column, priority_column, versioninstalled_column; @@ -103,6 +107,10 @@ class packagelist : public baselist { int calcssadone, calcsssdone; struct perpackagestate *headings; + // Package searching flags + int searchdescr; + regex_t searchfsm; + // Information displays struct infotype { int (packagelist::*relevant)(); // null means always relevant @@ -130,6 +138,8 @@ class packagelist : public baselist { int deselect_one_of(pkginfo *er, pkginfo *ed, dependency *display); // Define these virtuals + int checksearch(char* str); + int matchsearch(int index); void redraw1itemsel(int index, int selected); void redrawcolheads(); void redrawthisstate(); -- 2.39.5