///////////////////////////////////////////////////////////////// // wildcards.cpp - find strings that match, using wild cards // // ver 1.1 // // // // Language: Visual C++, ver 6.0 // // Platform: Micron Dual Pentium 200, Windows NT 4.0 // // Application: CSE687 Project #1, S98 // // Author: Jim Fawcett, CST 2-187, (315) 443-3948 // // fawcett@ecs.syr.edu // ///////////////////////////////////////////////////////////////// #include #include #include "wildcards.h" using namespace std; //----< does this string match s? >---------------------------- // // assumes that object may contain wildcards, but s does not // bool wildcards::matches(const string &s) { size_t i=0, j=0; while(i < wc.size() && j < s.size()) { switch (wc[i]) { case '*' : if(++i == wc.size()) return true; while(i < wc.size()) { if(j == s.size()) return false; if(s[j++] != wc[i]) continue; ++i; break; } break; case '?' : i++; j++; break; default : if(wc[i++] != s[j++]) return false; } } return (i==wc.size() && j==s.size()); } // //----< test stub >-------------------------------------------- #ifdef TEST_WILDCARDS void main() { cout << "\n Testing Wildcard Matching " << "\n ===========================\n"; string s[5] = { "*.bar", "foo.*", "f*.bar", "f?o.bar", "foo.bar" }; string t[3] = { "foo.bar", "fee.bar", "foo.bor" }; int i, j; for(i=0; i<5; i++) { cout << '\n'; wildcards wc(s[i]); for(j=0; j<3; j++) if(wc.matches(t[j])) cout << " \"" << s[i] << "\" matches \"" << t[j] << "\"" << endl; else cout << " \"" << s[i] << "\" does not match \"" << t[j] << "\"" << endl; } cout << "\n\n"; } #endif