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);