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

  // Read run names from file
  int line_count = 0;
  const int buffer_size = 16*1024;
  char buffer[buffer_size];
  while (fgets(buffer, buffer_size, fp)) {
    line_count++;
    if (buffer[0] == '\0') continue;
    if (buffer[0] == '#') continue;

    // Parse command keyword
    char cmd[4096];
    if (sscanf(buffer, "%s", cmd) != (unsigned int) 1) {
      RNFail("Unable to read command from line %d of %s\n", line_count, filename);
      return 0;
    }

    // Parse rest of command
    if (!strcmp(cmd, "raw_data")) {
      // Parse directory name
      char directory_name[1024];
      if (sscanf(buffer, "%s%s", cmd, directory_name) != (unsigned int) 2) {
        RNFail("Unable to read scan_directory from line %d of %s\n", line_count, filename);
        return 0;
      }

      // Set directory name
      SetRawDataDirectoryName(directory_name);
    }
    else if (!strcmp(cmd, "cache_data")) {
      // Parse directory name
      char directory_name[1024];
      if (sscanf(buffer, "%s%s", cmd, directory_name) != (unsigned int) 2) {
        RNFail("Unable to read scan_directory from line %d of %s\n", line_count, filename);
        return 0;
      }

      // Set directory name
      SetCacheDataDirectoryName(directory_name);
    }
    else if (!strcmp(cmd, "run")) {
      // Parse run name
      char run_name[4096];
      if (sscanf(buffer, "%s%s", cmd, run_name) != (unsigned int) 2) {
        RNFail("Unable to read run name from line %d of %s\n", line_count, filename);
        return 0;
      }

      // Create run
      GSVRun *run = new GSVRun(run_name);
      InsertRun(run);
      
      // Read camera info
      if (!run->ReadCameraInfoFile()) return 0;
      
      // Read image info
      RNArray<GSVPanorama *> panoramas;
      if (!run->ReadImageSegmentFile(panoramas)) return 0;
      if (!run->ReadImagePoseFile(panoramas)) return 0;

      // Read laser stuff
      const int max_laser_index = 2;
      for (int laser_index = 0; laser_index <= max_laser_index; laser_index++) {
        RNArray<GSVScanline *> scanlines;
        RNArray<GSVScan *> scanline_scans;
        if (!run->ReadLaserObjFile(laser_index, scanlines, scanline_scans, read_points)) return 0;
        if (!run->ReadLaserPoseFile(laser_index, scanlines, scanline_scans)) return 0;
      }
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0x68c8, %rsp           # imm = 0x68C8
movq %rdi, 0x68b8(%rsp)
movq %rsi, 0x68b0(%rsp)
movl %edx, 0x68ac(%rsp)
movq 0x68b8(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x68b0(%rsp), %rdi
leaq 0x246829(%rip), %rsi    # 0x2833f3
callq 0x2a520
movq %rax, 0x68a0(%rsp)
cmpq $0x0, 0x68a0(%rsp)
jne 0x3cc08
movq 0x68b0(%rsp), %rsi
leaq 0x22a015(%rip), %rdi    # 0x266c06
movb $0x0, %al
callq 0x1cbff0
movl $0x0, 0x68c4(%rsp)
jmp 0x3d0e4
movl $0x0, 0x689c(%rsp)
movl $0x4000, 0x6898(%rsp) # imm = 0x4000
leaq 0x2890(%rsp), %rdi
movq 0x68a0(%rsp), %rdx
movl $0x4000, %esi           # imm = 0x4000
callq 0x29bd0
cmpq $0x0, %rax
je 0x3d0cc
movl 0x689c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x689c(%rsp)
movsbl 0x2890(%rsp), %eax
cmpl $0x0, %eax
jne 0x3cc62
jmp 0x3cc1e
movsbl 0x2890(%rsp), %eax
cmpl $0x23, %eax
jne 0x3cc71
jmp 0x3cc1e
leaq 0x2890(%rsp), %rdi
leaq 0x1890(%rsp), %rdx
leaq 0x24359f(%rip), %rsi    # 0x280227
movb $0x0, %al
callq 0x29150
cmpl $0x1, %eax
je 0x3ccc1
movl 0x689c(%rsp), %esi
movq 0x68b0(%rsp), %rdx
leaq 0x229fab(%rip), %rdi    # 0x266c55
movb $0x0, %al
callq 0x1cbff0
movl $0x0, 0x68c4(%rsp)
jmp 0x3d0e4
leaq 0x1890(%rsp), %rdi
leaq 0x229eed(%rip), %rsi    # 0x266bbd
callq 0x29f00
cmpl $0x0, %eax
jne 0x3cd49
leaq 0x2890(%rsp), %rdi
leaq 0x1890(%rsp), %rdx
leaq 0x1490(%rsp), %rcx
leaq 0x22e0e3(%rip), %rsi    # 0x26addc
movb $0x0, %al
callq 0x29150
cmpl $0x2, %eax
je 0x3cd32
movl 0x689c(%rsp), %esi
movq 0x68b0(%rsp), %rdx
leaq 0x229f65(%rip), %rdi    # 0x266c80
movb $0x0, %al
callq 0x1cbff0
movl $0x0, 0x68c4(%rsp)
jmp 0x3d0e4
movq 0x30(%rsp), %rdi
leaq 0x1490(%rsp), %rsi
callq 0x3b9e0
jmp 0x3d0c7
leaq 0x1890(%rsp), %rdi
leaq 0x229f5a(%rip), %rsi    # 0x266cb2
callq 0x29f00
cmpl $0x0, %eax
jne 0x3cdd1
leaq 0x2890(%rsp), %rdi
leaq 0x1890(%rsp), %rdx
leaq 0x1090(%rsp), %rcx
leaq 0x22e05b(%rip), %rsi    # 0x26addc
movb $0x0, %al
callq 0x29150
cmpl $0x2, %eax
je 0x3cdba
movl 0x689c(%rsp), %esi
movq 0x68b0(%rsp), %rdx
leaq 0x229edd(%rip), %rdi    # 0x266c80
movb $0x0, %al
callq 0x1cbff0
movl $0x0, 0x68c4(%rsp)
jmp 0x3d0e4
movq 0x30(%rsp), %rdi
leaq 0x1090(%rsp), %rsi
callq 0x3ba40
jmp 0x3d0c5
leaq 0x1890(%rsp), %rdi
leaq 0x229edd(%rip), %rsi    # 0x266cbd
callq 0x29f00
cmpl $0x0, %eax
jne 0x3d0c3
leaq 0x2890(%rsp), %rdi
leaq 0x1890(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x22dfcf(%rip), %rsi    # 0x26addc
movb $0x0, %al
callq 0x29150
cmpl $0x2, %eax
je 0x3ce46
movl 0x689c(%rsp), %esi
movq 0x68b0(%rsp), %rdx
leaq 0x229dfa(%rip), %rdi    # 0x266c29
movb $0x0, %al
callq 0x1cbff0
movl $0x0, 0x68c4(%rsp)
jmp 0x3d0e4
movl $0x90, %edi
callq 0x292e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x28(%rsp)
leaq 0x90(%rsp), %rsi
callq 0x45090
jmp 0x3ce6a
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rsi
callq 0x3b700
movq 0x88(%rsp), %rdi
callq 0x46020
cmpl $0x0, %eax
jne 0x3ced0
movl $0x0, 0x68c4(%rsp)
jmp 0x3d0e4
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
movl $0x90, %esi
callq 0x291f0
jmp 0x3d0f3
leaq 0x68(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x44490
movq 0x18(%rsp), %rsi
movq 0x88(%rsp), %rdi
callq 0x464f0
movl %eax, 0x24(%rsp)
jmp 0x3cef7
movl 0x24(%rsp), %eax
cmpl $0x0, %eax
jne 0x3cf2e
movl $0x0, 0x68c4(%rsp)
movl $0x1, 0x64(%rsp)
jmp 0x3d09f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
jmp 0x3d0b7
movq 0x88(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x46850
movl %eax, 0x14(%rsp)
jmp 0x3cf46
movl 0x14(%rsp), %eax
cmpl $0x0, %eax
jne 0x3cf67
movl $0x0, 0x68c4(%rsp)
movl $0x1, 0x64(%rsp)
jmp 0x3d09f
movl $0x2, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
cmpl $0x2, 0x5c(%rsp)
jg 0x3d097
leaq 0x48(%rsp), %rdi
callq 0x444a0
jmp 0x3cf8e
leaq 0x38(%rsp), %rdi
callq 0x444b0
jmp 0x3cf9a
movq 0x88(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0x68ac(%rsp), %r8d
leaq 0x48(%rsp), %rdx
leaq 0x38(%rsp), %rcx
callq 0x46e80
movl %eax, 0x10(%rsp)
jmp 0x3cfc3
movl 0x10(%rsp), %eax
cmpl $0x0, %eax
jne 0x3d014
movl $0x0, 0x68c4(%rsp)
movl $0x1, 0x64(%rsp)
jmp 0x3d05b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
jmp 0x3d08b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x444c0
jmp 0x3d08b
movq 0x88(%rsp), %rdi
movl 0x5c(%rsp), %esi
leaq 0x48(%rsp), %rdx
leaq 0x38(%rsp), %rcx
callq 0x477e0
movl %eax, 0xc(%rsp)
jmp 0x3d035
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
jne 0x3d053
movl $0x0, 0x68c4(%rsp)
movl $0x1, 0x64(%rsp)
jmp 0x3d05b
movl $0x0, 0x64(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x444c0
leaq 0x48(%rsp), %rdi
callq 0x444d0
movl 0x64(%rsp), %eax
testl %eax, %eax
jne 0x3d09f
jmp 0x3d079
jmp 0x3d07b
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x3cf77
leaq 0x48(%rsp), %rdi
callq 0x444d0
jmp 0x3d0b7
movl $0x0, 0x64(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x444e0
movl 0x64(%rsp), %eax
testl %eax, %eax
je 0x3d0b5
jmp 0x3d0b3
jmp 0x3d0e4
jmp 0x3d0c3
leaq 0x68(%rsp), %rdi
callq 0x444e0
jmp 0x3d0f3
jmp 0x3d0c5
jmp 0x3d0c7
jmp 0x3cc1e
movq 0x68a0(%rsp), %rdi
callq 0x297e0
movl $0x1, 0x68c4(%rsp)
movl 0x68c4(%rsp), %eax
addq $0x68c8, %rsp           # imm = 0x68C8
retq
movq 0x80(%rsp), %rdi
callq 0x29c10