From 6dc9b0f96f56c18729db00e257e67235988c1cd1 Mon Sep 17 00:00:00 2001 From: Matej Sekoranja Date: Thu, 14 Apr 2016 11:31:30 +0200 Subject: [PATCH] replaced LGPL wildcard matcher w/ simplistic epics-base version --- src/utils/pv/wildcard.h | 20 ---- src/utils/wildcard.cpp | 163 ++------------------------------- testApp/utils/Makefile | 5 + testApp/utils/testWildcard.cpp | 53 +++++++++++ 4 files changed, 64 insertions(+), 177 deletions(-) create mode 100644 testApp/utils/testWildcard.cpp diff --git a/src/utils/pv/wildcard.h b/src/utils/pv/wildcard.h index 57822c8..9d48f16 100644 --- a/src/utils/pv/wildcard.h +++ b/src/utils/pv/wildcard.h @@ -55,26 +55,6 @@ public: * matches test. */ static int wildcardfit (const char *wildcard, const char *test); - -private: - /** - * Scans a set of characters and returns 0 if the set mismatches at this - * position in the teststring and 1 if it is matching - * wildcard is set to the closing ] and test is unmodified if mismatched - * and otherwise the char pointer is pointing to the next character - * @param wildcard UNIX style wildcard to be used - * @param test String we will test against the wildcard. - * @return 0 if the set mismatches. 1 otherwise. - */ - static int set (const char **wildcard, const char **test); - - /** - * Scans an asterisk. - * @param wildcard UNIX style wildcard to be used - * @param test String we will test against the wildcard. - * @return ??? - */ - static int asterisk (const char **wildcard, const char **test); }; } diff --git a/src/utils/wildcard.cpp b/src/utils/wildcard.cpp index bb04dae..9c137fd 100644 --- a/src/utils/wildcard.cpp +++ b/src/utils/wildcard.cpp @@ -1,168 +1,17 @@ -/******************************************************************* - * This implementation was adpoted from: - * - * Copyright (C) 1996, 1997, 1998, 1999, 2000 Florian Schintke - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA - * - * F.Schintke, the author of the original code, has authorized to - * distribute these files under LGPL License. - * - * ---------------------- - * Implementation of the UN*X wildcards - * Supported wild-characters: '*', '?'; sets: [a-z], '!' negation - * Examples: - * '[a-g]l*i?n' matches 'florian' - * '[!abc]*e' matches 'smile' - * '[-z] matches 'a' - * +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * pvAccessCPP is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. */ #define epicsExportSharedSymbols #include +#include using namespace epics::pvAccess; int Wildcard::wildcardfit (const char *wildcard, const char *test) { - int fit = 1; - - for (; ('\000' != *wildcard) && (1 == fit) && ('\000' != *test); wildcard++) - { - switch (*wildcard) - { - case '[': - wildcard++; /* leave out the opening square bracket */ - fit = set (&wildcard, &test); - /* we don't need to decrement the wildcard as in case */ - /* of asterisk because the closing ] is still there */ - break; - case '?': - test++; - break; - case '*': - fit = asterisk (&wildcard, &test); - /* the asterisk was skipped by asterisk() but the loop will */ - /* increment by itself. So we have to decrement */ - wildcard--; - break; - default: - fit = (int) (*wildcard == *test); - test++; - } - } - while ((*wildcard == '*') && (1 == fit)) - /* here the teststring is empty otherwise you cannot */ - /* leave the previous loop */ - wildcard++; - return (int) ((1 == fit) && ('\0' == *test) && ('\0' == *wildcard)); -} - -int -Wildcard::set (const char **wildcard, const char **test) -{ - int fit = 0; - int negation = 0; - int at_beginning = 1; - - if ('!' == **wildcard) - { - negation = 1; - (*wildcard)++; - } - while ((']' != **wildcard) || (1 == at_beginning)) - { - if (0 == fit) - { - if (('-' == **wildcard) - && ((*(*wildcard - 1)) < (*(*wildcard + 1))) - && (']' != *(*wildcard + 1)) - && (0 == at_beginning)) - { - if (((**test) >= (*(*wildcard - 1))) - && ((**test) <= (*(*wildcard + 1)))) - { - fit = 1; - (*wildcard)++; - } - } - else if ((**wildcard) == (**test)) - { - fit = 1; - } - } - (*wildcard)++; - at_beginning = 0; - } - if (1 == negation) - /* change from zero to one and vice versa */ - fit = 1 - fit; - if (1 == fit) - (*test)++; - - return (fit); -} - -int -Wildcard::asterisk (const char **wildcard, const char **test) -{ - /* Warning: uses multiple returns */ - int fit = 1; - - /* erase the leading asterisk */ - (*wildcard)++; - while (('\000' != (**test)) - && (('?' == **wildcard) - || ('*' == **wildcard))) - { - if ('?' == **wildcard) - (*test)++; - (*wildcard)++; - } - /* Now it could be that test is empty and wildcard contains */ - /* aterisks. Then we delete them to get a proper state */ - while ('*' == (**wildcard)) - (*wildcard)++; - - if (('\0' == (**test)) && ('\0' != (**wildcard))) - return (fit = 0); - if (('\0' == (**test)) && ('\0' == (**wildcard))) - return (fit = 1); - else - { - /* Neither test nor wildcard are empty! */ - /* the first character of wildcard isn't in [*?] */ - if (0 == wildcardfit(*wildcard, (*test))) - { - do - { - (*test)++; - /* skip as much characters as possible in the teststring */ - /* stop if a character match occurs */ - while (((**wildcard) != (**test)) - && ('[' != (**wildcard)) - && ('\0' != (**test))) - (*test)++; - } - while ((('\0' != **test))? - (0 == wildcardfit (*wildcard, (*test))) - : (0 != (fit = 0))); - } - if (('\0' == **test) && ('\0' == **wildcard)) - fit = 1; - return (fit); - } + return epicsStrGlobMatch(test, wildcard); } diff --git a/testApp/utils/Makefile b/testApp/utils/Makefile index 252455a..54ad974 100644 --- a/testApp/utils/Makefile +++ b/testApp/utils/Makefile @@ -51,3 +51,8 @@ TESTS += configurationTest TESTPROD_HOST += testFairQueue testFairQueue_SRCS += testFairQueue TESTS += testFairQueue + +TESTPROD_HOST += testWildcard +testWildcard = testWildcard.cpp +testHarness_SRCS += testWildcard.cpp +TESTS += testWildcard diff --git a/testApp/utils/testWildcard.cpp b/testApp/utils/testWildcard.cpp new file mode 100644 index 0000000..29354b1 --- /dev/null +++ b/testApp/utils/testWildcard.cpp @@ -0,0 +1,53 @@ +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * pvAccessCPP is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. + */ + +#include + +#include +#include + +using epics::pvAccess::Wildcard; + +static +void testWildcardCases() +{ + testDiag("Test testWildcardCases()"); + + testOk1(Wildcard::wildcardfit("*", "test")); + testOk1(Wildcard::wildcardfit("*", "")); + testOk1(!Wildcard::wildcardfit("?", "")); + testOk1(Wildcard::wildcardfit("?", "a")); + testOk1(Wildcard::wildcardfit("*?", "test")); + testOk1(Wildcard::wildcardfit("?*", "test")); + testOk1(Wildcard::wildcardfit("?*?", "test")); + testOk1(!Wildcard::wildcardfit("?", "")); + testOk1(!Wildcard::wildcardfit("?*?", "")); + + //testOk1(Wildcard::wildcardfit("[-aa-]*", "01 abAZ")); + //testOk1(Wildcard::wildcardfit("[\\!a\\-bc]*", "!!!b-bb-")); + testOk1(Wildcard::wildcardfit("*zz", "zz")); + //testOk1(Wildcard::wildcardfit("[abc]*zz", "zz")); + + //testOk1(!Wildcard::wildcardfit("[!abc]*a[def]", "xyzbd")); + //testOk1(Wildcard::wildcardfit("[!abc]*a[def]", "xyzad")); + //testOk1(Wildcard::wildcardfit("[a-g]l*i?", "gloria")); + //testOk1(Wildcard::wildcardfit("[!abc]*e", "smile")); + //testOk1(Wildcard::wildcardfit("[-z]", "a")); + //testOk1(!Wildcard::wildcardfit("[]", "")); + //testOk1(Wildcard::wildcardfit("[a-z]*", "java")); + testOk1(Wildcard::wildcardfit("*.*", "command.com")); + testOk1(!Wildcard::wildcardfit("*.*", "/var/etc")); + //testOk1(Wildcard::wildcardfit("**?*x*[abh-]*Q", "XYZxabbauuZQ")); +} + +MAIN(testWildcard) +{ + testPlan(12); + testDiag("Tests for Wildcard util"); + + testWildcardCases(); + return testDone(); +}