src/chess/pgn.c

changeset 59
3fa1de896666
parent 55
54ea19938d57
child 60
0c50aac49e55
equal deleted inserted replaced
58:7ba8a97a8b6e 59:3fa1de896666
47 if (c == '1') { 47 if (c == '1') {
48 readmoves = 1; 48 readmoves = 1;
49 break; 49 break;
50 } 50 }
51 if (c != '[') { 51 if (c != '[') {
52 return EXIT_FAILURE; 52 return 1;
53 } 53 }
54 while (isspace(c = fgetc(stream))); 54 while (isspace(c = fgetc(stream)));
55 i = 0; 55 i = 0;
56 do { 56 do {
57 tagkey[i++] = c; 57 tagkey[i++] = c;
58 } while (!isspace(c = fgetc(stream))); 58 } while (!isspace(c = fgetc(stream)));
59 tagkey[i] = '\0'; 59 tagkey[i] = '\0';
60 while (isspace(c = fgetc(stream))); 60 while (isspace(c = fgetc(stream)));
61 if (c != '"') { 61 if (c != '"') {
62 return EXIT_FAILURE; 62 return 1;
63 } 63 }
64 i = 0; 64 i = 0;
65 while ((c = fgetc(stream)) != '"') { 65 while ((c = fgetc(stream)) != '"') {
66 if (c == '\n') { 66 if (c == '\n') {
67 return EXIT_FAILURE; 67 return 1;
68 } 68 }
69 tagvalue[i++] = c; 69 tagvalue[i++] = c;
70 } 70 }
71 tagvalue[i] = '\0'; 71 tagvalue[i] = '\0';
72 if (fgetc(stream) != ']') { 72 if (fgetc(stream) != ']') {
73 return EXIT_FAILURE; 73 return 1;
74 } 74 }
75 75
76 if (strcmp("Result", tagkey) == 0) { 76 if (strcmp("Result", tagkey) == 0) {
77 memcpy(result, tagvalue, 8); 77 memcpy(result, tagvalue, 8);
78 } 78 }
79 } 79 }
80 80
81 // read moves 81 // read moves
82 if (fgetc(stream) != '.') { 82 if (fgetc(stream) != '.') {
83 return EXIT_FAILURE; 83 return 1;
84 } 84 }
85 85
86 char movestr[10]; 86 char movestr[10];
87 Move move; 87 Move move;
88 uint8_t curcol = WHITE; 88 uint8_t curcol = WHITE;
92 while (isspace(c = fgetc(stream))); 92 while (isspace(c = fgetc(stream)));
93 i = 0; 93 i = 0;
94 do { 94 do {
95 movestr[i++] = c; 95 movestr[i++] = c;
96 if (i >= 10) { 96 if (i >= 10) {
97 return EXIT_FAILURE; 97 return 1;
98 } 98 }
99 } while (!isspace(c = fgetc(stream))); 99 } while (!isspace(c = fgetc(stream)));
100 movestr[i] = '\0'; 100 movestr[i] = '\0';
101 if (eval_move(gamestate, movestr, &move, curcol) 101 if (eval_move(gamestate, movestr, &move, curcol)
102 != VALID_MOVE_SYNTAX) { 102 != VALID_MOVE_SYNTAX) {
103 return EXIT_FAILURE; 103 return 1;
104 } 104 }
105 if (validate_move(gamestate, &move) != VALID_MOVE_SEMANTICS) { 105 if (validate_move(gamestate, &move) != VALID_MOVE_SEMANTICS) {
106 return EXIT_FAILURE; 106 return 1;
107 } 107 }
108 apply_move(gamestate, &move); 108 apply_move(gamestate, &move);
109 109
110 // TODO: parse comments 110 // TODO: parse comments
111 while (isspace(c = fgetc(stream))); 111 while (isspace(c = fgetc(stream)));
133 133
134 // skip move number after black move 134 // skip move number after black move
135 if (curcol == BLACK) { 135 if (curcol == BLACK) {
136 while (isdigit(c = fgetc(stream))); 136 while (isdigit(c = fgetc(stream)));
137 if (c != '.') { 137 if (c != '.') {
138 return EXIT_FAILURE; 138 return 1;
139 } 139 }
140 } 140 }
141 curcol = opponent_color(curcol); 141 curcol = opponent_color(curcol);
142 } 142 }
143 143
144 return EXIT_SUCCESS; 144 return 0;
145 } 145 }
146 146
147 size_t write_pgn(FILE* stream, GameState *gamestate, GameInfo *gameinfo) { 147 size_t write_pgn(FILE* stream, GameState *gamestate, GameInfo *gameinfo) {
148 // TODO: tag pairs 148 // TODO: tag pairs
149 size_t bytes = 0; 149 size_t bytes = 0;

mercurial