// fenfile.cpp
 
 
 
#include "common.h"
 
 
 
 
 
// TODO: the numbering of plies (for the 50-move rule) is unsupported in this notation. Maybe we should warn the player when saving a game ?
 
 
 
 
 
bool FENFile_Load (const wchar_t *fenfile_pathname, wchar_t *fen_string, int fenstring_maxsize)
 
{
 
   // this function loads a game state in the Forsyth-Edwards notation format from a text file
 
 
 
   int file_size;
 
   FILE *fp;
 
 
 
   // try to open file for reading
 
   _wfopen_s (&fp, fenfile_pathname, L"r");
 
   if (fp == NULL)
 
      return (false); // on error, cancel
 
 
 
    // get file length
 
   fseek (fp, 0, SEEK_END);
 
   file_size = ftell (fp);
 
   fseek (fp, 0, SEEK_SET);
 
 
 
   // read all there is to read (FEN data is just one ASCII line)
 
   fgetws (fen_string, fenstring_maxsize, fp);
 
 
 
   // close the file
 
   fclose (fp);
 
 
 
   return (true); // finished, no error encountered so return TRUE
 
}
 
 
 
 
 
bool FENFile_Save (const wchar_t *fenfile_pathname, wchar_t *fen_string)
 
{
 
   // this function writes a FEN file describing the current game state in the Forsyth-Edwards notation format
 
 
 
   FILE *fp;
 
 
 
   // try to open file for writing
 
   _wfopen_s (&fp, fenfile_pathname, L"w");
 
   if (fp == NULL)
 
      return (false); // on error, cancel
 
 
 
   // dump the move's FEN data to file
 
   fputws (fen_string, fp);
 
 
 
   fclose (fp); // finished, close the file
 
 
 
   return (true); // file saved successfully, return TRUE
 
}