Subversion Repositories Games.Chess Giants

Rev

Rev 119 | Rev 150 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // pgnfile.cpp
  2.  
  3. #include "common.h"
  4.  
  5.  
  6. // handy definitions
  7. #define COPY_TIL_LAST_QUOTEMARK(dest,source) \
  8. { \
  9.    mbstowcs_s (&converted_count, (dest), WCHAR_SIZEOF (dest), (source), WCHAR_SIZEOF (dest)); \
  10.    for (char_index = wcslen (dest) - 1; char_index >= 0; char_index--) \
  11.       if ((dest)[char_index] == L'"') \
  12.       { \
  13.          (dest)[char_index] = 0; \
  14.          break; \
  15.       } \
  16. }
  17.  
  18.  
  19. // global variables used in this module only
  20. static char *pgnfile_data = NULL; // mallocated
  21. static size_t pgnfile_size = 0;
  22.  
  23.  
  24. // prototypes of local functions
  25. static void PGNFile_GameList_Init (int entry_count);
  26. static void PGNFile_GameList_Shutdown (void);
  27. static wchar_t *PGNFile_NAGTextFromCode (int nag_code);
  28. static char *sgets (char *destination_line, int max_length, char *source_buffer);
  29.  
  30.  
  31. bool PGNFile_Load (const wchar_t *pgnfile_pathname)
  32. {
  33.    // this function loads a PGN file and builds the game databases of the games described in this file
  34.  
  35.    char line_buffer[256]; // PGN files have 256 chars max per line by design
  36.    char *buffer;
  37.    char *ptr;
  38.    int file_index;
  39.    int char_index;
  40.    int entry_count;
  41.    FILE *fp;
  42.    size_t converted_count; // used by the STRING_TO_CHAR macro
  43.  
  44.    // try to open file for reading in BINARY mode so as NOT to convert end of lines
  45.    _wfopen_s (&fp, pgnfile_pathname, L"rb");
  46.    if (fp == NULL)
  47.       return (false); // on error, cancel
  48.  
  49.    // get file length
  50.    fseek (fp, 0, SEEK_END);
  51.    pgnfile_size = ftell (fp);
  52.    fseek (fp, 0, SEEK_SET);
  53.  
  54.    // mallocate space for it and read it all at once
  55.    pgnfile_data = (char *) SAFE_realloc (pgnfile_data, 0, pgnfile_size, sizeof (char), false);
  56.    fread (pgnfile_data, pgnfile_size, 1, fp);
  57.    fclose (fp); // we no longer need the file, so close it
  58.  
  59.    // now the file is fully loaded in memory
  60.  
  61.    // read line per line and count the number of games
  62.    buffer = pgnfile_data;
  63.    entry_count = 0;
  64.    while ((buffer = sgets (line_buffer, sizeof (line_buffer), buffer)) != NULL)
  65.       if (strncmp (line_buffer, "[Event \"", 8) == 0)
  66.          entry_count++; // we know now one game more
  67.  
  68.    // now prepare the games database for "entry_count" games
  69.    PGNFile_GameList_Init (entry_count);
  70.  
  71.    // read line per line
  72.    buffer = pgnfile_data;
  73.    entry_count = 0;
  74.    file_index = 0;
  75.    while ((buffer = sgets (line_buffer, sizeof (line_buffer), buffer)) != NULL)
  76.    {
  77.       // is it a new game ?
  78.       if (strncmp (line_buffer, "[Event \"", 8) == 0)
  79.       {
  80.          memset (&games[entry_count], 0, sizeof (games[entry_count]));
  81.          COPY_TIL_LAST_QUOTEMARK (games[entry_count].event_str, &line_buffer[8]); // copy event
  82.  
  83.          // assume a default "standard chess" start position unless told otherwise later
  84.          wcscpy_s (games[entry_count].fen_str, WCHAR_SIZEOF (games[entry_count].fen_str), FENSTARTUP_STANDARDCHESS);
  85.  
  86.          games[entry_count].gamedata_start = 0; // reset gamedata so far
  87.          entry_count++; // we know now one game more
  88.       }
  89.  
  90.       // else have we found a game already ?
  91.       else if (entry_count > 0)
  92.       {
  93.          // is it a tag ? (i.e: line BEGINS with a opening bracket and the first closing bracket ENDS it)
  94.          if ((line_buffer[0] == '[') && (ptr = strchr (line_buffer, ']')) && ((ptr[1] == '\r') || (ptr[1] == 0)))
  95.          {
  96.             if (strncmp (&line_buffer[1], "Site \"", 6) == 0)
  97.                COPY_TIL_LAST_QUOTEMARK (games[entry_count - 1].site_str, &line_buffer[7]) // copy site
  98.             else if (strncmp (&line_buffer[1], "Date \"", 6) == 0)
  99.                COPY_TIL_LAST_QUOTEMARK (games[entry_count - 1].date_str, &line_buffer[7]) // copy date
  100.             else if (strncmp (&line_buffer[1], "Round \"", 7) == 0)
  101.                COPY_TIL_LAST_QUOTEMARK (games[entry_count - 1].round_str, &line_buffer[8]) // copy round
  102.             else if (strncmp (&line_buffer[1], "White \"", 7) == 0)
  103.                COPY_TIL_LAST_QUOTEMARK (games[entry_count - 1].white_str, &line_buffer[8]) // copy white
  104.             else if (strncmp (&line_buffer[1], "Black \"", 7) == 0)
  105.                COPY_TIL_LAST_QUOTEMARK (games[entry_count - 1].black_str, &line_buffer[8]) // copy black
  106.             else if (strncmp (&line_buffer[1], "Result \"", 8) == 0)
  107.                COPY_TIL_LAST_QUOTEMARK (games[entry_count - 1].result_str, &line_buffer[9]) // copy results
  108.             else if (strncmp (&line_buffer[1], "ECO \"", 5) == 0)
  109.                COPY_TIL_LAST_QUOTEMARK (games[entry_count - 1].eco_str, &line_buffer[6]) // copy ECO code
  110.             else if (strncmp (&line_buffer[1], "FEN \"", 5) == 0)
  111.                COPY_TIL_LAST_QUOTEMARK (games[entry_count - 1].fen_str, &line_buffer[6]) // copy FEN string
  112.          }
  113.  
  114.          // else is it the beginning of a game ?
  115.          else if (strncmp (line_buffer, "1.", 2) == 0)
  116.             games[entry_count - 1].gamedata_start = file_index; // remember where this game starts
  117.          else if ((ptr = strstr (line_buffer, "} 1.")) != NULL)
  118.             games[entry_count - 1].gamedata_start = (ptr + 2 - pgnfile_data); // remember where this game starts
  119.       }
  120.  
  121.       file_index = buffer - pgnfile_data; // save current file pointer index
  122.    }
  123.  
  124.    return (true); // finished, return TRUE
  125. }
  126.  
  127.  
  128. bool PGNFile_LoadGame (board_t *board, const wchar_t *pgnfile_pathname, pgngame_t *game)
  129. {
  130.    // this function loads and parses a game data in a PGN file. If the selected game is NULL, it means that
  131.    // the user didn't want to chose any game at all, so just free the games list and return a success value.
  132.  
  133.    static wchar_t pgn_comment[65536]; // declared static so as not to reallocate it
  134.  
  135.    boardmove_t new_move;
  136.    int nag_code;
  137.    int length;
  138.    int char_index;
  139.    int fieldstart;
  140.    int fieldstop;
  141.    int program_index;
  142.    int variation_depth;
  143.    char movenumber_string[8];
  144.  
  145.    // did we chose NO game ?
  146.    if (game == NULL)
  147.    {
  148.       PGNFile_GameList_Shutdown (); // free the games list
  149.       SAFE_free ((void **) &pgnfile_data); // free the file data space
  150.       return (true); // return success as there's nothing to load
  151.    }
  152.  
  153.    // if either of the players is a chess engine, start it
  154.    for (program_index = 0; program_index < options.engine.program_count; program_index++)
  155.    {
  156.       if (wcscmp (game->black_str, options.engine.programs[program_index].name) == 0)
  157.       {
  158.          options.engine.selected_program = program_index; // update the preferred chess engine (FIXME: we should not)
  159.          Player_Shutdown (&board->players[COLOR_BLACK]);
  160.          Player_Init (&board->players[COLOR_BLACK], COLOR_BLACK, PLAYER_COMPUTER);
  161.       }
  162.       if (wcscmp (game->white_str, options.engine.programs[program_index].name) == 0)
  163.       {
  164.          options.engine.selected_program = program_index; // update the preferred chess engine (FIXME: we should not)
  165.          Player_Shutdown (&board->players[COLOR_WHITE]);
  166.          Player_Init (&board->players[COLOR_WHITE], COLOR_WHITE, PLAYER_COMPUTER);
  167.       }
  168.    }
  169.  
  170.    // reset the board (but NOT the players, just their view angles)
  171.    Board_Reset (board, game->fen_str);
  172.    animation_endtime = current_time + 2.0f; // HACK: this sorta prevents the "load file" dialog box trailing clicks to be misinterpreted
  173.  
  174.    // while we've not parsed til the end of game...
  175.    char_index = game->gamedata_start;
  176.    new_move.source[0] = -1;
  177.    new_move.source[1] = -1;
  178.    new_move.target[0] = -1;
  179.    new_move.target[1] = -1;
  180.    new_move.promotion_type = 0;
  181.    pgn_comment[0] = 0;
  182.    for (;;)
  183.    {
  184.       // build the move number string
  185.       sprintf_s (movenumber_string, sizeof (movenumber_string), "%d.", 1 + board->move_count / 2);
  186.  
  187.       // is it a space ?
  188.       if (isspace (pgnfile_data[char_index]))
  189.       {
  190.          char_index++; // if so, skip it
  191.          continue; // and proceed to the next data
  192.       }
  193.  
  194.       // else is what we're reading a move number ?
  195.       else if (strncmp (&pgnfile_data[char_index], movenumber_string, strlen (movenumber_string)) == 0)
  196.       {
  197.          char_index += strlen (movenumber_string); // if so, skip it
  198.          continue; // and proceed to the next data
  199.       }
  200.  
  201.       // else is it a dot ?
  202.       else if (pgnfile_data[char_index] == '.')
  203.       {
  204.          char_index++; // if so, skip it
  205.          continue; // and proceed to the next data
  206.       }
  207.  
  208.       // else is it an en passant notification ?
  209.       else if (strncmp (&pgnfile_data[char_index], "e.p.", 4) == 0)
  210.       {
  211.          char_index += 4; // this notification is superfluous, skip it
  212.          continue; // and proceed to the next data
  213.       }
  214.  
  215.       // else is it a comment ?
  216.       else if (pgnfile_data[char_index] == '{')
  217.       {
  218.          fieldstart = char_index + 1; // skip the leading brace
  219.  
  220.          while ((fieldstart < (int) pgnfile_size) && isspace (pgnfile_data[fieldstart]))
  221.             fieldstart++; // skip any leading spaces
  222.  
  223.          // move through all the other characters...
  224.          for (fieldstop = fieldstart; fieldstop < (int) pgnfile_size; fieldstop++)
  225.             if (pgnfile_data[fieldstop] == '}')
  226.                break; // and stop at the first closing brace we find
  227.  
  228.          char_index = fieldstop + 1; // remember where to continue reading (that is, after the closing brace)
  229.  
  230.          while ((fieldstop > 0) && isspace (pgnfile_data[fieldstop]))
  231.             fieldstop--; // chop off any trailing spaces
  232.  
  233.          pgnfile_data[fieldstop] = 0; // break the string at this location
  234.  
  235.          // now copy out the commentary by appending it to the one we know already
  236.          if (pgn_comment[0] != 0)
  237.             wcscat_s (pgn_comment, WCHAR_SIZEOF (pgn_comment), L" ");
  238.          length = wcslen (pgn_comment);
  239.          ConvertToWideChar (&pgn_comment[length], WCHAR_SIZEOF (pgn_comment) - length, &pgnfile_data[fieldstart]);
  240.          ConvertCRLFsToSingleSpaces (pgn_comment); // linearize string
  241.  
  242.          continue; // and proceed to the next data
  243.       }
  244.  
  245.       // else is it a numeric annotation glyph ?
  246.       else if (pgnfile_data[char_index] == '$')
  247.       {
  248.          nag_code = atoi (&pgnfile_data[char_index + 1]); // read it
  249.  
  250.          // now copy out as a comment
  251.          if (pgn_comment[0] != 0)
  252.             wcscat_s (pgn_comment, WCHAR_SIZEOF (pgn_comment), L" ");
  253.          length = wcslen (pgn_comment);
  254.          swprintf_s (&pgn_comment[length], WCHAR_SIZEOF (pgn_comment) - length, L"[NAG: %s]", PGNFile_NAGTextFromCode (nag_code));
  255.  
  256.          while ((char_index < (int) pgnfile_size) && !isspace (pgnfile_data[char_index]))
  257.             char_index++; // figure out where it stops
  258.          while ((char_index < (int) pgnfile_size) && isspace (pgnfile_data[char_index]))
  259.             char_index++; // figure out where the next word starts
  260.  
  261.          continue; // and proceed to the next data
  262.       }
  263.  
  264.       // else is it a variation ? if so, just ignore it (FIXME: better support this)
  265.       else if (pgnfile_data[char_index] == '(')
  266.       {
  267.          variation_depth = 1;
  268.          while ((char_index < (int) pgnfile_size) && (variation_depth != 0))
  269.          {
  270.             char_index++; // move through file data and cope with nested variations
  271.             if      (pgnfile_data[char_index] == '(') variation_depth++;
  272.             else if (pgnfile_data[char_index] == ')') variation_depth--;
  273.          }
  274.          char_index++; // skip the closing parenthese
  275.          while ((char_index < (int) pgnfile_size) && isspace (pgnfile_data[char_index]))
  276.             char_index++; // figure out where the next word starts
  277.  
  278.          continue; // and proceed to the next data
  279.       }
  280.  
  281.       // else is it a game end marker ?
  282.       else if ((strncmp (&pgnfile_data[char_index], "1/2-1/2", 7) == 0)
  283.                || (strncmp (&pgnfile_data[char_index], "1-0", 3) == 0)
  284.                || (strncmp (&pgnfile_data[char_index], "0-1", 3) == 0)
  285.                || (pgnfile_data[char_index] == '*'))
  286.       {
  287.          // if there's a move pending, validate it
  288.          if ((new_move.source[0] != -1) && (new_move.source[1] != -1) && (new_move.target[0] != -1) && (new_move.target[1] != -1))
  289.          {
  290.             Board_AppendMove (board, new_move.source[0], new_move.source[1], new_move.target[0], new_move.target[1], new_move.promotion_type, pgn_comment); // save move
  291.             board->has_playerchanged = true; // switch players
  292.             new_move.part = PART_NONE;
  293.             new_move.source[0] = -1;
  294.             new_move.source[1] = -1;
  295.             new_move.target[0] = -1;
  296.             new_move.target[1] = -1;
  297.             new_move.promotion_type = 0;
  298.             pgn_comment[0] = 0; // reset comment
  299.          }
  300.  
  301.          // has the game been closed ?
  302.          if (strncmp (&pgnfile_data[char_index], "1/2-1/2", 7) == 0)
  303.             board->game_state = STATE_DRAW_OTHER; // game closed on a draw (FIXME: identify variants)
  304.          else if (strncmp (&pgnfile_data[char_index], "1-0", 7) == 0)
  305.          {
  306.             // see if it's a checkmate
  307.             if (board->moves[board->move_count - 1].is_check && board->moves[board->move_count - 1].is_stalemate)
  308.                board->game_state = STATE_WHITEWIN_CHECKMATE; // game was won by white on a checkmate (checkmate = check + stalemate)
  309.             else
  310.                board->game_state = STATE_WHITEWIN_RESIGNORFORFEIT; // game was won by white for another reason
  311.          }
  312.          else if (strncmp (&pgnfile_data[char_index], "0-1", 7) == 0)
  313.          {
  314.             // see if it's a checkmate
  315.             if (board->moves[board->move_count - 1].is_check && board->moves[board->move_count - 1].is_stalemate)
  316.                board->game_state = STATE_BLACKWIN_CHECKMATE; // game was won by black on a checkmate (checkmate = check + stalemate)
  317.             else
  318.                board->game_state = STATE_BLACKWIN_RESIGNORFORFEIT; // game was won by black for another reason
  319.          }
  320.  
  321.          break; // we've finished reading the game
  322.       }
  323.  
  324.       // else it must be a move data
  325.       else
  326.       {
  327.          // if there's a move pending, validate it
  328.          if ((new_move.source[0] != -1) && (new_move.source[1] != -1) && (new_move.target[0] != -1) && (new_move.target[1] != -1))
  329.          {
  330.             Board_AppendMove (board, new_move.source[0], new_move.source[1], new_move.target[0], new_move.target[1], new_move.promotion_type, pgn_comment); // save move
  331.             board->has_playerchanged = true; // switch players
  332.             new_move.part = PART_NONE;
  333.             new_move.source[0] = -1;
  334.             new_move.source[1] = -1;
  335.             new_move.target[0] = -1;
  336.             new_move.target[1] = -1;
  337.             new_move.promotion_type = 0;
  338.             pgn_comment[0] = 0; // reset comment
  339.          }
  340.  
  341.          // convert the move string data to wide char
  342.          ConvertToWideChar (new_move.pgntext, WCHAR_SIZEOF (new_move.pgntext), &pgnfile_data[char_index]);
  343.  
  344.          // evaluate the string in Standard Algebraic Notation and find the source, destination, part type and promotion
  345.          if (!Move_SetupFromSAN (&board->moves[board->move_count - 1], &new_move, Board_ColorToMove (board)))
  346.          {
  347.             PGNFile_GameList_Shutdown (); // free the games list
  348.             SAFE_free ((void **) &pgnfile_data); // free the file data space
  349.             return (false); // on error, cancel
  350.          }
  351.  
  352.          // find where it stops
  353.          while ((char_index < (int) pgnfile_size) && !isspace (pgnfile_data[char_index]))
  354.             char_index++; // reach the next space
  355.  
  356.          char_index++; // remember where to continue reading (that is, after the next space)
  357.          continue; // and proceed to the next data
  358.       }
  359.    }
  360.  
  361.    // save the players' names
  362.    wcscpy_s (board->players[COLOR_WHITE].name, WCHAR_SIZEOF (board->players[COLOR_WHITE].name), game->white_str);
  363.    wcscpy_s (board->players[COLOR_BLACK].name, WCHAR_SIZEOF (board->players[COLOR_BLACK].name), game->black_str);
  364.  
  365.    // we loaded the game we want, we no longer need the games array
  366.    PGNFile_GameList_Shutdown ();
  367.  
  368.    // we no longer need the file data space, so free it
  369.    SAFE_free ((void **) &pgnfile_data);
  370.  
  371.    return (true); // game loaded successfully, return TRUE
  372. }
  373.  
  374.  
  375. bool PGNFile_Save (board_t *board, const wchar_t *pgnfile_pathname)
  376. {
  377.    // this function writes a PGN file in the standard, international Chess format
  378.  
  379.    FILE *fp;
  380.    wchar_t machine_name[256];
  381.    unsigned long buffer_size;
  382.    wchar_t result_string[16];
  383.    time_t rawtime;
  384.    struct tm timeinfo;
  385.    boardmove_t *move;
  386.    int move_index;
  387.    int char_index;
  388.    int length;
  389.    int count;
  390.    int consecutive_count;
  391.    bool needs_newline;
  392.  
  393.    // try to open file for writing
  394.    _wfopen_s (&fp, pgnfile_pathname, L"w");
  395.    if (fp == NULL)
  396.       return (false); // on error, cancel
  397.  
  398.    // get the machine name as an ASCII string
  399.    buffer_size = WCHAR_SIZEOF (machine_name);
  400.    GetComputerName (machine_name, &buffer_size);
  401.  
  402.    // get the current date and time
  403.    time (&rawtime);
  404.    localtime_s (&timeinfo, &rawtime);
  405.  
  406.    // build the result string
  407.    if ((board->game_state == STATE_WHITEWIN_CHECKMATE) || (board->game_state == STATE_WHITEWIN_RESIGNORFORFEIT))
  408.       wcscpy_s (result_string, WCHAR_SIZEOF (result_string), L"1-0"); // white won
  409.    else if ((board->game_state == STATE_BLACKWIN_CHECKMATE) || (board->game_state == STATE_BLACKWIN_RESIGNORFORFEIT))
  410.       wcscpy_s (result_string, WCHAR_SIZEOF (result_string), L"0-1"); // black won
  411.    else if ((board->game_state == STATE_DRAW_STALEMATE) || (board->game_state == STATE_DRAW_AGREEMENT) || (board->game_state == STATE_DRAW_OTHER))
  412.       wcscpy_s (result_string, WCHAR_SIZEOF (result_string), L"1/2-1/2"); // game is a draw
  413.    else
  414.       wcscpy_s (result_string, WCHAR_SIZEOF (result_string), L"*"); // game still in progress
  415.  
  416.    // write the mandatory header parts
  417.    fwprintf (fp, L"[Event \"Chess Giants game\"]\n");
  418.    fwprintf (fp, L"[Site \"%s\"]\n", machine_name);
  419.    fwprintf (fp, L"[Date \"%d.%02d.%02d\"]\n", 1900 + timeinfo.tm_year, 1 + timeinfo.tm_mon, timeinfo.tm_mday);
  420.    fwprintf (fp, L"[Round \"1\"]\n");
  421.    fwprintf (fp, L"[White \"%s\"]\n", board->players[COLOR_WHITE].name);
  422.    fwprintf (fp, L"[Black \"%s\"]\n", board->players[COLOR_BLACK].name);
  423.    fwprintf (fp, L"[Result \"%s\"]\n", result_string);
  424.    fwprintf (fp, L"[FEN \"%s\"]\n", board->moves[0].fen_string);
  425.    fwprintf (fp, L"\n");
  426.  
  427.    // now write the game
  428.    consecutive_count = 0;
  429.    for (move_index = 1; move_index < board->move_count; move_index++)
  430.    {
  431.       // every seven move pairs, drop a carriage return
  432.       if (consecutive_count == 14)
  433.       {
  434.          fwprintf (fp, L"\n"); // one blank line
  435.          consecutive_count = 0;
  436.       }
  437.  
  438.       move = &board->moves[move_index]; // quick access to move
  439.  
  440.       // white to move ?
  441.       if (move->color == COLOR_WHITE)
  442.          fwprintf (fp, L"%d.", (move_index + 1) / 2); // if so, display double-move number
  443.  
  444.       // dump move data
  445.       fwprintf (fp, L"%s ", move->pgntext);
  446.       consecutive_count++;
  447.  
  448.       // is there a comment for this move ?
  449.       if ((move->comment != NULL) && (move->comment[0] != 0))
  450.       {
  451.          fwprintf (fp, L"\n{\n"); // dump an open brace
  452.          length = wcslen (move->comment); // get comment length
  453.  
  454.          // for each character in comment text...
  455.          needs_newline = false;
  456.          count = 0;
  457.          for (char_index = 0; char_index < length; char_index++)
  458.          {
  459.             // is it a space and do we need a newline ?
  460.             if (iswspace (move->comment[char_index]) && needs_newline)
  461.             {
  462.                fputwc (L'\n', fp); // dump a newline
  463.                needs_newline = false; // we no longer need a newline
  464.                count = 0; // reset the number of characters written
  465.             }
  466.  
  467.             // else it's a character
  468.             else
  469.             {
  470.                fputwc (move->comment[char_index], fp); // dump it
  471.                count++; // we dumped one character more on this line
  472.                if (count > 80)
  473.                   needs_newline = true; // if we need a newline, remember it
  474.             }
  475.          }
  476.  
  477.          fwprintf (fp, L"\n}\n"); // dump an open brace
  478.          consecutive_count = 0;
  479.       }
  480.  
  481.       // if it's the last move, dump the game results
  482.       if (move_index == board->move_count - 1)
  483.          fwprintf (fp, result_string);
  484.    }
  485.  
  486.    fclose (fp); // finished, close the file
  487.  
  488.    return (true); // file saved successfully, return TRUE
  489. }
  490.  
  491.  
  492. static void PGNFile_GameList_Init (int entry_count)
  493. {
  494.    // helper function to initialize the game lists array pointer and the games count
  495.  
  496.    // allocate space for all the games in a row (it's faster than reallocating)
  497.    games = (pgngame_t *) SAFE_malloc (entry_count, sizeof (pgngame_t), true);
  498.    game_count = entry_count;
  499.  
  500.    return; // finished
  501. }
  502.  
  503.  
  504. static void PGNFile_GameList_Shutdown (void)
  505. {
  506.    // helper function to free the game lists array and reset the games count
  507.  
  508.    SAFE_free ((void **) &games); // free the PGN games array
  509.    game_count = 0;
  510.  
  511.    return; // finished
  512. }
  513.  
  514.  
  515. static wchar_t *PGNFile_NAGTextFromCode (int nag_code)
  516. {
  517.    // helper function that returns the NAG (numeric annotation glyph) text that corresponds to a particular code
  518.  
  519.    typedef struct nag_s
  520.    {
  521.       int code;
  522.       wchar_t *text;
  523.    } nag_t;
  524.    nag_t nag_codes[] =
  525.    {
  526.       { 1, L"good move" },
  527.       { 2, L"poor move or mistake" },
  528.       { 3, L"very good or brilliant move" },
  529.       { 4, L"very poor move or blunder" },
  530.       { 5, L"speculative or interesting move" },
  531.       { 6, L"questionable or dubious move" },
  532.       { 7, L"forced move (all others lose quickly) or only move" },
  533.       { 8, L"singular move (no reasonable alternatives)" },
  534.       { 9, L"worst move" },
  535.       { 10, L"drawish position or even" },
  536.       { 11, L"equal chances, quiet position" },
  537.       { 12, L"equal chances, active position" },
  538.       { 13, L"unclear position" },
  539.       { 14, L"White has a slight advantage" },
  540.       { 15, L"Black has a slight advantage" },
  541.       { 16, L"White has a moderate advantage" },
  542.       { 17, L"Black has a moderate advantage" },
  543.       { 18, L"White has a decisive advantage" },
  544.       { 19, L"Black has a decisive advantage" },
  545.       { 20, L"White has a crushing advantage (Black should resign)" },
  546.       { 21, L"Black has a crushing advantage (White should resign)" },
  547.       { 22, L"White is in zugzwang" },
  548.       { 23, L"Black is in zugzwang" },
  549.       { 24, L"White has a slight space advantage" },
  550.       { 25, L"Black has a slight space advantage" },
  551.       { 26, L"White has a moderate space advantage" },
  552.       { 27, L"Black has a moderate space advantage" },
  553.       { 28, L"White has a decisive space advantage" },
  554.       { 29, L"Black has a decisive space advantage" },
  555.       { 30, L"White has a slight time (development) advantage" },
  556.       { 31, L"Black has a slight time (development) advantage" },
  557.       { 32, L"White has a moderate time (development) advantage" },
  558.       { 33, L"Black has a moderate time (development) advantage" },
  559.       { 34, L"White has a decisive time (development) advantage" },
  560.       { 35, L"Black has a decisive time (development) advantage" },
  561.       { 36, L"White has the initiative" },
  562.       { 37, L"Black has the initiative" },
  563.       { 38, L"White has a lasting initiative" },
  564.       { 39, L"Black has a lasting initiative" },
  565.       { 40, L"White has the attack" },
  566.       { 41, L"Black has the attack" },
  567.       { 42, L"White has insufficient compensation for material deficit" },
  568.       { 43, L"Black has insufficient compensation for material deficit" },
  569.       { 44, L"White has sufficient compensation for material deficit" },
  570.       { 45, L"Black has sufficient compensation for material deficit" },
  571.       { 46, L"White has more than adequate compensation for material deficit" },
  572.       { 47, L"Black has more than adequate compensation for material deficit" },
  573.       { 48, L"White has a slight center control advantage" },
  574.       { 49, L"Black has a slight center control advantage" },
  575.       { 50, L"White has a moderate center control advantage" },
  576.       { 51, L"Black has a moderate center control advantage" },
  577.       { 52, L"White has a decisive center control advantage" },
  578.       { 53, L"Black has a decisive center control advantage" },
  579.       { 54, L"White has a slight kingside control advantage" },
  580.       { 55, L"Black has a slight kingside control advantage" },
  581.       { 56, L"White has a moderate kingside control advantage" },
  582.       { 57, L"Black has a moderate kingside control advantage" },
  583.       { 58, L"White has a decisive kingside control advantage" },
  584.       { 59, L"Black has a decisive kingside control advantage" },
  585.       { 60, L"White has a slight queenside control advantage" },
  586.       { 61, L"Black has a slight queenside control advantage" },
  587.       { 62, L"White has a moderate queenside control advantage" },
  588.       { 63, L"Black has a moderate queenside control advantage" },
  589.       { 64, L"White has a decisive queenside control advantage" },
  590.       { 65, L"Black has a decisive queenside control advantage" },
  591.       { 66, L"White has a vulnerable first rank" },
  592.       { 67, L"Black has a vulnerable first rank" },
  593.       { 68, L"White has a well protected first rank" },
  594.       { 69, L"Black has a well protected first rank" },
  595.       { 70, L"White has a poorly protected king" },
  596.       { 71, L"Black has a poorly protected king" },
  597.       { 72, L"White has a well protected king" },
  598.       { 73, L"Black has a well protected king" },
  599.       { 74, L"White has a poorly placed king" },
  600.       { 75, L"Black has a poorly placed king" },
  601.       { 76, L"White has a well placed king" },
  602.       { 77, L"Black has a well placed king" },
  603.       { 78, L"White has a very weak pawn structure" },
  604.       { 79, L"Black has a very weak pawn structure" },
  605.       { 80, L"White has a moderately weak pawn structure" },
  606.       { 81, L"Black has a moderately weak pawn structure" },
  607.       { 82, L"White has a moderately strong pawn structure" },
  608.       { 83, L"Black has a moderately strong pawn structure" },
  609.       { 84, L"White has a very strong pawn structure" },
  610.       { 85, L"Black has a very strong pawn structure" },
  611.       { 86, L"White has poor knight placement" },
  612.       { 87, L"Black has poor knight placement" },
  613.       { 88, L"White has good knight placement" },
  614.       { 89, L"Black has good knight placement" },
  615.       { 90, L"White has poor bishop placement" },
  616.       { 91, L"Black has poor bishop placement" },
  617.       { 92, L"White has good bishop placement" },
  618.       { 93, L"Black has good bishop placement" },
  619.       { 94, L"White has poor rook placement" },
  620.       { 95, L"Black has poor rook placement" },
  621.       { 96, L"White has good rook placement" },
  622.       { 97, L"Black has good rook placement" },
  623.       { 98, L"White has poor queen placement" },
  624.       { 99, L"Black has poor queen placement" },
  625.       { 100, L"White has good queen placement" },
  626.       { 101, L"Black has good queen placement" },
  627.       { 102, L"White has poor piece coordination" },
  628.       { 103, L"Black has poor piece coordination" },
  629.       { 104, L"White has good piece coordination" },
  630.       { 105, L"Black has good piece coordination" },
  631.       { 106, L"White has played the opening very poorly" },
  632.       { 107, L"Black has played the opening very poorly" },
  633.       { 108, L"White has played the opening poorly" },
  634.       { 109, L"Black has played the opening poorly" },
  635.       { 110, L"White has played the opening well" },
  636.       { 111, L"Black has played the opening well" },
  637.       { 112, L"White has played the opening very well" },
  638.       { 113, L"Black has played the opening very well" },
  639.       { 114, L"White has played the middlegame very poorly" },
  640.       { 115, L"Black has played the middlegame very poorly" },
  641.       { 116, L"White has played the middlegame poorly" },
  642.       { 117, L"Black has played the middlegame poorly" },
  643.       { 118, L"White has played the middlegame well" },
  644.       { 119, L"Black has played the middlegame well" },
  645.       { 120, L"White has played the middlegame very well" },
  646.       { 121, L"Black has played the middlegame very well" },
  647.       { 122, L"White has played the ending very poorly" },
  648.       { 123, L"Black has played the ending very poorly" },
  649.       { 124, L"White has played the ending poorly" },
  650.       { 125, L"Black has played the ending poorly" },
  651.       { 126, L"White has played the ending well" },
  652.       { 127, L"Black has played the ending well" },
  653.       { 128, L"White has played the ending very well" },
  654.       { 129, L"Black has played the ending very well" },
  655.       { 130, L"White has slight counterplay" },
  656.       { 131, L"Black has slight counterplay" },
  657.       { 132, L"White has moderate counterplay" },
  658.       { 133, L"Black has moderate counterplay" },
  659.       { 134, L"White has decisive counterplay" },
  660.       { 135, L"Black has decisive counterplay" },
  661.       { 136, L"White has moderate time control pressure" },
  662.       { 137, L"Black has moderate time control pressure" },
  663.       { 138, L"White has severe time control pressure" },
  664.       { 139, L"Black has severe time control pressure" }
  665.    };
  666.    int nag_index;
  667.  
  668.    // cycle through all known NAGs and return the text that corresponds to the requested code
  669.    for (nag_index = 0; nag_index < sizeof (nag_codes) / sizeof (nag_t); nag_index++)
  670.       if (nag_codes[nag_index].code == nag_code)
  671.          return (nag_codes[nag_index].text); // return the text as soon as we find it
  672.  
  673.    return (L"undocumented code"); // not found, return a placeholder string
  674. }
  675.  
  676.  
  677. static char *sgets (char *destination_line, int max_length, char *source_buffer)
  678. {
  679.    // copy a line from a given string. Kinda like fgets() when you're reading from a string.
  680.    // use it like:
  681.    // while (blah = sgets (dest, sizeof (dest), blah)) != NULL)
  682.  
  683.    char *pointer;
  684.    int char_index;
  685.  
  686.    if (source_buffer[0] == 0)
  687.    {
  688.       destination_line[0] = 0;
  689.       return (NULL); // if EOS return a NULL pointer
  690.    }
  691.  
  692.    pointer = strchr (source_buffer, '\n'); // get to the first carriage return we can find
  693.  
  694.    // found none ?
  695.    if (pointer == NULL)
  696.    {
  697.       // if so, copy the line we found
  698.       for (char_index = 0; char_index < max_length; char_index++)
  699.       {
  700.          destination_line[char_index] = source_buffer[char_index]; // copy the line we found
  701.          if (source_buffer[char_index] == 0)
  702.             break; // don't copy beyond the end of source
  703.       }
  704.  
  705.       if (char_index == max_length)
  706.          destination_line[max_length - 1] = 0; // ensure string is terminated
  707.  
  708.       return (&source_buffer[strlen (source_buffer)]); // and return a pointer to the end of the string
  709.    }
  710.  
  711.    *pointer = 0; // temporarily turn the carriage return to an end of string
  712.  
  713.    for (char_index = 0; char_index < max_length; char_index++)
  714.    {
  715.       destination_line[char_index] = source_buffer[char_index]; // copy the line we found
  716.       if (source_buffer[char_index] == 0)
  717.          break; // don't copy beyond the end of source
  718.    }
  719.  
  720.    destination_line[max_length - 1] = 0; // terminate string
  721.    *pointer = '\n'; // put the carriage return back
  722.  
  723.    return (&pointer[1]); // and return next line's source buffer pointer
  724. }
  725.