' File search: text pattern search, line-by-line, case options
#COMPILE EXE
#DIM ALL

SUB SplitIntoLines(BYVAL text AS STRING)
    LOCAL i AS LONG
    LOCAL startPos AS LONG
    LOCAL line AS STRING
    startPos = 1
    FOR i = 1 TO LEN(text)
        IF MID$(text, i, 1) = CHR$(10) THEN
            line = MID$(text, startPos, i - startPos)
            IF RIGHT$(line, 1) = CHR$(13) THEN line = LEFT$(line, LEN(line) - 1)
            IF LEN(line) > 0 THEN
                PRINT line
            END IF
            startPos = i + 1
        END IF
    NEXT i
    IF startPos <= LEN(text) THEN
        line = MID$(text, startPos)
        IF LEN(line) > 0 THEN PRINT line
    END IF
END SUB

SUB FindInText(BYVAL text AS STRING, BYVAL pattern AS STRING, BYVAL caseSensitive AS LONG)
    LOCAL i AS LONG
    LOCAL startPos AS LONG
    LOCAL lineNum AS LONG
    LOCAL pos AS LONG
    LOCAL line AS STRING
    LOCAL searchText AS STRING
    LOCAL searchPattern AS STRING
    startPos = 1
    lineNum = 0
    FOR i = 1 TO LEN(text)
        IF MID$(text, i, 1) = CHR$(10) THEN
            lineNum = lineNum + 1
            line = MID$(text, startPos, i - startPos)
            IF RIGHT$(line, 1) = CHR$(13) THEN line = LEFT$(line, LEN(line) - 1)
            IF caseSensitive THEN
                pos = INSTR(line, pattern)
            ELSE
                pos = INSTR(UCASE$(line), UCASE$(pattern))
            END IF
            IF pos > 0 THEN
                PRINT "Line" & FORMAT$(lineNum) & ":" & LEFT$(line, 60)
            END IF
            startPos = i + 1
        END IF
    NEXT i
END SUB

SUB MakeSampleText(BYREF t AS STRING)
    LOCAL crlf AS STRING
    crlf = CHR$(13) & CHR$(10)
    t = "The quick brown fox jumps over the lazy dog." & crlf
    t = t & "The quick brown fox jumps over the lazy cat." & crlf
    t = t & "PowerBASIC is a powerful programming language." & crlf
    t = t & "PBXB64 compiles PowerBASIC to native x64 code." & crlf
    t = t & "FOXES are quick animals that hunt at night." & crlf
    t = t & "Case sensitive SEARCH should find this." & crlf
    t = t & "case sensitive search should NOT find this." & crlf
END SUB

FUNCTION CountOccurrences(BYVAL text AS STRING, BYVAL pattern AS STRING) AS LONG
    LOCAL count AS LONG
    LOCAL pos AS LONG
    count = 0
    pos = 1
    DO
        pos = INSTR(pos, text, pattern)
        IF pos = 0 THEN EXIT DO
        count = count + 1
        pos = pos + LEN(pattern)
    LOOP
    FUNCTION = count
END FUNCTION

FUNCTION PBMAIN() AS LONG
    LOCAL sample AS STRING
    PRINT "=== File Search Utility ==="
    PRINT
    CALL MakeSampleText(sample)
    PRINT "Sample text:"
    PRINT sample
    PRINT
    PRINT "Search for 'fox':"
    CALL FindInText(sample, "fox", 0)
    PRINT
    PRINT "Search for 'BASIC' (case sensitive):"
    CALL FindInText(sample, "BASIC", 1)
    PRINT
    PRINT "Occurrences of 'the': " & FORMAT$(CountOccurrences(UCASE$(sample), "THE"))
    PRINT "Occurrences of 'fox': " & FORMAT$(CountOccurrences(sample, "fox"))
    PRINT "Occurrences of 'XYZ': " & FORMAT$(CountOccurrences(sample, "XYZ"))
    PRINT
    PRINT "File search tests complete."
    FUNCTION = 0
END FUNCTION
