// File: /workspace/github_cpp_makefile_star2_O2/kobanium[P]Ray/src/sgf/SgfExtractor.cpp
// Function: ExtractKifu(char const*, SGF_record_t*)
int
ExtractKifu( const char *file_name, SGF_record_t *kifu )
{
  FILE *fp;
  char sgf_text[100005], buffer[10000];
  int cursor = 0;
  
#if defined (_WIN32)
  errno_t err;

  if((err = fopen_s(&fp, file_name, "r")) != 0) {
    printf("Cannot open this file (%s)!!\n", file_name);
    return -1;
  } 
#else
  if((fp = fopen(file_name, "r")) == NULL) {
    printf("Cannot open this file (%s)!!\n", file_name);
    return -1;
  }
#endif  

  memset(sgf_text, 0, sizeof(char) * 100005);
  
  while (fgets(buffer, 10000, fp) != NULL) {
#if defined (_WIN32)
    strcat_s(sgf_text, 10000, buffer);
#else
    strncat(sgf_text, buffer, 10000);
#endif
  }
  fclose(fp);
  
  kifu->board_size = 19;
  kifu->result = R_UNKNOWN;
  kifu->moves = 0;
  kifu->handicaps = 0;
  kifu->handicap_stones = 0;
  kifu->komi = 0.0;
  kifu->start_color = S_BLACK;
  memset(kifu->move_x, 0, sizeof(kifu->move_x));
  memset(kifu->move_y, 0, sizeof(kifu->move_y));
  memset(kifu->black_name, 0, sizeof(kifu->black_name));
  memset(kifu->white_name, 0, sizeof(kifu->white_name));
  memset(kifu->handicap_x, 0, sizeof(kifu->handicap_x));
  memset(kifu->handicap_y, 0, sizeof(kifu->handicap_y));
  memset(kifu->handicap_color, 0, sizeof(kifu->handicap_color));
  
  while ((cursor < 100000) && (sgf_text[cursor] != '\0')) {
    if (IsSgfSpace(sgf_text[cursor]) ||
        sgf_text[cursor] == ';'  ||
        sgf_text[cursor] == '('  ||
        sgf_text[cursor] == ')') { 
      while (cursor < 100000 - 1) {
        cursor++;
        if (!IsSgfSpace(sgf_text[cursor]) &&
            sgf_text[cursor] != ';'  &&
            sgf_text[cursor] != '('  &&
            sgf_text[cursor] != ')') 
          break;
      }
    }    
    if (cursor >= 100000){
      break;
    }

    // GetSize             : 碁盤のサイズの抽出 
    // GetResult           : 結果の抽出 
    // GetHandicaps        : 置き石の個数の抽出 
    // GetHandicapPosition : 置き石の座標
    // GetMove             : 着手の抽出
    // GetKomi             : コミの抽出
    // GetPlayerName       : 対局者の名前の抽出
    if (strncmp(&sgf_text[cursor], "SZ[", 3) == 0) {
      cursor = GetSize(kifu, sgf_text, cursor);
    } else if (strncmp(&sgf_text[cursor], "RE[", 3) == 0) {
      cursor = GetResult(kifu, sgf_text, cursor);
    } else if (strncmp(&sgf_text[cursor], "HA[", 3) == 0) {
      cursor = GetHandicaps(kifu, sgf_text, cursor);
    } else if (strncmp(&sgf_text[cursor], "AB[", 3) == 0) {
      cursor = GetHandicapPosition(kifu, sgf_text, cursor, S_BLACK);
    } else if (strncmp(&sgf_text[cursor], "AW[", 3) == 0) {
      cursor = GetHandicapPosition(kifu, sgf_text, cursor, S_WHITE);
    } else if (strncmp(&sgf_text[cursor], "KM[", 3) == 0) {
      cursor = GetKomi(kifu, sgf_text, cursor);
    } else if (strncmp(&sgf_text[cursor], "PB[", 3) == 0) {
      cursor = GetPlayerName(kifu, sgf_text, cursor, S_BLACK);
    } else if (strncmp(&sgf_text[cursor], "PW[", 3) == 0) {
      cursor = GetPlayerName(kifu, sgf_text, cursor, S_WHITE);
    } else if (strncmp(&sgf_text[cursor],  "B[", 2) == 0) {
      cursor = GetMove(kifu, sgf_text, cursor);
    } else if (strncmp(&sgf_text[cursor],  "W[", 2) == 0) {
      cursor = GetMove(kifu, sgf_text, cursor);
    } else if ((strncmp(&sgf_text[cursor], "GM[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "FF[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "DT[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "PC[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "RU[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "CA[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "TM[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "OT[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "TB[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "TW[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "WR[", 3) == 0) ||
               (strncmp(&sgf_text[cursor], "BR[", 3) == 0)) {      
      // 無視するデータ
      cursor = SkipData(sgf_text, cursor);
    }
    cursor++;   // 文字を一つ進める
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1add8, %rsp          # imm = 0x1ADD8
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x16602(%rip), %rsi     # 0x454a3
callq 0x64a0
testq %rax, %rax
je 0x2f440
movq %rax, %r14
leaq 0x10(%rsp), %r15
movl $0x186a5, %edx          # imm = 0x186A5
movq %r15, %rdi
xorl %esi, %esi
callq 0x61f0
leaq 0x186c0(%rsp), %r12
movq %r12, %rdi
movl $0x2710, %esi           # imm = 0x2710
movq %r14, %rdx
callq 0x6630
testq %rax, %rax
je 0x2eef5
movl $0x2710, %edx           # imm = 0x2710
movq %r15, %rdi
movq %r12, %rsi
callq 0x6240
jmp 0x2eece
movq %r14, %rdi
callq 0x63d0
movq $0x3, 0x1908(%rbx)
movabsq $0x1300000000, %rax     # imm = 0x1300000000
movq %rax, 0x3e90(%rbx)
andq $0x0, 0x4098(%rbx)
movq $0x1, (%rbx)
leaq 0x8(%rbx), %rdi
leaq 0x3e98(%rbx), %r15
leaq 0x1910(%rbx), %r12
xorl %r14d, %r14d
movl $0x1900, %edx           # imm = 0x1900
xorl %esi, %esi
callq 0x61f0
movl $0x2580, %edx           # imm = 0x2580
movq %r12, %rdi
xorl %esi, %esi
callq 0x61f0
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x61f0
leaq 0x17f52(%rip), %rbp     # 0x46ec0
leaq 0x10(%rsp), %r15
movabsq $0x800030100002600, %r13 # imm = 0x800030100002600
xorl %eax, %eax
cmpl $0x1869f, %eax          # imm = 0x1869F
jg 0x2f455
movslq %eax, %r12
movzbl 0x10(%rsp,%r12), %edx
cmpq $0x3b, %rdx
ja 0x2efd4
btq %rdx, %r13
jae 0x2f1bb
incl %eax
cmpq $0x1869e, %r12          # imm = 0x1869E
jg 0x2efc7
movzbl 0x11(%rsp,%r12), %ecx
cmpq $0x3b, %rcx
ja 0x2efcc
btq %rcx, %r13
jae 0x2efcc
incl %eax
incq %r12
jmp 0x2efa5
movl $0x1869f, %eax          # imm = 0x1869F
movslq %eax, %r12
movb 0x10(%rsp,%r12), %dl
movzbl %dl, %ecx
leal -0x41(%rcx), %esi
cmpl $0x16, %esi
ja 0x2f439
movslq (%rbp,%rsi,4), %rsi
addq %rbp, %rsi
jmpq *%rsi
cmpb $0x5b, 0x11(%rsp,%r12)
jne 0x2f139
movq %rbx, %rdi
movq %r15, %rsi
movl %eax, %edx
callq 0x2f6f2
jmp 0x2f21c
cmpb $0x5a, 0x11(%rsp,%r12)
jne 0x2f267
cmpb $0x5b, 0x12(%rsp,%r12)
jne 0x2f439
movq %r15, %r14
andw $0x0, 0x8(%rsp)
andq $0x0, (%rsp)
leaq (%rsp,%r12), %rcx
addq $0x10, %rcx
leaq 0x3(%r12), %r15
xorl %eax, %eax
pushq $0x3
popq %rdx
leaq (%r12,%rdx), %rsi
cmpq $0x1869f, %rsi          # imm = 0x1869F
jg 0x2f2fe
cmpb $0x5d, (%rcx,%rdx)
je 0x2f2fe
incq %rdx
incq %rax
incq %r15
jmp 0x2f043
cmpb $0x4d, 0x11(%rsp,%r12)
jne 0x2f221
cmpb $0x5b, 0x12(%rsp,%r12)
jne 0x2f267
movq %r15, %r14
andw $0x0, 0x8(%rsp)
andq $0x0, (%rsp)
leaq (%rsp,%r12), %rcx
addq $0x10, %rcx
leaq 0x3(%r12), %r15
xorl %eax, %eax
pushq $0x3
popq %rdx
leaq (%r12,%rdx), %rsi
cmpq $0x1869f, %rsi          # imm = 0x1869F
jg 0x2f34f
cmpb $0x5d, (%rcx,%rdx)
je 0x2f34f
incq %rdx
incq %rax
incq %r15
jmp 0x2f0a1
cmpb $0x45, 0x11(%rsp,%r12)
jne 0x2f1c9
cmpb $0x5b, 0x12(%rsp,%r12)
jne 0x2f1c9
leaq (%rsp,%r12), %rdx
addq $0x10, %rdx
leaq 0x3(%r12), %rcx
pushq $0x3
popq %rsi
leaq (%r12,%rsi), %rdi
cmpq $0x1869f, %rdi          # imm = 0x1869F
jg 0x2f3ab
cmpb $0x5d, (%rdx,%rsi)
je 0x2f3ab
incq %rsi
incq %rcx
jmp 0x2f0ef
cmpb $0x42, 0x11(%rsp,%r12)
jne 0x2f1da
cmpb $0x5b, 0x12(%rsp,%r12)
jne 0x2f1da
movq %rbx, %rdi
movq %r15, %rsi
movl %eax, %edx
pushq $0x1
jmp 0x2f1f4
cmpb $0x52, 0x11(%rsp,%r12)
je 0x2f2ce
jmp 0x2f439
cmpb $0x42, 0x11(%rsp,%r12)
jne 0x2f1fc
cmpb $0x5b, 0x12(%rsp,%r12)
jne 0x2f1fc
movq %rbx, %rdi
movq %r15, %rsi
movl %eax, %edx
pushq $0x1
jmp 0x2f216
cmpb $0x41, 0x11(%rsp,%r12)
jne 0x2f439
cmpb $0x5b, 0x12(%rsp,%r12)
jne 0x2f221
andw $0x0, 0x8(%rsp)
andq $0x0, (%rsp)
leaq 0x3(%r12), %rcx
xorl %eax, %eax
cmpq $0x1869f, %rcx          # imm = 0x1869F
jg 0x2f3d4
cmpb $0x5d, 0x10(%rsp,%rcx)
je 0x2f3d4
decq %rax
incq %rcx
jmp 0x2f19b
testq %rdx, %rdx
jne 0x2efd4
jmp 0x2f46a
cmpb $0x55, 0x11(%rsp,%r12)
je 0x2f25f
jmp 0x2f267
cmpb $0x57, 0x11(%rsp,%r12)
jne 0x2f257
cmpb $0x5b, 0x12(%rsp,%r12)
jne 0x2f257
movq %rbx, %rdi
movq %r15, %rsi
movl %eax, %edx
pushq $0x2
popq %rcx
callq 0x2f645
jmp 0x2f21c
cmpb $0x57, 0x11(%rsp,%r12)
jne 0x2f221
cmpb $0x5b, 0x12(%rsp,%r12)
jne 0x2f221
movq %rbx, %rdi
movq %r15, %rsi
movl %eax, %edx
pushq $0x2
popq %rcx
callq 0x2f46f
jmp 0x2f439
leal -0x43(%rcx), %edx
cmpl $0x4, %edx
ja 0x2f287
leaq 0x17cec(%rip), %rsi     # 0x46f1c
movslq (%rsi,%rdx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
cmpb $0x46, 0x11(%rsp,%r12)
je 0x2f2ce
jmp 0x2f439
cmpb $0x4d, 0x11(%rsp,%r12)
je 0x2f2ce
jmp 0x2f439
cmpb $0x43, 0x11(%rsp,%r12)
jne 0x2f267
cmpb $0x5b, 0x12(%rsp,%r12)
je 0x2f2da
cmpb $0x54, %dl
je 0x2f295
cmpl $0x4f, %ecx
je 0x2f2c2
cmpl $0x43, %ecx
jne 0x2f439
cmpb $0x41, 0x11(%rsp,%r12)
je 0x2f2ce
jmp 0x2f439
cmpl $0x4f, %ecx
je 0x2f2c2
cmpl $0x54, %ecx
jne 0x2f439
cmpb $0x4d, 0x11(%rsp,%r12)
jne 0x2f2a5
cmpb $0x5b, 0x12(%rsp,%r12)
je 0x2f2da
cmpb $0x42, 0x11(%rsp,%r12)
jne 0x2f2b5
cmpb $0x5b, 0x12(%rsp,%r12)
je 0x2f2da
cmpb $0x57, 0x11(%rsp,%r12)
je 0x2f2ce
jmp 0x2f439
cmpb $0x54, 0x11(%rsp,%r12)
jne 0x2f439
cmpb $0x5b, 0x12(%rsp,%r12)
jne 0x2f439
addq $0x3, %r12
movq %r12, %rax
cmpq $0x1869f, %r12          # imm = 0x1869F
jg 0x2f439
leaq 0x1(%rax), %r12
cmpb $0x5d, 0x10(%rsp,%rax)
jne 0x2f2de
jmp 0x2f439
leaq 0x13(%rsp), %rcx
addq %r12, %rcx
xorl %edx, %edx
subq $0x1, %rax
jb 0x2f328
leaq (%r12,%rdx), %rsi
cmpq $0x1869c, %rsi          # imm = 0x1869C
jg 0x2f323
movb (%rcx,%rdx), %sil
movb %sil, (%rsp,%rdx)
incq %rdx
jmp 0x2f308
movq %rsp, %rdi
callq 0x6600
movl %eax, 0x3e94(%rbx)
movq %r15, %rax
cmpq $0x1869f, %r15          # imm = 0x1869F
jg 0x2f3a0
leaq 0x1(%rax), %r15
cmpb $0x5d, 0x10(%rsp,%rax)
jne 0x2f336
jmp 0x2f3a0
leaq 0x13(%rsp), %rcx
addq %r12, %rcx
xorl %edx, %edx
subq $0x1, %rax
jb 0x2f379
leaq (%r12,%rdx), %rsi
cmpq $0x1869c, %rsi          # imm = 0x1869C
jg 0x2f374
movb (%rcx,%rdx), %sil
movb %sil, (%rsp,%rdx)
incq %rdx
jmp 0x2f359
movq %rsp, %rdi
callq 0x64d0
movsd %xmm0, 0x4098(%rbx)
movq %r15, %rax
cmpq $0x1869f, %r15          # imm = 0x1869F
jg 0x2f3a0
leaq 0x1(%rax), %r15
cmpb $0x5d, 0x10(%rsp,%rax)
jne 0x2f389
movq %r14, %r15
xorl %r14d, %r14d
jmp 0x2f439
cmpl $0x1869c, %eax          # imm = 0x1869C
jg 0x2f422
movzbl 0x13(%rsp,%r12), %eax
cmpl $0x30, %eax
je 0x2f41a
pushq $0x1
popq %rdx
cmpl $0x42, %eax
je 0x2f41c
cmpl $0x44, %eax
je 0x2f41a
cmpl $0x57, %eax
jne 0x2f422
pushq $0x2
popq %rdx
jmp 0x2f41c
leaq 0x13(%rsp), %rcx
addq %r12, %rcx
xorl %edx, %edx
movq %rax, %rsi
addq %rdx, %rsi
je 0x2f400
leaq (%r12,%rdx), %rsi
cmpq $0x1869c, %rsi          # imm = 0x1869C
jg 0x2f3fb
movb (%rcx,%rdx), %sil
movb %sil, (%rsp,%rdx)
incq %rdx
jmp 0x2f3de
subl %eax, %r12d
addl $0x3, %r12d
movq %rsp, %rdi
callq 0x6600
movl %eax, 0x190c(%rbx)
movl %r12d, %eax
jmp 0x2f439
xorl %edx, %edx
movl %edx, 0x1908(%rbx)
movq %rcx, %rax
cmpq $0x1869f, %rcx          # imm = 0x1869F
jg 0x2f439
leaq 0x1(%rax), %rcx
cmpb $0x5d, 0x10(%rsp,%rax)
jne 0x2f422
incl %eax
jmp 0x2ef7f
leaq 0x17ae9(%rip), %rdi     # 0x46f30
movq %r15, %rsi
xorl %eax, %eax
callq 0x6050
pushq $-0x1
popq %r14
movl %r14d, %eax
addq $0x1add8, %rsp          # imm = 0x1ADD8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %edx, %r14d
jmp 0x2f455