' xprint_barcode.pb — Barcode generation simulation (Code 128 style)
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL barcodeData(30) AS LONG
    LOCAL barcodePattern(200) AS LONG
    LOCAL dataLen AS LONG
    LOCAL patternLen AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL k AS LONG
    LOCAL checksum AS LONG
    LOCAL bitPos AS LONG
    LOCAL barWidth AS LONG
    LOCAL totalWidth AS LONG
    LOCAL charVal AS LONG
    LOCAL patternVal AS LONG
    LOCAL quietZone AS LONG
    LOCAL startPattern AS LONG
    LOCAL stopPattern AS LONG
    LOCAL numBarcodes AS LONG
    LOCAL iter AS LONG
    LOCAL barcodeStr AS STRING
    LOCAL result AS LONG
    LOCAL barChar AS STRING

    quietZone = 10
    startPattern = 104
    stopPattern = 106

    PRINT "=== Barcode Generator (Code 128) ==="

    FOR numBarcodes = 1 TO 4
        IF numBarcodes = 1 THEN barcodeData(0) = 72
        IF numBarcodes = 1 THEN barcodeData(1) = 69
        IF numBarcodes = 1 THEN barcodeData(2) = 76
        IF numBarcodes = 1 THEN barcodeData(3) = 76
        IF numBarcodes = 1 THEN barcodeData(4) = 79
        IF numBarcodes = 1 THEN dataLen = 5

        IF numBarcodes = 2 THEN barcodeData(0) = 87
        IF numBarcodes = 2 THEN barcodeData(1) = 79
        IF numBarcodes = 2 THEN barcodeData(2) = 82
        IF numBarcodes = 2 THEN barcodeData(3) = 76
        IF numBarcodes = 2 THEN barcodeData(4) = 68
        IF numBarcodes = 2 THEN dataLen = 5

        IF numBarcodes = 3 THEN barcodeData(0) = 84
        IF numBarcodes = 3 THEN barcodeData(1) = 69
        IF numBarcodes = 3 THEN barcodeData(2) = 83
        IF numBarcodes = 3 THEN barcodeData(3) = 84
        IF numBarcodes = 3 THEN dataLen = 4

        IF numBarcodes = 4 THEN barcodeData(0) = 49
        IF numBarcodes = 4 THEN barcodeData(1) = 50
        IF numBarcodes = 4 THEN barcodeData(2) = 51
        IF numBarcodes = 4 THEN barcodeData(3) = 52
        IF numBarcodes = 4 THEN barcodeData(4) = 53
        IF numBarcodes = 4 THEN dataLen = 5

        barcodeStr = ""
        FOR i = 0 TO quietZone - 1
            barcodeStr = barcodeStr + " "
        NEXT i

        checksum = startPattern
        barcodeStr = barcodeStr + "S"
        FOR i = 0 TO dataLen - 1
            charVal = barcodeData(i)
            checksum = checksum + charVal * (i + 1)
            barcodeStr = barcodeStr + CHR$(charVal)
        NEXT i
        checksum = checksum MOD 103
        barcodeStr = barcodeStr + "C"
        barcodeStr = barcodeStr + "E"

        FOR i = 0 TO quietZone - 1
            barcodeStr = barcodeStr + " "
        NEXT i

        PRINT "  Barcode " + STR$(numBarcodes) + " (len=" + STR$(dataLen) + "): checksum=" + STR$(checksum)
        PRINT "  " + barcodeStr
    NEXT numBarcodes

    PRINT "=== End Barcode ==="
    FUNCTION = 0
END FUNCTION
