' File Diff — compares two text files line by line, no string arrays
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL fileA AS STRING
    LOCAL fileB AS STRING
    LOCAL lineA AS STRING
    LOCAL lineB AS STRING
    LOCAL lineA0 AS STRING
    LOCAL lineA1 AS STRING
    LOCAL lineA2 AS STRING
    LOCAL lineA3 AS STRING
    LOCAL lineA4 AS STRING
    LOCAL lineA5 AS STRING
    LOCAL lineA6 AS STRING
    LOCAL lineA7 AS STRING
    LOCAL lineA8 AS STRING
    LOCAL lineA9 AS STRING
    LOCAL lineA10 AS STRING
    LOCAL lineB0 AS STRING
    LOCAL lineB1 AS STRING
    LOCAL lineB2 AS STRING
    LOCAL lineB3 AS STRING
    LOCAL lineB4 AS STRING
    LOCAL lineB5 AS STRING
    LOCAL lineB6 AS STRING
    LOCAL lineB7 AS STRING
    LOCAL lineB8 AS STRING
    LOCAL lineB9 AS STRING
    LOCAL lineB10 AS STRING
    LOCAL countA AS LONG
    LOCAL countB AS LONG
    LOCAL maxLines AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL differences AS LONG
    LOCAL matchCount AS LONG
    LOCAL lineLenA AS LONG
    LOCAL lineLenB AS LONG
    LOCAL lineStr AS STRING
    LOCAL maxLen AS LONG
    PRINT "============================================"
    PRINT "          FILE DIFF UTILITY"
    PRINT "============================================"
    PRINT ""
    fileA = "test_diff_a.txt"
    fileB = "test_diff_b.txt"
    PRINT "Creating test files..."
    PRINT ""
    OPEN fileA FOR OUTPUT AS #1
    FOR i = 0 TO 10
        IF i = 5 THEN
            PRINT #1, "Different line at position 5 in file A"
        ELSE
            IF i = 8 THEN
                PRINT #1, "Another unique line in file A"
            ELSE
                PRINT #1, "Common line number " + STR$(i)
            END IF
        END IF
    NEXT i
    CLOSE #1
    OPEN fileB FOR OUTPUT AS #1
    FOR i = 0 TO 10
        IF i = 5 THEN
            PRINT #1, "Different content at position 5 in file B"
        ELSE
            IF i = 8 THEN
                PRINT #1, "Extra line only in file B"
            ELSE
                PRINT #1, "Common line number " + STR$(i)
            END IF
        END IF
    NEXT i
    CLOSE #1
    PRINT "Reading file A..."
    countA = 0
    OPEN fileA FOR INPUT AS #1
    FOR i = 0 TO 10
        IF NOT EOF(1) THEN
            LINE INPUT #1, lineStr
            IF countA = 0 THEN lineA0 = lineStr
            IF countA = 1 THEN lineA1 = lineStr
            IF countA = 2 THEN lineA2 = lineStr
            IF countA = 3 THEN lineA3 = lineStr
            IF countA = 4 THEN lineA4 = lineStr
            IF countA = 5 THEN lineA5 = lineStr
            IF countA = 6 THEN lineA6 = lineStr
            IF countA = 7 THEN lineA7 = lineStr
            IF countA = 8 THEN lineA8 = lineStr
            IF countA = 9 THEN lineA9 = lineStr
            IF countA = 10 THEN lineA10 = lineStr
            countA = countA + 1
        END IF
    NEXT i
    CLOSE #1
    PRINT "Read " + STR$(countA) + " lines from file A"
    PRINT ""
    PRINT "Reading file B..."
    countB = 0
    OPEN fileB FOR INPUT AS #1
    FOR i = 0 TO 10
        IF NOT EOF(1) THEN
            LINE INPUT #1, lineStr
            IF countB = 0 THEN lineB0 = lineStr
            IF countB = 1 THEN lineB1 = lineStr
            IF countB = 2 THEN lineB2 = lineStr
            IF countB = 3 THEN lineB3 = lineStr
            IF countB = 4 THEN lineB4 = lineStr
            IF countB = 5 THEN lineB5 = lineStr
            IF countB = 6 THEN lineB6 = lineStr
            IF countB = 7 THEN lineB7 = lineStr
            IF countB = 8 THEN lineB8 = lineStr
            IF countB = 9 THEN lineB9 = lineStr
            IF countB = 10 THEN lineB10 = lineStr
            countB = countB + 1
        END IF
    NEXT i
    CLOSE #1
    PRINT "Read " + STR$(countB) + " lines from file B"
    PRINT ""
    PRINT "============================================"
    PRINT "      LINE-BY-LINE COMPARISON"
    PRINT "============================================"
    PRINT ""
    IF countA > countB THEN
        maxLines = countA
    ELSE
        maxLines = countB
    END IF
    differences = 0
    matchCount = 0
    FOR i = 0 TO maxLines - 1
        IF i < countA THEN
            IF i = 0 THEN lineA = lineA0
            IF i = 1 THEN lineA = lineA1
            IF i = 2 THEN lineA = lineA2
            IF i = 3 THEN lineA = lineA3
            IF i = 4 THEN lineA = lineA4
            IF i = 5 THEN lineA = lineA5
            IF i = 6 THEN lineA = lineA6
            IF i = 7 THEN lineA = lineA7
            IF i = 8 THEN lineA = lineA8
            IF i = 9 THEN lineA = lineA9
            IF i = 10 THEN lineA = lineA10
        ELSE
            lineA = ""
        END IF
        IF i < countB THEN
            IF i = 0 THEN lineB = lineB0
            IF i = 1 THEN lineB = lineB1
            IF i = 2 THEN lineB = lineB2
            IF i = 3 THEN lineB = lineB3
            IF i = 4 THEN lineB = lineB4
            IF i = 5 THEN lineB = lineB5
            IF i = 6 THEN lineB = lineB6
            IF i = 7 THEN lineB = lineB7
            IF i = 8 THEN lineB = lineB8
            IF i = 9 THEN lineB = lineB9
            IF i = 10 THEN lineB = lineB10
        ELSE
            lineB = ""
        END IF
        IF lineA = "" AND lineB = "" THEN
            GOTO skipDiff
        END IF
        IF lineA = "" THEN
            lineStr = "  - " + STR$(i) + ": [EOF]"
            lineStr = lineStr + "  |  + " + STR$(i) + ": " + MID$(lineB, 1, 40)
            PRINT lineStr
            differences = differences + 1
        ELSE
            IF lineB = "" THEN
                lineStr = "  + " + STR$(i) + ": " + MID$(lineA, 1, 40)
                lineStr = lineStr + "  |  - " + STR$(i) + ": [EOF]"
                PRINT lineStr
                differences = differences + 1
            ELSE
                IF lineA = lineB THEN
                    matchCount = matchCount + 1
                ELSE
                    lineStr = "  * " + STR$(i) + ": A=" + MID$(lineA, 1, 35)
                    lineStr = lineStr + "  B=" + MID$(lineB, 1, 35)
                    PRINT lineStr
                    differences = differences + 1
                END IF
            END IF
        END IF
skipDiff:
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "           DIFF SUMMARY"
    PRINT "============================================"
    PRINT ""
    PRINT "File A: " + STR$(countA) + " lines"
    PRINT "File B: " + STR$(countB) + " lines"
    PRINT "Matching: " + STR$(matchCount)
    PRINT "Differences: " + STR$(differences)
    PRINT ""
    PRINT "============================================"
    PRINT "      CHARACTER-BY-CHARACTER DIFF"
    PRINT "============================================"
    PRINT ""
    lineA = "The quick brown fox jumps over the lazy dog"
    lineB = "The quick brown cat jumps over the lazy dog"
    lineLenA = LEN(lineA)
    lineLenB = LEN(lineB)
    PRINT "Comparing two strings char by char:"
    PRINT "  A: " + lineA
    PRINT "  B: " + lineB
    PRINT ""
    IF lineLenA > lineLenB THEN
        maxLen = lineLenA
    ELSE
        maxLen = lineLenB
    END IF
    differences = 0
    lineStr = "     "
    FOR i = 1 TO maxLen
        IF i <= lineLenA AND i <= lineLenB THEN
            IF MID$(lineA, i, 1) <> MID$(lineB, i, 1) THEN
                lineStr = lineStr + "^"
                differences = differences + 1
            ELSE
                lineStr = lineStr + "."
            END IF
        ELSE
            lineStr = lineStr + "^"
            differences = differences + 1
        END IF
    NEXT i
    PRINT lineStr
    PRINT ""
    PRINT "Character differences: " + STR$(differences)
    PRINT ""
    PRINT "Cleaning up test files..."
    KILL fileA
    KILL fileB
    PRINT "Cleanup complete"
    PRINT ""
    PRINT "File diff operations complete"
    FUNCTION = 0
END FUNCTION
