-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchess.c
669 lines (577 loc) · 19.5 KB
/
chess.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#include <stdio.h>
#include <postgres.h>
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "lib/stringinfo.h"
#include "utils/array.h"
#include "catalog/pg_type.h"
#include "utils/lsyscache.h"
#include "utils/builtins.h"
#include "libpq/pqformat.h"
#include "smallchesslib.h"
PG_MODULE_MAGIC;
/************************************************************************************************************
Chessboard Structure
************************************************************************************************************/
typedef SCL_Board chessboard;
// register the function as a PostgreSQL function that follows
// version 1 calling conventions
PG_FUNCTION_INFO_V1(chessboard_in);
PG_FUNCTION_INFO_V1(chessboard_out);
PG_FUNCTION_INFO_V1(chessboard_recv);
PG_FUNCTION_INFO_V1(chessboard_send);
PG_FUNCTION_INFO_V1(chessboard_constructor);
PG_FUNCTION_INFO_V1(chessboard_eq);
PG_FUNCTION_INFO_V1(chessboard_neq);
PG_FUNCTION_INFO_V1(chessboard_lt);
PG_FUNCTION_INFO_V1(chessboard_lte);
PG_FUNCTION_INFO_V1(chessboard_gt);
PG_FUNCTION_INFO_V1(chessboard_gte);
PG_FUNCTION_INFO_V1(chessboard_to_text);
PG_FUNCTION_INFO_V1(text_to_chessboard);
chessboard *create_chessboard_from_fen(const char *fenStr)
{
chessboard *board = (chessboard *)palloc(sizeof(chessboard));
SCL_boardFromFEN(*board, fenStr);
return board;
}
Datum chessboard_in(PG_FUNCTION_ARGS)
{
char *fenStr = PG_GETARG_CSTRING(0);
chessboard *board = create_chessboard_from_fen(fenStr);
PG_RETURN_POINTER(board);
}
Datum chessboard_out(PG_FUNCTION_ARGS)
{
chessboard *board = (chessboard *)PG_GETARG_POINTER(0);
char *fenStr = palloc(SCL_FEN_MAX_LENGTH);
SCL_boardToFEN(*board, fenStr);
PG_FREE_IF_COPY(board, 0);
PG_RETURN_CSTRING(fenStr);
}
Datum chessboard_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo)PG_GETARG_POINTER(0);
chessboard *board = (chessboard *)palloc(sizeof(chessboard));
// Assuming the binary format is a direct copy of the SCL_Board
pq_copymsgbytes(buf, (char *)board, sizeof(chessboard));
PG_RETURN_POINTER(board);
}
Datum chessboard_send(PG_FUNCTION_ARGS)
{
chessboard *board = (chessboard *)PG_GETARG_POINTER(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendbytes(&buf, (char *)board, sizeof(chessboard));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
Datum chessboard_eq(PG_FUNCTION_ARGS)
{
chessboard *board1 = (chessboard *)PG_GETARG_POINTER(0);
chessboard *board2 = (chessboard *)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(strcmp(*board1, *board2) == 0);
}
Datum chessboard_neq(PG_FUNCTION_ARGS)
{
chessboard *board1 = (chessboard *)PG_GETARG_POINTER(0);
chessboard *board2 = (chessboard *)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(strcmp(*board1, *board2) != 0);
}
Datum chessboard_lt(PG_FUNCTION_ARGS)
{
chessboard *board1 = (chessboard *)PG_GETARG_POINTER(0);
chessboard *board2 = (chessboard *)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(strcmp(*board1, *board2) < 0);
}
Datum chessboard_lte(PG_FUNCTION_ARGS)
{
chessboard *board1 = (chessboard *)PG_GETARG_POINTER(0);
chessboard *board2 = (chessboard *)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(strcmp(*board1, *board2) <= 0);
}
Datum chessboard_gt(PG_FUNCTION_ARGS)
{
chessboard *board1 = (chessboard *)PG_GETARG_POINTER(0);
chessboard *board2 = (chessboard *)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(strcmp(*board1, *board2) > 0);
}
Datum chessboard_gte(PG_FUNCTION_ARGS)
{
chessboard *board1 = (chessboard *)PG_GETARG_POINTER(0);
chessboard *board2 = (chessboard *)PG_GETARG_POINTER(1);
PG_RETURN_BOOL(strcmp(*board1, *board2) >= 0);
}
Datum chessboard_to_text(PG_FUNCTION_ARGS)
{
chessboard *board = (chessboard *)PG_GETARG_POINTER(0);
char *fenStr = palloc(SCL_FEN_MAX_LENGTH);
SCL_boardToFEN(*board, fenStr);
text *result = cstring_to_text(fenStr);
PG_FREE_IF_COPY(board, 0);
PG_RETURN_TEXT_P(result);
}
Datum text_to_chessboard(PG_FUNCTION_ARGS)
{
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("input string must not be null")));
text *inputText = PG_GETARG_TEXT_PP(0);
char *fenStr = text_to_cstring(inputText);
chessboard *board = create_chessboard_from_fen(fenStr);
PG_FREE_IF_COPY(inputText, 0);
PG_RETURN_POINTER(board);
}
/************************************************************************************************************
Chessgame structure
************************************************************************************************************/
// Helper function to create a string builder
SCL_StringBuilder *SCL_createStringBuilder()
{
SCL_StringBuilder *sb = palloc0(sizeof(SCL_StringBuilder));
if (!sb)
return NULL;
// Initial capacity
sb->capacity = 256;
sb->length = 0;
sb->buffer = palloc0(sb->capacity * sizeof(char));
if (!sb->buffer)
{
pfree(sb);
return NULL;
}
sb->buffer[0] = '\0';
return sb;
}
// Function to append a character to the string builder
void SCL_appendChar(SCL_StringBuilder *sb, char c)
{
if (sb->length + 1 >= sb->capacity)
{
size_t newCapacity = sb->capacity * 2;
char *newBuffer = repalloc(sb->buffer, newCapacity);
if (!newBuffer)
return; // Handle reallocation failure
sb->buffer = newBuffer;
sb->capacity = newCapacity;
}
sb->buffer[sb->length] = c;
sb->length++;
sb->buffer[sb->length] = '\0';
}
// Function to finalize the string builder and get the final string
char *SCL_finalizeStringBuilder(SCL_StringBuilder *sb)
{
char *finalString = repalloc(sb->buffer, sb->length + 1); // Resize to actual size
pfree(sb);
return finalString;
}
// Helper function to trim the result from a SAN moves string
char *trim_san_moves(const char *sanMovesStr)
{
// Copy the original string to avoid modifying it directly
char *trimmedStr = pstrdup(sanMovesStr);
// Define tokens that indicate the end of the moves and start of the result
const char *endTokens[] = {" 1-0", " 0-1", " 1/2-1/2", NULL};
for (int i = 0; endTokens[i] != NULL; i++)
{
// Finds the first occurrence of s2 in s1
char *found = strstr(trimmedStr, endTokens[i]);
if (found != NULL)
{
// Truncate the string at the start of the result
*found = '\0';
break;
}
}
return trimmedStr;
}
typedef struct
{
// header field for postgresql
int32 length;
// SAN moves encoded as uint8 pairs
uint8 record[FLEXIBLE_ARRAY_MEMBER];
} chessgame;
uint8 getChessgameHalfMoves(chessgame *game)
{
return SCL_recordLength(game->record);
}
char *getChessgameSanMoves(chessgame *game)
{
return SCL_printPGN(game->record);
}
char *getChessgameBoard(chessgame *game, int halfMoveIndex)
{
// Handle invalid index
if (halfMoveIndex < 0)
return NULL;
// maximum length of fen can be 136
char *fen = palloc(137 * sizeof(char));
SCL_Board board = SCL_BOARD_START_STATE;
// applies the SAN to the board for `halfMoveIndex` moves
SCL_recordApply(game->record, board, halfMoveIndex);
// stores the FEN in `fen`
SCL_boardToFEN(board, fen);
return fen;
}
PG_FUNCTION_INFO_V1(chessgame_in);
PG_FUNCTION_INFO_V1(chessgame_out);
PG_FUNCTION_INFO_V1(chessgame_recv);
PG_FUNCTION_INFO_V1(chessgame_send);
PG_FUNCTION_INFO_V1(text_to_chessgame);
PG_FUNCTION_INFO_V1(getBoard);
PG_FUNCTION_INFO_V1(getFirstMoves);
PG_FUNCTION_INFO_V1(hasOpening);
PG_FUNCTION_INFO_V1(hasBoard);
PG_FUNCTION_INFO_V1(chessgame_lt);
PG_FUNCTION_INFO_V1(chessgame_lte);
PG_FUNCTION_INFO_V1(chessgame_eq);
PG_FUNCTION_INFO_V1(chessgame_neq);
PG_FUNCTION_INFO_V1(chessgame_gte);
PG_FUNCTION_INFO_V1(chessgame_gt);
PG_FUNCTION_INFO_V1(chessgame_cmp);
PG_FUNCTION_INFO_V1(chessgame_get_all_states);
chessgame *create_chessgame(const char *SAN)
{
char *trimmedSAN = trim_san_moves(SAN);
// Create an empty SCL Record
SCL_Record r;
// Populate it with all of the SAN moves
SCL_recordFromPGN(r, trimmedSAN);
// Calculate the number of half moves
uint16_t numHalfMoves = SCL_recordLength(r);
// Each half move is encoded as a pair of numbers
uint16_t recordNumElements = numHalfMoves * 2;
// Calculate the total size of the elements in the record
uint16_t recordSize = sizeof(uint8) * recordNumElements;
// Calculate the total size of the chessgame structure
uint16_t totalSize = VARHDRSZ + recordSize;
chessgame *game = (chessgame *)palloc(totalSize);
// Set the size of the chessgame structure
SET_VARSIZE(game, totalSize);
// Populate the record inside the chessgame by copying data from SCL Record
memcpy(game->record, r, recordSize);
return game;
}
Datum chessgame_in(PG_FUNCTION_ARGS)
{
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("input string must not be null")));
char *sanMovesStr = PG_GETARG_CSTRING(0);
chessgame *game = create_chessgame(sanMovesStr);
PG_FREE_IF_COPY(sanMovesStr, 0);
PG_RETURN_POINTER(game);
}
Datum chessgame_out(PG_FUNCTION_ARGS)
{
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("chessgame must not be null")));
chessgame *game = (chessgame *)PG_GETARG_POINTER(0);
char *SAN = getChessgameSanMoves(game);
PG_FREE_IF_COPY(game, 0);
PG_RETURN_CSTRING(SAN);
}
Datum chessgame_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo)PG_GETARG_POINTER(0);
// Read the length of the record
int32 recordLength;
recordLength = pq_getmsgint(buf, sizeof(int32));
// Validate record length
if (recordLength < VARHDRSZ)
ereport(ERROR,
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
errmsg("invalid length in external \"chessgame\" representation")));
// Allocate memory for chessgame
chessgame *game = (chessgame *)palloc(recordLength);
SET_VARSIZE(game, recordLength);
// Copy the SAN moves
pq_copymsgbytes(buf, (char *)game->record, recordLength - VARHDRSZ);
PG_RETURN_POINTER(game);
}
Datum chessgame_send(PG_FUNCTION_ARGS)
{
chessgame *game = (chessgame *)PG_GETARG_POINTER(0);
StringInfoData buf;
// Begin populating the buffer
pq_begintypsend(&buf);
// Send the length of the record
pq_sendint32(&buf, VARSIZE(game));
// Send the SAN moves
pq_sendbytes(&buf, (char *)game->record, VARSIZE(game) - VARHDRSZ);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
Datum text_to_chessgame(PG_FUNCTION_ARGS)
{
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("input string must not be null")));
text *inputText = PG_GETARG_TEXT_PP(0);
char *sanMovesStr = text_to_cstring(inputText);
chessgame *game = create_chessgame(sanMovesStr);
PG_FREE_IF_COPY(inputText, 0);
PG_RETURN_POINTER(game);
}
/************************************************************************************************************
Chessgame functions
************************************************************************************************************/
Datum getBoard(PG_FUNCTION_ARGS)
{
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("chessgame must not be null")));
if (PG_ARGISNULL(1))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("half-move index must not be null")));
chessgame *game = (chessgame *)PG_GETARG_POINTER(0);
int halfMoveIndex = PG_GETARG_INT32(1);
// Max number of half moves is equivalent to number of half moves in game
if (halfMoveIndex > getChessgameHalfMoves(game))
halfMoveIndex = getChessgameHalfMoves(game);
// return the FEN of chessboard at index i
char *fen = getChessgameBoard(game, halfMoveIndex);
if (fen == NULL)
PG_RETURN_NULL();
text *result = cstring_to_text(fen);
PG_FREE_IF_COPY(game, 0);
PG_RETURN_TEXT_P(result);
}
char *getFirstNHalfMoves(const char *inputSAN, int N)
{
if (N <= 0 || inputSAN == NULL)
{
// Return an empty string for invalid input.
return pstrdup("");
}
int halfMoveCount = 0;
int i = 0;
int length = strlen(inputSAN);
// Traverse the string to find the Nth half-move.
while (i < length && halfMoveCount < N)
{
// Increment halfMoveCount on encountering a space,
// but ensure it's not a space following a move number.
if (inputSAN[i] == ' ' && (i == 0 || inputSAN[i - 1] != '.'))
{
halfMoveCount++;
}
i++;
}
// If the string ends with a space, move back to exclude it.
if (i > 0 && inputSAN[i - 1] == ' ')
{
i--;
}
// Allocate memory for the output string and copy the substring.
char *output = (char *)palloc(sizeof(char) * (i + 1));
if (output == NULL)
{
perror("Unable to allocate memory");
exit(EXIT_FAILURE);
}
strncpy(output, inputSAN, i);
// Null-terminate the string
output[i] = '\0';
return output;
}
Datum getFirstMoves(PG_FUNCTION_ARGS)
{
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("chessgame must not be null")));
if (PG_ARGISNULL(1))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("half-move count must not be null")));
chessgame *game = (chessgame *)PG_GETARG_POINTER(0);
int n = PG_GETARG_INT32(1);
if (n <= 0)
{
text *empty_text = cstring_to_text("");
PG_RETURN_TEXT_P(empty_text);
}
char *SAN = getChessgameSanMoves(game);
char *trimmedSAN = getFirstNHalfMoves(SAN, n);
PG_FREE_IF_COPY(game, 0);
PG_RETURN_TEXT_P(cstring_to_text(trimmedSAN));
}
/**
* B Tree Index Operators
* for chessgame - chessgame comparison
* less than
* less than or equal
* equal
* greater than or equal
* greater than
* comparison (support function)
*/
// Helper function to check if 'pre' is a prefix of 'str'
bool prefix(const char *pre, const char *str)
{
return strncmp(pre, str, strlen(pre)) == 0;
}
bool lessThan(const char *SAN1, const char *SAN2)
{
/*
* game1 is 'less than' game2 if SAN of game1 is a prefix of SAN of game2
*/
return prefix(SAN1, SAN2);
}
Datum chessgame_lt(PG_FUNCTION_ARGS)
{
chessgame *game1 = (chessgame *)PG_GETARG_POINTER(0);
chessgame *game2 = (chessgame *)PG_GETARG_POINTER(1);
const char *SAN1 = getChessgameSanMoves(game1);
const char *SAN2 = getChessgameSanMoves(game2);
bool result = lessThan(SAN1, SAN2);
pfree(SAN1);
pfree(SAN2);
PG_FREE_IF_COPY(game1, 0);
PG_FREE_IF_COPY(game2, 1);
PG_RETURN_BOOL(result);
}
Datum chessgame_lte(PG_FUNCTION_ARGS)
{
chessgame *game1 = (chessgame *)PG_GETARG_POINTER(0);
chessgame *game2 = (chessgame *)PG_GETARG_POINTER(1);
const char *SAN1 = getChessgameSanMoves(game1);
const char *SAN2 = getChessgameSanMoves(game2);
bool result = lessThan(SAN1, SAN2) || strcmp(SAN1, SAN2) == 0;
pfree(SAN1);
pfree(SAN2);
PG_FREE_IF_COPY(game1, 0);
PG_FREE_IF_COPY(game2, 1);
PG_RETURN_BOOL(result);
}
Datum chessgame_eq(PG_FUNCTION_ARGS)
{
chessgame *game1 = (chessgame *)PG_GETARG_POINTER(0);
chessgame *game2 = (chessgame *)PG_GETARG_POINTER(1);
const char *SAN1 = getChessgameSanMoves(game1);
const char *SAN2 = getChessgameSanMoves(game2);
bool result = strcmp(SAN1, SAN2) == 0;
pfree(SAN1);
pfree(SAN2);
PG_FREE_IF_COPY(game1, 0);
PG_FREE_IF_COPY(game2, 1);
PG_RETURN_BOOL(result);
}
Datum chessgame_neq(PG_FUNCTION_ARGS)
{
chessgame *game1 = (chessgame *)PG_GETARG_POINTER(0);
chessgame *game2 = (chessgame *)PG_GETARG_POINTER(1);
const char *SAN1 = getChessgameSanMoves(game1);
const char *SAN2 = getChessgameSanMoves(game2);
bool result = strcmp(SAN1, SAN2) != 0;
pfree(SAN1);
pfree(SAN2);
PG_FREE_IF_COPY(game1, 0);
PG_FREE_IF_COPY(game2, 1);
PG_RETURN_BOOL(result);
}
Datum chessgame_gt(PG_FUNCTION_ARGS)
{
chessgame *game1 = (chessgame *)PG_GETARG_POINTER(0);
chessgame *game2 = (chessgame *)PG_GETARG_POINTER(1);
const char *SAN1 = getChessgameSanMoves(game1);
const char *SAN2 = getChessgameSanMoves(game2);
bool result = lessThan(SAN2, SAN1);
pfree(SAN1);
pfree(SAN2);
PG_FREE_IF_COPY(game1, 0);
PG_FREE_IF_COPY(game2, 1);
PG_RETURN_BOOL(result);
}
Datum chessgame_gte(PG_FUNCTION_ARGS)
{
chessgame *game1 = (chessgame *)PG_GETARG_POINTER(0);
chessgame *game2 = (chessgame *)PG_GETARG_POINTER(1);
const char *SAN1 = getChessgameSanMoves(game1);
const char *SAN2 = getChessgameSanMoves(game2);
bool result = lessThan(SAN2, SAN1) || strcmp(SAN1, SAN2) == 0;
pfree(SAN1);
pfree(SAN2);
PG_FREE_IF_COPY(game1, 0);
PG_FREE_IF_COPY(game2, 1);
PG_RETURN_BOOL(result);
}
Datum chessgame_cmp(PG_FUNCTION_ARGS)
{
chessgame *game1 = (chessgame *)PG_GETARG_POINTER(0);
chessgame *game2 = (chessgame *)PG_GETARG_POINTER(1);
const char *SAN1 = getChessgameSanMoves(game1);
const char *SAN2 = getChessgameSanMoves(game2);
int result = strcmp(SAN1, SAN2);
if (result != 0)
{
// check less than
if (lessThan(SAN1, SAN2))
result = -1;
// check greater than
else if (lessThan(SAN2, SAN1))
result = 1;
}
pfree(SAN1);
pfree(SAN2);
PG_FREE_IF_COPY(game1, 0);
PG_FREE_IF_COPY(game2, 1);
PG_RETURN_INT32(result);
}
// return an array containing all the FEN states of the chessgame
Datum chessgame_get_all_states(PG_FUNCTION_ARGS)
{
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("chessgame must not be null")));
chessgame *game = (chessgame *)PG_GETARG_POINTER(0);
uint8 numHalfMoves = getChessgameHalfMoves(game);
// Create an array to store text representations of FEN states
Datum *fenStates = palloc(sizeof(Datum) * (numHalfMoves + 1));
// Populate the array with FEN states
for (int i = 0; i <= numHalfMoves; ++i)
{
char *fen = getChessgameBoard(game, i);
if (fen != NULL)
{
// prepend move number to FEN state like this "i:FEN"
// can be max 4 bytes (3 digit number and a colon)
char *fenWithMoveNumber = palloc(strlen(fen) + 4);
sprintf(fenWithMoveNumber, "%d:%s", i, fen);
pfree(fen);
// Convert C string to PostgreSQL text type
text *fenText = cstring_to_text(fenWithMoveNumber);
fenStates[i] = PointerGetDatum(fenText);
}
else
{
// Handle NULL FEN state
fenStates[i] = PointerGetDatum(cstring_to_text(""));
}
}
// Construct the array of text
int16 elemTyplen;
bool elemTypbyval;
char elemTypalign;
get_typlenbyvalalign(TEXTOID, &elemTyplen, &elemTypbyval, &elemTypalign);
ArrayType *resultArray = construct_array(fenStates, numHalfMoves + 1, TEXTOID, elemTyplen, elemTypbyval, elemTypalign);
// Clean up
for (int i = 0; i <= numHalfMoves; ++i)
{
pfree(DatumGetPointer(fenStates[i]));
}
pfree(fenStates);
PG_FREE_IF_COPY(game, 0);
PG_RETURN_ARRAYTYPE_P(resultArray);
}