' Platform detection via #IF, conditional compilation
#COMPILE EXE
#DIM ALL

#DEFINE PLATFORM_WIN32 AS 1
#DEFINE PLATFORM_WIN64 AS 2
#DEFINE PLATFORM_LINUX AS 3
#DEFINE PLATFORM_MAC AS 4

#DEFINE TARGET_PLATFORM AS PLATFORM_WIN64

#IF TARGET_PLATFORM = PLATFORM_WIN32
    #DEFINE PLATFORM_NAME AS "Windows 32-bit"
    #DEFINE PLATFORM_MAX_PATH AS 260
    #DEFINE PLATFORM_SEP AS "\"
#ELSEIF TARGET_PLATFORM = PLATFORM_WIN64
    #DEFINE PLATFORM_NAME AS "Windows 64-bit"
    #DEFINE PLATFORM_MAX_PATH AS 32767
    #DEFINE PLATFORM_SEP AS "\"
#ELSEIF TARGET_PLATFORM = PLATFORM_LINUX
    #DEFINE PLATFORM_NAME AS "Linux"
    #DEFINE PLATFORM_MAX_PATH AS 4096
    #DEFINE PLATFORM_SEP AS "/"
#ELSE
    #DEFINE PLATFORM_NAME AS "Unknown"
    #DEFINE PLATFORM_MAX_PATH AS 1024
    #DEFINE PLATFORM_SEP AS "/"
#ENDIF

FUNCTION IsAbsolutePath(BYVAL p AS STRING) AS LONG
    LOCAL n AS LONG
    n = LEN(p)
    #IF TARGET_PLATFORM = PLATFORM_WIN32 OR TARGET_PLATFORM = PLATFORM_WIN64
        IF n >= 2 THEN
            LOCAL s AS STRING
            s = MID$(p, 2, 1)
            IF s = ":" THEN
                FUNCTION = 1
            END IF
            IF LEFT$(p, 1) = "\" AND s = "\" THEN
                FUNCTION = 1
            END IF
        END IF
    #ELSE
        IF n >= 1 THEN
            IF LEFT$(p, 1) = "/" THEN
                FUNCTION = 1
            END IF
        END IF
    #ENDIF
    FUNCTION = 0
END FUNCTION

FUNCTION NormalizePath(BYVAL p AS STRING) AS STRING
    #IF TARGET_PLATFORM = PLATFORM_WIN32 OR TARGET_PLATFORM = PLATFORM_WIN64
        FUNCTION = UCASE$(LEFT$(p, 1)) + MID$(p, 2)
    #ELSE
        FUNCTION = p
    #ENDIF
END FUNCTION

SUB GetPlatformLimits(BYREF mp AS LONG, BYREF ma AS LONG)
    #IF TARGET_PLATFORM = PLATFORM_WIN32
        mp = 260
        ma = 8192
    #ELSEIF TARGET_PLATFORM = PLATFORM_WIN64
        mp = 32767
        ma = 32767
    #ELSE
        mp = 4096
        ma = 131072
    #ENDIF
END SUB

FUNCTION GetLineEnding() AS STRING
    #IF TARGET_PLATFORM = PLATFORM_WIN32 OR TARGET_PLATFORM = PLATFORM_WIN64
        FUNCTION = CHR$(13) + CHR$(10)
    #ELSE
        FUNCTION = CHR$(10)
    #ENDIF
END FUNCTION

FUNCTION GetConfigDir(BYVAL app AS STRING) AS STRING
    #IF TARGET_PLATFORM = PLATFORM_WIN32 OR TARGET_PLATFORM = PLATFORM_WIN64
        FUNCTION = "C:\Users\Default\AppData\Roaming\" + app
    #ELSE
        FUNCTION = "/home/user/.config/" + app
    #ENDIF
END FUNCTION

SUB PrintSystemInfo()
    PRINT "Platform: "; PLATFORM_NAME
    PRINT "Max path: "; PLATFORM_MAX_PATH
    PRINT "Separator: "; PLATFORM_SEP
    #IF TARGET_PLATFORM = PLATFORM_WIN32 OR TARGET_PLATFORM = PLATFORM_WIN64
        PRINT "Case: insensitive"
        PRINT "EOL: CR+LF"
    #ELSE
        PRINT "Case: sensitive"
        PRINT "EOL: LF"
    #ENDIF
END SUB

SUB SimulateIO(BYVAL op AS STRING, BYVAL p AS STRING)
    PRINT "["; PLATFORM_NAME; "] "; op; ": "; p
    #IF TARGET_PLATFORM = PLATFORM_LINUX
        PRINT "  (Linux syscalls)"
    #ELSE
        PRINT "  (Windows API)"
    #ENDIF
END SUB

FUNCTION IsWinPlatform() AS LONG
    #IF TARGET_PLATFORM = PLATFORM_WIN32 OR TARGET_PLATFORM = PLATFORM_WIN64
        FUNCTION = 1
    #ELSE
        FUNCTION = 0
    #ENDIF
END FUNCTION

FUNCTION PBMAIN() AS LONG
    PRINT "=== Platform Detection Demo ==="
    PrintSystemInfo()
    PRINT "IDs: win32="; PLATFORM_WIN32; " win64="; PLATFORM_WIN64
    PRINT "linux="; PLATFORM_LINUX; " mac="; PLATFORM_MAC

    LOCAL p AS STRING
    p = "C:" + PLATFORM_SEP + "Windows" + PLATFORM_SEP + "System32"
    PRINT "Path: "; p

    LOCAL a AS LONG
    a = IsAbsolutePath(p)
    PRINT "IsAbsolute: "; a
    a = IsAbsolutePath("rel" + PLATFORM_SEP + "path")
    PRINT "IsAbsolute(rel): "; a

    LOCAL mp  AS LONG

    LOCAL ma  AS LONG
    GetPlatformLimits(mp, ma)
    PRINT "Limits: path="; mp; " args="; ma

    LOCAL e AS STRING
    e = GetLineEnding()
    PRINT "EOL length: "; LEN(e)

    LOCAL d AS STRING
    d = GetConfigDir("MyApp")
    PRINT "Config: "; d

    LOCAL n AS STRING
    n = NormalizePath("c:\windows")
    PRINT "Normalized: "; n

    SimulateIO("OPEN", "C:\data\file.txt")
    SimulateIO("WRITE", "C:\data\output.log")

    PRINT "IsWinPlatform = "; IsWinPlatform()
    PRINT "=== End Platform Detection ==="
    FUNCTION = 0
END FUNCTION
