aboutsummaryrefslogtreecommitdiff
path: root/src/match.c
diff options
context:
space:
mode:
authorJohn Ankarstr\xf6m <john@ankarstrom.se>2021-05-29 12:54:47 +0200
committerJohn Ankarstr\xf6m <john@ankarstrom.se>2021-05-29 13:18:40 +0200
commita041d9898e6d699bd8c0c25482ec574feb03c547 (patch)
tree7f094e33fb530152c3ab6238ce7300750b47addb /src/match.c
downloadjwm-a041d9898e6d699bd8c0c25482ec574feb03c547.tar.gz
First commit
This is the original state of the released tarball for JWM 1.8, which will serve as my starting point for further modifications.
Diffstat (limited to 'src/match.c')
-rw-r--r--src/match.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/match.c b/src/match.c
new file mode 100644
index 0000000..95edd39
--- /dev/null
+++ b/src/match.c
@@ -0,0 +1,80 @@
+/****************************************************************************
+ * Expression matching.
+ * Copyright (C) 2004 Joe Wingbermuehle
+ ****************************************************************************/
+
+#include "jwm.h"
+#include "match.h"
+
+typedef struct MatchStateType {
+ const char *pattern;
+ const char *expression;
+ int patternOffset;
+ int expressionOffset;
+ int expressionLength;
+} MatchStateType;
+
+static int DoMatch(MatchStateType state);
+
+/****************************************************************************
+ ****************************************************************************/
+int Match(const char *pattern, const char *expression) {
+
+ MatchStateType state;
+
+ if(!pattern && !expression) {
+ return 1;
+ } else if(!pattern || !expression) {
+ return 0;
+ }
+
+ state.pattern = pattern;
+ state.expression = expression;
+ state.patternOffset = 0;
+ state.expressionOffset = 0;
+ state.expressionLength = strlen(expression);
+
+ return DoMatch(state);
+
+}
+
+/****************************************************************************
+ ****************************************************************************/
+int DoMatch(MatchStateType state) {
+
+ char p, e;
+
+ for(;;) {
+ p = state.pattern[state.patternOffset];
+ e = state.expression[state.expressionOffset];
+
+ if(p == 0 && e == 0) {
+ return 1;
+ } else if(p == 0 || e == 0) {
+ return 0;
+ }
+
+ switch(p) {
+ case '*':
+ ++state.patternOffset;
+ while(state.expressionOffset < state.expressionLength) {
+ if(DoMatch(state)) {
+ return 1;
+ }
+ ++state.expressionOffset;
+ }
+ return 0;
+ default:
+ if(p == e) {
+ ++state.patternOffset;
+ ++state.expressionOffset;
+ break;
+ } else {
+ return 0;
+ }
+ }
+ }
+
+}
+
+