Guest User

Untitled

a guest
Mar 5th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #ifndef NEGATE
  2. #define NEGATE  '^'         /* std cset negation char */
  3. #endif
  4.  
  5. #include <iostream>
  6.  
  7. bool amatch(const char *str, const char *p)
  8. {
  9.     int negate;
  10.     int match;
  11.     int c;
  12.     bool np;
  13.  
  14.     while (*p) {
  15.         if (!*str && *p != '*' && *p!=':')
  16.             return false;
  17.  
  18.         switch (c = *p++) {
  19.  
  20.         case ':':
  21.         case '*':
  22.             np=false;
  23.             if( c==':')
  24.                 np=true;
  25.  
  26.             while (*p == '*' || *p==':')
  27.                 p++;
  28.  
  29.             if(!np)
  30.             {
  31.                 if (!*p)
  32.                     return true;
  33.             }
  34.  
  35.             if (*p != '?' && *p != '[' && *p != '\\')
  36.                 while (*str && (!np || (*str!='/' && *str!='\\') ) && *p != *str)
  37.                     str++;
  38.  
  39.             if(np)
  40.             {
  41.                 if(!*p && !*str)
  42.                     return true;
  43.             }
  44.  
  45.             while (*str) {
  46.                 if (amatch(str, p))
  47.                     return true;
  48.                 if( np && (*str=='\\' || *str=='/') )
  49.                     break;
  50.                 str++;
  51.             }
  52.             return false;
  53.  
  54.         case '?':
  55.             if (*str)
  56.                 break;
  57.             return false;
  58. /*
  59.  * set specification is inclusive, that is [a-z] is a, z and
  60.  * everything in between. this means [z-a] may be interpreted
  61.  * as a set that contains z, a and nothing in between.
  62.  */
  63.         case '[':
  64.             if (*p != NEGATE)
  65.                 negate = false;
  66.             else {
  67.                 negate = true;
  68.                 p++;
  69.             }
  70.  
  71.             match = false;
  72.  
  73.             while (!match && (c = *p++)) {
  74.                 if (!*p)
  75.                     return false;
  76.                 if (*p == '-') {    /* c-c */
  77.                     if (!*++p)
  78.                         return false;
  79.                     if (*p != ']') {
  80.                         if (*str == c || *str == *p ||
  81.                             (*str > c && *str < *p))
  82.                             match = true;
  83.                     }
  84.                     else {      /* c-] */
  85.                         if (*str >= c)
  86.                             match = true;
  87.                         break;
  88.                     }
  89.                 }
  90.                 else {          /* cc or c] */
  91.                     if (c == *str)
  92.                         match = true;
  93.                     if (*p != ']') {
  94.                         if (*p == *str)
  95.                             match = true;
  96.                     }
  97.                     else
  98.                         break;
  99.                 }
  100.             }
  101.  
  102.             if (negate == match)
  103.                 return false;
  104. /*
  105.  * if there is a match, skip past the cset and continue on
  106.  */
  107.             while (*p && *p != ']')
  108.                 p++;
  109.             if (!*p++)  /* oops! */
  110.                 return false;
  111.             break;
  112.  
  113.         case '\\':
  114.             if (*p)
  115.                 c = *p++;
  116.         default:
  117.             if (c != *str)
  118.                 return false;
  119.             break;
  120.  
  121.         }
  122.         str++;
  123.     }
  124.  
  125.     return !*str;
  126. }
  127.  
  128. int main(int argc, char** argv)
  129. {
  130.     if (amatch(argv[1], argv[2])) { std::cout<<"Excluded"<<std::endl;}
  131.     else {std::cout<<"No match"<<std::endl;}
  132.  
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment