Tue, 28 Aug 2018 14:37:09 +0200
adds error messages to PGN reader
src/chess/pgn.c | file | annotate | diff | comparison | revisions | |
src/chess/pgn.h | file | annotate | diff | comparison | revisions | |
src/game.c | file | annotate | diff | comparison | revisions | |
src/server.c | file | annotate | diff | comparison | revisions | |
src/terminal-chess.h | file | annotate | diff | comparison | revisions |
1.1 --- a/src/chess/pgn.c Tue Aug 28 14:16:30 2018 +0200 1.2 +++ b/src/chess/pgn.c Tue Aug 28 14:37:09 2018 +0200 1.3 @@ -32,6 +32,27 @@ 1.4 #include <stdlib.h> 1.5 #include <string.h> 1.6 1.7 +enum { 1.8 + pgn_error_missing_quote = 1, 1.9 + pgn_error_missing_bracket, 1.10 + pgn_error_missing_dot, 1.11 + pgn_error_move_syntax, 1.12 + pgn_error_move_semantics 1.13 +}; 1.14 + 1.15 +static const char* pgn_error_strings[] = { 1.16 + "No Error.", 1.17 + "Tag values must be enclosed in double-quotes.", 1.18 + "Tags must be enclosed in square brackets: '[Key \"Value\"]'.", 1.19 + "Move numbers must be terminated with a dot (e.g. '13.' - not '13').", 1.20 + "Move is syntactically incorrect.", 1.21 + "Move is not valid according to chess rules." 1.22 +}; 1.23 + 1.24 +const char* pgn_error_str(int code) { 1.25 + return pgn_error_strings[code]; 1.26 +} 1.27 + 1.28 int read_pgn(FILE* stream, GameState *gamestate, GameInfo *gameinfo) { 1.29 int c, i; 1.30 1.31 @@ -49,7 +70,7 @@ 1.32 break; 1.33 } 1.34 if (c != '[') { 1.35 - return 1; 1.36 + return pgn_error_missing_bracket; 1.37 } 1.38 while (isspace(c = fgetc(stream))); 1.39 i = 0; 1.40 @@ -59,18 +80,18 @@ 1.41 tagkey[i] = '\0'; 1.42 while (isspace(c = fgetc(stream))); 1.43 if (c != '"') { 1.44 - return 1; 1.45 + return pgn_error_missing_quote; 1.46 } 1.47 i = 0; 1.48 while ((c = fgetc(stream)) != '"') { 1.49 - if (c == '\n') { 1.50 - return 1; 1.51 + if (c == '\n' || c == EOF) { 1.52 + return pgn_error_missing_quote; 1.53 } 1.54 tagvalue[i++] = c; 1.55 } 1.56 tagvalue[i] = '\0'; 1.57 if (fgetc(stream) != ']') { 1.58 - return 1; 1.59 + return pgn_error_missing_bracket; 1.60 } 1.61 1.62 if (strcmp("Result", tagkey) == 0) { 1.63 @@ -80,7 +101,7 @@ 1.64 1.65 // read moves 1.66 if (fgetc(stream) != '.') { 1.67 - return 1; 1.68 + return pgn_error_missing_dot; 1.69 } 1.70 1.71 char movestr[10]; 1.72 @@ -100,10 +121,10 @@ 1.73 movestr[i] = '\0'; 1.74 if (eval_move(gamestate, movestr, &move, curcol) 1.75 != VALID_MOVE_SYNTAX) { 1.76 - return 1; 1.77 + return pgn_error_move_syntax; 1.78 } 1.79 if (validate_move(gamestate, &move) != VALID_MOVE_SEMANTICS) { 1.80 - return 1; 1.81 + return pgn_error_move_semantics; 1.82 } 1.83 apply_move(gamestate, &move); 1.84 1.85 @@ -135,7 +156,7 @@ 1.86 if (curcol == BLACK) { 1.87 while (isdigit(c = fgetc(stream))); 1.88 if (c != '.') { 1.89 - return 1; 1.90 + return pgn_error_missing_dot; 1.91 } 1.92 } 1.93 curcol = opponent_color(curcol);
2.1 --- a/src/chess/pgn.h Tue Aug 28 14:16:30 2018 +0200 2.2 +++ b/src/chess/pgn.h Tue Aug 28 14:37:09 2018 +0200 2.3 @@ -42,6 +42,8 @@ 2.4 size_t write_pgn(FILE* stream, GameState *gamestate, GameInfo *gameinfo); 2.5 void compute_fen(char *str, GameState *gamestate); 2.6 2.7 +const char* pgn_error_str(int code); 2.8 + 2.9 #ifdef __cplusplus 2.10 } 2.11 #endif
3.1 --- a/src/game.c Tue Aug 28 14:16:30 2018 +0200 3.2 +++ b/src/game.c Tue Aug 28 14:37:09 2018 +0200 3.3 @@ -498,9 +498,11 @@ 3.4 FILE *pgnfile = fopen(settings->continuepgn, "r"); 3.5 if (pgnfile) { 3.6 int result = read_pgn(pgnfile, &gamestate, &(settings->gameinfo)); 3.7 + long position = ftell(pgnfile); 3.8 fclose(pgnfile); 3.9 if (result) { 3.10 - addstr("Invalid PGN file content.\n"); 3.11 + printw("Invalid PGN file content at position %ld:\n%s\n", 3.12 + position, pgn_error_str(result)); 3.13 return; 3.14 } 3.15 if (!is_game_running(&gamestate)) {
4.1 --- a/src/server.c Tue Aug 28 14:16:30 2018 +0200 4.2 +++ b/src/server.c Tue Aug 28 14:37:09 2018 +0200 4.3 @@ -74,9 +74,11 @@ 4.4 if (pgnfile) { 4.5 int result = read_pgn(pgnfile, &continuegame, 4.6 &(settings->gameinfo)); 4.7 + long position = ftell(pgnfile); 4.8 fclose(pgnfile); 4.9 if (result) { 4.10 - addstr("Invalid PGN file content.\n"); 4.11 + printw("Invalid PGN file content at position %ld:\n%s\n", 4.12 + position, pgn_error_str(result)); 4.13 return 1; 4.14 } 4.15 if (!is_game_running(&continuegame)) {
5.1 --- a/src/terminal-chess.h Tue Aug 28 14:16:30 2018 +0200 5.2 +++ b/src/terminal-chess.h Tue Aug 28 14:37:09 2018 +0200 5.3 @@ -36,7 +36,7 @@ 5.4 #ifndef TERMINAL_CHESS_H 5.5 #define TERMINAL_CHESS_H 5.6 5.7 -#define PROGRAM_VERSION "0.9-r59" 5.8 +#define PROGRAM_VERSION "0.9-r60" 5.9 5.10 #ifdef __cplusplus 5.11 extern "C" {