This commit is contained in:
bronku 2025-10-14 16:35:41 +02:00
parent 2b222dda0b
commit 3b1db93d74
5 changed files with 126 additions and 72 deletions

131
main.c
View file

@ -1,89 +1,124 @@
#include "record.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define MAX_RECORDS 1000
#define DEFAULT_NN 10000000
#define DEFAULT_N 1001
#define DEFAULT_B 10
#define END_PROGRAM -1
#define SUCCESS 0
#define ERROR -2
void print_usage()
{
printf("Usage: [-f filename] [-h]\n");
printf("Options:\n");
printf(" -f filename\tRead records from file\n");
printf(" -h\tShow this help message");
printf(" -f [filename]\tRead/Generate records from [filename]\n");
printf(" -r\t randomly generate N records, and process them\n");
printf(" -N [count]\tSet total number of records, default %d\n", DEFAULT_NN);
printf(" -n [count]\tSet blocking factor, default %d\n", DEFAULT_N);
printf(" -b [count]\tSet number of buffers, default %d\n", DEFAULT_B);
printf(" -h\tShow this help message\n");
}
// stream is function output
int process_args(int argc, char** argv, FILE** stream)
typedef struct
{
FILE* input;
bool generate_data;
bool manual_input;
int N; // number of records in a file
int b; // blocking factor
int n; // number of buffers
} options;
// reads args into opts
int process_args(int argc, char** argv, options* opts)
{
if (opts == NULL) {
return ERROR;
}
opts->input = stdin;
opts->generate_data = false;
opts->manual_input = false;
opts->N = DEFAULT_NN;
opts->n = DEFAULT_N;
opts->b = DEFAULT_B;
char* filename = NULL;
int opt;
while ((opt = getopt(argc, argv, "f:h")) != -1) {
while ((opt = getopt(argc, argv, "f:N:n:b:rhm")) != -1) {
switch (opt) {
case 'f':
filename = optarg;
break;
case 'r':
opts->generate_data = true;
break;
case 'N':
opts->N = atoi(optarg);
break;
case 'n':
opts->n = atoi(optarg);
break;
case 'b':
opts->b = atoi(optarg);
break;
case 'm':
opts->manual_input = true;
case 'h':
print_usage();
return END_PROGRAM;
case '?':
printf("invalid option, terminating\n");
fprintf(stderr, "invalid option, terminating\n");
return ERROR;
}
}
if (filename != NULL) {
printf("%s\n", filename);
*stream = fopen(filename, "r");
if (!*stream) {
fprintf(stderr, "Error: Cannot open file '%s'\n", filename);
return END_PROGRAM;
}
if (opts->manual_input == true && opts->generate_data == true) {
fprintf(stderr, "Incompatible arguments: mr, aborting\n");
return ERROR;
}
if (filename == NULL) {
fprintf(stderr, "No file specified, aborting\n");
return ERROR;
}
opts->input = fopen(filename, "a+");
if (!opts->input) {
fprintf(stderr, "Error: Cannot open file '%s'\n", filename);
return ERROR;
}
return SUCCESS;
}
void process_input(FILE* input)
void generate_file(options* opts)
{
record records[MAX_RECORDS];
int count = 0;
while (count < MAX_RECORDS) {
if (read_record(input, &records[count]) != SUCCESS) {
break;
}
count++;
}
}
printf("\nRead %d records\n", count);
qsort(records, count, sizeof(record), compare_records);
printf("\nSorted records:\n");
for (int i = 0; i < count; i++) {
printf("Record %d: ", i);
print_record(stdout, &records[i]);
printf("\n");
}
void write_file(options* opts)
{
}
void sort_file(options* opts)
{
}
int main(int argc, char** argv)
{
FILE* input = NULL;
if (argc > 1) {
int result = process_args(argc, argv, &input);
if (result != SUCCESS) {
return result;
}
if (input == NULL) {
printf("ok\n");
}
options opts;
if (argc <= 1) {
fprintf(stderr, "please specify input args\n");
return ERROR;
}
if (input == NULL) {
printf("Enter records (format: a_0 a_1 a_2 a_3 a_4 x), or 'q' to quit:\n");
input = stdin;
int result = process_args(argc, argv, &opts);
if (result != SUCCESS) {
return result;
}
process_input(input);
if (input != stdin) {
fclose(input);
if (opts.generate_data) {
srand(time(0));
generate_file(&opts);
}
if (opts.manual_input) {
write_file(&opts);
}
sort_file(&opts);
fclose(opts.input);
}