<commit_before>#include "io_interface.h"
#include "bignum_types.h"
#include "random_bignum_generator.h"
#include "constants.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

void read_bignum_arrays_from_files(uint32_t* a, uint32_t* b, const char* file_name_1, const char* file_name_2)
{
    printf("reading bignums from files ... ");
    fflush(stdout);
    read_bignum_array_from_file(file_name_1, a, NUMBER_OF_BIGNUMS);
    read_bignum_array_from_file(file_name_2, b, NUMBER_OF_BIGNUMS);
    printf("done\n");
    fflush(stdout);
}

void generate_random_bignum_arrays_to_files(const char* file_name_1, const char* file_name_2)
{
    start_random_number_generator();

    generate_random_bignum_array_to_file(file_name_1);
    generate_random_bignum_array_to_file(file_name_2);

    stop_random_number_generator();
}

void generate_random_bignum_array_to_file(const char* file_name)
{
    uint32_t* bignum = (uint32_t*) calloc(BIGNUM_NUMBER_OF_WORDS, sizeof(uint32_t));
    FILE* file = fopen(file_name, "w");

    for (uint32_t i = 0; i < NUMBER_OF_BIGNUMS; i++)
    {
        generate_random_bignum(bignum);

        for (uint32_t j = 0; j < BIGNUM_NUMBER_OF_WORDS; j++)
        {
            fprintf(file, "%u ", bignum[j]);
        }

        fprintf(file, "\n");
    }
}

void read_bignum_array_from_file(const char* file_name, uint32_t* bignum, uint32_t amount_to_read)
{
    FILE* file = fopen(file_name, "r");

    for (uint32_t i = 0; i < amount_to_read; i++)
    {
        for (uint32_t j = 0; j < BIGNUM_NUMBER_OF_WORDS; j++)
        {
            fscanf(file, "%u", &bignum[IDX(i, j)]);
        }

        fprintf(file, "\n");
    }

    fclose(file);
}

void write_bignum_array_to_file(const char* file_name, uint32_t* bignum)
{
    FILE* file = fopen(file_name, "w");

    for (uint32_t i = 0; i < NUMBER_OF_BIGNUMS; i++)
    {
        for (uint32_t j = 0; j < BIGNUM_NUMBER_OF_WORDS; j++)
        {
            fprintf(file, "%u ", bignum[IDX(i, j)]);
        }

        fprintf(file, "\n");
    }

    fclose(file);
}<commit_msg>adding error checking code for io_interface.cpp<commit_after>#include "io_interface.h"
#include "bignum_types.h"
#include "random_bignum_generator.h"
#include "constants.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

void read_bignum_arrays_from_files(uint32_t* a, uint32_t* b, const char* file_name_1, const char* file_name_2)
{
    if (a != NULL && b != NULL && file_name_1 != NULL && file_name_2 != NULL)
    {
        printf("reading bignums from files ... ");
        fflush(stdout);
        read_bignum_array_from_file(file_name_1, a, NUMBER_OF_BIGNUMS);
        read_bignum_array_from_file(file_name_2, b, NUMBER_OF_BIGNUMS);
        printf("done\n");
        fflush(stdout);
    }
    else
    {
        if (a == NULL)
        {
            printf("Error: bignum array \"a\" is NULL\n");
        }

        if (b == NULL)
        {
            printf("Error: bignum array \"b\" is NULL\n");
        }

        if (file_name_1 == NULL)
        {
            printf("Error: \"file_name_1\" is NULL\n");
        }

        if (file_name_2 == NULL)
        {
            printf("Error: \"file_name_2\" is NULL\n");
        }

        exit(0);
    }
}

void generate_random_bignum_arrays_to_files(const char* file_name_1, const char* file_name_2)
{
    if (file_name_1 != NULL && file_name_2 != NULL)
    {
        start_random_number_generator();

        generate_random_bignum_array_to_file(file_name_1);
        generate_random_bignum_array_to_file(file_name_2);

        stop_random_number_generator();
    }
    else
    {
        if (file_name_1 == NULL)
        {
            printf("Error: \"file_name_1\" is NULL\n");
        }

        if (file_name_2 == NULL)
        {
            printf("Error: \"file_name_2\" is NULL\n");
        }

        exit(0);
    }
}

void generate_random_bignum_array_to_file(const char* file_name)
{
    if (file_name != NULL)
    {
        FILE* file = fopen(file_name, "w");

        if (file != NULL)
        {
            uint32_t* bignum = (uint32_t*) calloc(BIGNUM_NUMBER_OF_WORDS, sizeof(uint32_t));

            if (bignum != NULL)
            {
                for (uint32_t i = 0; i < NUMBER_OF_BIGNUMS; i++)
                {
                    generate_random_bignum(bignum);

                    for (uint32_t j = 0; j < BIGNUM_NUMBER_OF_WORDS; j++)
                    {
                        fprintf(file, "%u ", bignum[j]);
                    }

                    fprintf(file, "\n");
                }
            }
            else
            {
                printf("Error: could not allocate memory for generated bignum\n");
                exit(0);
            }

            fclose(file);
        }
        else
        {
            printf("Error: could not open file \"%s\" for writing\n", file_name);
            exit(0);
        }
    }
    else
    {
        printf("Error: \"file_name\" is NULL\n");
        exit(0);
    }
}

void read_bignum_array_from_file(const char* file_name, uint32_t* bignum, uint32_t amount_to_read)
{
    // TODO
    if (file_name != NULL && bignum != NULL && amount_to_read > 0)
    {
        FILE* file = fopen(file_name, "r");

        if (file != NULL)
        {
            for (uint32_t i = 0; i < amount_to_read; i++)
            {
                for (uint32_t j = 0; j < BIGNUM_NUMBER_OF_WORDS; j++)
                {
                    fscanf(file, "%u", &bignum[IDX(i, j)]);
                }

                fprintf(file, "\n");
            }

            fclose(file);
        }
        else
        {
            printf("Error: could not open file \"%s\" for reading\n", file_name);
            exit(0);
        }
    }
    else
    {
        if (file_name == NULL)
        {
            printf("Error: \"file_name\" is NULL\n");
        }

        if (bignum == NULL)
        {
            printf("Error: bignum array \"bignum\" is NULL\n");
        }

        exit(0);
    }
}

void write_bignum_array_to_file(const char* file_name, uint32_t* bignum)
{
    FILE* file = fopen(file_name, "w");

    if (file != NULL)
    {
        for (uint32_t i = 0; i < NUMBER_OF_BIGNUMS; i++)
        {
            for (uint32_t j = 0; j < BIGNUM_NUMBER_OF_WORDS; j++)
            {
                fprintf(file, "%u ", bignum[IDX(i, j)]);
            }

            fprintf(file, "\n");
        }

        fclose(file);
    }
    else
    {
        printf("Error: could not open file \"%s\" for writing\n", file_name);
        exit(0);
    }
}
{'fraction_non_alphanumeric': 0.13108728943338438, 'fraction_numerical': 0.016079632465543645, 'mean_word_length': 1.965940054495913, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6303763', 'n_tokens_mistral': 2448, 'n_tokens_neox': 2354, 'n_words': 452}