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

SUB SplitIntoLines(BYVAL text AS STRING)
    LOCAL i AS LONG
    LOCAL startPos AS LONG
    LOCAL outLine AS STRING
    startPos = 1
    FOR i = 1 TO LEN(text)
        IF MID$(text, i, 1) = CHR$(10) THEN
            outLine = MID$(text, startPos, i - startPos)
            IF RIGHT$(outLine, 1) = CHR$(13) THEN outLine = LEFT$(outLine, LEN(outLine) - 1)
            IF LEN(outLine) > 0 THEN
                PRINT outLine
            END IF
            startPos = i + 1
        END IF
    NEXT i
    IF startPos <= LEN(text) THEN
        outLine = MID$(text, startPos)
        IF LEN(outLine) > 0 THEN PRINT outLine
    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 outLine 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
            outLine = MID$(text, startPos, i - startPos)
            IF RIGHT$(outLine, 1) = CHR$(13) THEN outLine = LEFT$(outLine, LEN(outLine) - 1)
            IF caseSensitive THEN
                pos = INSTR(outLine, pattern)
            ELSE
                pos = INSTR(UCASE$(outLine), UCASE$(pattern))
            END IF
            IF pos > 0 THEN
                PRINT "Line" & STR$(lineNum) & ":" & LEFT$(outLine,1)
            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': " + STR$(CountOccurrences(sample, "the"))
    PRINT "Occurrences of 'fox': " + STR$(CountOccurrences(sample, "fox"))
    PRINT "Occurrences of 'XYZ': " + STR$(CountOccurrences(sample, "XYZ"))
    PRINT
    PRINT "File search tests complete."
    FUNCTION = 0
END FUNCTION
