add read file functionality

This commit is contained in:
2018-12-08 15:40:36 -03:00
parent bb8625a129
commit a917b68766
9 changed files with 451 additions and 107 deletions

View File

@@ -17,9 +17,9 @@
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include "timer.h"
#include "points.h"
#include "read_file.h"
#define POINTS_VERSION "1.0.0"
@@ -28,6 +28,7 @@
*/
void print_usage() {
fprintf(stdout, "uso: points [OPCIÓN]\n");
fprintf(stdout, " -f, --file el archivo de puntos a correr\n");
fprintf(stdout, " -a, --all usar todos los algoritmos de ordenamentio\n");
fprintf(stdout, " -b, --brute usar brute force\n");
fprintf(stdout, " -d, --divide usar divide and conquer\n");
@@ -38,14 +39,19 @@ void print_usage() {
/**
* La entrada del programa
* @param argc La cantidad de argumentos pasado al programa
* @param argv Los argumento pasado al programa
* @return Retorna el codigo de error o 0 por exito
*/
int main (int argc, char **argv) {
point_t *points = NULL;
char *filename = NULL;
int brute = 0;
int divide = 0;
int opt;
int long_index = 0;
unsigned int n = 0;
static struct option long_options[] = {
{"file", required_argument, 0, 'f'},
{"all", no_argument, 0, 'a'},
{"brute", no_argument, 0, 'b'},
{"divide", no_argument, 0, 'd'},
@@ -59,8 +65,16 @@ int main (int argc, char **argv) {
return 0;
}
while ((opt = getopt_long(argc, argv, "abdhv", long_options, &long_index)) != -1) {
while ((opt = getopt_long(argc, argv, "f:abdhv", long_options, &long_index)) != -1) {
switch (opt) {
case 'f':
if (filename != NULL) {
free(filename);
filename = NULL;
}
filename = malloc(sizeof(optarg) * strlen(optarg));
strcpy(filename, optarg);
break;
case 'a':
brute = 1;
divide = 1;
@@ -73,13 +87,22 @@ int main (int argc, char **argv) {
break;
case 'h':
print_usage();
if (filename != NULL) {
free(filename);
}
return 0;
case 'v':
printf("points versión: %s\n", POINTS_VERSION);
if (filename != NULL) {
free(filename);
}
return 0;
break;
default:
print_usage();
if (filename != NULL) {
free(filename);
}
return 1;
}
}
@@ -87,7 +110,26 @@ int main (int argc, char **argv) {
if (!brute && !divide) {
fprintf(stderr, "Error: No se seleccionó un algoritmo valido!\n");
print_usage();
return 4;
if (filename != NULL) {
free(filename);
}
return 2;
}
points = malloc(sizeof(point_t));
if ((opt = read_file(filename, &points, &n)) == 1) {
if (filename != NULL) {
free(filename);
}
if (opt > 3) {
// Only print this error if the file was able to be opened
fprintf(stderr, "Error: El archivo es corrupto!\n");
}
return 3;
}
if (filename != NULL) {
free(filename);
}
return 0;