// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
leaq 0x18e36(%rip), %rsi     # 0x5e483
callq 0x64e0
testq %rax, %rax
je 0x45da3
movq %rax, %r15
leaq 0x10(%rsp), %rdi
xorl %r14d, %r14d
movl $0x186a5, %edx          # imm = 0x186A5
xorl %esi, %esi
callq 0x6210
leaq 0x186c0(%rsp), %rdi
movl $0x2710, %esi           # imm = 0x2710
movq %r15, %rdx
callq 0x6670
testq %rax, %rax
je 0x456be
leaq 0x10(%rsp), %r12
leaq 0x186c0(%rsp), %r13
movl $0x2710, %edx           # imm = 0x2710
movq %r12, %rdi
movq %r13, %rsi
callq 0x6260
movq %r13, %rdi
movl $0x2710, %esi           # imm = 0x2710
movq %r15, %rdx
callq 0x6670
testq %rax, %rax
jne 0x45699
movq %r15, %rdi
callq 0x6400
movq $0x3, 0x1908(%rbx)
movabsq $0x1300000000, %rax     # imm = 0x1300000000
movq %rax, 0x3e90(%rbx)
movq $0x0, 0x4098(%rbx)
movq $0x1, (%rbx)
leaq 0x8(%rbx), %rdi
leaq 0x3e98(%rbx), %r15
leaq 0x1910(%rbx), %r12
movl $0x1900, %edx           # imm = 0x1900
xorl %esi, %esi
callq 0x6210
movl $0x2580, %edx           # imm = 0x2580
movq %r12, %rdi
xorl %esi, %esi
callq 0x6210
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x6210
movabsq $0x800030100002600, %r13 # imm = 0x800030100002600
movslq %r14d, %rax
movzbl 0x10(%rsp,%rax), %ebp
cmpq $0x3b, %rbp
ja 0x45783
btq %rbp, %r13
jae 0x45baa
cmpl $0x1869e, %r14d         # imm = 0x1869E
jg 0x45783
incl %r14d
movzbl 0x11(%rsp,%rax), %ecx
cmpq $0x3b, %rcx
ja 0x45783
btq %rcx, %r13
jae 0x45783
incq %rax
incl %r14d
cmpq $0x1869f, %rax          # imm = 0x1869F
jne 0x4575e
movl $0x1869f, %r14d         # imm = 0x1869F
xorl %ebp, %ebp
cmpl $0x1869f, %r14d         # imm = 0x1869F
jg 0x45db9
movslq %r14d, %r15
leaq (%rsp,%r15), %r12
addq $0x10, %r12
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a6e1(%rip), %rsi     # 0x5fe8d
callq 0x6190
testl %eax, %eax
je 0x45a2e
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a6c9(%rip), %rsi     # 0x5fe91
callq 0x6190
testl %eax, %eax
je 0x45a7f
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a6b1(%rip), %rsi     # 0x5fe95
callq 0x6190
testl %eax, %eax
je 0x45abf
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a699(%rip), %rsi     # 0x5fe99
callq 0x6190
testl %eax, %eax
je 0x45b8a
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a681(%rip), %rsi     # 0x5fe9d
callq 0x6190
testl %eax, %eax
je 0x45bf0
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a669(%rip), %rsi     # 0x5fea1
callq 0x6190
testl %eax, %eax
je 0x45c9f
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a651(%rip), %rsi     # 0x5fea5
callq 0x6190
testl %eax, %eax
je 0x45ce5
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a639(%rip), %rsi     # 0x5fea9
callq 0x6190
testl %eax, %eax
je 0x45d0c
movl $0x2, %edx
movq %r12, %rdi
leaq 0x1a60e(%rip), %rsi     # 0x5fe9a
callq 0x6190
testl %eax, %eax
je 0x45cf7
movl $0x2, %edx
movq %r12, %rdi
leaq 0x1a5f6(%rip), %rsi     # 0x5fe9e
callq 0x6190
testl %eax, %eax
je 0x45cf7
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a5e9(%rip), %rsi     # 0x5fead
callq 0x6190
testl %eax, %eax
je 0x459f1
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a5d1(%rip), %rsi     # 0x5feb1
callq 0x6190
testl %eax, %eax
je 0x459f1
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a5b9(%rip), %rsi     # 0x5feb5
callq 0x6190
testl %eax, %eax
je 0x459f1
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a5a1(%rip), %rsi     # 0x5feb9
callq 0x6190
testl %eax, %eax
je 0x459f1
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a589(%rip), %rsi     # 0x5febd
callq 0x6190
testl %eax, %eax
je 0x459f1
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a571(%rip), %rsi     # 0x5fec1
callq 0x6190
testl %eax, %eax
je 0x459f1
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a559(%rip), %rsi     # 0x5fec5
callq 0x6190
testl %eax, %eax
je 0x459f1
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a545(%rip), %rsi     # 0x5fec9
callq 0x6190
testl %eax, %eax
je 0x459f1
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a531(%rip), %rsi     # 0x5fecd
callq 0x6190
testl %eax, %eax
je 0x459f1
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a51d(%rip), %rsi     # 0x5fed1
callq 0x6190
testl %eax, %eax
je 0x459f1
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a509(%rip), %rsi     # 0x5fed5
callq 0x6190
testl %eax, %eax
je 0x459f1
movl $0x3, %edx
movq %r12, %rdi
leaq 0x1a4f5(%rip), %rsi     # 0x5fed9
callq 0x6190
testl %eax, %eax
jne 0x45c86
leal 0x3(%r14), %eax
cmpl $0x1869c, %r14d         # imm = 0x1869C
jg 0x45c05
movl $0x186a0, %ecx          # imm = 0x186A0
subl %r14d, %ecx
addq $-0x3, %rcx
movslq %eax, %rdx
cmpb $0x5d, 0x10(%rsp,%rdx)
je 0x45c05
incl %eax
movl $0x186a0, %r14d         # imm = 0x186A0
decq %rcx
jne 0x45a0e
jmp 0x45c86
movw $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
movl $0x3, %r12d
cmpl $0x1869c, %r14d         # imm = 0x1869C
jg 0x45b12
movl $0x186a0, %r12d         # imm = 0x186A0
subl %r14d, %r12d
movl %r14d, %ecx
movl $0x3, %eax
leal (%rcx,%rax), %edx
movslq %edx, %rdx
cmpb $0x5d, 0x10(%rsp,%rdx)
je 0x45b0f
incq %rax
cmpq %rax, %r12
jne 0x45a61
jmp 0x45b12
movl $0x3, %eax
cmpl $0x1869c, %r14d         # imm = 0x1869C
jg 0x45bba
movl $0x186a0, %eax          # imm = 0x186A0
subl %r14d, %eax
movl %r14d, %edx
movl $0x3, %ecx
leal (%rdx,%rcx), %esi
movslq %esi, %rsi
cmpb $0x5d, 0x10(%rsp,%rsi)
je 0x45bb8
incq %rcx
cmpq %rcx, %rax
jne 0x45aa1
jmp 0x45bba
movw $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
cmpl $0x1869c, %r14d         # imm = 0x1869C
jg 0x45b9c
movl $0x186a0, %eax          # imm = 0x186A0
subl %r14d, %eax
movl %r14d, %edx
movl $0x3, %ecx
leal (%rdx,%rcx), %esi
movslq %esi, %rsi
cmpb $0x5d, 0x10(%rsp,%rsi)
je 0x45c44
incq %rcx
cmpq %rcx, %rax
jne 0x45aeb
movl $0x186a0, %r14d         # imm = 0x186A0
jmp 0x45c49
movl %eax, %r12d
cmpl $0x4, %r12d
jl 0x45b44
leal -0x3(%r12), %eax
leaq 0x13(%rsp), %rcx
addq %r15, %rcx
xorl %edx, %edx
leaq (%r15,%rdx), %rsi
cmpq $0x1869c, %rsi          # imm = 0x1869C
jg 0x45b3c
movb (%rcx,%rdx), %sil
movb %sil, (%rsp,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0x45b27
movq %rsp, %rdi
callq 0x6640
movl %eax, 0x3e94(%rbx)
addl %r12d, %r14d
cmpl $0x1869f, %r14d         # imm = 0x1869F
jg 0x45c86
movslq %r12d, %rax
addq %rax, %r15
movq %r15, %r14
movslq %r14d, %rax
cmpb $0x5d, 0x10(%rsp,%rax)
je 0x45c86
incq %r14
cmpq $0x1869f, %r14          # imm = 0x1869F
jle 0x45b6b
jmp 0x45c3c
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movl %r14d, %edx
movl $0x1, %ecx
jmp 0x45c00
addl $0x3, %r14d
movl $0x3, %eax
jmp 0x45c49
testq %rbp, %rbp
jne 0x45783
jmp 0x45db9
movl %ecx, %eax
cmpl $0x1869c, %r14d         # imm = 0x1869C
jg 0x45c12
movzbl 0x13(%rsp,%r15), %ecx
cmpl $0x43, %ecx
jg 0x45bdf
cmpl $0x30, %ecx
je 0x45c0a
movl $0x1, %edx
cmpl $0x42, %ecx
je 0x45c0c
jmp 0x45c12
cmpl $0x44, %ecx
je 0x45c0a
cmpl $0x57, %ecx
jne 0x45c12
movl $0x2, %edx
jmp 0x45c0c
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movl %r14d, %edx
movl $0x2, %ecx
callq 0x45dcd
movl %eax, %r14d
jmp 0x45c86
xorl %edx, %edx
movl %edx, 0x1908(%rbx)
addl %eax, %r14d
cmpl $0x1869f, %r14d         # imm = 0x1869F
jg 0x45c86
cltq
addq %rax, %r15
movq %r15, %r14
movslq %r14d, %rax
cmpb $0x5d, 0x10(%rsp,%rax)
je 0x45c86
incq %r14
cmpq $0x1869f, %r14          # imm = 0x1869F
jle 0x45c26
movl $0x186a0, %r14d         # imm = 0x186A0
jmp 0x45c86
addl %ecx, %r14d
movl %ecx, %eax
cmpl $0x4, %eax
jl 0x45c78
addl $-0x3, %eax
leaq 0x13(%rsp), %rcx
addq %r15, %rcx
xorl %edx, %edx
leaq (%r15,%rdx), %rsi
cmpq $0x1869c, %rsi          # imm = 0x1869C
jg 0x45c70
movb (%rcx,%rdx), %sil
movb %sil, (%rsp,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0x45c5b
movq %rsp, %rdi
callq 0x6640
movl %eax, 0x190c(%rbx)
leal 0x1(%r14), %eax
cmpl $0x1869f, %r14d         # imm = 0x1869F
movl %eax, %r14d
jl 0x4573a
jmp 0x45db9
movw $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
movl $0x3, %r12d
cmpl $0x1869c, %r14d         # imm = 0x1869C
jg 0x45d29
movl $0x186a0, %r12d         # imm = 0x186A0
subl %r14d, %r12d
movl %r14d, %ecx
movl $0x3, %eax
leal (%rcx,%rax), %edx
movslq %edx, %rdx
cmpb $0x5d, 0x10(%rsp,%rdx)
je 0x45d26
incq %rax
cmpq %rax, %r12
jne 0x45cce
jmp 0x45d29
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movl %r14d, %edx
movl $0x1, %ecx
jmp 0x45d1c
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movl %r14d, %edx
callq 0x4609d
jmp 0x45c05
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
movl %r14d, %edx
movl $0x2, %ecx
callq 0x45fc5
jmp 0x45c05
movl %eax, %r12d
cmpl $0x4, %r12d
jl 0x45d5b
leal -0x3(%r12), %eax
leaq 0x13(%rsp), %rcx
addq %r15, %rcx
xorl %edx, %edx
leaq (%r15,%rdx), %rsi
cmpq $0x1869c, %rsi          # imm = 0x1869C
jg 0x45d53
movb (%rcx,%rdx), %sil
movb %sil, (%rsp,%rdx)
incq %rdx
cmpq %rdx, %rax
jne 0x45d3e
movq %rsp, %rdi
callq 0x6510
movsd %xmm0, 0x4098(%rbx)
addl %r12d, %r14d
cmpl $0x1869f, %r14d         # imm = 0x1869F
jg 0x45c86
movslq %r12d, %rax
addq %rax, %r15
movq %r15, %r14
movslq %r14d, %rax
cmpb $0x5d, 0x10(%rsp,%rax)
je 0x45c86
incq %r14
cmpq $0x1869f, %r14          # imm = 0x1869F
jle 0x45d84
jmp 0x45c3c
leaq 0x1a0c5(%rip), %rdi     # 0x5fe6f
movq %r14, %rsi
xorl %eax, %eax
callq 0x6060
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x1add8, %rsp          # imm = 0x1ADD8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq