#include #include #include #include #include #include "mailbox.h" #include "main.h" /** * Parse a line of input into an input struct. * * @param input_line A line of input. * @param res An already allocated input struct to store the result in. * * @return int -1 if the input is malformed, and 0 otherwise. */ static int parse_input_line(char *input_line, struct input *res) { // if tmp has anything in it, then we know that extra values have been entered. char tmp[2]; int num_matched = sscanf(input_line, " %d %d %1s", &(res->value), &(res->recipient), tmp); return num_matched == 2 ? 0 : -1; } /** * Convert the given input to an array of input structs. * * @param num_threads The number of threads that the program has been spawned with. * @param res A pointer that will be allocated as an array of inputs. Must be freed by the caller. * * @return int the number of input lines that were read. */ static int get_input(int num_threads, struct input **res) { int input_index = 0; int current_buffer_size = BUFFER_SIZE; char *input_buffer = malloc(current_buffer_size); // Keep reaidng until we hit an EOF while (true) { char *read_result; while((read_result = fgets(input_buffer, current_buffer_size, stdin))) { int read_size = strlen(input_buffer); if (input_buffer[read_size - 1] == '\n') { break; } // Grow our buffer if we nede to read more input. current_buffer_size *= 2; input_buffer = realloc(input_buffer, current_buffer_size); } if (read_result == NULL) { break; } struct input current_input; int parse_result = parse_input_line(input_buffer, ¤t_input); if (parse_result == -1) { // On bad input, we should assume an EOF and stop. break; } else if (current_input.recipient >= num_threads || current_input.recipient < 0) { printf("Invalid recipient id.\n"); continue; } *res = realloc(*res, sizeof(struct input)); memcpy(*res + input_index, ¤t_input, sizeof(struct input)); input_index++; } free(input_buffer); return input_index; } int main(int argc, char *argv[]) { struct input *inputs = NULL; if (argc < 2 || argc > 3) { printf("%s\n", USAGE_STRING); return -1; } else if (argc == 3) { printf("Not yet implemented."); return -1; } int num_threads = strtol(argv[1], NULL, 10); if (errno == ERANGE || (num_threads == 0 && errno == EINVAL)) { printf("Invalid number of threads.\n"); } num_threads = num_threads > MAXTHREAD ? MAXTHREAD : num_threads; // TODO: Remove this. It's for debugging. int num_inputs = get_input(num_threads, &inputs); for (int i = 0; i < num_inputs; i++) { printf("#%d recipient=%d value=%d\n", i, inputs[i].recipient, inputs[i].value); } free(inputs); }