<commit_before>#include "record.h"

#include "data.h"
#include "logger.h"
#include "sort.h"

#include <stdio.h>
#include <string.h>

// Get as close to 64 KiB as possible.
#define RECORD_ARRAY_SIZE 963
#define RECORD_TABLE_FILENAME "records.bin"

typedef struct RecordTable {
    size_t record_count;
    Record records[RECORD_ARRAY_SIZE];
} RecordTable;

/**
 * Creates a new Record from the provided data.
 *
 * If the name is too big to fit into the array, it is truncated to the maximum
 * possible size.
 */
Record make_record(const char * name, const int score) {
    Record record;
    
    // Safely copy the provided name into the array.
    strncpy(record.name, name, PLAYER_NAME_MAXIMUM_SIZE - 1);
    // We must manually assure that the last char is a null character.
    record.name[PLAYER_NAME_MAXIMUM_SIZE - 1] = '\0';

    record.score = score;
    
    return record;
}

int compare_records(const Record * const a, const Record * const b) {
    if (a->score < b->score) {
        return -1;
    } else if (a->score == b->score) {
        return 0;
    } else {
        return 1;
    };
}

int compare_void_record_pointers(const void *a, const void *b) {
    return compare_records((const Record *) a, (const Record *) b);
}

void populate_table_with_default_records(RecordTable * table) {
    static char *names[] = {"Adam", "Bree", "Cora", "Dave", "Elmo"};
    static int scores[] =  {    18,     14,     10,      8,      2};
    size_t i;
    size_t end = sizeof(scores) / sizeof(int);
    if (RECORD_ARRAY_SIZE < end) {
        end = RECORD_ARRAY_SIZE;
    }
    for (i = 0; i < end; i++) {
        table->records[i] = make_record(names[i], scores[i]);
        table->record_count++;
    }
}

/**
 * Reads a RecordTable into the provided destination.
 *
 * If the existing table cannot be used a new one is created.
 */
void read_table(RecordTable * const table) {
    int read_error = 0;
    if (file_exists(RECORD_TABLE_FILENAME)) {
        if (read_bytes(RECORD_TABLE_FILENAME, table, sizeof(RecordTable), 1)) {
            char message[512];
            sprintf(message, "Failed to read a RecordTable from %s", RECORD_TABLE_FILENAME);
            log_message(message);
            read_error = 1; // Set the error flag to trigger the creation of a new table.
        }
    } else {
        read_error = 1; // Set the error flag to trigger the creation of a new table.
    }
    if (read_error) {
        populate_table_with_default_records(table);
        log_message("read_table populated the table with the default records");
    }
}
<commit_msg>Fixed record loading not zeroing record_count<commit_after>#include "record.h"

#include "data.h"
#include "logger.h"
#include "sort.h"

#include <stdio.h>
#include <string.h>

// Get as close to 64 KiB as possible.
#define RECORD_ARRAY_SIZE 963
#define RECORD_TABLE_FILENAME "records.bin"

typedef struct RecordTable {
    size_t record_count;
    Record records[RECORD_ARRAY_SIZE];
} RecordTable;

/**
 * Creates a new Record from the provided data.
 *
 * If the name is too big to fit into the array, it is truncated to the maximum
 * possible size.
 */
Record make_record(const char * name, const int score) {
    Record record;
    
    // Safely copy the provided name into the array.
    strncpy(record.name, name, PLAYER_NAME_MAXIMUM_SIZE - 1);
    // We must manually assure that the last char is a null character.
    record.name[PLAYER_NAME_MAXIMUM_SIZE - 1] = '\0';

    record.score = score;
    
    return record;
}

int compare_records(const Record * const a, const Record * const b) {
    if (a->score < b->score) {
        return -1;
    } else if (a->score == b->score) {
        return 0;
    } else {
        return 1;
    };
}

int compare_void_record_pointers(const void *a, const void *b) {
    return compare_records((const Record *) a, (const Record *) b);
}

void populate_table_with_default_records(RecordTable * table) {
    static char *names[] = {"Adam", "Bree", "Cora", "Dave", "Elmo"};
    static int scores[] =  {    18,     14,     10,      8,      2};
    size_t i;
    size_t end = sizeof(scores) / sizeof(int);
    if (RECORD_ARRAY_SIZE < end) {
        end = RECORD_ARRAY_SIZE;
    }
    table->record_count = 0;
    for (i = 0; i < end; i++) {
        table->records[i] = make_record(names[i], scores[i]);
        table->record_count++;
    }
}

/**
 * Reads a RecordTable into the provided destination.
 *
 * If the existing table cannot be used a new one is created.
 */
void read_table(RecordTable * const table) {
    int read_error = 0;
    if (file_exists(RECORD_TABLE_FILENAME)) {
        if (read_bytes(RECORD_TABLE_FILENAME, table, sizeof(RecordTable), 1)) {
            char message[512];
            sprintf(message, "Failed to read a RecordTable from %s", RECORD_TABLE_FILENAME);
            log_message(message);
            read_error = 1; // Set the error flag to trigger the creation of a new table.
        }
    } else {
        read_error = 1; // Set the error flag to trigger the creation of a new table.
    }
    if (read_error) {
        populate_table_with_default_records(table);
        log_message("read_table populated the table with the default records");
    }
}

{'fraction_non_alphanumeric': 0.11143131604226705, 'fraction_numerical': 0.010566762728146013, 'mean_word_length': 2.8, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5647915', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1652, 'n_words': 535}