// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshPropertySet.cpp
// Function: gaps::R3MeshPropertySet::ReadARFF(char const*)
int R3MeshPropertySet::
ReadARFF(const char *filename)
{
  // Open file
  FILE *fp = fopen(filename, "r");
  if (!fp) {
    RNFail("Unable to open ARFF file: %s\n", filename);
    return 0;
  }

  // Allocate large buffer for reading ARFF lines
  const int max_line_size = 64 * 1024;
  char *buffer = new char [ max_line_size ];
  if (!buffer) {
    RNFail("Unable to allocate buffer for reading ARFF file\n");
    return 0;
  }
  
  // Read attribute names and allocate property for each one
  int property_count = 0;
  char token[1024], name[1024];
  while (fgets(buffer, max_line_size, fp)) {
    if (strstr(buffer, "@data")) break;
    else if (strstr(buffer, "@attribute")) {
      if (sscanf(buffer, "%s%s", token, name) == (unsigned int) 2) {
        if (!strcmp(token, "@attribute")) {
          R3MeshProperty *property = new R3MeshProperty(mesh, name);
          Insert(property);
          property_count++;
        }
      }
    }
  }

  // Check number of properties
  if (property_count == 0) { delete [] buffer; return 1; }

  // Read data and assign property values
  int vertex_index = 0;
  RNScalar property_value = 0;
  while (fgets(buffer, max_line_size, fp)) {
    // Check for header line
    if (buffer[0] == '#') continue;
    if (buffer[0] == '@') continue;
    if (buffer[0] == '\0') continue;
    if (buffer[0] == '\n') continue;
    char *bufferp = strtok(buffer, "\n\t ");
    for (int i = 0; i < property_count; i++) {
      R3MeshProperty *property = Property(NProperties()-property_count+i);
      if (!bufferp) { 
        RNFail("Unable to read property value %d for vertex %d\n", i, vertex_index);
        delete [] buffer;
        return 0;
      }
      if (sscanf(bufferp, "%lf", &property_value) != (unsigned int) 1) {
        RNFail("Unable to read property value %d for vertex %d\n", i, vertex_index);
        delete [] buffer;
        return 0;
      }
      if (vertex_index >= mesh->NVertices()) {
        RNFail("Too many data lines in %s\n", filename);
        delete [] buffer;
        return 0;
      }
      property->SetVertexValue(vertex_index, property_value);
      bufferp = strtok(NULL, "\n\t ");
    }
    vertex_index++;
  }

  // Check if read value for every vertex
  // if (vertex_index != mesh->NVertices()) {
  //   RNFail("Mismatching number of data lines (%d) and vertices (%d) in %s\n", vertex_index, mesh->NVertices(), filename);
  //   delete [] buffer;
  //   return 0;
  // }

  // Close file
  fclose(fp);

  // Delete buffer
  delete [] buffer;
  
  // Return success
  return 1;
}

