' Glob Pattern Matching — matches file paths with glob patterns
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL pattern AS STRING
    LOCAL text AS STRING
    LOCAL pIdx AS LONG
    LOCAL tIdx AS LONG
    LOCAL starIdx AS LONG
    LOCAL matchIdx AS LONG
    LOCAL matched AS LONG
    LOCAL pLen AS LONG
    LOCAL tLen AS LONG
    LOCAL pChar AS STRING
    LOCAL tChar AS STRING
    LOCAL testCount AS LONG
    LOCAL passCount AS LONG
    LOCAL failCount AS LONG
    LOCAL status AS STRING
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL arrCount AS LONG
    LOCAL tempStr AS STRING
    LOCAL s0 AS STRING
    LOCAL s1 AS STRING
    LOCAL s2 AS STRING
    LOCAL s3 AS STRING
    LOCAL s4 AS STRING
    LOCAL s5 AS STRING
    LOCAL s6 AS STRING
    LOCAL s7 AS STRING
    LOCAL s8 AS STRING
    LOCAL s9 AS STRING
    testCount = 0
    passCount = 0
    failCount = 0
    PRINT "============================================"
    PRINT "        GLOB PATTERN MATCHING"
    PRINT "============================================"
    PRINT ""
    PRINT "Glob patterns with * and ?"
    PRINT ""
    arrCount = 10
    s0 = "readme.txt"
    s1 = "readme.md"
    s2 = "readme.adoc"
    s3 = "document.txt"
    s4 = "script.bas"
    s5 = "script.bi"
    s6 = "main.c"
    s7 = "main.h"
    s8 = "Makefile"
    s9 = "readme.txt.bak"
    pattern = "*.txt"
    PRINT "Pattern: '" + pattern + "'"
    FOR i = 0 TO arrCount - 1
        IF i = 0 THEN tempStr = s0
        IF i = 1 THEN tempStr = s1
        IF i = 2 THEN tempStr = s2
        IF i = 3 THEN tempStr = s3
        IF i = 4 THEN tempStr = s4
        IF i = 5 THEN tempStr = s5
        IF i = 6 THEN tempStr = s6
        IF i = 7 THEN tempStr = s7
        IF i = 8 THEN tempStr = s8
        IF i = 9 THEN tempStr = s9
        text = tempStr
        pLen = LEN(pattern)
        tLen = LEN(text)
        pIdx = 1
        tIdx = 1
        starIdx = -1
        matchIdx = -1
        DO WHILE tIdx <= tLen
            IF pIdx <= pLen THEN
                pChar = MID$(pattern, pIdx, 1)
            ELSE
                pChar = ""
            END IF
            IF tIdx <= tLen THEN
                tChar = MID$(text, tIdx, 1)
            ELSE
                tChar = ""
            END IF
            IF pIdx <= pLen AND pChar = "*" THEN
                starIdx = pIdx
                matchIdx = tIdx
                pIdx = pIdx + 1
            ELSE
                IF pIdx <= pLen AND (pChar = "?" OR pChar = tChar) THEN
                    pIdx = pIdx + 1
                    tIdx = tIdx + 1
                ELSE
                    IF starIdx <> -1 THEN
                        pIdx = starIdx + 1
                        matchIdx = matchIdx + 1
                        tIdx = matchIdx
                    ELSE
                        matched = 0
                        EXIT DO
                    END IF
                END IF
            END IF
        LOOP
        DO WHILE pIdx <= pLen AND MID$(pattern, pIdx, 1) = "*"
            pIdx = pIdx + 1
        LOOP
        IF pIdx > pLen THEN
            matched = 1
        ELSE
            matched = 0
        END IF
        IF matched = 1 THEN
            PRINT "  " + text + " -> match"
        ELSE
            PRINT "  " + text + " -> no match"
        END IF
    NEXT i
    PRINT ""
    pattern = "readme.*"
    PRINT "Pattern: '" + pattern + "'"
    FOR i = 0 TO arrCount - 1
        IF i = 0 THEN tempStr = s0
        IF i = 1 THEN tempStr = s1
        IF i = 2 THEN tempStr = s2
        IF i = 3 THEN tempStr = s3
        IF i = 4 THEN tempStr = s4
        IF i = 5 THEN tempStr = s5
        IF i = 6 THEN tempStr = s6
        IF i = 7 THEN tempStr = s7
        IF i = 8 THEN tempStr = s8
        IF i = 9 THEN tempStr = s9
        text = tempStr
        pLen = LEN(pattern)
        tLen = LEN(text)
        pIdx = 1
        tIdx = 1
        starIdx = -1
        matchIdx = -1
        DO WHILE tIdx <= tLen
            IF pIdx <= pLen THEN
                pChar = MID$(pattern, pIdx, 1)
            ELSE
                pChar = ""
            END IF
            IF tIdx <= tLen THEN
                tChar = MID$(text, tIdx, 1)
            ELSE
                tChar = ""
            END IF
            IF pIdx <= pLen AND pChar = "*" THEN
                starIdx = pIdx
                matchIdx = tIdx
                pIdx = pIdx + 1
            ELSE
                IF pIdx <= pLen AND (pChar = "?" OR pChar = tChar) THEN
                    pIdx = pIdx + 1
                    tIdx = tIdx + 1
                ELSE
                    IF starIdx <> -1 THEN
                        pIdx = starIdx + 1
                        matchIdx = matchIdx + 1
                        tIdx = matchIdx
                    ELSE
                        matched = 0
                        EXIT DO
                    END IF
                END IF
            END IF
        LOOP
        DO WHILE pIdx <= pLen AND MID$(pattern, pIdx, 1) = "*"
            pIdx = pIdx + 1
        LOOP
        IF pIdx > pLen THEN
            matched = 1
        ELSE
            matched = 0
        END IF
        IF matched = 1 THEN
            PRINT "  " + text + " -> match"
        ELSE
            PRINT "  " + text + " -> no match"
        END IF
    NEXT i
    PRINT ""
    pattern = "*.???"
    PRINT "Pattern: '" + pattern + "' (extension exactly 3 chars)"
    FOR i = 0 TO arrCount - 1
        IF i = 0 THEN tempStr = s0
        IF i = 1 THEN tempStr = s1
        IF i = 2 THEN tempStr = s2
        IF i = 3 THEN tempStr = s3
        IF i = 4 THEN tempStr = s4
        IF i = 5 THEN tempStr = s5
        IF i = 6 THEN tempStr = s6
        IF i = 7 THEN tempStr = s7
        IF i = 8 THEN tempStr = s8
        IF i = 9 THEN tempStr = s9
        text = tempStr
        pLen = LEN(pattern)
        tLen = LEN(text)
        pIdx = 1
        tIdx = 1
        starIdx = -1
        matchIdx = -1
        DO WHILE tIdx <= tLen
            IF pIdx <= pLen THEN
                pChar = MID$(pattern, pIdx, 1)
            ELSE
                pChar = ""
            END IF
            IF tIdx <= tLen THEN
                tChar = MID$(text, tIdx, 1)
            ELSE
                tChar = ""
            END IF
            IF pIdx <= pLen AND pChar = "*" THEN
                starIdx = pIdx
                matchIdx = tIdx
                pIdx = pIdx + 1
            ELSE
                IF pIdx <= pLen AND (pChar = "?" OR pChar = tChar) THEN
                    pIdx = pIdx + 1
                    tIdx = tIdx + 1
                ELSE
                    IF starIdx <> -1 THEN
                        pIdx = starIdx + 1
                        matchIdx = matchIdx + 1
                        tIdx = matchIdx
                    ELSE
                        matched = 0
                        EXIT DO
                    END IF
                END IF
            END IF
        LOOP
        DO WHILE pIdx <= pLen AND MID$(pattern, pIdx, 1) = "*"
            pIdx = pIdx + 1
        LOOP
        IF pIdx > pLen THEN
            matched = 1
        ELSE
            matched = 0
        END IF
        IF matched = 1 THEN
            PRINT "  " + text + " -> match"
        ELSE
            PRINT "  " + text + " -> no match"
        END IF
    NEXT i
    PRINT ""
    pattern = "script.*"
    PRINT "Pattern: '" + pattern + "'"
    FOR i = 0 TO arrCount - 1
        IF i = 0 THEN tempStr = s0
        IF i = 1 THEN tempStr = s1
        IF i = 2 THEN tempStr = s2
        IF i = 3 THEN tempStr = s3
        IF i = 4 THEN tempStr = s4
        IF i = 5 THEN tempStr = s5
        IF i = 6 THEN tempStr = s6
        IF i = 7 THEN tempStr = s7
        IF i = 8 THEN tempStr = s8
        IF i = 9 THEN tempStr = s9
        text = tempStr
        pLen = LEN(pattern)
        tLen = LEN(text)
        pIdx = 1
        tIdx = 1
        starIdx = -1
        matchIdx = -1
        DO WHILE tIdx <= tLen
            IF pIdx <= pLen THEN
                pChar = MID$(pattern, pIdx, 1)
            ELSE
                pChar = ""
            END IF
            IF tIdx <= tLen THEN
                tChar = MID$(text, tIdx, 1)
            ELSE
                tChar = ""
            END IF
            IF pIdx <= pLen AND pChar = "*" THEN
                starIdx = pIdx
                matchIdx = tIdx
                pIdx = pIdx + 1
            ELSE
                IF pIdx <= pLen AND (pChar = "?" OR pChar = tChar) THEN
                    pIdx = pIdx + 1
                    tIdx = tIdx + 1
                ELSE
                    IF starIdx <> -1 THEN
                        pIdx = starIdx + 1
                        matchIdx = matchIdx + 1
                        tIdx = matchIdx
                    ELSE
                        matched = 0
                        EXIT DO
                    END IF
                END IF
            END IF
        LOOP
        DO WHILE pIdx <= pLen AND MID$(pattern, pIdx, 1) = "*"
            pIdx = pIdx + 1
        LOOP
        IF pIdx > pLen THEN
            matched = 1
        ELSE
            matched = 0
        END IF
        IF matched = 1 THEN
            PRINT "  " + text + " -> match"
        ELSE
            PRINT "  " + text + " -> no match"
        END IF
    NEXT i
    PRINT ""
    pattern = "main.?"
    PRINT "Pattern: '" + pattern + "' (single char extension)"
    FOR i = 0 TO arrCount - 1
        IF i = 0 THEN tempStr = s0
        IF i = 1 THEN tempStr = s1
        IF i = 2 THEN tempStr = s2
        IF i = 3 THEN tempStr = s3
        IF i = 4 THEN tempStr = s4
        IF i = 5 THEN tempStr = s5
        IF i = 6 THEN tempStr = s6
        IF i = 7 THEN tempStr = s7
        IF i = 8 THEN tempStr = s8
        IF i = 9 THEN tempStr = s9
        text = tempStr
        pLen = LEN(pattern)
        tLen = LEN(text)
        pIdx = 1
        tIdx = 1
        starIdx = -1
        matchIdx = -1
        DO WHILE tIdx <= tLen
            IF pIdx <= pLen THEN
                pChar = MID$(pattern, pIdx, 1)
            ELSE
                pChar = ""
            END IF
            IF tIdx <= tLen THEN
                tChar = MID$(text, tIdx, 1)
            ELSE
                tChar = ""
            END IF
            IF pIdx <= pLen AND pChar = "*" THEN
                starIdx = pIdx
                matchIdx = tIdx
                pIdx = pIdx + 1
            ELSE
                IF pIdx <= pLen AND (pChar = "?" OR pChar = tChar) THEN
                    pIdx = pIdx + 1
                    tIdx = tIdx + 1
                ELSE
                    IF starIdx <> -1 THEN
                        pIdx = starIdx + 1
                        matchIdx = matchIdx + 1
                        tIdx = matchIdx
                    ELSE
                        matched = 0
                        EXIT DO
                    END IF
                END IF
            END IF
        LOOP
        DO WHILE pIdx <= pLen AND MID$(pattern, pIdx, 1) = "*"
            pIdx = pIdx + 1
        LOOP
        IF pIdx > pLen THEN
            matched = 1
        ELSE
            matched = 0
        END IF
        IF matched = 1 THEN
            PRINT "  " + text + " -> match"
        ELSE
            PRINT "  " + text + " -> no match"
        END IF
    NEXT i
    PRINT ""
    pattern = "Makefile"
    PRINT "Pattern: '" + pattern + "' (exact match)"
    FOR i = 0 TO arrCount - 1
        IF i = 0 THEN tempStr = s0
        IF i = 1 THEN tempStr = s1
        IF i = 2 THEN tempStr = s2
        IF i = 3 THEN tempStr = s3
        IF i = 4 THEN tempStr = s4
        IF i = 5 THEN tempStr = s5
        IF i = 6 THEN tempStr = s6
        IF i = 7 THEN tempStr = s7
        IF i = 8 THEN tempStr = s8
        IF i = 9 THEN tempStr = s9
        text = tempStr
        pLen = LEN(pattern)
        tLen = LEN(text)
        pIdx = 1
        tIdx = 1
        starIdx = -1
        matchIdx = -1
        DO WHILE tIdx <= tLen
            IF pIdx <= pLen THEN
                pChar = MID$(pattern, pIdx, 1)
            ELSE
                pChar = ""
            END IF
            IF tIdx <= tLen THEN
                tChar = MID$(text, tIdx, 1)
            ELSE
                tChar = ""
            END IF
            IF pIdx <= pLen AND pChar = "*" THEN
                starIdx = pIdx
                matchIdx = tIdx
                pIdx = pIdx + 1
            ELSE
                IF pIdx <= pLen AND (pChar = "?" OR pChar = tChar) THEN
                    pIdx = pIdx + 1
                    tIdx = tIdx + 1
                ELSE
                    IF starIdx <> -1 THEN
                        pIdx = starIdx + 1
                        matchIdx = matchIdx + 1
                        tIdx = matchIdx
                    ELSE
                        matched = 0
                        EXIT DO
                    END IF
                END IF
            END IF
        LOOP
        DO WHILE pIdx <= pLen AND MID$(pattern, pIdx, 1) = "*"
            pIdx = pIdx + 1
        LOOP
        IF pIdx > pLen THEN
            matched = 1
        ELSE
            matched = 0
        END IF
        IF matched = 1 THEN
            PRINT "  " + text + " -> match"
        ELSE
            PRINT "  " + text + " -> no match"
        END IF
    NEXT i
    PRINT ""
    PRINT "Test summary: " + STR$(passCount) + " passed, " + STR$(failCount) + " failed"
    FUNCTION = 0
END FUNCTION
