OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
wildcards.cpp
1// Copyright (C) 1996 - 2002 Florian Schintke
2//
3// This is free software; you can redistribute it and/or modify it under
4// the terms of the GNU General Public License as published by the Free
5// Software Foundation; either version 2, or (at your option) any later
6// version.
7//
8// Thanks to the E.S.O. - ACS project that has done this C++ interface
9// to the wildcards pttern matching algorithm
10
11#include "wildcards.hpp"
12
13int Wildcard::wildcardfit(const char* wildcard, const char* test) {
14 int fit = 1;
15
16 for (; ('\000' != *wildcard) && (1 == fit) && ('\000' != *test); wildcard++) {
17 switch (*wildcard) {
18 case '[':
19 wildcard++; /* leave out the opening square bracket */
20 fit = set(&wildcard, &test);
21 /* we don't need to decrement the wildcard as in case */
22 /* of asterisk because the closing ] is still there */
23 break;
24 case '?':
25 test++;
26 break;
27 case '*':
28 fit = asterisk(&wildcard, &test);
29 /* the asterisk was skipped by asterisk() but the loop will */
30 /* increment by itself. So we have to decrement */
31 wildcard--;
32 break;
33 default:
34 fit = (int)(*wildcard == *test);
35 test++;
36 }
37 }
38 while ((*wildcard == '*') && (1 == fit))
39 /* here the teststring is empty otherwise you cannot */
40 /* leave the previous loop */
41 wildcard++;
42 return (int)((1 == fit) && ('\0' == *test) && ('\0' == *wildcard));
43}
44
45int Wildcard::set(const char** wildcard, const char** test) {
46 int fit = 0;
47 int negation = 0;
48 int at_beginning = 1;
49
50 if ('!' == **wildcard) {
51 negation = 1;
52 (*wildcard)++;
53 }
54 while ((']' != **wildcard) || (1 == at_beginning)) {
55 if (0 == fit) {
56 if (('-' == **wildcard) && ((*(*wildcard - 1)) < (*(*wildcard + 1))) &&
57 (']' != *(*wildcard + 1)) && (0 == at_beginning)) {
58 if (((**test) >= (*(*wildcard - 1))) &&
59 ((**test) <= (*(*wildcard + 1)))) {
60 fit = 1;
61 (*wildcard)++;
62 }
63 } else if ((**wildcard) == (**test)) {
64 fit = 1;
65 }
66 }
67 (*wildcard)++;
68 at_beginning = 0;
69 }
70 if (1 == negation) /* change from zero to one and vice versa */
71 fit = 1 - fit;
72 if (1 == fit) (*test)++;
73
74 return (fit);
75}
76
77int Wildcard::asterisk(const char** wildcard, const char** test) {
78 /* Warning: uses multiple returns */
79 int fit = 1;
80
81 /* erase the leading asterisk */
82 (*wildcard)++;
83 while (('\000' != (**test)) && (('?' == **wildcard) || ('*' == **wildcard))) {
84 if ('?' == **wildcard) (*test)++;
85 (*wildcard)++;
86 }
87 /* Now it could be that test is empty and wildcard contains */
88 /* aterisks. Then we delete them to get a proper state */
89 while ('*' == (**wildcard))
90 (*wildcard)++;
91
92 if (('\0' == (**test)) && ('\0' != (**wildcard))) return (fit = 0);
93 if (('\0' == (**test)) && ('\0' == (**wildcard)))
94 return (fit = 1);
95 else {
96 /* Neither test nor wildcard are empty! */
97 /* the first character of wildcard isn't in [*?] */
98 if (0 == wildcardfit(*wildcard, (*test))) {
99 do {
100 (*test)++;
101 /* skip as much characters as possible in the teststring */
102 /* stop if a character match occurs */
103 while (((**wildcard) != (**test)) && ('[' != (**wildcard)) &&
104 ('\0' != (**test)))
105 (*test)++;
106 } while ((('\0' != **test)) ? (0 == wildcardfit(*wildcard, (*test))) :
107 (0 != (fit = 0)));
108 }
109 if (('\0' == **test) && ('\0' == **wildcard)) fit = 1;
110 return (fit);
111 }
112}