<commit_before>#include <stdlib.h>

int main()
{
	return EXIT_SUCCESS;
}
<commit_msg>Implement the generator for the hash function.<commit_after>#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "hash_func.h"

static const unsigned int MAX_MESSAGES = 100;

struct message {
	char *name;
	uint32_t hash;
};

static int get_messages(FILE *message_string_file, struct message *buffer, unsigned int max_msg)
{
	ssize_t read;
	char *line = NULL;
	size_t len = 0;
	unsigned int i = 0;
	unsigned int j;

	while ((read = getline(&line, &len, message_string_file)) != -1) {
		if (line[read - 1] == '\n') {
		    line[read - 1] = '\0';
			read--;
		}
		if (i >= max_msg) {
			fprintf(stderr, "Too many messages in file, adjust MAX_MESSAGES!\n");
			goto too_many_messages;
		}
		if (strcmp(line, "") != 0) {
			char *name = malloc(read + 1);
			if (name == NULL) {
				fprintf(stderr, "Could not allocate memory for message name!\n");
				goto malloc_failed;
			}
			strncpy(name, line, read);
			buffer[i].name = name;
			buffer[i].hash = hash_func_string(name);
			i++;
		}
	}
	free(line);
	return i;

too_many_messages:
malloc_failed:
	for (j = 0; j < i; j++) {
		free(buffer[j].name);
	}
	free(line);
	return -1;
}

static void free_message_names(struct message *buffer, int num_messages)
{
	int i;
	for (i = 0; i < num_messages; i++) {
		free(buffer[i].name);
	}

}

static int check_hashes(struct message *buffer, int num_messages)
{
	int i;
	int j;
	if (num_messages <= 1) {
		return 0;
	}

	for (i = 0; i < num_messages - 1; i++) {
		for (j = i + 1; j < num_messages; j++) {
			if (buffer[i].hash == buffer[j].hash) {
				fprintf(stderr, "Hashes for \"%s\" (%u) and \"%s\" (%u) are identical!\n",
				        buffer[i].name, buffer[i].hash, buffer[j].name, buffer[j].hash);
				return -1;
			}
		}
	}
	return 0;
}

static void generate_hash_file(FILE *hash_algo_file, struct message *buffer, int num_messages)
{
	int i;
	size_t nread;
	char buf[1000];

	fprintf(stdout, "#ifndef MESSAGE_HASH\n");
	fprintf(stdout, "#define MESSAGE_HASH\n\n");

	for (i = 0; i < num_messages; i++) {
		unsigned int j;
		fprintf(stdout, "#define ");
		for (j = 0; j < strlen(buffer[i].name); j++) {
			fprintf(stdout, "%c", toupper(buffer[i].name[j]));
		}
		fprintf(stdout, "_MESSAGE_HASH %u\n", buffer[i].hash);
	}
	fprintf(stdout, "\n#include <stdint.h>\n\n");

	while ((nread = fread(buf, 1, sizeof buf, hash_algo_file)) > 0)
		fwrite(buf, 1, nread, stdout);

	fprintf(stdout, "\n#endif\n");
}

int main(int argc, char *argv[])
{
	FILE *message_string_file;
	FILE *hash_algo_file;
	int num_messages;
	int hashes_unique;
	struct message *buffer;

	if (argc != 3) {
		fprintf(stderr, "Usage: %s <file with message strings> <file with C hash function>\n", argv[0]);
		return EXIT_FAILURE;
	}

	message_string_file = fopen(argv[1], "r");
	if (message_string_file == NULL) {
		fprintf(stderr, "Cannot open message string file %s\n", argv[1]);
		return EXIT_FAILURE;
	}

	buffer = malloc(MAX_MESSAGES * sizeof(*buffer));
	if (buffer == NULL) {
		fprintf(stderr, "Could not allocate memory for message buffer!\n");
		goto malloc_failed;
	}
	num_messages = get_messages(message_string_file, buffer, MAX_MESSAGES);
	if (num_messages == -1) {
		fprintf(stderr, "Error reading messages\n");
		goto msg_read_failed;
	}

	hashes_unique = check_hashes(buffer, num_messages);
	if (hashes_unique == -1) {
		goto hashes_not_unique;
	}

	hash_algo_file = fopen(argv[2], "r");
	if (hash_algo_file == NULL) {
		fprintf(stderr, "Cannot open hash file %s\n", argv[2]);
		goto hash_algo_file_failed;
	}

	generate_hash_file(hash_algo_file, buffer, num_messages);

	fclose(hash_algo_file);
	free_message_names(buffer, num_messages);
	free(buffer);
	fclose(message_string_file);
	return EXIT_SUCCESS;

hash_algo_file_failed:
hashes_not_unique:
	free_message_names(buffer, num_messages);
msg_read_failed:
	free(buffer);
malloc_failed:
	fclose(message_string_file);
	return EXIT_FAILURE;

}

{'fraction_non_alphanumeric': 0.15655430711610488, 'fraction_numerical': 0.009987515605493134, 'mean_word_length': 4.257217847769029, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 14, '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': '2611513', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1546, 'n_words': 384}