// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2PixelDatabase.cpp
// Function: gaps::R2PixelDatabase::OpenFile(char const*, char const*)
int R2PixelDatabase::
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")) {
    // Just checking ...
    assert(entries_count == 0);

    // File is new -- write header
    if (!WriteHeader(fp, 0)) return 0;

    // Update entries info
    entries_seek = RNFileTell(fp);
  }
  else {
    // Read header
    if (!ReadHeader(fp)) return 0;

    // Read entries
    if (!ReadEntries(fp, swap_endian)) return 0;
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq 0x18(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, 0x18(%rax)
je 0x1259e0
movq (%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x136a0
movq 0x10(%rsp), %rdi
callq 0x1461b0
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x18(%rax)
cmpq $0x0, 0x20(%rax)
je 0x125a09
movq (%rsp), %rax
movq 0x20(%rax), %rdi
callq 0x136a0
cmpq $0x0, 0x8(%rsp)
jne 0x125a2a
leaq 0x80254(%rip), %rdi     # 0x1a5c6c
callq 0x1461b0
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x20(%rax)
jmp 0x125aa5
movq 0x8(%rsp), %rdi
leaq 0x814c8(%rip), %rsi     # 0x1a6efe
callq 0x13120
cmpq $0x0, %rax
je 0x125a5a
leaq 0x80224(%rip), %rdi     # 0x1a5c6c
callq 0x1461b0
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x20(%rax)
jmp 0x125aa3
movq 0x8(%rsp), %rdi
leaq 0x7e993(%rip), %rsi     # 0x1a43f9
callq 0x13120
cmpq $0x0, %rax
je 0x125a8a
leaq 0x801f8(%rip), %rdi     # 0x1a5c70
callq 0x1461b0
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x20(%rax)
jmp 0x125aa1
leaq 0x7e764(%rip), %rdi     # 0x1a41f5
callq 0x1461b0
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x20(%rax)
jmp 0x125aa3
jmp 0x125aa5
movq (%rsp), %rax
movq 0x10(%rsp), %rdi
movq 0x20(%rax), %rsi
callq 0x13690
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x8(%rax)
cmpq $0x0, 0x8(%rax)
jne 0x125aee
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
leaq 0x8019a(%rip), %rdi     # 0x1a5c74
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x24(%rsp)
jmp 0x125b87
movq (%rsp), %rax
movq 0x20(%rax), %rdi
leaq 0x8016f(%rip), %rsi     # 0x1a5c6c
callq 0x137e0
cmpl $0x0, %eax
jne 0x125b40
movq (%rsp), %rdi
movq 0x8(%rdi), %rsi
movq (%rdi), %rax
xorl %edx, %edx
callq *0x70(%rax)
cmpl $0x0, %eax
jne 0x125b26
movl $0x0, 0x24(%rsp)
jmp 0x125b87
movq (%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x1453b0
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x38(%rax)
jmp 0x125b7f
movq (%rsp), %rdi
movq 0x8(%rdi), %rsi
movq (%rdi), %rax
callq *0x68(%rax)
cmpl $0x0, %eax
jne 0x125b5d
movl $0x0, 0x24(%rsp)
jmp 0x125b87
movq (%rsp), %rdi
movq 0x8(%rdi), %rsi
movl 0x30(%rdi), %edx
movq (%rdi), %rax
callq *0x78(%rax)
cmpl $0x0, %eax
jne 0x125b7d
movl $0x0, 0x24(%rsp)
jmp 0x125b87
jmp 0x125b7f
movl $0x1, 0x24(%rsp)
movl 0x24(%rsp), %eax
addq $0x28, %rsp
retq