Solver. C
Solver. C
h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>
#include <ctype.h> // Added for handling case-insensitive options
#include "hashset.h" // Header file for hash set functions #include
"bst.h" // Header file for binary search tree functions
int i = 1;
while (i < argc) {
if (argv[i][0] != '-')
error("Fatal Error. Invalid CLA", 2);
else if (!strcmp(argv[i], "-size")) {
puzzle_size = atoi(argv[++i]);
if (puzzle_size < 15 || puzzle_size > 46340)
error("Fatal Error. Illegal value passed after -size", 3);
} else if (!strcmp(argv[i], "-nbuffer")) {
buf_cells = atoi(argv[++i]);
if (buf_cells != 1 && buf_cells != 4 && buf_cells != 16 &&
buf_cells != 64)
error("Fatal Error. Illegal value passed after -nbuffer",
4);
buf_dimension = (int)sqrt(MEM_LIMIT / buf_cells); // Compute
the dimension of each sub-puzzle buffer
} else if (!strcmp(argv[i], "-input")) {
filename = strdup(argv[++i]);
int fd = open(filename, O_RDONLY, 0); // Open the input file
if (fd < 0)
error("Fatal Error. Illegal value passed after -input",
5);
// Rest of the file reading code...
close(fd); // Close the input file
} else if (!strcmp(argv[i], "-dict")) {
dictionary_file = fopen(argv[++i], "r"); // Open the
dictionary file
if (!dictionary_file)
error("Fatal Error. Illegal value passed after -dict", 6);
} else if (!strcmp(argv[i], "-len")) {
char *min_max = strdup(argv[++i]);
char *max_str = strrchr(min_max, ':');
if (!max_str || max_str == min_max)
error("Fatal Error. Illegal value passed after -len", 7);
max_len = atoi(max_str + 1); // Extract the maximum word
length
*max_str = '\0';
min_len = atoi(min_max); // Extract the minimum word length
free(min_max);
if (min_len < 3 || max_len > 14 || min_len > max_len)
error("Fatal Error. Illegal value passed after -len", 7);
} else if (!strcmp(argv[i], "-s"))
sorted = 1; // Set the flag to sort the output
else
error("Fatal Error. Usage: solve -dict dict.txt -input
puzzle1mb.txt -size 1000 -nbuffer 64 -len 4:7 [-s]", 1);
i++;
}
// Read and move all words from dictionary_file to a new hash table
(hashset)
set = set_init(); // Initialize the hash set
char word[256];
while (fscanf(dictionary_file, "%s", word) == 1) {
insert(&set, word); // Insert each word from the dictionary into
the hash set
}
fclose(dictionary_file); // Close the dictionary file
int buf_index = 0;
pthread_t t_id[buf_cells]; // Array to store thread IDs of consumer
threads
for (i = 0; i < buf_cells; i++)
t_id[i] = NULL; // Initialize all thread IDs to NULL
// Code for reading the input file and filling the buffer cells...
if (sorted) {
// Print the binary search tree using in-order traversal...
inorder_print(root); // Print the words in alphabetical order
printf("\n");
}
// Free the allocated memory for the buffer and the hash set
for (i = 0; i < buf_cells; i++) {
for (j = 0; j < buf_dimension; j++)
free(buffer[i][j]);
free(buffer[i]);
}
free(buffer);
for (i = 0; i < 65536; i++) {
tnode *entry = set.table[i];
while (entry != NULL) {
tnode *temp = entry;
entry = entry->right;
free(temp);
}
}
free(set.table);
return 0;
}