From ead3238e857e6ff6960f50e9ccc540888a74ffee Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gon=C3=A7alo=20Ferreira?= Date: Wed, 16 Oct 2019 18:58:54 +0100 Subject: [PATCH] Part 5 of code style change --- src/sgf.c | 278 +++++++++++++++++++++++++++++--------------------------------- 1 file changed, 130 insertions(+), 148 deletions(-) diff --git a/src/sgf.c b/src/sgf.c index 37402a9..a926471 100644 --- a/src/sgf.c +++ b/src/sgf.c @@ -39,13 +39,17 @@ static u8 guess_board_size( const char * sgf ) { char buf[8]; + for (u8 size = 23; size > 4; size -= 2) { snprintf(buf, 8, ";B[%c", 'a' + size - 1); - if (strstr(sgf, buf) != NULL) + if (strstr(sgf, buf) != NULL) { return size; + } + snprintf(buf, 8, ";W[%c", 'a' + size - 1); - if (strstr(sgf, buf) != NULL) + if (strstr(sgf, buf) != NULL) { return size; + } } return 0; } @@ -74,28 +78,26 @@ u32 export_game_as_sgf_to_buffer( release(kstr); if (gr->finished) { - if (gr->resignation) - buf += snprintf(buf, size - (buf - buffer), "RE[%c+R]\n", - gr->final_score > 0 ? 'B' : 'W'); - else - buf += snprintf(buf, size - (buf - buffer), "RE[%c+%u.5]\n", - gr->final_score > 0 ? 'B' : 'W', abs(gr->final_score) / 2); - } - else + if (gr->resignation) { + buf += snprintf(buf, size - (buf - buffer), "RE[%c+R]\n", gr->final_score > 0 ? 'B' : 'W'); + } else { + buf += snprintf(buf, size - (buf - buffer), "RE[%c+%u.5]\n", gr->final_score > 0 ? 'B' : 'W', abs(gr->final_score) / 2); + } + } else { buf += snprintf(buf, size - (buf - buffer), "RE[Void]\n"); + } /* Not standard but as used in KGS; closest would be AGA rules */ buf += snprintf(buf, size - (buf - buffer), "RU[Chinese]\n"); - buf += snprintf(buf, size - (buf - buffer), "AP[matilda:%s]\n", - MATILDA_VERSION); + buf += snprintf(buf, size - (buf - buffer), "AP[matilda:%s]\n", MATILDA_VERSION); char * mstr = alloc(); /* Handicap stones */ if (gr->handicap_stones.count > 1) { - buf += snprintf(buf, size - (buf - buffer), "HA[%u]\nAB", - gr->handicap_stones.count); + buf += snprintf(buf, size - (buf - buffer), "HA[%u]\nAB", gr->handicap_stones.count); + for (u16 i = 0; i < gr->handicap_stones.count; ++i) { coord_to_alpha_alpha(mstr, gr->moves[i]); buf += snprintf(buf, size - (buf - buffer), "[%s]\n", mstr); @@ -108,30 +110,25 @@ u32 export_game_as_sgf_to_buffer( buf += snprintf(buf, size - (buf - buffer), "\n"); assert(gr->moves[i] != NONE); + if (gr->moves[i] == PASS) { - if (gr->handicap_stones.count == 0) - buf += snprintf(buf, size - (buf - buffer), ";%c[]", (i & 1) == - 0 ? 'B' : 'W'); - else - buf += snprintf(buf, size - (buf - buffer), ";%c[]", (i & 1) == - 1 ? 'B' : 'W'); - } - else - { - coord_to_alpha_alpha(mstr, gr->moves[i]); - if (gr->handicap_stones.count == 0) - { - buf += snprintf(buf, size - (buf - buffer), ";%c[%s]", (i & 1) - == 0 ? 'B' : 'W', mstr); + if (gr->handicap_stones.count == 0) { + buf += snprintf(buf, size - (buf - buffer), ";%c[]", (i & 1) == 0 ? 'B' : 'W'); + } else { + buf += snprintf(buf, size - (buf - buffer), ";%c[]", (i & 1) == 1 ? 'B' : 'W'); } - else - { + } else { + coord_to_alpha_alpha(mstr, gr->moves[i]); + + if (gr->handicap_stones.count == 0) { + buf += snprintf(buf, size - (buf - buffer), ";%c[%s]", (i & 1) == 0 ? 'B' : 'W', mstr); + } else { coord_to_alpha_alpha(mstr, gr->moves[i]); - buf += snprintf(buf, size - (buf - buffer), ";%c[%s]", (i & 1) - == 1 ? 'B' : 'W', mstr); + buf += snprintf(buf, size - (buf - buffer), ";%c[%s]", (i & 1) == 1 ? 'B' : 'W', mstr); } } } + buf += snprintf(buf, size - (buf - buffer), ")\n"); release(mstr); @@ -149,8 +146,9 @@ bool export_game_as_sgf_auto_named( char filename[static MAX_PAGE_SIZ] ) { int fid = create_and_open_file(filename, MAX_PAGE_SIZ, "matilda", "sgf"); - if (fid == -1) + if (fid == -1) { return false; + } char * buffer = alloc(); if (buffer == NULL) { @@ -182,13 +180,16 @@ bool export_game_as_sgf( fclose(fp); return false; } + fp = fopen(filename, "w"); - if (fp == NULL) + if (fp == NULL) { return false; + } char * buffer = alloc(); - if (buffer == NULL) + if (buffer == NULL) { return false; + } u32 chars = export_game_as_sgf_to_buffer(gr, buffer, 4 * 1024); fwrite(buffer, sizeof(char), chars, fp); @@ -237,11 +238,12 @@ bool import_game_from_sgf2( /* Game */ - if (strstr(buf, "GM[1]") == NULL) + if (strstr(buf, "GM[1]") == NULL) { if (!sgf_format_undeclared_warned) { sgf_format_undeclared_warned = true; flog_warn("sgff", "GM[1] annotation not found"); } + } char * tmp = alloc(); @@ -251,15 +253,13 @@ bool import_game_from_sgf2( str_between(tmp, buf, "KM[", "]"); if (tmp[0] != 0) { double komid; + if (!parse_float(&komid, tmp)) { - if (!komi_format_error) - { + if (!komi_format_error) { komi_format_error = true; flog_warn("sgff", "komi format error; current komi kept"); } - } - else - { + } else { komi = (d16)(komid * 2.0); } } @@ -279,25 +279,26 @@ bool import_game_from_sgf2( board_size_cant_be_guessed_warned = true; flog_warn("sgff", "board size can not be guessed from play coordinates"); } + if (board_size != BOARD_SIZ && board_size + 1 != BOARD_SIZ) { if (!wrong_board_size_warned) { wrong_board_size_warned = true; flog_warn("sgff", "wrong board size"); } + release(tmp); return false; } - } - else - if (strcmp(tmp, BOARD_SIZ_AS_STR) != 0) { - if (!wrong_board_size_warned) { - wrong_board_size_warned = true; - flog_warn("sgff", "wrong board size"); - } - release(tmp); - return false; + } else if (strcmp(tmp, BOARD_SIZ_AS_STR) != 0) { + if (!wrong_board_size_warned) { + wrong_board_size_warned = true; + flog_warn("sgff", "wrong board size"); } + release(tmp); + return false; + } + /* Player names */ @@ -317,7 +318,7 @@ bool import_game_from_sgf2( /* Result */ - bool finished = false; + bool finished = true; bool resignation = false; bool timeout = false; d16 final_score = 0; @@ -325,81 +326,60 @@ bool import_game_from_sgf2( char * result = tmp; str_between(result, buf, "RE[", "]"); - if (result[0] == 0 || strcmp(result, "Void") == 0) - gr->finished = false; - else - if (strcmp(result, "?") == 0 || strcmp(result, "Draw") == 0 || - strcmp(result, "0") == 0) { - finished = true; - } - else - if (result[0] == 'B') - { - finished = true; - if (strlen(result) > 2) - { - result += 2; - if (result[0] == 'R') - { - resignation = true; - final_score = 1; + if (result[0] == 0 || strcmp(result, "Void") == 0) { + finished = false; + } else if (strcmp(result, "?") == 0 || strcmp(result, "Draw") == 0 || strcmp(result, "0") == 0) { + /* do nothing */ + } else if (result[0] == 'B') { + if (strlen(result) > 2) { + result += 2; + + if (result[0] == 'R') { + resignation = true; + final_score = 1; + } else if (result[0] == 'T') { + timeout = true; + final_score = 1; + } else { + double f; + if (!parse_float(&f, result)) { + if (!illegal_final_score_warned) { + illegal_final_score_warned = true; + flog_warn("sgff", "illegal result format"); } - else - if (result[0] == 'T') - { - timeout = true; - final_score = 1; - } - else - { - double f; - if (!parse_float(&f, result)) - { - if (!illegal_final_score_warned) { - illegal_final_score_warned = true; - flog_warn("sgff", "illegal result format"); - } - finished = false; - goto after_game_result; - } - final_score = (d32)(f * 2.0); - } + + finished = false; + goto after_game_result; } + + final_score = (d32)(f * 2.0); } - else - { - finished = true; - if (strlen(result) > 2) - { - result += 2; - if (result[0] == 'R') - { - resignation = true; - final_score = -1; - } - else - if (result[0] == 'T') - { - timeout = true; - final_score = -1; - } - else - { - double f; - if (!parse_float(&f, result)) - { - if (!illegal_final_score_warned) { - illegal_final_score_warned = true; - flog_warn("sgff", "illegal result format"); - } - finished = false; - goto after_game_result; - } - final_score = (d32)(f * -2.0); - } + } + } else if (strlen(result) > 2) { + result += 2; + + if (result[0] == 'R') { + resignation = true; + final_score = -1; + } else if (result[0] == 'T') { + timeout = true; + final_score = -1; + } else { + double f; + if (!parse_float(&f, result)) { + if (!illegal_final_score_warned) { + illegal_final_score_warned = true; + flog_warn("sgff", "illegal result format"); } + + finished = false; + goto after_game_result; } + final_score = (d32)(f * -2.0); + } + } + after_game_result: release(tmp); @@ -407,31 +387,33 @@ after_game_result: Handicap stones */ char * hs = strstr(buf, "AB["); - if (hs != NULL) + if (hs != NULL) { while (1) { hs += 2; - if (hs[0] == '[' && hs[3] == ']') - { + + if (hs[0] == '[' && hs[3] == ']') { u8 x = hs[1] - 'a'; u8 y = hs[2] - 'a'; - if (x >= BOARD_SIZ || y >= BOARD_SIZ) - { + + if (x >= BOARD_SIZ || y >= BOARD_SIZ) { if (!illegal_handicap_placement_warned) { illegal_handicap_placement_warned = true; flog_warn("sgff", "handicap placement error (1)"); } return false; } - if (!add_handicap_stone(gr, coord_to_move(x, y))) - { + + if (!add_handicap_stone(gr, coord_to_move(x, y))) { flog_warn("sgff", "handicap placement error (2)"); return false; } + hs += 4; - } - else + } else { break; + } } + } /* Plays @@ -439,31 +421,31 @@ after_game_result: char * start = buf; char * token; char * save_ptr; - while (gr->turns < MAX_GAME_LENGTH && (token = strtok_r(start, ";)\n\r", - &save_ptr)) != NULL) { - if (start != NULL) + while (gr->turns < MAX_GAME_LENGTH && (token = strtok_r(start, ";)\n\r",&save_ptr)) != NULL) { + if (start != NULL) { start = NULL; + } + size_t token_len = strlen(token); - if ((token_len >= 3) && (token[0] == 'B' || token[0] == 'W') && token[1] - == '[') { + if ((token_len >= 3) && (token[0] == 'B' || token[0] == 'W') && token[1] == '[') { bool is_black = (token[0] == 'B'); - if (token[2] == ']') + if (token[2] == ']') { add_play_out_of_order(gr, is_black, PASS); - else - if (token_len >= 5 && token[4] == ']') - { - u8 x = token[2] - 'a'; - u8 y = token[3] - 'a'; - if (x >= BOARD_SIZ || y >= BOARD_SIZ) - { - if (!illegal_stone_placement_warned) { - illegal_stone_placement_warned = true; - flog_warn("sgff", "play coordinate illegal"); - } - return false; + } else if (token_len >= 5 && token[4] == ']') { + u8 x = token[2] - 'a'; + u8 y = token[3] - 'a'; + + if (x >= BOARD_SIZ || y >= BOARD_SIZ) { + if (!illegal_stone_placement_warned) { + illegal_stone_placement_warned = true; + flog_warn("sgff", "play coordinate illegal"); } - add_play_out_of_order(gr, is_black, coord_to_move(x, y)); + + return false; } + + add_play_out_of_order(gr, is_black, coord_to_move(x, y)); + } } } -- 2.11.4.GIT