00001 #include "Glob.h"
00002
00003 namespace oasys {
00004
00005 bool
00006 Glob::fixed_glob(const char* pat, const char* str)
00007 {
00008 const int STACK_SIZE = 32;
00009 State stk[STACK_SIZE];
00010 int stk_size = 1;
00011
00012 stk[0].pat_ = pat;
00013 stk[0].to_match_ = str;
00014
00015 while (stk_size > 0)
00016 {
00017 int old_stk_size = stk_size;
00018
00019
00020 for (int i = 0; i < old_stk_size; ++i)
00021 {
00022 State* state = &stk[i];
00023
00024 switch (* (state->pat_))
00025 {
00026 case '*':
00027 if (*(state->pat_ + 1) == *state->to_match_)
00028 {
00029
00030
00031 if (stk_size == STACK_SIZE)
00032 {
00033
00034 return false;
00035 }
00036
00037 stk[stk_size].pat_ = state->pat_ + 1;
00038 stk[stk_size].to_match_ = state->to_match_;
00039 stk_size++;
00040 }
00041
00042 state->to_match_++;
00043 break;
00044
00045 default:
00046 if (* (state->pat_) == *(state->to_match_))
00047 {
00048 state->pat_++;
00049 state->to_match_++;
00050 }
00051 else
00052 {
00053 state->pat_ = "";
00054 state->to_match_ = "NO_MATCH";
00055 }
00056 break;
00057 }
00058 }
00059
00060
00061 old_stk_size = stk_size;
00062 for (int i = 0, j = 0; i < old_stk_size; i++)
00063 {
00064 State* state = &stk[i];
00065
00066 if ((*state->pat_ == '\0' && *state->to_match_ == '\0') ||
00067 (*state->pat_ == '*' && *(state->pat_ + 1) == '\0' &&
00068 *state->to_match_ == '\0'))
00069 {
00070 return true;
00071 }
00072
00073 if (*state->pat_ == '\0' || *state->to_match_ == '\0')
00074 {
00075 stk_size--;
00076 continue;
00077 }
00078
00079 stk[j] = stk[i];
00080 j++;
00081 }
00082 }
00083
00084 return false;
00085 }
00086
00087 }
00088
00089 #if 0
00090
00091 #include <cstdio>
00092
00093 void
00094 g(const char* pat, const char* str)
00095 {
00096 printf("%s:%s - %s\n",
00097 pat, str, oasys::Glob::fixed_glob(pat, str) ? "match" : "no match");
00098 }
00099
00100 int
00101 main()
00102 {
00103 g("hello", "hello");
00104 g("hello", "world");
00105 g("hel*", "world");
00106 g("hel*", "helicopter");
00107 g("hel*lo*world", "hello there world");
00108 g("hel*lo*world", "hello there world!");
00109 g("hel*", "hello, world!");
00110 g("hel*o", "hellow");
00111 g("*world", "hello cruel world");
00112 }
00113
00114 #endif