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