// Assembly:
subq $0x8b8, %rsp            # imm = 0x8B8
movq %rdi, 0x8a8(%rsp)
movq %rsi, 0x8a0(%rsp)
movq 0x8a8(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x8a0(%rsp), %rdi
leaq 0x134080(%rip), %rsi    # 0x19d8e3
callq 0x22bf0
movq %rax, 0x898(%rsp)
cmpq $0x0, 0x898(%rsp)
jne 0x698a1
movq 0x8a0(%rsp), %rsi
leaq 0x12e69e(%rip), %rdi    # 0x197f28
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x8b4(%rsp)
jmp 0x69cb8
movl $0x10000, 0x894(%rsp) # imm = 0x10000
movl $0x10000, %edi          # imm = 0x10000
callq 0x22040
movq %rax, 0x888(%rsp)
cmpq $0x0, 0x888(%rsp)
jne 0x698e7
leaq 0x12e676(%rip), %rdi    # 0x197f46
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x8b4(%rsp)
jmp 0x69cb8
movl $0x0, 0x884(%rsp)
movq 0x888(%rsp), %rdi
movq 0x898(%rsp), %rdx
movl $0x10000, %esi          # imm = 0x10000
callq 0x230d0
cmpq $0x0, %rax
je 0x69a24
movq 0x888(%rsp), %rdi
leaq 0x12e652(%rip), %rsi    # 0x197f77
callq 0x22190
cmpq $0x0, %rax
je 0x69935
jmp 0x69a24
movq 0x888(%rsp), %rdi
leaq 0x12e639(%rip), %rsi    # 0x197f7d
callq 0x22190
cmpq $0x0, %rax
je 0x69a1d
movq 0x888(%rsp), %rdi
leaq 0x480(%rsp), %rdx
leaq 0x80(%rsp), %rcx
leaq 0x12efdc(%rip), %rsi    # 0x19894e
movb $0x0, %al
callq 0x22990
cmpl $0x2, %eax
jne 0x69a1b
leaq 0x480(%rsp), %rdi
leaq 0x12e5ec(%rip), %rsi    # 0x197f7d
callq 0x22ea0
cmpl $0x0, %eax
jne 0x69a19
movl $0x448, %edi            # imm = 0x448
callq 0x229d0
movq %rax, %rdi
movq 0x40(%rsp), %rax
movq %rdi, %rcx
movq %rcx, 0x38(%rsp)
movq (%rax), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq 0x80(%rsp), %rdx
callq 0x6c2b0
jmp 0x699cb
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rsi
callq 0x69220
movl 0x884(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x884(%rsp)
jmp 0x69a19
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
movl $0x448, %esi            # imm = 0x448
callq 0x229f0
jmp 0x69cc7
jmp 0x69a1b
jmp 0x69a1d
jmp 0x69a1f
jmp 0x698f2
cmpl $0x0, 0x884(%rsp)
jne 0x69a5b
movq 0x888(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x69a4b
movq 0x30(%rsp), %rdi
callq 0x22d80
movl $0x1, 0x8b4(%rsp)
jmp 0x69cb8
movl $0x0, 0x68(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x888(%rsp), %rdi
movq 0x898(%rsp), %rdx
movl $0x10000, %esi          # imm = 0x10000
callq 0x230d0
cmpq $0x0, %rax
je 0x69c83
movq 0x888(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x69aa2
jmp 0x69a6c
movq 0x888(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x40, %eax
jne 0x69ab4
jmp 0x69a6c
movq 0x888(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x69ac6
jmp 0x69a6c
movq 0x888(%rsp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0x69ad8
jmp 0x69a6c
movq 0x888(%rsp), %rdi
leaq 0x12e4a1(%rip), %rsi    # 0x197f88
callq 0x23080
movq %rax, 0x58(%rsp)
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x884(%rsp), %eax
jge 0x69c73
movq 0x40(%rsp), %rdi
callq 0x2ae90
movq 0x40(%rsp), %rdi
movl %eax, %esi
subl 0x884(%rsp), %esi
addl 0x54(%rsp), %esi
callq 0x2b330
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0x69b7b
movl 0x54(%rsp), %esi
movl 0x68(%rsp), %edx
leaq 0x12e445(%rip), %rdi    # 0x197f8c
movb $0x0, %al
callq 0xfbc30
movq 0x888(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x69b6b
movq 0x28(%rsp), %rdi
callq 0x22d80
movl $0x0, 0x8b4(%rsp)
jmp 0x69cb8
movq 0x58(%rsp), %rdi
leaq 0x12ee2e(%rip), %rsi    # 0x1989b5
leaq 0x60(%rsp), %rdx
movb $0x0, %al
callq 0x22990
cmpl $0x1, %eax
je 0x69bdb
movl 0x54(%rsp), %esi
movl 0x68(%rsp), %edx
leaq 0x12e3e5(%rip), %rdi    # 0x197f8c
movb $0x0, %al
callq 0xfbc30
movq 0x888(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x69bcb
movq 0x20(%rsp), %rdi
callq 0x22d80
movl $0x0, 0x8b4(%rsp)
jmp 0x69cb8
movq 0x40(%rsp), %rax
movl 0x68(%rsp), %ecx
movl %ecx, 0x1c(%rsp)
movq (%rax), %rdi
callq 0x2abd0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jl 0x69c3a
movq 0x8a0(%rsp), %rsi
leaq 0x12e3b3(%rip), %rdi    # 0x197fbc
movb $0x0, %al
callq 0xfbc30
movq 0x888(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x69c2d
movq 0x10(%rsp), %rdi
callq 0x22d80
movl $0x0, 0x8b4(%rsp)
jmp 0x69cb8
movq 0x48(%rsp), %rdi
movl 0x68(%rsp), %esi
movsd 0x60(%rsp), %xmm0
callq 0x6f210
xorl %eax, %eax
movl %eax, %edi
leaq 0x12e32f(%rip), %rsi    # 0x197f88
callq 0x23080
movq %rax, 0x58(%rsp)
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x69af9
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x69a6c
movq 0x898(%rsp), %rdi
callq 0x229c0
movq 0x888(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x69cad
movq 0x8(%rsp), %rdi
callq 0x22d80
movl $0x1, 0x8b4(%rsp)
movl 0x8b4(%rsp), %eax
addq $0x8b8, %rsp            # imm = 0x8B8
retq
movq 0x70(%rsp), %rdi
callq 0x23270
nopw %cs:(%rax,%rax)