// File: 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
movq %rsp, %rbp
subq $0x1adf0, %rsp          # imm = 0x1ADF0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x1ade4(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x2bc13(%rip), %rsi     # 0xad52b
callq 0x7680
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x81945
movq -0x10(%rbp), %rsi
leaq 0x2d71a(%rip), %rdi     # 0xaf04c
movb $0x0, %al
callq 0x7080
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x820c9
leaq -0x186d0(%rbp), %rdi
xorl %esi, %esi
movl $0x186a5, %edx          # imm = 0x186A5
callq 0x72b0
leaq -0x1ade0(%rbp), %rdi
movq -0x20(%rbp), %rdx
movl $0x2710, %esi           # imm = 0x2710
callq 0x7870
cmpq $0x0, %rax
je 0x8198d
leaq -0x186d0(%rbp), %rdi
leaq -0x1ade0(%rbp), %rsi
movl $0x2710, %edx           # imm = 0x2710
callq 0x7360
jmp 0x81958
movq -0x20(%rbp), %rdi
callq 0x7540
movq -0x18(%rbp), %rax
movl $0x13, 0x3e94(%rax)
movq -0x18(%rbp), %rax
movl $0x3, 0x1908(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x190c(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x3e90(%rax)
movq -0x18(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x4098(%rax)
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
xorl %esi, %esi
movl $0xc80, %edx            # imm = 0xC80
callq 0x72b0
movq -0x18(%rbp), %rdi
addq $0xc88, %rdi            # imm = 0xC88
xorl %esi, %esi
movl $0xc80, %edx            # imm = 0xC80
callq 0x72b0
movq -0x18(%rbp), %rdi
addq $0x3e98, %rdi           # imm = 0x3E98
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x72b0
movq -0x18(%rbp), %rdi
addq $0x3f98, %rdi           # imm = 0x3F98
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x72b0
movq -0x18(%rbp), %rdi
addq $0x1910, %rdi           # imm = 0x1910
xorl %esi, %esi
movl $0xc80, %edx            # imm = 0xC80
callq 0x72b0
movq -0x18(%rbp), %rdi
addq $0x2590, %rdi           # imm = 0x2590
xorl %esi, %esi
movl $0xc80, %edx            # imm = 0xC80
callq 0x72b0
movq -0x18(%rbp), %rdi
addq $0x3210, %rdi           # imm = 0x3210
xorl %esi, %esi
movl $0xc80, %edx            # imm = 0xC80
callq 0x72b0
xorl %eax, %eax
cmpl $0x186a0, -0x1ade4(%rbp) # imm = 0x186A0
movb %al, -0x1ade5(%rbp)
jge 0x81abf
movslq -0x1ade4(%rbp), %rax
movsbl -0x186d0(%rbp,%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x1ade5(%rbp)
movb -0x1ade5(%rbp), %al
testb $0x1, %al
jne 0x81ace
jmp 0x820c2
movslq -0x1ade4(%rbp), %rax
movsbl -0x186d0(%rbp,%rax), %edi
callq 0x820e0
testb $0x1, %al
jne 0x81b22
movslq -0x1ade4(%rbp), %rax
movsbl -0x186d0(%rbp,%rax), %eax
cmpl $0x3b, %eax
je 0x81b22
movslq -0x1ade4(%rbp), %rax
movsbl -0x186d0(%rbp,%rax), %eax
cmpl $0x28, %eax
je 0x81b22
movslq -0x1ade4(%rbp), %rax
movsbl -0x186d0(%rbp,%rax), %eax
cmpl $0x29, %eax
jne 0x81b99
jmp 0x81b24
cmpl $0x1869f, -0x1ade4(%rbp) # imm = 0x1869F
jge 0x81b97
movl -0x1ade4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1ade4(%rbp)
movslq -0x1ade4(%rbp), %rax
movsbl -0x186d0(%rbp,%rax), %edi
callq 0x820e0
testb $0x1, %al
jne 0x81b95
movslq -0x1ade4(%rbp), %rax
movsbl -0x186d0(%rbp,%rax), %eax
cmpl $0x3b, %eax
je 0x81b95
movslq -0x1ade4(%rbp), %rax
movsbl -0x186d0(%rbp,%rax), %eax
cmpl $0x28, %eax
je 0x81b95
movslq -0x1ade4(%rbp), %rax
movsbl -0x186d0(%rbp,%rax), %eax
cmpl $0x29, %eax
je 0x81b95
jmp 0x81b97
jmp 0x81b24
jmp 0x81b99
cmpl $0x186a0, -0x1ade4(%rbp) # imm = 0x186A0
jl 0x81baa
jmp 0x820c2
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d4a8(%rip), %rsi     # 0xaf06a
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
jne 0x81bf2
movq -0x18(%rbp), %rdi
leaq -0x186d0(%rbp), %rsi
movl -0x1ade4(%rbp), %edx
callq 0x82130
movl %eax, -0x1ade4(%rbp)
jmp 0x820ae
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d464(%rip), %rsi     # 0xaf06e
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
jne 0x81c3a
movq -0x18(%rbp), %rdi
leaq -0x186d0(%rbp), %rsi
movl -0x1ade4(%rbp), %edx
callq 0x82250
movl %eax, -0x1ade4(%rbp)
jmp 0x820ac
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d420(%rip), %rsi     # 0xaf072
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
jne 0x81c82
movq -0x18(%rbp), %rdi
leaq -0x186d0(%rbp), %rsi
movl -0x1ade4(%rbp), %edx
callq 0x82370
movl %eax, -0x1ade4(%rbp)
jmp 0x820aa
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d3dc(%rip), %rsi     # 0xaf076
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
jne 0x81ccf
movq -0x18(%rbp), %rdi
leaq -0x186d0(%rbp), %rsi
movl -0x1ade4(%rbp), %edx
movl $0x1, %ecx
callq 0x82450
movl %eax, -0x1ade4(%rbp)
jmp 0x820a8
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d393(%rip), %rsi     # 0xaf07a
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
jne 0x81d1c
movq -0x18(%rbp), %rdi
leaq -0x186d0(%rbp), %rsi
movl -0x1ade4(%rbp), %edx
movl $0x2, %ecx
callq 0x82450
movl %eax, -0x1ade4(%rbp)
jmp 0x820a6
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d34a(%rip), %rsi     # 0xaf07e
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
jne 0x81d64
movq -0x18(%rbp), %rdi
leaq -0x186d0(%rbp), %rsi
movl -0x1ade4(%rbp), %edx
callq 0x82710
movl %eax, -0x1ade4(%rbp)
jmp 0x820a4
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d306(%rip), %rsi     # 0xaf082
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
jne 0x81db1
movq -0x18(%rbp), %rdi
leaq -0x186d0(%rbp), %rsi
movl -0x1ade4(%rbp), %edx
movl $0x1, %ecx
callq 0x82830
movl %eax, -0x1ade4(%rbp)
jmp 0x820a2
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d2bd(%rip), %rsi     # 0xaf086
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
jne 0x81dfe
movq -0x18(%rbp), %rdi
leaq -0x186d0(%rbp), %rsi
movl -0x1ade4(%rbp), %edx
movl $0x2, %ecx
callq 0x82830
movl %eax, -0x1ade4(%rbp)
jmp 0x820a0
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d261(%rip), %rsi     # 0xaf077
movl $0x2, %edx
callq 0x7250
cmpl $0x0, %eax
jne 0x81e46
movq -0x18(%rbp), %rdi
leaq -0x186d0(%rbp), %rsi
movl -0x1ade4(%rbp), %edx
callq 0x82990
movl %eax, -0x1ade4(%rbp)
jmp 0x8209e
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d21d(%rip), %rsi     # 0xaf07b
movl $0x2, %edx
callq 0x7250
cmpl $0x0, %eax
jne 0x81e8e
movq -0x18(%rbp), %rdi
leaq -0x186d0(%rbp), %rsi
movl -0x1ade4(%rbp), %edx
callq 0x82990
movl %eax, -0x1ade4(%rbp)
jmp 0x8209c
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d1e4(%rip), %rsi     # 0xaf08a
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
je 0x82082
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d1bd(%rip), %rsi     # 0xaf08e
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
je 0x82082
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d196(%rip), %rsi     # 0xaf092
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
je 0x82082
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d16f(%rip), %rsi     # 0xaf096
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
je 0x82082
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d148(%rip), %rsi     # 0xaf09a
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
je 0x82082
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d121(%rip), %rsi     # 0xaf09e
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
je 0x82082
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d0fa(%rip), %rsi     # 0xaf0a2
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
je 0x82082
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d0d3(%rip), %rsi     # 0xaf0a6
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
je 0x82082
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d0ac(%rip), %rsi     # 0xaf0aa
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
je 0x82082
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d089(%rip), %rsi     # 0xaf0ae
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
je 0x82082
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d066(%rip), %rsi     # 0xaf0b2
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
je 0x82082
movslq -0x1ade4(%rbp), %rax
leaq -0x186d0(%rbp), %rdi
addq %rax, %rdi
leaq 0x2d043(%rip), %rsi     # 0xaf0b6
movl $0x3, %edx
callq 0x7250
cmpl $0x0, %eax
jne 0x8209a
leaq -0x186d0(%rbp), %rdi
movl -0x1ade4(%rbp), %esi
callq 0x82b00
movl %eax, -0x1ade4(%rbp)
jmp 0x8209c
jmp 0x8209e
jmp 0x820a0
jmp 0x820a2
jmp 0x820a4
jmp 0x820a6
jmp 0x820a8
jmp 0x820aa
jmp 0x820ac
jmp 0x820ae
movl -0x1ade4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1ade4(%rbp)
jmp 0x81a90
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1adf0, %rsp          # imm = 0x1ADF0
popq %rbp
retq
nopw %cs:(%rax,%rax)