' File browser with listbox, directory navigation via Win32 DECLARE
#COMPILE EXE
#DIM ALL

DECLARE FUNCTION FindFirstFile LIB "KERNEL32.DLL" ALIAS "FindFirstFileA" (BYVAL lpFileName AS ASCIIZ, BYREF lpFindFileData As Byte) AS LONG
DECLARE FUNCTION FindNextFile LIB "KERNEL32.DLL" ALIAS "FindNextFileA" (BYVAL hFindFile AS LONG, BYREF lpFindFileData As Byte) AS LONG
DECLARE FUNCTION FindClose LIB "KERNEL32.DLL" (BYVAL hFindFile AS LONG) AS LONG
DECLARE FUNCTION GetFileAttributes LIB "KERNEL32.DLL" ALIAS "GetFileAttributesA" (BYVAL lpFileName AS ASCIIZ) AS LONG

FUNCTION IsDirectory(BYVAL path AS STRING) AS LONG
    LOCAL attr AS LONG
    attr = GetFileAttributes(path)
    IF attr AND &H10 THEN FUNCTION = 1
    FUNCTION = 0
END FUNCTION

FUNCTION GetFileExt(BYVAL name AS STRING) AS STRING
    LOCAL i  AS LONG
    LOCAL n  AS LONG
    n = LEN(name)
    FOR i = n TO 1 STEP -1
        IF MID$(name, i, 1) = "." THEN
            FUNCTION = MID$(name, i + 1)
        END IF
    NEXT i
    FUNCTION = ""
END FUNCTION

FUNCTION GetFileName(BYVAL path AS STRING) AS STRING
    LOCAL i  AS LONG
    LOCAL n  AS LONG
    n = LEN(path)
    FOR i = n TO 1 STEP -1
        LOCAL ch AS STRING
        ch = MID$(path, i, 1)
        IF ch = "\" OR ch = "/" THEN
            FUNCTION = MID$(path, i + 1)
        END IF
    NEXT i
    FUNCTION = path
END FUNCTION

SUB ListDirectory(BYVAL dirPath AS STRING)
    PRINT "Contents of "; dirPath; ":"
    PRINT "----------------"
    LOCAL searchPath AS STRING
    searchPath = dirPath + "\*.*"
    LOCAL data(0:63) AS BYTE
    LOCAL hFind AS LONG
    hFind = FindFirstFile(searchPath, data(0))
    IF hFind = -1 THEN
        PRINT "(cannot access)"
        EXIT SUB
    END IF
    LOCAL iter AS LONG
    FOR iter = 1 TO 10
        IF hFind = -1 THEN EXIT FOR
        LOCAL name AS STRING
        name = ""
        LOCAL i AS LONG
        FOR i = 44 TO 75
            LOCAL b AS LONG
            b = data(i)
            IF b = 0 THEN EXIT FOR
            name = name + CHR$(b)
        NEXT i
        IF name = "" THEN EXIT FOR
        IF name <> "." AND name <> ".." THEN
            IF IsDirectory(dirPath + "\" + name) THEN
                PRINT "  [DIR]  "; name
            ELSE
                PRINT "  [FILE] "; name; "  (."; GetFileExt(name); ")"
            END IF
        END IF
        hFind = FindNextFile(hFind, data(0))
    NEXT iter
    FindClose(hFind)
    PRINT "----------------"
END SUB

SUB NavigateSimulation()
    PRINT "Directory navigation simulation:"
    PRINT "  C:\Users\Default\Documents\Projects"
    PRINT ""
    PRINT "Available items:"
    PRINT "  [DIR]  .."
    PRINT "  [DIR]  Work"
    PRINT "  [DIR]  Personal"
    PRINT "  [FILE] notes.txt"
    PRINT "  [FILE] budget.xlsx"
    PRINT "  [FILE] README.md"
    PRINT ""
    PRINT "Select '..' -> go to parent"
    PRINT "Select 'Work' -> enter directory"
END SUB

SUB FileFilterDemo()
    PRINT "File filters: *.txt, *.exe, *.*"
    DIM exts(0:4) AS STRING
    exts(0) = "txt"
    exts(1) = "exe"
    exts(2) = "dll"
    exts(3) = "b64"
    exts(4) = "pb"
    LOCAL i AS LONG
    FOR i = 0 TO 4
        PRINT "  Filter: *."; exts(i)
    NEXT i
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "=== File Browser Demo ==="
    PRINT "Win32: FindFirstFile, GetFileAttributes via DECLARE"
    NavigateSimulation()
    FileFilterDemo()
    PRINT "=== End File Browser ==="
    FUNCTION = 0
END FUNCTION
