// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3e8, %rsp            # imm = 0x3E8
movl %r8d, -0x34(%rbp)
andl $0x0, -0x30(%rbp)
pushq $-0x1
popq %rbx
testq %rdi, %rdi
je 0x8637
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, %r12
leaq 0x116c(%rip), %rsi      # 0x90b4
callq 0x41c0
movq %rax, -0x60(%rbp)
testq %rax, %rax
je 0x8637
xorl %ecx, %ecx
cmpl $0x0, -0x34(%rbp)
sete %cl
leaq (%r15,%rcx,8), %rax
imulq $0x70, %rcx, %rsi
shll $0x2, %ecx
leaq (%r14,%rcx), %rdx
orq $-0xe0, %rcx
addq %r12, %rsi
addq $0x8, %rsi
xorl %edi, %edi
xorps %xmm0, %xmm0
movq %rcx, %r8
addq %rdi, %r8
je 0x7fcc
orl $-0x1, -0x8(%rsi)
andl $0x0, 0x60(%rsi)
movups %xmm0, (%rsi)
movups %xmm0, 0x10(%rsi)
movups %xmm0, 0x20(%rsi)
movups %xmm0, 0x30(%rsi)
movups %xmm0, 0x40(%rsi)
movups %xmm0, 0x50(%rsi)
testq %r14, %r14
je 0x7fb5
andl $0x0, (%rdx,%rdi)
testq %r15, %r15
je 0x7fc2
movq (%rax,%rdi,2), %r8
movb $0x0, (%r8)
addq $0x4, %rdi
addq $0x70, %rsi
jmp 0x7f85
movq %r12, -0x58(%rbp)
movq %r14, -0x68(%rbp)
leaq 0x123b(%rip), %rbx      # 0x9216
leaq 0x1a4e(%rip), %r13      # 0x9a30
movq $0x0, -0x48(%rbp)
movq -0x60(%rbp), %r12
movq %r15, -0x88(%rbp)
movq %r12, %rdi
callq 0x4250
testl %eax, %eax
jne 0x862d
leaq -0x410(%rbp), %r14
movq %r14, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdx
callq 0x4280
testq %rax, %rax
je 0x862d
leaq -0x310(%rbp), %rdi
movq %r14, %rsi
callq 0x4150
subq $0x8, %rsp
movq %r14, %rdi
leaq 0x1ac6(%rip), %rsi      # 0x9b08
leaq -0x38(%rbp), %rdx
leaq -0x2c(%rbp), %rcx
leaq -0x78(%rbp), %r8
leaq -0xc0(%rbp), %r9
xorl %eax, %eax
leaq -0x210(%rbp), %r10
pushq %r10
leaq -0x7c(%rbp), %r10
pushq %r10
leaq -0xb0(%rbp), %r10
pushq %r10
leaq -0xb8(%rbp), %r10
pushq %r10
leaq -0x70(%rbp), %r10
pushq %r10
callq 0x4160
addq $0x30, %rsp
testl %eax, %eax
je 0x80ae
movl %eax, %ecx
andl $-0x2, %ecx
cmpl $0x8, %ecx
jne 0x80ae
cmpl $0x0, -0x34(%rbp)
je 0x8495
movl -0x2c(%rbp), %ecx
movq -0x48(%rbp), %rdx
movl %edx, %r14d
jmp 0x84bf
subq $0x8, %rsp
movq %r14, %rdi
leaq 0x1a6f(%rip), %rsi      # 0x9b2b
leaq -0x38(%rbp), %rdx
leaq -0x2c(%rbp), %rcx
leaq -0x78(%rbp), %r8
leaq -0x70(%rbp), %r9
xorl %eax, %eax
leaq -0x210(%rbp), %r10
pushq %r10
callq 0x4160
addq $0x10, %rsp
testl %eax, %eax
je 0x8107
movl %eax, %ecx
andl $-0x2, %ecx
cmpl $0x4, %ecx
jne 0x8107
cmpl $0x0, -0x34(%rbp)
je 0x8539
movl -0x2c(%rbp), %ecx
movq -0x48(%rbp), %rdx
movl %edx, %r14d
jmp 0x8575
orl $-0x1, -0x3c(%rbp)
andq $0x0, -0x90(%rbp)
andq $0x0, -0x98(%rbp)
andq $0x0, -0xa0(%rbp)
andl $0x0, -0x4c(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x100(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0xe0(%rbp)
andq $0x0, -0xd0(%rbp)
movq %r14, %rdi
leaq 0x1940(%rip), %rsi      # 0x9a98
callq 0x4270
movq %rax, %r15
xorl %edi, %edi
leaq 0x17d2(%rip), %rsi      # 0x993b
callq 0x4270
movq %rax, -0xa8(%rbp)
pushq $0x1
popq %rcx
movl %ecx, %r14d
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4270
testq %rax, %rax
je 0x83d8
movq %rax, %r12
movb (%rax), %r15b
movsbl %r15b, %edi
callq 0x42e0
addl $-0x41, %eax
cmpl $0x19, %eax
ja 0x8263
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
incq %r12
movq %r12, %rdi
leaq 0xef8(%rip), %rsi       # 0x90b9
leaq -0xf8(%rbp), %rdx
jmp 0x833d
incq %r12
movq %r12, %rdi
leaq 0xedf(%rip), %rsi       # 0x90b9
leaq -0x108(%rbp), %rdx
jmp 0x833d
incq %r12
movq %r12, %rdi
leaq 0xec6(%rip), %rsi       # 0x90b9
leaq -0xd8(%rbp), %rdx
jmp 0x833d
incq %r12
movq %r12, %rdi
leaq 0xead(%rip), %rsi       # 0x90b9
leaq -0x110(%rbp), %rdx
jmp 0x833d
incq %r12
movq %r12, %rdi
leaq 0xe94(%rip), %rsi       # 0x90b9
leaq -0xe8(%rbp), %rdx
jmp 0x833d
incq %r12
movq %r12, %rdi
leaq 0xe7b(%rip), %rsi       # 0x90b9
leaq -0x90(%rbp), %rdx
jmp 0x833d
incq %r12
movq %r12, %rdi
leaq 0xe62(%rip), %rsi       # 0x90b9
leaq -0xd0(%rbp), %rdx
jmp 0x833d
cmpb $0xa, %r15b
jmp 0x8347
incq %r12
movq %r12, %rdi
leaq 0xe40(%rip), %rsi       # 0x90b9
leaq -0xf0(%rbp), %rdx
jmp 0x833d
incq %r12
xorl %r15d, %r15d
movq %r12, %rdi
leaq 0x1805(%rip), %rsi      # 0x9a9a
leaq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x4160
movl $0x0, %ecx
cmpl $0x1, %eax
jne 0x8178
cmpl $0x0, -0x34(%rbp)
je 0x835b
movl -0x30(%rbp), %r12d
jmp 0x83a7
incq %r12
movq %r12, %rdi
leaq 0x17cc(%rip), %rsi      # 0x9a9a
leaq -0x4c(%rbp), %rdx
jmp 0x833d
incq %r12
movq %r12, %rdi
leaq 0xdd8(%rip), %rsi       # 0x90b9
leaq -0x100(%rbp), %rdx
jmp 0x833d
incq %r12
movq %r12, %rdi
leaq 0xdc2(%rip), %rsi       # 0x90b9
leaq -0xa0(%rbp), %rdx
jmp 0x833d
incq %r12
movq %r12, %rdi
leaq 0xdac(%rip), %rsi       # 0x90b9
leaq -0x98(%rbp), %rdx
jmp 0x833d
incq %r12
movq %r12, %rdi
leaq 0xd96(%rip), %rsi       # 0x90b9
leaq -0xe0(%rbp), %rdx
jmp 0x833d
incq %r12
movq %r12, %rdi
leaq 0x1761(%rip), %rsi      # 0x9a9a
leaq -0x3c(%rbp), %rdx
xorl %eax, %eax
callq 0x4160
cmpl $0x1, %eax
movl $0x0, %eax
cmovnel %eax, %r14d
xorl %r15d, %r15d
movl %r14d, %ecx
jmp 0x8178
movq -0x48(%rbp), %rax
leal 0x1(%rax), %r12d
cmpl $0x37, %eax
jl 0x838a
movl -0x3c(%rbp), %esi
xorl %r15d, %r15d
leaq 0x1728(%rip), %rdi      # 0x9a9d
xorl %eax, %eax
callq 0x4030
movl %r12d, %eax
movq %rax, -0x48(%rbp)
xorl %ecx, %ecx
jmp 0x8178
movq -0x68(%rbp), %rdx
testq %rdx, %rdx
je 0x839c
movl -0x30(%rbp), %eax
movslq %r12d, %rcx
movl %eax, (%rdx,%rcx,4)
movl %r12d, -0x30(%rbp)
movl %r12d, %eax
movq %rax, -0x48(%rbp)
movl $0x0, %r15d
movl %r14d, %ecx
cmpl $0x38, %r12d
jb 0x8178
movl -0x3c(%rbp), %edx
xorl %r15d, %r15d
leaq 0x16f8(%rip), %rdi      # 0x9abf
pushq $0x37
popq %rsi
xorl %eax, %eax
callq 0x4030
xorl %ecx, %ecx
jmp 0x8178
testl %r14d, %r14d
je 0x85fe
movq -0x88(%rbp), %r15
testq %r15, %r15
setne %r8b
movl -0x3c(%rbp), %ecx
movslq -0x30(%rbp), %rdx
imulq $0x70, %rdx, %rax
movq -0x58(%rbp), %rsi
addq %rsi, %rax
addq $0x8, %rax
movl %ecx, -0x8(%rax)
pushq $0x9
popq %rcx
movq %rax, %rdi
leaq -0x110(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, 0x48(%rax)
movsd -0x98(%rbp), %xmm0
movsd %xmm0, 0x50(%rax)
movsd -0x90(%rbp), %xmm0
movsd %xmm0, 0x58(%rax)
movl -0x4c(%rbp), %ecx
movl %ecx, 0x60(%rax)
movq -0xa8(%rbp), %rsi
testq %rsi, %rsi
setne %al
andb %r8b, %al
cmpb $0x1, %al
movq -0x60(%rbp), %r12
jne 0x8466
movq (%r15,%rdx,8), %rdi
callq 0x4150
cmpl $0x0, -0x34(%rbp)
jne 0x7ff5
movq -0x58(%rbp), %rdi
movl (%rdi), %eax
movslq -0x30(%rbp), %rcx
imulq $0x70, %rcx, %rsi
cmpl (%rdi,%rsi), %eax
jne 0x7ff5
addq %rdi, %rsi
pushq $0xe
popq %rcx
rep  movsq (%rsi), %es:(%rdi)
jmp 0x7ff5
movq -0x48(%rbp), %rcx
leal 0x1(%rcx), %r14d
cmpl $0x37, %ecx
jge 0x8546
movq -0x68(%rbp), %rsi
testq %rsi, %rsi
je 0x84b8
movl -0x2c(%rbp), %ecx
movslq %r14d, %rdx
movl %ecx, (%rsi,%rdx,4)
movl %r14d, -0x2c(%rbp)
movl %r14d, %ecx
movl -0x38(%rbp), %edx
cmpl $0x38, %ecx
jae 0x857d
cmpl $0x9, %eax
setne %sil
testq %r15, %r15
sete %dil
movl %ecx, %eax
imulq $0x70, %rax, %rcx
movq -0x58(%rbp), %r8
movl %edx, (%r8,%rcx)
movsd -0x78(%rbp), %xmm0
movsd %xmm0, 0x18(%r8,%rcx)
movsd -0xc0(%rbp), %xmm0
movsd %xmm0, 0x8(%r8,%rcx)
movsd -0x70(%rbp), %xmm0
movsd %xmm0, 0x50(%r8,%rcx)
movsd -0xb8(%rbp), %xmm0
movsd %xmm0, 0x58(%r8,%rcx)
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, 0x60(%r8,%rcx)
movl -0x7c(%rbp), %edx
movl %edx, 0x68(%r8,%rcx)
jmp 0x85d9
movq -0x48(%rbp), %rcx
leal 0x1(%rcx), %r14d
cmpl $0x37, %ecx
jl 0x855c
movl -0x38(%rbp), %esi
leaq 0x154d(%rip), %rdi      # 0x9a9d
xorl %eax, %eax
callq 0x4030
jmp 0x85ee
movq -0x68(%rbp), %rsi
testq %rsi, %rsi
je 0x856e
movl -0x2c(%rbp), %ecx
movslq %r14d, %rdx
movl %ecx, (%rsi,%rdx,4)
movl %r14d, -0x2c(%rbp)
movl %r14d, %ecx
movl -0x38(%rbp), %edx
cmpl $0x38, %ecx
jb 0x8590
leaq 0x153b(%rip), %rdi      # 0x9abf
pushq $0x37
popq %rsi
xorl %eax, %eax
callq 0x4030
jmp 0x85ee
cmpl $0x5, %eax
setne %sil
testq %r15, %r15
sete %dil
movl %ecx, %eax
imulq $0x70, %rax, %rcx
movq -0x58(%rbp), %r8
movl %edx, (%r8,%rcx)
movsd -0x78(%rbp), %xmm0
movsd %xmm0, 0x18(%r8,%rcx)
movsd -0x70(%rbp), %xmm0
movsd %xmm0, 0x50(%r8,%rcx)
andq $0x0, 0x8(%r8,%rcx)
xorps %xmm0, %xmm0
movups %xmm0, 0x58(%r8,%rcx)
andl $0x0, 0x68(%r8,%rcx)
orb %sil, %dil
jne 0x85ee
movq (%r15,%rax,8), %rdi
leaq -0x210(%rbp), %rsi
callq 0x4150
movl %r14d, %eax
movq %rax, -0x48(%rbp)
movq -0x60(%rbp), %r12
jmp 0x7ff5
movq 0x49db(%rip), %rax      # 0xcfe0
movq (%rax), %rdi
leaq 0x14db(%rip), %rsi      # 0x9aea
leaq -0x310(%rbp), %rdx
xorl %eax, %eax
callq 0x4240
movq -0x88(%rbp), %r15
movq -0x60(%rbp), %r12
jmp 0x8466
movq %r12, %rdi
callq 0x4170
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x3e8, %rsp            # imm = 0x3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq