<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "regexvm.h"

char *rgx;

static void regexvm_print_err (int err);

static char *get_matching_text (FILE *fp, uint64_t size)
{
    char *ret;

    if ((ret = malloc(size + 1)) == NULL) {
        return NULL;
    }

    fread(ret, sizeof(char), size, fp);
    ret[size] = '\0';
    return ret;
}

int main (int argc, char *argv[])
{
    FILE *fp;
    uint64_t size;
    int i;
    int ret;
    char *match;
    regexvm_t compiled;

    ret = 0;
    if (argc != 3) {
        printf("Usage: %s <regex> <filename>\n", argv[0]);
        exit(1);
    }

    /* Compile the expression */
    if ((ret = regexvm_compile(&compiled, argv[1])) < 0) {
        regexvm_print_err(ret);
        exit(ret);
    }

    /* Open the file */
    if ((fp = fopen(argv[2], "r")) == NULL) {
        printf("Error opening file %s. Exiting.\n", argv[2]);
        exit(1);
    }

    /* Find all occurrences of compiled expression in file */
    while (regexvm_fsearch(&compiled, fp, &size, 0)) {
        match = get_matching_text(fp, size);
        printf("Found match: %s\n", match);
        free(match);
    }

    fclose(fp);
    regexvm_free(&compiled);
    return ret;
}

void regexvm_print_err (int err)
{
    const char *msg;

    switch (err) {
        case RVM_BADOP:
            msg = "Operator used incorrectly";
        break;
        case RVM_BADCLASS:
            msg = "Unexpected character class closing character";
        break;
        case RVM_BADREP:<commit_msg>regexvm_fsearch example: check fread() return value<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "regexvm.h"

char *rgx;

static void regexvm_print_err (int err);

static char *get_matching_text (FILE *fp, uint64_t size)
{
    char *ret;

    if ((ret = malloc(size + 1)) == NULL) {
        return NULL;
    }

    if (fread(ret, sizeof(char), size, fp) != (sizeof(char) * size)) {
        return NULL;
    }

    ret[size] = '\0';
    return ret;
}

int main (int argc, char *argv[])
{
    FILE *fp;
    uint64_t size;
    int i;
    int ret;
    char *match;
    regexvm_t compiled;

    ret = 0;
    if (argc != 3) {
        printf("Usage: %s <regex> <filename>\n", argv[0]);
        exit(1);
    }

    /* Compile the expression */
    if ((ret = regexvm_compile(&compiled, argv[1])) < 0) {
        regexvm_print_err(ret);
        exit(ret);
    }

    /* Open the file */
    if ((fp = fopen(argv[2], "r")) == NULL) {
        printf("Error opening file %s. Exiting.\n", argv[2]);
        exit(1);
    }

    /* Find all occurrences of compiled expression in file */
    while (regexvm_fsearch(&compiled, fp, &size, 0)) {
        if ((match = get_matching_text(fp, size)) == NULL) {
            printf("Error reading matching text from file '%s'\n", argv[2]);
            exit(1);
        }

        printf("Found match: %s\n", match);
        free(match);
    }

    fclose(fp);
    regexvm_free(&compiled);
    return ret;
}

void regexvm_print_err (int err)
{
    const char *msg;

    switch (err) {
        case RVM_BADOP:
            msg = "Operator used incorrectly";
        break;
        case RVM_BADCLASS:
            msg = "Unexpected character class closing character";
        break;
        case RVM_BADREP:
{'fraction_non_alphanumeric': 0.1465778316172017, 'fraction_numerical': 0.01029678982434888, 'mean_word_length': 2.263833992094862, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '7189943', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1116, 'n_words': 310}