int polyglot_book_open(struct book *book, const char *path) { book->type = bt_polyglot; if (path == NULL) return -1; if ((book->file = fopen(path, "rb")) == NULL) return -1; size_t file_size; if (bin_file_size(book->file, &file_size) != 0) return -1; if ((file_size % file_entry_size) != 0) return -1; book->polyglot_book.size = file_size / file_entry_size; if (book->polyglot_book.size == 0) return -1; return 0; }
struct book *fen_book_open(const char *path) { FILE *f = NULL; struct book *book = NULL; size_t file_size; if ((f = fopen(path, "r")) == NULL) return NULL; if (bin_file_size(f, &file_size) != 0) goto error; book = xmalloc(sizeof *book + file_size + 1); book->type = bt_fen; book->file = NULL; if (fread(book->raw, 1, file_size, f) != 1) goto error; book->raw[file_size] = '\0'; if (parse_raw(book->raw, &book->fen_book) != 0) goto error; if (ferror(f)) goto error; (void) fclose(f); return book; error: if (book != NULL) free(book); if (f != NULL) fclose(f); return NULL; }