// File: sundale[P]gcode/tool_parse.cc
// Function: loadToolTable(char const*, CANON_TOOL_TABLE*, int*, char**, int)
int loadToolTable(const char *filename,
			 CANON_TOOL_TABLE toolTable[],
			 int fms[],
			 char *ttcomments[],
			 int random_toolchanger)
{
    int fakepocket = 0;
    int t;
    FILE *fp;
    char buffer[CANON_TOOL_ENTRY_LEN];
    char orig_line[CANON_TOOL_ENTRY_LEN];
    int pocket = 0;

    if(!filename) return -1;

    // open tool table file
    if (NULL == (fp = fopen(filename, "r"))) {
	// can't open file
	return -1;
    }
    // clear out tool table
    for (t = random_toolchanger? 0: 1; t < CANON_POCKETS_MAX; t++) {
        toolTable[t].toolno = -1;
        ZERO_EMC_POSE(toolTable[t].offset);
        toolTable[t].diameter = 0.0;
        toolTable[t].frontangle = 0.0;
        toolTable[t].backangle = 0.0;
        toolTable[t].orientation = 0;
        if(fms) fms[t] = 0;
        if(ttcomments) ttcomments[t][0] = '\0';
    }

    /*
      Override 0's with codes from tool file
      File format is:

      <header>
      <pocket # 0..CANON_TOOL_MAX> <FMS id> <length> <diameter>
      ...

    */

    while (!feof(fp)) {
        const char *token;
        char *buff, *comment;
        int toolno, orientation, valid = 1;
        EmcPose offset;  // tlo
        double diameter, frontangle, backangle;

        // for nonrandom machines, just read the tools into pockets 1..n
        // no matter their tool numbers.  NB leave the spindle pocket 0
        // unchanged/empty.

        if (NULL == fgets(buffer, CANON_TOOL_ENTRY_LEN, fp)) {
            break;
        }
        strcpy(orig_line, buffer);

        if(scan_old_style(buffer, toolTable, fms, ttcomments,
                                random_toolchanger, fakepocket)) continue;

        toolno = -1;
        diameter = frontangle = backangle = 0.0;
        orientation = 0;
        ZERO_EMC_POSE(offset);
        buff = strtok(buffer, ";");
        comment = strtok(NULL, "\n");

        token = strtok(buff, " ");
        while (token != NULL) {
            switch (toupper(token[0])) {
            case 'T':
                if (sscanf(&token[1], "%d", &toolno) != 1)
                    valid = 0;
                break;
            case 'P':
                if (sscanf(&token[1], "%d", &pocket) != 1) {
                    valid = 0;
                    break;
                }
                if (!random_toolchanger) {
                    fakepocket++;
                    if (fakepocket >= CANON_POCKETS_MAX) {
                        printf("too many tools. skipping tool %d\n", toolno);
                        valid = 0;
                        break;
                    }
                    if(fms) fms[fakepocket] = pocket;
                    pocket = fakepocket;
                }
                if (pocket < 0 || pocket >= CANON_POCKETS_MAX) {
                    printf("max pocket number is %d. skipping tool %d\n", CANON_POCKETS_MAX - 1, toolno);
                    valid = 0;
                    break;
                }
                break;
            case 'D':
                if (sscanf(&token[1], "%lf", &diameter) != 1)
                    valid = 0;
                break;
            case 'X':
                if (sscanf(&token[1], "%lf", &offset.tran.x) != 1)
                    valid = 0;
                break;
            case 'Y':
                if (sscanf(&token[1], "%lf", &offset.tran.y) != 1)
                    valid = 0;
                break;
            case 'Z':
                if (sscanf(&token[1], "%lf", &offset.tran.z) != 1)
                    valid = 0;
                break;
            case 'A':
                if (sscanf(&token[1], "%lf", &offset.a) != 1)
                    valid = 0;
                break;
            case 'B':
                if (sscanf(&token[1], "%lf", &offset.b) != 1)
                    valid = 0;
                break;
            case 'C':
                if (sscanf(&token[1], "%lf", &offset.c) != 1)
                    valid = 0;
                break;
            case 'U':
                if (sscanf(&token[1], "%lf", &offset.u) != 1)
                    valid = 0;
                break;
            case 'V':
                if (sscanf(&token[1], "%lf", &offset.v) != 1)
                    valid = 0;
                break;
            case 'W':
                if (sscanf(&token[1], "%lf", &offset.w) != 1)
                    valid = 0;
                break;
            case 'I':
                if (sscanf(&token[1], "%lf", &frontangle) != 1)
                    valid = 0;
                break;
            case 'J':
                if (sscanf(&token[1], "%lf", &backangle) != 1)
                    valid = 0;
                break;
            case 'Q':
                if (sscanf(&token[1], "%d", &orientation) != 1)
                    valid = 0;
                break;
            default:
                if (strncmp(token, "\n", 1) != 0)
                    valid = 0;
                break;
            }
            token = strtok(NULL, " ");
        }
        if (valid) {
            toolTable[pocket].toolno = toolno;
            toolTable[pocket].offset = offset;
            toolTable[pocket].diameter = diameter;
            toolTable[pocket].frontangle = frontangle;
            toolTable[pocket].backangle = backangle;
            toolTable[pocket].orientation = orientation;

            if (ttcomments && comment)
                strcpy(ttcomments[pocket], comment);
        } else {
            fprintf(stderr, "Unrecognized line skipped: %s", orig_line);
        }
        if (!random_toolchanger && toolTable[0].toolno == toolTable[pocket].toolno) {
            toolTable[0] = toolTable[pocket];
        }
    }

    // close the file
    fclose(fp);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2e0, %rsp            # imm = 0x2E0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x244(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x9823
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa106
movq -0x10(%rbp), %rdi
leaq 0x18a6(%rip), %rsi      # 0xb0d4
callq 0x41d0
movq %rax, %rcx
movq %rcx, -0x40(%rbp)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x984d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa106
movl -0x2c(%rbp), %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x34(%rbp)
cmpl $0x38, -0x34(%rbp)
jge 0x99e5
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rax)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rax)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rax)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rax)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rax)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rax)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rax)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rax)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rax)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x50(%rax)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x58(%rax)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x60(%rax)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x68(%rax)
cmpq $0x0, -0x20(%rbp)
je 0x99bf
movq -0x20(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
cmpq $0x0, -0x28(%rbp)
je 0x99d5
movq -0x28(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movb $0x0, (%rax)
jmp 0x99d7
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x9860
jmp 0x99e7
movq -0x40(%rbp), %rdi
callq 0x4270
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x9a01
jmp 0xa0f6
movl $0x1, -0x26c(%rbp)
leaq -0x140(%rbp), %rdi
movq -0x40(%rbp), %rdx
movl $0x100, %esi            # imm = 0x100
callq 0x42a0
movq %rax, %rcx
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x9a2f
jmp 0xa0f6
leaq -0x240(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x4170
leaq -0x140(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl -0x2c(%rbp), %r8d
leaq -0x30(%rbp), %r9
callq 0xa120
testb $0x1, %al
jne 0x9a68
jmp 0x9a6d
jmp 0x99e7
movl $0xffffffff, -0x264(%rbp) # imm = 0xFFFFFFFF
xorps %xmm0, %xmm0
movsd %xmm0, -0x2d0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x2c8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x2c0(%rbp)
movl $0x0, -0x268(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x2b8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x2b0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x2a8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x2a0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x298(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x290(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x288(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x280(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x278(%rbp)
leaq -0x140(%rbp), %rdi
leaq 0x1f89(%rip), %rsi      # 0xba9c
callq 0x4290
movq %rax, -0x258(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq 0x1e31(%rip), %rsi      # 0xb95b
callq 0x4290
movq %rax, -0x260(%rbp)
movq -0x258(%rbp), %rdi
leaq 0x16f2(%rip), %rsi      # 0xb236
callq 0x4290
movq %rax, -0x250(%rbp)
cmpq $0x0, -0x250(%rbp)
je 0x9f9c
movq -0x250(%rbp), %rax
movsbl (%rax), %edi
callq 0x4300
addl $-0x41, %eax
movl %eax, %ecx
movq %rcx, -0x2d8(%rbp)
subl $0x19, %eax
ja 0x9f57
movq -0x2d8(%rbp), %rax
leaq 0x1ea4(%rip), %rcx      # 0xba34
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq 0x1ef3(%rip), %rsi      # 0xba9e
leaq -0x264(%rbp), %rdx
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9bc8
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq 0x1ebf(%rip), %rsi      # 0xba9e
leaq -0x244(%rbp), %rdx
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9c01
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
cmpl $0x0, -0x2c(%rbp)
jne 0x9c5a
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
cmpl $0x38, -0x30(%rbp)
jl 0x9c39
movl -0x264(%rbp), %esi
leaq 0x1e7e(%rip), %rdi      # 0xbaa1
movb $0x0, %al
callq 0x4030
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
cmpq $0x0, -0x20(%rbp)
je 0x9c51
movl -0x244(%rbp), %edx
movq -0x20(%rbp), %rax
movslq -0x30(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x30(%rbp), %eax
movl %eax, -0x244(%rbp)
cmpl $0x0, -0x244(%rbp)
jl 0x9c6c
cmpl $0x38, -0x244(%rbp)
jl 0x9c94
movl -0x264(%rbp), %edx
leaq 0x1e4a(%rip), %rdi      # 0xbac3
movl $0x37, %esi
movb $0x0, %al
callq 0x4030
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq 0x142e(%rip), %rsi      # 0xb0d9
leaq -0x2c0(%rbp), %rdx
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9cc8
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq -0x2b8(%rbp), %rdx
leaq 0x13f3(%rip), %rsi      # 0xb0d9
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9cfc
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq -0x2b8(%rbp), %rdx
addq $0x8, %rdx
leaq 0x13bb(%rip), %rsi      # 0xb0d9
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9d34
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq -0x2b8(%rbp), %rdx
addq $0x10, %rdx
leaq 0x1383(%rip), %rsi      # 0xb0d9
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9d6c
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq -0x2b8(%rbp), %rdx
addq $0x18, %rdx
leaq 0x134b(%rip), %rsi      # 0xb0d9
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9da4
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq -0x2b8(%rbp), %rdx
addq $0x20, %rdx
leaq 0x1313(%rip), %rsi      # 0xb0d9
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9ddc
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq -0x2b8(%rbp), %rdx
addq $0x28, %rdx
leaq 0x12db(%rip), %rsi      # 0xb0d9
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9e14
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq -0x2b8(%rbp), %rdx
addq $0x30, %rdx
leaq 0x12a3(%rip), %rsi      # 0xb0d9
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9e4c
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq -0x2b8(%rbp), %rdx
addq $0x38, %rdx
leaq 0x126b(%rip), %rsi      # 0xb0d9
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9e84
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq -0x2b8(%rbp), %rdx
addq $0x40, %rdx
leaq 0x1233(%rip), %rsi      # 0xb0d9
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9ebc
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq 0x1206(%rip), %rsi      # 0xb0d9
leaq -0x2c8(%rbp), %rdx
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9ef0
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq 0x11d2(%rip), %rsi      # 0xb0d9
leaq -0x2d0(%rbp), %rdx
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9f24
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
addq $0x1, %rdi
leaq 0x1b66(%rip), %rsi      # 0xba9e
leaq -0x268(%rbp), %rdx
movb $0x0, %al
callq 0x4180
cmpl $0x1, %eax
je 0x9f55
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
movq -0x250(%rbp), %rdi
leaq 0x19f6(%rip), %rsi      # 0xb95b
movl $0x1, %edx
callq 0x40b0
cmpl $0x0, %eax
je 0x9f7e
movl $0x0, -0x26c(%rbp)
jmp 0x9f80
xorl %eax, %eax
movl %eax, %edi
leaq 0x12ab(%rip), %rsi      # 0xb236
callq 0x4290
movq %rax, -0x250(%rbp)
jmp 0x9b50
cmpl $0x0, -0x26c(%rbp)
je 0xa090
movl -0x264(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq -0x244(%rbp), %rdx
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
movslq -0x244(%rbp), %rax
imulq $0x70, %rax, %rax
addq %rax, %rdi
addq $0x8, %rdi
leaq -0x2b8(%rbp), %rsi
movl $0x48, %edx
callq 0x4140
movsd -0x2c0(%rbp), %xmm0
movq -0x18(%rbp), %rax
movslq -0x244(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x50(%rax)
movsd -0x2c8(%rbp), %xmm0
movq -0x18(%rbp), %rax
movslq -0x244(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x58(%rax)
movsd -0x2d0(%rbp), %xmm0
movq -0x18(%rbp), %rax
movslq -0x244(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movsd %xmm0, 0x60(%rax)
movl -0x268(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq -0x244(%rbp), %rdx
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x68(%rax)
cmpq $0x0, -0x28(%rbp)
je 0xa08e
cmpq $0x0, -0x260(%rbp)
je 0xa08e
movq -0x28(%rbp), %rax
movslq -0x244(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x260(%rbp), %rsi
callq 0x4170
jmp 0xa0af
movq 0x4f49(%rip), %rax      # 0xefe0
movq (%rax), %rdi
leaq -0x240(%rbp), %rdx
leaq 0x1a46(%rip), %rsi      # 0xbaee
movb $0x0, %al
callq 0x4260
cmpl $0x0, -0x2c(%rbp)
jne 0xa0f1
movq -0x18(%rbp), %rax
movl (%rax), %eax
movq -0x18(%rbp), %rcx
movslq -0x244(%rbp), %rdx
imulq $0x70, %rdx, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
jne 0xa0f1
movq -0x18(%rbp), %rsi
movslq -0x244(%rbp), %rax
imulq $0x70, %rax, %rax
addq %rax, %rsi
movq -0x18(%rbp), %rdi
movl $0x70, %edx
callq 0x4140
jmp 0x99e7
movq -0x40(%rbp), %rdi
callq 0x4190
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
nopw %cs:(%rax,%rax)