' convert jpatch dump to sunflow file

x = {}
y = {}
z = {}
nx = {}
ny = {}
nz = {}
red = {}
green = {}
blue = {}
v1 = {}
v2 = {}
v3 = {}
startFace = {}

points = 0
materials = 0
faces = 0
open "cartoonrabbit.txt" for input as #1
print "reading the file..."
while not eof(1)

    ' get a line of text
    line input #1, text

    ' break into tokens
    i = 0
    text &= " "
    word = ""
    token = {}
    for index = 1 to count(text)
    ' for each index, char in text
        char = text[index]
        if char = " " then
            i = i + 1
            token[i] = word
            word = ""
        else
            word = word & char
        end if
    end for

    ' parse
    select case token[1]
        case "p"
            points += 1
            x[points] = token[2]
            y[points] = token[3]
            z[points] = token[4]
            nx[points] = token[5]
            ny[points] = token[6]
            nz[points] = token[7]
     
        case "m"
            materials += 1
            red[materials] = token[2]
            green[materials] = token[3]
            blue[materials] = token[4]
            startFace[materials] = faces+1            

        case "f"
            faces += 1
            v1[faces] = token[2]
            v2[faces] = token[3]
            v3[faces] = token[4]

        case else
            print "Unexpected data in file:" & text
            end

    end select
end while
close #1

