]> git.sesse.net Git - pgn-extract/blobdiff - output.c
Add support for outputting positions in my own bit-packed FEN format.
[pgn-extract] / output.c
index 2878b00a3b195165c2ef6c38f51246d5f6e4a55a..6e47275ca8842e32106a35d3b0117d95230c6975 100644 (file)
--- a/output.c
+++ b/output.c
@@ -62,6 +62,8 @@ static char promoted_piece_letter(Piece piece);
 static void print_algebraic_game(Game current_game,FILE *outputfile,
                            unsigned move_number,Boolean white_to_move,
                            Board *final_board);
+static void output_sesse_bin_game(Game current_game,FILE *outputfile,
+                                  unsigned move_number,Boolean white_to_move);
 static void print_epd_game(Game current_game,FILE *outputfile,
                            unsigned move_number,Boolean white_to_move,
                            Board *final_board);
@@ -149,6 +151,7 @@ which_output_format(const char *arg)
         { "elalg", ELALG },
         { "uci", UCI },
         { "cm", CM },
+        { "sessebin", SESSE_BIN },
         { "", SOURCE },
         /* Add others before the terminating NULL. */
         { (const char *) NULL, SAN }
@@ -188,6 +191,7 @@ output_file_suffix(OutputFormat format)
     static const char PGN_suffix[] = ".pgn";
     static const char EPD_suffix[] = ".epd";
     static const char CM_suffix[] = ".cm";
+    static const char BIN_suffix[] = ".bin";
 
     switch(format){
         case SOURCE:
@@ -200,6 +204,8 @@ output_file_suffix(OutputFormat format)
             return EPD_suffix;
         case CM:
             return CM_suffix;
+        case SESSE_BIN:
+            return BIN_suffix;
         default:
             return PGN_suffix;
     }
@@ -996,6 +1002,9 @@ output_game(Game current_game,FILE *outputfile)
            case CM:
                output_cm_game(outputfile,move_number,white_to_move,current_game);
                break;
+           case SESSE_BIN:
+               output_sesse_bin_game(current_game,outputfile,move_number,white_to_move);
+               break;
            default:
                fprintf(GlobalState.logfile,
                            "Internal error: unknown output type %d in output_game().\n",
@@ -1156,6 +1165,61 @@ print_algebraic_game(Game current_game,FILE *outputfile,
     putc('\n',outputfile);
 }
 
+static void
+output_sesse_bin_game(Game current_game,FILE *outputfile,
+                      unsigned move_number,Boolean white_to_move)
+{
+    const char *result = NULL;
+    Move *move;
+
+    // Find the result. Skip games with no result.
+    for (move = current_game.moves; move != NULL; move = move->next) {
+        if (move->terminating_result) {
+            result = move->terminating_result;
+        }
+    }
+    if (result == NULL || strcmp(result, "*") == 0) {
+        return;
+    }
+
+    int result_int = -1;
+    if (strcmp(result, "1-0") == 0) {
+        result_int = 0;
+    } else if (strcmp(result, "1/2-1/2") == 0) {
+        result_int = 1;
+    } else if (strcmp(result, "0-1") == 0) {
+        result_int = 2;
+    } else {
+        fprintf(stderr, "Unknown result '%s'\n", result);
+        return;
+    }
+
+    // Find Black and White Elos. Skip games with no Elo.
+    const char *white_elo_tag = current_game.tags[WHITE_ELO_TAG];
+    const char *black_elo_tag = current_game.tags[BLACK_ELO_TAG];
+    if (white_elo_tag == NULL || black_elo_tag == NULL) {
+        return;
+    }
+
+    int white_elo = atoi(white_elo_tag);
+    int black_elo = atoi(black_elo_tag);
+
+    for (move = current_game.moves; move != NULL; move = move->next) {
+        unsigned int opening = 0;  // TODO
+
+        // key
+        putc(move->bpfen_len + strlen((char *)move->move), outputfile);
+        fwrite(move->bpfen, move->bpfen_len, 1, outputfile);
+        fwrite(move->move, strlen((char *)move->move), 1, outputfile);
+
+        // value
+        putc(result_int, outputfile);
+        fwrite(&white_elo, sizeof(white_elo), 1, outputfile);
+        fwrite(&black_elo, sizeof(black_elo), 1, outputfile);
+        fwrite(&opening, sizeof(opening), 1, outputfile);
+    }
+}
+
 static void
 print_epd_move_list(Game current_game,FILE *outputfile,
                     unsigned move_number, Boolean white_to_move,