Subversion Repositories Games.Chess Giants

Rev

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