// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelDatabase.cpp
// Function: gaps::R3SurfelDatabase::ReadFile(char const*)
int R3SurfelDatabase::
ReadFile(const char *filename)
{
  // Parse input filename extension
  const char *extension;
  if (!(extension = strrchr(filename, '.'))) {
    printf("Filename %s has no extension (e.g., .ply)\n", filename);
    return 0;
  }

  // Read file of appropriate type
  if (!strncmp(extension, ".ssb", 5)) {
    // Open file
    FILE *f = fopen(filename, "rb");
    if (!f) {
      RNFail("Unable to open file %s\n", filename);
      return 0;
    }

    // Read stream
    if (!ReadStream(f)) {
      fclose(f);
      return 0;
    }

    // Close file
    fclose(f);
  }
  else if (!strncmp(extension, ".list", 5)) {
    // Open file
    FILE *f = fopen(filename, "r");
    if (!f) {
      RNFail("Unable to open file %s\n", filename);
      return 0;
    }

    // Read file names
    char buffer[4096];
    while (fscanf(f, "%s", buffer) == (unsigned int) 1) {
      // Create block
      R3SurfelBlock *block = new R3SurfelBlock();
      if (!block) {
        RNFail("Unable to create block for %s\n", buffer);
        fclose(f);
        return 0;
      }
    
      // Read block
      if (!block->ReadFile(buffer)) { 
        delete block; 
        fclose(f);
        return 0; 
      }

      // Update properties
      block->UpdateProperties();

      // Insert block
      InsertBlock(block);

      // Release block
      ReleaseBlock(block);
    }

    // Close file
    fclose(f);
  }
  else { 
    // Create block
    R3SurfelBlock *block = new R3SurfelBlock();
    if (!block) {
      RNFail("Unable to create block\n");
      return 0;
    }
    
    // Read file
    if (!block->ReadFile(filename)) { 
      delete block; 
      return 0; 
    }
    
    // Update properties
    block->UpdateProperties();

    // Insert block
    InsertBlock(block);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x1088, %rsp           # imm = 0x1088
movq %rdi, 0x1078(%rsp)
movq %rsi, 0x1070(%rsp)
movq 0x1078(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x1070(%rsp), %rdi
movl $0x2e, %esi
callq 0x13630
movq %rax, 0x1068(%rsp)
cmpq $0x0, %rax
jne 0x72c4a
movq 0x1070(%rsp), %rsi
leaq 0x13233d(%rip), %rdi    # 0x1a4f70
movb $0x0, %al
callq 0x13070
movl $0x0, 0x1084(%rsp)
jmp 0x72f91
movq 0x1068(%rsp), %rdi
leaq 0x132e53(%rip), %rsi    # 0x1a5aac
movl $0x5, %edx
callq 0x13260
cmpl $0x0, %eax
jne 0x72d00
movq 0x1070(%rsp), %rdi
leaq 0x13157a(%rip), %rsi    # 0x1a41f5
callq 0x13690
movq %rax, 0x1060(%rsp)
cmpq $0x0, 0x1060(%rsp)
jne 0x72cb9
movq 0x1070(%rsp), %rsi
leaq 0x131804(%rip), %rdi    # 0x1a44a6
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x1084(%rsp)
jmp 0x72f91
movq 0x28(%rsp), %rdi
movq 0x1060(%rsp), %rsi
movq (%rdi), %rax
callq *0x60(%rax)
cmpl $0x0, %eax
jne 0x72cee
movq 0x1060(%rsp), %rdi
callq 0x13560
movl $0x0, 0x1084(%rsp)
jmp 0x72f91
movq 0x1060(%rsp), %rdi
callq 0x13560
jmp 0x72f86
movq 0x1068(%rsp), %rdi
leaq 0x132da2(%rip), %rsi    # 0x1a5ab1
movl $0x5, %edx
callq 0x13260
cmpl $0x0, %eax
jne 0x72eaf
movq 0x1070(%rsp), %rdi
leaq 0x138622(%rip), %rsi    # 0x1ab353
callq 0x13690
movq %rax, 0x1058(%rsp)
cmpq $0x0, 0x1058(%rsp)
jne 0x72d6f
movq 0x1070(%rsp), %rsi
leaq 0x13174e(%rip), %rdi    # 0x1a44a6
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x1084(%rsp)
jmp 0x72f91
jmp 0x72d71
movq 0x1058(%rsp), %rdi
leaq 0x50(%rsp), %rdx
leaq 0x13483e(%rip), %rsi    # 0x1a75c3
movb $0x0, %al
callq 0x13a10
cmpl $0x1, %eax
jne 0x72e9d
movl $0xc8, %edi
callq 0x13570
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
callq 0x47c30
jmp 0x72db1
movq 0x20(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x48(%rsp)
jne 0x72e15
leaq 0x50(%rsp), %rsi
leaq 0x132ce8(%rip), %rdi    # 0x1a5ab7
movb $0x0, %al
callq 0x1464b0
movq 0x1058(%rsp), %rdi
callq 0x13560
movl $0x0, 0x1084(%rsp)
jmp 0x72f91
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
movl $0xc8, %esi
callq 0x13580
jmp 0x72fa0
movq 0x48(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x4cd40
cmpl $0x0, %eax
jne 0x72e6f
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x72e52
movq 0x18(%rsp), %rdi
callq 0x4a1c0
movq 0x18(%rsp), %rdi
movl $0xc8, %esi
callq 0x13580
movq 0x1058(%rsp), %rdi
callq 0x13560
movl $0x0, 0x1084(%rsp)
jmp 0x72f91
movq 0x48(%rsp), %rdi
callq 0x4c380
movq 0x28(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq (%rdi), %rax
callq *0x10(%rax)
movq 0x28(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x311c0
jmp 0x72d71
movq 0x1058(%rsp), %rdi
callq 0x13560
jmp 0x72f84
movl $0xc8, %edi
callq 0x13570
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x10(%rsp)
callq 0x47c30
jmp 0x72ecb
movq 0x10(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x72f1d
leaq 0x132bf2(%rip), %rdi    # 0x1a5ad6
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x1084(%rsp)
jmp 0x72f91
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
movl $0xc8, %esi
callq 0x13580
jmp 0x72fa0
movq 0x30(%rsp), %rdi
movq 0x1070(%rsp), %rsi
callq 0x4cd40
cmpl $0x0, %eax
jne 0x72f6a
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x72f5d
movq 0x8(%rsp), %rdi
callq 0x4a1c0
movq 0x8(%rsp), %rdi
movl $0xc8, %esi
callq 0x13580
movl $0x0, 0x1084(%rsp)
jmp 0x72f91
movq 0x30(%rsp), %rdi
callq 0x4c380
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x72f86
movl $0x1, 0x1084(%rsp)
movl 0x1084(%rsp), %eax
addq $0x1088, %rsp           # imm = 0x1088
retq
movq 0x40(%rsp), %rdi
callq 0x13970
nopw (%rax,%rax)