// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelDatabase.cpp
// Function: gaps::R3SurfelDatabase::OpenFile(char const*, char const*)
int R3SurfelDatabase::
OpenFile(const char *filename, const char *rwaccess)
{
  // Remember file name
  if (this->filename) free(this->filename);
  this->filename = RNStrdup(filename);

  // Parse rwaccess
  if (this->rwaccess) free(this->rwaccess);
  if (!rwaccess) this->rwaccess = RNStrdup("w+b");
  else if (strstr(rwaccess, "w")) this->rwaccess = RNStrdup("w+b");
  else if (strstr(rwaccess, "+")) this->rwaccess = RNStrdup("r+b");
  else this->rwaccess = RNStrdup("rb"); 

  // Open file
  fp = fopen(filename, this->rwaccess);
  if (!fp) {
    RNFail("Unable to open database file %s with rwaccess %s\n", filename, rwaccess);
    return 0;
  }

  // Check if file is new
  if (!strcmp(this->rwaccess, "w+b")) {
    // File is new -- write header
    if (!WriteFileHeader(fp, 0)) {
      fclose(fp);
      fp = NULL;
      return 0;
    }
  }
  else {
    // Read header
    unsigned int nblocks = 0;
    if (!ReadFileHeader(fp, nblocks)) {
      fclose(fp);
      fp = NULL;
    }
    
    // Read blocks
    if (!ReadBlockHeader(fp, nblocks, swap_endian)) {
      fclose(fp);
      fp = NULL;
    }
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x10(%rax)
je 0x75842
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x136a0
movq 0x20(%rsp), %rdi
callq 0x1461b0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x10(%rax)
cmpq $0x0, 0x18(%rax)
je 0x7586d
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x136a0
cmpq $0x0, 0x18(%rsp)
jne 0x7588f
leaq 0x1303f0(%rip), %rdi    # 0x1a5c6c
callq 0x1461b0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x18(%rax)
jmp 0x7590d
movq 0x18(%rsp), %rdi
leaq 0x131663(%rip), %rsi    # 0x1a6efe
callq 0x13120
cmpq $0x0, %rax
je 0x758c0
leaq 0x1303bf(%rip), %rdi    # 0x1a5c6c
callq 0x1461b0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x18(%rax)
jmp 0x7590b
movq 0x18(%rsp), %rdi
leaq 0x12eb2d(%rip), %rsi    # 0x1a43f9
callq 0x13120
cmpq $0x0, %rax
je 0x758f1
leaq 0x130392(%rip), %rdi    # 0x1a5c70
callq 0x1461b0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x18(%rax)
jmp 0x75909
leaq 0x12e8fd(%rip), %rdi    # 0x1a41f5
callq 0x1461b0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x18(%rax)
jmp 0x7590b
jmp 0x7590d
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %rdi
movq 0x18(%rax), %rsi
callq 0x13690
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x8(%rax)
cmpq $0x0, 0x8(%rax)
jne 0x75958
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
leaq 0x130330(%rip), %rdi    # 0x1a5c74
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x34(%rsp)
jmp 0x75a37
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
leaq 0x130304(%rip), %rsi    # 0x1a5c6c
callq 0x137e0
cmpl $0x0, %eax
jne 0x759b5
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
movq (%rdi), %rax
xorl %edx, %edx
callq *0xc0(%rax)
cmpl $0x0, %eax
jne 0x759b3
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x13560
movq 0x8(%rsp), %rax
movq $0x0, 0x8(%rax)
movl $0x0, 0x34(%rsp)
jmp 0x75a37
jmp 0x75a2f
movq 0x8(%rsp), %rdi
movl $0x0, 0x14(%rsp)
movq 0x8(%rdi), %rsi
movq (%rdi), %rax
leaq 0x14(%rsp), %rdx
callq *0xb0(%rax)
cmpl $0x0, %eax
jne 0x759f4
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x13560
movq 0x8(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x8(%rsp), %rdi
movq 0x8(%rdi), %rsi
movl 0x14(%rsp), %edx
movl 0x28(%rdi), %ecx
movq (%rdi), %rax
callq *0xb8(%rax)
cmpl $0x0, %eax
jne 0x75a2d
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x13560
movq 0x8(%rsp), %rax
movq $0x0, 0x8(%rax)
jmp 0x75a2f
movl $0x1, 0x34(%rsp)
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq