Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef NEGATE
- #define NEGATE '^' /* std cset negation char */
- #endif
- #include <iostream>
- bool amatch(const char *str, const char *p)
- {
- int negate;
- int match;
- int c;
- bool np;
- while (*p) {
- if (!*str && *p != '*' && *p!=':')
- return false;
- switch (c = *p++) {
- case ':':
- case '*':
- np=false;
- if( c==':')
- np=true;
- while (*p == '*' || *p==':')
- p++;
- if(!np)
- {
- if (!*p)
- return true;
- }
- if (*p != '?' && *p != '[' && *p != '\\')
- while (*str && (!np || (*str!='/' && *str!='\\') ) && *p != *str)
- str++;
- if(np)
- {
- if(!*p && !*str)
- return true;
- }
- while (*str) {
- if (amatch(str, p))
- return true;
- if( np && (*str=='\\' || *str=='/') )
- break;
- str++;
- }
- return false;
- case '?':
- if (*str)
- break;
- return false;
- /*
- * set specification is inclusive, that is [a-z] is a, z and
- * everything in between. this means [z-a] may be interpreted
- * as a set that contains z, a and nothing in between.
- */
- case '[':
- if (*p != NEGATE)
- negate = false;
- else {
- negate = true;
- p++;
- }
- match = false;
- while (!match && (c = *p++)) {
- if (!*p)
- return false;
- if (*p == '-') { /* c-c */
- if (!*++p)
- return false;
- if (*p != ']') {
- if (*str == c || *str == *p ||
- (*str > c && *str < *p))
- match = true;
- }
- else { /* c-] */
- if (*str >= c)
- match = true;
- break;
- }
- }
- else { /* cc or c] */
- if (c == *str)
- match = true;
- if (*p != ']') {
- if (*p == *str)
- match = true;
- }
- else
- break;
- }
- }
- if (negate == match)
- return false;
- /*
- * if there is a match, skip past the cset and continue on
- */
- while (*p && *p != ']')
- p++;
- if (!*p++) /* oops! */
- return false;
- break;
- case '\\':
- if (*p)
- c = *p++;
- default:
- if (c != *str)
- return false;
- break;
- }
- str++;
- }
- return !*str;
- }
- int main(int argc, char** argv)
- {
- if (amatch(argv[1], argv[2])) { std::cout<<"Excluded"<<std::endl;}
- else {std::cout<<"No match"<<std::endl;}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment