2 * Program: pgn-extract: a Portable Game Notation (PGN) extractor.
3 * Copyright (C) 1994-2014 David Barnes
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 1, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * David Barnes may be contacted as D.J.Barnes@kent.ac.uk
19 * http://www.cs.kent.ac.uk/people/staff/djb/
41 /* Functions for outputting games in the required format. */
43 /* Define the width in which to print a CM move and move number. */
44 #define MOVE_NUMBER_WIDTH 3
46 #define CM_COMMENT_CHAR ';'
47 /* Define the width of the moves area before a comment. */
48 #define COMMENT_INDENT (MOVE_NUMBER_WIDTH+2+2*MOVE_WIDTH)
50 /* Define a macro to calculate an array's size. */
51 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(*arr))
53 /* How much text we have output on the current line. */
54 static size_t line_length = 0;
55 /* The buffer in which each output line of a game is built. */
56 static char *output_line = NULL;
58 static Boolean print_move(FILE *outputfile, unsigned move_number,
59 unsigned print_move_number, Boolean white_to_move,
60 const Move *move_details);
61 static void output_STR(FILE *outfp,char **Tags);
62 static void show_tags(FILE *outfp,char **Tags,int tags_length);
63 static char promoted_piece_letter(Piece piece);
64 static void print_algebraic_game(Game current_game,FILE *outputfile,
65 unsigned move_number,Boolean white_to_move,
67 static void output_sesse_bin_game(Game current_game,FILE *outputfile,
68 unsigned move_number,Boolean white_to_move);
69 static void print_epd_game(Game current_game,FILE *outputfile,
70 unsigned move_number,Boolean white_to_move,
72 static void print_epd_move_list(Game current_game,FILE *outputfile,
73 unsigned move_number, Boolean white_to_move,
75 static const char *build_FEN_comment(const Board *board);
76 static void add_total_plycount(const Game *game, Boolean count_variations);
77 static void add_hashcode_tag(const Game *game);
78 static unsigned count_single_move_ply(const Move *move_details, Boolean count_variations);
79 static unsigned count_move_list_ply(Move *move_list, Boolean count_variations);
81 /* List, the order in which the tags should be output.
82 * The first seven should be the Seven Tag Roster that should
83 * be present in every game.
84 * The order of the remainder is, I believe, a matter of taste.
85 * any PSEUDO_*_TAGs should not appear in this list.
88 /* Give the array an int type, because a negative value is
89 * used as a terminator.
91 static int DefaultTagOrder[] = {
92 EVENT_TAG, SITE_TAG, DATE_TAG, ROUND_TAG, WHITE_TAG, BLACK_TAG, RESULT_TAG,
94 /* @@@ Consider omitting some of these from the default ordering,
95 * and allow the output order to be determined from the
98 WHITE_TITLE_TAG, BLACK_TITLE_TAG, WHITE_ELO_TAG, BLACK_ELO_TAG,
99 WHITE_USCF_TAG, BLACK_USCF_TAG,
100 WHITE_TYPE_TAG, BLACK_TYPE_TAG,
101 WHITE_NA_TAG, BLACK_NA_TAG,
102 ECO_TAG, NIC_TAG, OPENING_TAG, VARIATION_TAG, SUB_VARIATION_TAG,
106 EVENT_DATE_TAG, EVENT_SPONSOR_TAG, SECTION_TAG, STAGE_TAG, BOARD_TAG,
107 TIME_TAG, UTC_DATE_TAG, UTC_TIME_TAG,
110 TERMINATION_TAG, MODE_TAG, PLY_COUNT_TAG,
112 /* The final value should be negative. */
116 /* Provision for a user-defined tag ordering.
117 * See add_to_output_tag_order().
118 * Once allocated, the end of the list must be negative.
120 static int *TagOrder = NULL;
121 static int tag_order_space = 0;
124 set_output_line_length(unsigned length)
126 if(output_line != NULL) {
127 (void) free((void *) output_line);
129 output_line = (char *) MallocOrDie(length + 1);
130 GlobalState.max_line_length = length;
133 /* Which output format does the user require, based upon the
134 * given command line argument?
137 which_output_format(const char *arg)
156 { "sessebin", SESSE_BIN },
158 /* Add others before the terminating NULL. */
159 { (const char *) NULL, SAN }
162 for(i = 0; formats[i].arg != NULL; i++){
163 const char *format_prefix = formats[i].arg;
164 const size_t format_prefix_len = strlen(format_prefix);
165 if(strncmp(arg,format_prefix,format_prefix_len) == 0){
166 OutputFormat format = formats[i].format;
168 if(*format_prefix == '\0' && *arg != '\0') {
169 fprintf(GlobalState.logfile,
170 "Unknown output format %s.\n", arg);
173 /* If the format is SAN, it is possible to supply
174 * a 6-piece suffix listing language-specific
175 * letters to use in the output.
177 if((format == SAN || format == ELALG) &&
178 (strlen(arg) > format_prefix_len)){
179 set_output_piece_characters(&arg[format_prefix_len]);
184 fprintf(GlobalState.logfile,"Unknown output format %s.\n",arg);
188 /* Which file suffix should be used for this output format. */
190 output_file_suffix(OutputFormat format)
192 /* Define a suffix for the output files. */
193 static const char PGN_suffix[] = ".pgn";
194 static const char EPD_suffix[] = ".epd";
195 static const char CM_suffix[] = ".cm";
196 static const char BIN_suffix[] = ".bin";
217 select_tag_string(TagName tag)
218 { const char *tag_string;
220 if((tag == PSEUDO_PLAYER_TAG) || (tag == PSEUDO_ELO_TAG) || (tag == PSEUDO_FEN_PATTERN_TAG)){
224 tag_string = tag_header_string(tag);
246 /* Output the tags held in the Tags structure.
247 * At least the full Seven Tag Roster is printed.
250 output_tag(TagName tag, char **Tags, FILE *outfp)
251 { const char *tag_string;
253 /* Must print STR elements and other non-NULL tags. */
254 if((is_STR(tag)) || (Tags[tag] != NULL)){
255 tag_string = select_tag_string(tag);
257 if(tag_string != NULL){
258 fprintf(outfp,"[%s \"",tag_string);
259 if(Tags[tag] != NULL){
260 fwrite(Tags[tag],sizeof(char),strlen(Tags[tag]),outfp);
264 fprintf(outfp,"????.??.??");
270 fprintf(outfp,"\"]\n");
275 /* Output the Seven Tag Roster. */
277 output_STR(FILE *outfp,char **Tags)
278 { unsigned tag_index;
280 /* Use the default ordering to ensure that STR is output
281 * in the way it should be.
283 for(tag_index = 0; tag_index < 7; tag_index++){
284 output_tag(DefaultTagOrder[tag_index],Tags,outfp);
288 /* Print out on outfp the current details.
289 * These can be used in the case of an error.
292 show_tags(FILE *outfp,char **Tags,int tags_length)
294 /* Take a copy of the Tags data, so that we can keep
295 * track of what has been printed. This will make
296 * it possible to print tags that were identified
297 * in the source but are not defined with _TAG values.
298 * See lex.c for how these extra tags are handled.
300 char **copy_of_tags =
301 (char **) MallocOrDie(tags_length*sizeof(*copy_of_tags));
303 for(i = 0; i < tags_length; i++){
304 copy_of_tags[i] = Tags[i];
307 /* Ensure that a tag ordering is available. */
308 if(TagOrder == NULL){
309 /* None set by the user - use the default. */
310 /* Handle the standard tags.
311 * The end of the list is marked with a negative value.
313 for(tag_index = 0; DefaultTagOrder[tag_index] >= 0; tag_index++){
314 TagName tag = DefaultTagOrder[tag_index];
315 output_tag(tag,copy_of_tags,outfp);
316 copy_of_tags[tag] = (char *)NULL;
318 /* Handle the extra tags. */
319 for(tag_index = 0; tag_index < tags_length; tag_index++){
320 if(copy_of_tags[tag_index] != NULL){
321 output_tag(tag_index,copy_of_tags,outfp);
326 for(tag_index = 0; TagOrder[tag_index] >= 0; tag_index++){
327 TagName tag = TagOrder[tag_index];
328 output_tag(tag,copy_of_tags,outfp);
329 copy_of_tags[tag] = (char *)NULL;
332 (void) free(copy_of_tags);
336 /* Ensure that there is room for len more characters on the
340 check_line_length(FILE *fp,size_t len)
342 if((line_length + len) > GlobalState.max_line_length){
347 /* Print ch to fp and update how much of the line
348 * has been printed on.
351 single_char(FILE *fp, char ch)
353 check_line_length(fp,1);
354 output_line[line_length] = ch;
358 /* Print a space, unless at the beginning of a line. */
360 print_separator(FILE *fp)
362 /* Lines shouldn't have trailing spaces, so ensure that there
363 * will be room for at least one more character after the space.
365 check_line_length(fp,2);
366 if(line_length != 0){
367 output_line[line_length] = ' ';
372 /* Ensure that what comes next starts on a fresh line. */
374 terminate_line(FILE *fp)
376 /* Delete any trailing space(s). */
377 while(line_length >= 1 && output_line[line_length - 1] == ' ') {
380 if(line_length > 0) {
381 output_line[line_length] = '\0';
382 fprintf(fp, "%s\n", output_line);
387 /* Print str to fp and update how much of the line
388 * has been printed on.
391 print_str(FILE *fp, const char *str)
392 { size_t len = strlen(str);
394 check_line_length(fp,len);
395 if(len > GlobalState.max_line_length) {
396 fprintf(GlobalState.logfile, "String length %lu is too long for the line length of %lu:\n",
398 (unsigned long) GlobalState.max_line_length);
399 fprintf(GlobalState.logfile, "%s\n", str);
400 report_details(GlobalState.logfile);
401 fprintf(fp, "%s\n", str);
404 sprintf(&(output_line[line_length]),"%s",str);
410 print_comment_list(FILE *fp, CommentList *comment_list)
411 { CommentList *next_comment;
413 for(next_comment = comment_list ; next_comment != NULL;
414 next_comment = next_comment->next){
415 StringList *comment = next_comment->Comment;
418 /* We will use strtok to break up the comment string,
419 * with chunk to point to each bit in turn.
424 for( ; comment != NULL; comment = comment->next){
425 /* Make a copy because the string is altered. */
426 char *str = copy_string(comment->str);
427 chunk = strtok(str," ");
428 while(chunk != NULL){
431 chunk = strtok((char *)NULL," ");
433 (void) free((void *) str);
437 if(next_comment->next != NULL){
445 print_move_list(FILE *outputfile, unsigned move_number, Boolean white_to_move,
446 const Move *move_details,const Board *final_board)
447 { unsigned print_move_number = 1;
448 const Move *move = move_details;
449 Boolean keepPrinting;
452 /* Work out the ply depth. */
453 plies = 2*(move_number) - 1;
457 if(GlobalState.output_ply_limit >= 0 &&
458 plies > GlobalState.output_ply_limit) {
459 keepPrinting = FALSE;
465 while(move != NULL && keepPrinting){
466 /* Reset print_move number if a variation was printed. */
467 print_move_number = print_move(outputfile,move_number,
471 /* See if there is a result attached. This may be attached either
472 * to a move or a comment.
474 if(!GlobalState.check_only && (move != NULL) &&
475 (move->terminating_result != NULL)){
476 if(GlobalState.output_FEN_string && final_board != NULL){
477 print_separator(outputfile);
478 print_str(outputfile,build_FEN_comment(final_board));
480 if(GlobalState.keep_results) {
481 print_separator(outputfile);
482 print_str(outputfile,move->terminating_result);
485 if(move->move[0] != '\0'){
486 /* A genuine move was just printed, rather than a comment. */
488 white_to_move = FALSE;
492 white_to_move = TRUE;
495 if(GlobalState.output_ply_limit >= 0 &&
496 plies > GlobalState.output_ply_limit) {
497 keepPrinting = FALSE;
501 /* The following is slightly inaccurate.
502 * If the previous value of move was a comment and
503 * we aren't printing comments, then this results in two
504 * separators being printed after the move preceding the comment.
505 * Not sure how to cleanly fix it, because there might have
506 * been nags attached to the comment that were printed, for instance!
508 if(move != NULL && keepPrinting){
509 print_separator(outputfile);
513 if(move != NULL && !keepPrinting) {
514 /* We ended printing the game prematurely. */
517 * Decide whether to print a result indicator.
519 if(GlobalState.keep_results) {
520 /* Find the final move to see if there was a result there. */
521 while(move->next != NULL) {
524 if(move->terminating_result != NULL) {
525 print_separator(outputfile);
526 print_str(outputfile, "*");
532 /* Output the current move along with associated information.
533 * Return TRUE if either a variation or comment was printed,
535 * This is needed to determine whether a new move number
536 * is to be printed after a variation.
539 print_move(FILE *outputfile, unsigned move_number, unsigned print_move_number,
540 Boolean white_to_move, const Move *move_details)
541 { Boolean something_printed = FALSE;
543 if(move_details == NULL){
544 /* Shouldn't happen. */
545 fprintf(GlobalState.logfile,
546 "Internal error: NULL move in print_move.\n");
547 report_details(GlobalState.logfile);
550 if(GlobalState.check_only){
551 /* Nothing to be output. */
554 StringList *nags = move_details->Nags;
555 Variation *variants = move_details->Variants;
556 const unsigned char *move_text = move_details->move;
558 if(*move_text != '\0'){
559 if(GlobalState.keep_move_numbers &&
560 (white_to_move || print_move_number)){
561 static char small_number[] = "99999...";
563 /* @@@ Should 1... be written as 1. ... ? */
564 sprintf(small_number,"%u.%s",move_number, white_to_move?"":"..");
565 print_str(outputfile,small_number);
566 print_separator(outputfile);
568 switch(GlobalState.output_format){
571 /* @@@ move_text should be handled as unsigned
572 * char text, as the source may be 8-bit rather
575 if(GlobalState.keep_checks) {
576 print_str(outputfile, (const char *) move_text);
579 /* Look for a check or mate symbol. */
580 char *check = strchr((const char *) move_text, '+');
582 check = strchr((const char *) move_text, '#');
585 /* We need to drop it from move_text. */
586 int len = check - ((char *) move_text);
587 char *just_move = (char *) MallocOrDie(len + 1);
588 strncpy(just_move, (const char *) move_text, len);
589 just_move[len] = '\0';
590 print_str(outputfile, just_move);
591 (void) free(just_move);
594 print_str(outputfile, (const char *) move_text);
599 { char algebraic[MAX_MOVE_LEN+1];
602 switch(move_details->class){
604 case ENPASSANT_PAWN_MOVE:
605 case KINGSIDE_CASTLE:
606 case QUEENSIDE_CASTLE:
608 sprintf(algebraic,"%c%c-%c%c",
609 move_details->from_col,
610 move_details->from_rank,
611 move_details->to_col,
612 move_details->to_rank);
614 case PAWN_MOVE_WITH_PROMOTION:
615 sprintf(algebraic,"%c%c-%c%c%c",
616 move_details->from_col,
617 move_details->from_rank,
618 move_details->to_col,
619 move_details->to_rank,
620 promoted_piece_letter(move_details->promoted_piece));
623 strcpy(algebraic, NULL_MOVE_STRING);
626 strcpy(algebraic,"???");
629 if(GlobalState.keep_checks) {
630 switch(move_details->check_status){
634 strcat(algebraic,"+");
637 strcat(algebraic,"#");
641 print_str(outputfile,algebraic);
646 { char algebraic[MAX_MOVE_LEN+1];
649 /* Prefix with a piece name if ELALG. */
650 if(GlobalState.output_format == ELALG &&
651 move_details->class == PIECE_MOVE){
653 piece_str(move_details->piece_to_move));
654 ind = strlen(algebraic);
656 /* Format the basics. */
657 if(move_details->class != NULL_MOVE) {
658 sprintf(&algebraic[ind],"%c%c%c%c",
659 move_details->from_col,
660 move_details->from_rank,
661 move_details->to_col,
662 move_details->to_rank);
666 strcpy(algebraic, NULL_MOVE_STRING);
667 ind += strlen(NULL_MOVE_STRING);
669 switch(move_details->class){
671 case KINGSIDE_CASTLE:
672 case QUEENSIDE_CASTLE:
675 /* Nothing more to do at this stage. */
677 case ENPASSANT_PAWN_MOVE:
678 if(GlobalState.output_format == ELALG){
679 strcat(algebraic,"ep");
683 case PAWN_MOVE_WITH_PROMOTION:
684 sprintf(&algebraic[ind],"%s",
685 piece_str(move_details->promoted_piece));
686 ind = strlen(algebraic);
689 strcpy(algebraic,"???");
693 if(GlobalState.keep_checks) {
694 switch(move_details->check_status){
698 strcat(algebraic,"+");
702 strcat(algebraic,"#");
707 print_str(outputfile,algebraic);
711 fprintf(GlobalState.logfile,
712 "Unknown output format %d in print_move()\n",
713 GlobalState.output_format);
720 fprintf(GlobalState.logfile,
721 "Internal error: Empty move in print_move.\n");
722 report_details(GlobalState.logfile);
724 /* Print further information, that may be attached to moves
727 if(GlobalState.keep_NAGs){
729 print_separator(outputfile);
730 print_str(outputfile,nags->str);
733 /* We don't need to output move numbers after just
734 * NAGs, so don't set something_printed.
737 if(GlobalState.keep_comments){
738 if(move_details->Comment != NULL){
739 print_separator(outputfile);
740 print_comment_list(outputfile,move_details->Comment);
741 something_printed = TRUE;
744 if((GlobalState.keep_variations) && (variants != NULL)){
745 while(variants != NULL){
746 print_separator(outputfile);
747 single_char(outputfile,'(');
748 if(GlobalState.keep_comments &&
749 (variants->prefix_comment != NULL)){
750 print_comment_list(outputfile,variants->prefix_comment);
751 print_separator(outputfile);
753 /* Always start with a move number.
754 * The final board position is not needed.
756 print_move_list(outputfile,move_number,
757 white_to_move,variants->moves,
758 (const Board *)NULL);
759 single_char(outputfile,')');
760 if(GlobalState.keep_comments &&
761 (variants->suffix_comment != NULL)){
762 print_separator(outputfile);
763 print_comment_list(outputfile,variants->suffix_comment);
765 variants = variants->next;
767 something_printed = TRUE;
771 return something_printed;
774 /* Return the letter associated with the given piece. */
776 promoted_piece_letter(Piece piece)
792 /* Output a comment in CM format. */
794 output_cm_comment(CommentList *comment,FILE *outputfile,unsigned indent)
795 { /* Don't indent for the first comment line, because
796 * we should already be positioned at the correct spot.
798 unsigned indent_for_this_line = 0;
800 putc(CM_COMMENT_CHAR,outputfile);
802 while(comment != NULL){
803 /* We will use strtok to break up the comment string,
804 * with chunk to point to each bit in turn.
807 StringList *comment_str = comment->Comment;
809 for( ; comment_str != NULL; comment_str = comment_str->next){
810 char *str = copy_string(comment_str->str);
811 chunk = strtok(str," ");
812 while(chunk != NULL){
813 size_t len = strlen(chunk);
815 if((line_length+1+len) > GlobalState.max_line_length){
816 /* Start a new line. */
817 indent_for_this_line = indent;
818 fprintf(outputfile,"\n%*s%c ",indent_for_this_line,"",
820 line_length = indent_for_this_line+2;
823 putc(' ',outputfile);
826 fprintf(outputfile,"%s",chunk);
828 chunk = strtok((char *)NULL," ");
830 (void) free((void *) str);
832 comment = comment->next;
834 putc('\n',outputfile);
839 output_cm_result(const char *result,FILE *outputfile)
841 fprintf(outputfile,"%c ",CM_COMMENT_CHAR);
842 if(strcmp(result,"1-0") == 0){
843 fprintf(outputfile,"and black resigns");
845 else if(strcmp(result,"0-1") == 0){
846 fprintf(outputfile,"and white resigns");
848 else if(strncmp(result,"1/2",3) == 0){
849 fprintf(outputfile,"draw");
852 fprintf(outputfile,"incomplete result");
857 /* Output the game in Chess Master format. */
859 output_cm_game(FILE *outputfile,unsigned move_number,
860 Boolean white_to_move,const Game game)
861 { const Move *move = game.moves;
863 if((move_number != 1) || (!white_to_move)){
864 fprintf(GlobalState.logfile,
865 "Unable to output CM games other than from the starting position.\n");
866 report_details(GlobalState.logfile);
868 fprintf(outputfile,"WHITE: %s\n",
869 game.tags[WHITE_TAG] != NULL? game.tags[WHITE_TAG] : "");
870 fprintf(outputfile,"BLACK: %s\n",
871 game.tags[BLACK_TAG] != NULL? game.tags[BLACK_TAG] : "");
872 putc('\n',outputfile);
874 if(game.prefix_comment != NULL){
876 output_cm_comment(game.prefix_comment,outputfile,0);
879 if(move->move[0] != '\0'){
880 /* A genuine move. */
882 fprintf(outputfile,"%*u. ",MOVE_NUMBER_WIDTH,move_number);
883 fprintf(outputfile,"%*s",-MOVE_WIDTH,move->move);
884 white_to_move = FALSE;
887 fprintf(outputfile,"%*s",-MOVE_WIDTH,move->move);
889 white_to_move = TRUE;
892 if((move->Comment != NULL) && GlobalState.keep_comments){
893 const char *result = move->terminating_result;
896 fprintf(outputfile,"%*s",-MOVE_WIDTH,"...");
898 line_length = COMMENT_INDENT;
899 output_cm_comment(move->Comment,outputfile,COMMENT_INDENT);
900 if((result != NULL) && (move->check_status != CHECKMATE)){
901 /* Give some information on the nature of the finish. */
903 fprintf(outputfile,"%*s",COMMENT_INDENT,"");
906 /* Print out a string representing the result. */
907 fprintf(outputfile,"%*s %*s%*s",
908 MOVE_NUMBER_WIDTH+1,"",-MOVE_WIDTH,"...",
911 output_cm_result(result,outputfile);
912 putc('\n',outputfile);
916 /* Indicate that the next move is Black's. */
917 fprintf(outputfile,"%*s %*s",
918 MOVE_NUMBER_WIDTH+1,"",-MOVE_WIDTH,"...");
923 if((move->terminating_result != NULL) &&
924 (move->check_status != CHECKMATE)){
925 /* Give some information on the nature of the finish. */
926 const char *result = move->terminating_result;
929 fprintf(outputfile,"%*s",-MOVE_WIDTH,"...");
931 output_cm_result(result,outputfile);
933 putc('\n',outputfile);
934 fprintf(outputfile,"%*s %*s",
935 MOVE_NUMBER_WIDTH+1,"",-MOVE_WIDTH,"...");
937 putc('\n',outputfile);
941 /* Terminate the move pair. */
942 putc('\n',outputfile);
948 putc('\n',outputfile);
951 /* Output the current game according to the required output format. */
953 output_game(Game current_game,FILE *outputfile)
954 { Boolean white_to_move = TRUE;
955 unsigned move_number = 1;
956 /* The final board position, if available. */
957 Board *final_board = NULL;
959 /* If we aren't starting from the initial setup, then we
960 * need to know the current move number and whose
963 if(current_game.tags[FEN_TAG] != NULL){
964 Board *board = new_fen_board(current_game.tags[FEN_TAG]);
967 move_number = board->move_number;
968 white_to_move = board->to_move == WHITE;
969 (void) free((void *)board);
973 /* Start at the beginning of a line. */
975 /* See if the moves should be rewritten into
978 if(GlobalState.output_format != SOURCE){
979 /* Rewrite the moves of the game into
980 * SAN (Standard Algebraic Notation).
982 final_board = rewrite_game(¤t_game);
984 if(final_board != NULL) {
985 if(GlobalState.output_total_plycount) {
986 add_total_plycount(¤t_game, GlobalState.keep_variations);
988 if(GlobalState.add_hashcode_tag || current_game.tags[HASHCODE_TAG] != NULL) {
989 add_hashcode_tag(¤t_game);
991 switch(GlobalState.output_format){
997 print_algebraic_game(current_game,outputfile,move_number,white_to_move,
1001 print_epd_game(current_game,outputfile,move_number,white_to_move,
1005 output_cm_game(outputfile,move_number,white_to_move,current_game);
1008 output_sesse_bin_game(current_game,outputfile,move_number,white_to_move);
1011 fprintf(GlobalState.logfile,
1012 "Internal error: unknown output type %d in output_game().\n",
1013 GlobalState.output_format);
1017 (void) free((void *) final_board);
1021 /* Add the given tag to the output ordering. */
1023 add_to_output_tag_order(TagName tag)
1026 if(TagOrder == NULL){
1027 tag_order_space = ARRAY_SIZE(DefaultTagOrder);
1028 TagOrder = (int *) MallocOrDie(tag_order_space*sizeof(*TagOrder));
1029 /* Always ensure that there is a negative value at the end. */
1032 /* Check to ensure a position has not already been indicated
1035 for(tag_index = 0; (TagOrder[tag_index] != -1) &&
1036 (TagOrder[tag_index] != (int) tag); tag_index++){
1039 if(TagOrder[tag_index] == -1){
1040 /* Make sure there is enough space for another. */
1041 if(tag_index >= tag_order_space){
1042 /* Allocate some more. */
1043 tag_order_space += 10;
1044 TagOrder = (int *) ReallocOrDie((void *)TagOrder,
1045 tag_order_space*sizeof(*TagOrder));
1047 TagOrder[tag_index] = tag;
1048 TagOrder[tag_index+1] = -1;
1051 fprintf(GlobalState.logfile,"Duplicate position for tag: %s\n",
1052 select_tag_string(tag));
1056 static const char *format_epd_game_comment(char **Tags)
1058 static char comment_prefix[] = "c0 ";
1059 static char player_separator[] = "-";
1060 static size_t prefix_and_separator_len =
1061 sizeof(comment_prefix)+sizeof(player_separator);
1062 size_t space_needed = prefix_and_separator_len;
1065 if(Tags[WHITE_TAG] != NULL){
1066 space_needed += strlen(Tags[WHITE_TAG]);
1068 if(Tags[BLACK_TAG] != NULL){
1069 space_needed += strlen(Tags[BLACK_TAG]);
1071 /* Allow a space character before each of the remaining tags. */
1072 if(Tags[EVENT_TAG] != NULL){
1073 space_needed += 1+strlen(Tags[EVENT_TAG]);
1075 if(Tags[SITE_TAG] != NULL){
1076 space_needed += 1+strlen(Tags[SITE_TAG]);
1078 if(Tags[DATE_TAG] != NULL){
1079 space_needed += 1+strlen(Tags[DATE_TAG]);
1081 /* Allow for a terminating semicolon. */
1084 comment = (char *) MallocOrDie(space_needed+1);
1086 strcpy(comment,comment_prefix);
1087 if(Tags[WHITE_TAG] != NULL){
1088 strcat(comment,Tags[WHITE_TAG]);
1090 strcat(comment,player_separator);
1091 if(Tags[BLACK_TAG] != NULL){
1092 strcat(comment,Tags[BLACK_TAG]);
1094 if(Tags[EVENT_TAG] != NULL){
1095 strcat(comment," ");
1096 strcat(comment,Tags[EVENT_TAG]);
1098 if(Tags[SITE_TAG] != NULL){
1099 strcat(comment," ");
1100 strcat(comment,Tags[SITE_TAG]);
1102 if(Tags[DATE_TAG] != NULL){
1103 strcat(comment," ");
1104 strcat(comment,Tags[DATE_TAG]);
1106 strcat(comment,";");
1107 if(strlen(comment) >= space_needed){
1108 fprintf(GlobalState.logfile,
1109 "Internal error: overflow in format_epd_game_comment\n");
1115 print_algebraic_game(Game current_game,FILE *outputfile,
1116 unsigned move_number,Boolean white_to_move,
1119 /* Report details on the output. */
1120 if(GlobalState.tag_output_format == ALL_TAGS) {
1121 show_tags(outputfile,current_game.tags,current_game.tags_length);
1123 else if(GlobalState.tag_output_format == SEVEN_TAG_ROSTER){
1124 output_STR(outputfile,current_game.tags);
1125 if(GlobalState.add_ECO && !GlobalState.parsing_ECO_file){
1126 /* If ECO classification has been requested, then assume
1127 * that ECO tags are also required.
1129 output_tag(ECO_TAG,current_game.tags,outputfile);
1130 output_tag(OPENING_TAG,current_game.tags,outputfile);
1131 output_tag(VARIATION_TAG,current_game.tags,outputfile);
1132 output_tag(SUB_VARIATION_TAG,current_game.tags,outputfile);
1135 /* Output any FEN that there might be. */
1136 output_tag(FEN_TAG,current_game.tags,outputfile);
1137 putc('\n',outputfile);
1139 else if(GlobalState.tag_output_format == NO_TAGS) {
1142 fprintf(GlobalState.logfile,
1143 "Unknown output form for tags: %d\n",
1144 GlobalState.tag_output_format);
1147 if((GlobalState.keep_comments) &&
1148 (current_game.prefix_comment != NULL)){
1149 print_comment_list(outputfile,
1150 current_game.prefix_comment);
1151 terminate_line(outputfile);
1152 putc('\n',outputfile);
1154 print_move_list(outputfile,move_number,white_to_move,
1155 current_game.moves,final_board);
1156 /* Take account of a possible zero move game. */
1157 if(current_game.moves == NULL) {
1158 if(current_game.tags[RESULT_TAG] != NULL) {
1159 print_str(outputfile,current_game.tags[RESULT_TAG]);
1162 fprintf(GlobalState.logfile,
1163 "Internal error: Zero move game with no result\n");
1166 terminate_line(outputfile);
1167 putc('\n',outputfile);
1171 output_sesse_bin_game(Game current_game,FILE *outputfile,
1172 unsigned move_number,Boolean white_to_move)
1174 const char *result = NULL;
1177 // Find the result. Skip games with no result.
1178 for (move = current_game.moves; move != NULL; move = move->next) {
1179 if (move->terminating_result) {
1180 result = move->terminating_result;
1183 if (result == NULL || strcmp(result, "*") == 0) {
1187 int result_int = -1;
1188 if (strcmp(result, "1-0") == 0) {
1190 } else if (strcmp(result, "1/2-1/2") == 0) {
1192 } else if (strcmp(result, "0-1") == 0) {
1195 fprintf(stderr, "Unknown result '%s'\n", result);
1199 // Find Black and White Elos. Skip games with no Elo.
1200 const char *white_elo_tag = current_game.tags[WHITE_ELO_TAG];
1201 const char *black_elo_tag = current_game.tags[BLACK_ELO_TAG];
1202 if (white_elo_tag == NULL || black_elo_tag == NULL) {
1206 int white_elo = atoi(white_elo_tag);
1207 int black_elo = atoi(black_elo_tag);
1208 int file_num = current_game.file_number;
1209 long start_position = current_game.start_position;
1211 // Parse date and time, if it exists. Set invalid dates to year 3000.
1212 const char *date_tag = current_game.tags[DATE_TAG];
1213 const char *time_tag = current_game.tags[TIME_TAG];
1216 int year, month, day;
1217 if (date_tag && sscanf(date_tag, "%u.%u.%u", &year, &month, &day) == 3) {
1218 int hour, minute, second;
1219 tm.tm_year = year - 1900;
1220 tm.tm_mon = month - 1;
1223 if (time_tag && sscanf(time_tag, "%u:%u:%u", &hour, &minute, &second) == 3) {
1228 timestamp = mktime(&tm);
1230 timestamp = 32503680000;
1233 for (move = current_game.moves; move != NULL; move = move->next) {
1234 unsigned int opening = move->eco ? move->eco->cumulative_hash_value : 0; // Truncate to 32 bits.
1237 putc(move->bpfen_len, outputfile);
1238 fwrite(move->bpfen, move->bpfen_len, 1, outputfile);
1241 putc(result_int, outputfile);
1242 fwrite(&white_elo, sizeof(white_elo), 1, outputfile);
1243 fwrite(&black_elo, sizeof(black_elo), 1, outputfile);
1244 fwrite(&opening, sizeof(opening), 1, outputfile);
1245 fwrite(×tamp, sizeof(timestamp), 1, outputfile);
1246 fwrite(&file_num, sizeof(file_num), 1, outputfile);
1247 fwrite(&start_position, sizeof(start_position), 1, outputfile);
1248 putc(strlen((char *)move->move), outputfile);
1249 fwrite(move->move, strlen((char *)move->move), 1, outputfile);
1254 print_epd_move_list(Game current_game,FILE *outputfile,
1255 unsigned move_number, Boolean white_to_move,
1257 { const Move *move = current_game.moves;
1258 const char *game_comment = format_epd_game_comment(current_game.tags);
1259 static char epd[FEN_SPACE];
1261 while(move != NULL){
1262 if(move->epd != NULL){
1263 fprintf(outputfile,"%s",move->epd);
1266 fprintf(GlobalState.logfile,"Internal error: Missing EPD\n");
1268 fprintf(outputfile," %s",game_comment);
1269 putc('\n',outputfile);
1272 if(final_board != NULL) {
1273 build_basic_EPD_string(final_board,epd);
1274 fprintf(outputfile,"%s %s",epd,game_comment);
1275 putc('\n',outputfile);
1277 (void) free((void *)game_comment);
1281 print_epd_game(Game current_game,FILE *outputfile,
1282 unsigned move_number,Boolean white_to_move,
1285 if(!GlobalState.check_only){
1286 print_epd_move_list(current_game,outputfile,move_number,white_to_move,
1288 putc('\n',outputfile);
1293 * Build a comment containing a FEN representation of the board.
1294 * NB: This function re-uses static space each time it is called
1295 * so a later call will overwrite an earlier call.
1298 build_FEN_comment(const Board *board)
1300 static char fen[FEN_SPACE];
1302 build_FEN_string(board,&fen[strlen(fen)]);
1304 /* A belated sanity check. */
1305 if(strlen(fen) >= FEN_SPACE){
1306 fprintf(GlobalState.logfile,
1307 "Internal error: string overflow in build_FEN_comment.\n");
1313 * Count how many ply recorded for the given move.
1314 * Include variations if count_variations.
1316 static unsigned count_single_move_ply(const Move *move_details, Boolean count_variations)
1319 if(count_variations) {
1320 Variation *variant = move_details->Variants;
1321 while(variant != NULL) {
1322 count += count_move_list_ply(variant->moves, count_variations);
1323 variant = variant->next;
1330 * Count how many plies in the game in total.
1331 * Include variations if count_variations.
1333 static unsigned count_move_list_ply(Move *move_list, Boolean count_variations)
1336 while(move_list != NULL) {
1337 count += count_single_move_ply(move_list, count_variations);
1338 move_list = move_list->next;
1344 * Count how many plies in the game in total.
1345 * Include variations if count_variations.
1347 static void add_total_plycount(const Game *game, Boolean count_variations)
1349 unsigned count = count_move_list_ply(game->moves, count_variations);
1350 char formatted_count[20];
1351 sprintf(formatted_count, "%u", count);
1353 if(game->tags[TOTAL_PLY_COUNT_TAG] != NULL) {
1354 (void) free(game->tags[TOTAL_PLY_COUNT_TAG]);
1356 game->tags[TOTAL_PLY_COUNT_TAG] = copy_string(formatted_count);
1360 * Include a tag containing a hashcode for the game.
1361 * Use the cumulative hash value.
1363 static void add_hashcode_tag(const Game *game)
1365 HashCode hashcode = game->cumulative_hash_value;
1366 char formatted_code[20];
1367 sprintf(formatted_code, "%08x", (unsigned) hashcode);
1369 if(game->tags[HASHCODE_TAG] != NULL) {
1370 (void) free(game->tags[HASHCODE_TAG]);
1372 game->tags[HASHCODE_TAG] = copy_string(formatted_code);