// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshProperty.cpp
// Function: gaps::R3MeshProperty::ReadARFF(char const*)
int R3MeshProperty::
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 to read lines
  const int max_line_size = 64 * 1024;
  char *buffer = new char [max_line_size];
  if (!buffer) {
    RNFail("Unable to allocate buffer to read %s\n", filename);
    return 0;
  }
  
  // Read attribute names and set property name for first 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")) {
          if (property_count == 0) SetName(name);
          property_count++;
        }
      }
    }
  }

  // Check number of properties
  if (property_count == 0) {
    RNFail("No properties in %s\n", filename);
    delete [] buffer;
    return 0;
  }

  // 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 ");
    if (!bufferp) { 
      RNFail("Unable to read property value for vertex %d\n", vertex_index);
      delete [] buffer;
      return 0;
    }
    if (sscanf(bufferp, "%lf", &property_value) != (unsigned int) 1) {
      RNFail("Unable to read property value for vertex %d\n", vertex_index);
      delete [] buffer;
      return 0;
    }
    if (vertex_index >= mesh->NVertices()) {
      RNFail("Too many data lines in %s\n", filename);
      delete [] buffer;
      return 0;
    }
    SetVertexValue(vertex_index, property_value);
    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;
  // }

  // Delete buffer
  delete [] buffer;
  
  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0x888, %rsp            # imm = 0x888
movq %rdi, 0x878(%rsp)
movq %rsi, 0x870(%rsp)
movq 0x878(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x870(%rsp), %rdi
leaq 0x12d010(%rip), %rsi    # 0x19d8e3
callq 0x22bf0
movq %rax, 0x868(%rsp)
cmpq $0x0, 0x868(%rsp)
jne 0x70911
movq 0x870(%rsp), %rsi
leaq 0x12762e(%rip), %rdi    # 0x197f28
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x884(%rsp)
jmp 0x70c79
movl $0x10000, 0x864(%rsp) # imm = 0x10000
movl $0x10000, %edi          # imm = 0x10000
callq 0x22040
movq %rax, 0x858(%rsp)
cmpq $0x0, 0x858(%rsp)
jne 0x7095f
movq 0x870(%rsp), %rsi
leaq 0x127b9b(%rip), %rdi    # 0x1984e3
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x884(%rsp)
jmp 0x70c79
movl $0x0, 0x854(%rsp)
movq 0x858(%rsp), %rdi
movq 0x868(%rsp), %rdx
movl $0x10000, %esi          # imm = 0x10000
callq 0x230d0
cmpq $0x0, %rax
je 0x70a3d
movq 0x858(%rsp), %rdi
leaq 0x1275da(%rip), %rsi    # 0x197f77
callq 0x22190
cmpq $0x0, %rax
je 0x709ad
jmp 0x70a3d
movq 0x858(%rsp), %rdi
leaq 0x1275c1(%rip), %rsi    # 0x197f7d
callq 0x22190
cmpq $0x0, %rax
je 0x70a36
movq 0x858(%rsp), %rdi
leaq 0x450(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x127f6b(%rip), %rsi    # 0x19894e
movb $0x0, %al
callq 0x22990
cmpl $0x2, %eax
jne 0x70a34
leaq 0x450(%rsp), %rdi
leaq 0x12757f(%rip), %rsi    # 0x197f7d
callq 0x22ea0
cmpl $0x0, %eax
jne 0x70a32
cmpl $0x0, 0x854(%rsp)
jne 0x70a21
movq 0x30(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x6f500
movl 0x854(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x854(%rsp)
jmp 0x70a34
jmp 0x70a36
jmp 0x70a38
jmp 0x7096a
cmpl $0x0, 0x854(%rsp)
jne 0x70a8a
movq 0x870(%rsp), %rsi
leaq 0x127ab3(%rip), %rdi    # 0x198509
movb $0x0, %al
callq 0xfbc30
movq 0x858(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x70a7a
movq 0x28(%rsp), %rdi
callq 0x22d80
movl $0x0, 0x884(%rsp)
jmp 0x70c79
movl $0x0, 0x4c(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x858(%rsp), %rdi
movq 0x868(%rsp), %rdx
movl $0x10000, %esi          # imm = 0x10000
callq 0x230d0
cmpq $0x0, %rax
je 0x70c46
movq 0x858(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x70ad1
jmp 0x70a9b
movq 0x858(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x40, %eax
jne 0x70ae3
jmp 0x70a9b
movq 0x858(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x70af5
jmp 0x70a9b
movq 0x858(%rsp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
jne 0x70b07
jmp 0x70a9b
movq 0x858(%rsp), %rdi
leaq 0x127472(%rip), %rsi    # 0x197f88
callq 0x23080
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x70b67
movl 0x4c(%rsp), %esi
leaq 0x1279eb(%rip), %rdi    # 0x19851e
movb $0x0, %al
callq 0xfbc30
movq 0x858(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x70b57
movq 0x20(%rsp), %rdi
callq 0x22d80
movl $0x0, 0x884(%rsp)
jmp 0x70c79
movq 0x38(%rsp), %rdi
leaq 0x127e42(%rip), %rsi    # 0x1989b5
leaq 0x40(%rsp), %rdx
movb $0x0, %al
callq 0x22990
cmpl $0x1, %eax
je 0x70bc3
movl 0x4c(%rsp), %esi
leaq 0x12798f(%rip), %rdi    # 0x19851e
movb $0x0, %al
callq 0xfbc30
movq 0x858(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x70bb3
movq 0x18(%rsp), %rdi
callq 0x22d80
movl $0x0, 0x884(%rsp)
jmp 0x70c79
movq 0x30(%rsp), %rax
movl 0x4c(%rsp), %ecx
movl %ecx, 0x14(%rsp)
movq (%rax), %rdi
callq 0x2abd0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jl 0x70c22
movq 0x870(%rsp), %rsi
leaq 0x1273cb(%rip), %rdi    # 0x197fbc
movb $0x0, %al
callq 0xfbc30
movq 0x858(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x70c15
movq 0x8(%rsp), %rdi
callq 0x22d80
movl $0x0, 0x884(%rsp)
jmp 0x70c79
movq 0x30(%rsp), %rdi
movl 0x4c(%rsp), %esi
movsd 0x40(%rsp), %xmm0
callq 0x6f210
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x70a9b
movq 0x858(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x70c61
movq (%rsp), %rdi
callq 0x22d80
movq 0x868(%rsp), %rdi
callq 0x229c0
movl $0x1, 0x884(%rsp)
movl 0x884(%rsp), %eax
addq $0x888, %rsp            # imm = 0x888
retq
nopl (%rax,%rax)