Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | // fenfile.cpp |
| 2 | |||
| 3 | #include "common.h" |
||
| 4 | |||
| 5 | |||
| 6 | // 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 ? |
||
| 7 | |||
| 8 | |||
| 9 | bool FENFile_Load (const wchar_t *fenfile_pathname, wchar_t *fen_string, int fenstring_maxsize) |
||
| 10 | { |
||
| 11 | // this function loads a game state in the Forsyth-Edwards notation format from a text file |
||
| 12 | |||
| 13 | int file_size; |
||
| 14 | FILE *fp; |
||
| 15 | |||
| 16 | // try to open file for reading |
||
| 17 | _wfopen_s (&fp, fenfile_pathname, L"r"); |
||
| 18 | if (fp == NULL) |
||
| 19 | return (false); // on error, cancel |
||
| 20 | |||
| 21 | // get file length |
||
| 22 | fseek (fp, 0, SEEK_END); |
||
| 23 | file_size = ftell (fp); |
||
| 24 | fseek (fp, 0, SEEK_SET); |
||
| 25 | |||
| 26 | // read all there is to read (FEN data is just one ASCII line) |
||
| 27 | fgetws (fen_string, fenstring_maxsize, fp); |
||
| 28 | |||
| 29 | // close the file |
||
| 30 | fclose (fp); |
||
| 31 | |||
| 32 | return (true); // finished, no error encountered so return TRUE |
||
| 33 | } |
||
| 34 | |||
| 35 | |||
| 36 | bool FENFile_Save (const wchar_t *fenfile_pathname, wchar_t *fen_string) |
||
| 37 | { |
||
| 38 | // this function writes a FEN file describing the current game state in the Forsyth-Edwards notation format |
||
| 39 | |||
| 40 | FILE *fp; |
||
| 41 | |||
| 42 | // try to open file for writing |
||
| 43 | _wfopen_s (&fp, fenfile_pathname, L"w"); |
||
| 44 | if (fp == NULL) |
||
| 45 | return (false); // on error, cancel |
||
| 46 | |||
| 47 | // dump the move's FEN data to file |
||
| 48 | fputws (fen_string, fp); |
||
| 49 | |||
| 50 | fclose (fp); // finished, close the file |
||
| 51 | |||
| 52 | return (true); // file saved successfully, return TRUE |
||
| 53 | } |