From a041d9898e6d699bd8c0c25482ec574feb03c547 Mon Sep 17 00:00:00 2001 From: "John Ankarstr\\xf6m" Date: Sat, 29 May 2021 12:54:47 +0200 Subject: 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. --- src/match.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/match.c (limited to 'src/match.c') 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; + } + } + } + +} + + -- cgit v1.2.3