Merge branch 'readfile' of UBB/points into master

This commit is contained in:
Chris Cromer 2018-12-08 15:43:05 -03:00 committed by Gitea
commit 7837d51ab6
9 changed files with 452 additions and 7 deletions

10
.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
Informe.pdf
doc/*.aux
doc/*.out
doc/*.log
doc/*.synctex.gz
doc/*.toc
test/test
*.o
points
core

101
100.pto Normal file
View File

@ -0,0 +1,101 @@
100
4.70399 2.61546
4.11505 5.40782
5.38695 7.04457
7.28393 6.57592
7.22851 4.6192
9.30246 1.96946
3.27011 4.08827
4.38336 2.60827
3.58596 1.25516
2.83329 1.84097
1.57041 7.29407
1.08953 8.75504
6.11049 6.26775
4.42073 4.45547
2.51608 4.17832
7.5074 7.86617
7.39983 8.79166
8.37735 9.50368
8.28499 9.42695
8.54374 7.33608
9.44417 7.20781
8.43204 4.21497
8.43641 0.312691
8.99538 1.95157
9.00337 3.45095
1.18444 1.77786
0.619263 2.57976
1.1813 1.11385
2.43991 1.47131
2.72304 0.350919
3.56181 0.339801
4.39165 1.2697
3.30581 1.2275
2.79924 1.89947
3.74776 5.40936
4.26677 3.85951
4.34214 5.17149
3.46184 4.94453
2.15366 4.32258
1.98295 3.55355
1.30004 3.26112
1.66377 3.69903
1.707 4.2865
1.07164 7.35545
1.60081 6.9651
0.305693 7.65618
1.66486 7.6809
0.0521513 7.69084
0.488579 9.24692
1.66901 9.25729
1.35521 9.09874
1.34367 7.98094
2.70188 8.11911
2.11048 8.532
2.89328 9.40763
4.26524 7.32178
3.27553 6.72305
2.50356 6.35372
2.37225 6.40923
3.1371 5.97335
3.19697 5.73857
3.36636 6.2637
4.52454 6.5996
6.18448 5.30826
5.78067 5.81179
4.72225 6.57439
4.84385 9.4136
4.49361 9.59739
5.35009 8.95877
5.86514 8.11254
5.69065 6.91125
8.91671 9.17126
8.81438 9.74364
9.95562 9.5857
9.11793 9.58001
9.79628 9.12007
9.18419 8.51274
8.99504 6.68699
6.70575 5.62865
8.45891 6.25965
8.67494 5.68547
8.66115 3.10929
8.77804 4.7744
9.93093 3.45255
8.46596 3.4775
8.30869 4.83072
7.58965 4.04073
5.67133 2.20668
7.47257 1.87875
7.89039 2.16117
5.7009 1.01409
5.31577 1.66729
4.41841 0.812257
5.98034 0.690348
6.44768 0.604096
7.35945 0.788557
7.40586 0.891187
8.45234 1.65136
7.911 1.36198
9.56373 0.684329

View File

@ -1,7 +1,7 @@
CC=gcc
CFLAGS=-Wall -Isrc/include -DDEBUG -g
LDFLAGS=-lm
SRC=src/points.c src/timer.c src/random.c
SRC=src/points.c src/timer.c src/random.c src/read_file.c
OBJ=$(SRC:.c=.o)
all: points informe

22
src/include/points.h Normal file
View File

@ -0,0 +1,22 @@
/*
* Copyright 2018 Christopher Cromer
* Copyright 2018 Rodolfo Cuevas
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _POINTS_POINTS
#define _POINTS_POINTS
typedef struct Point {
float x;
float y;
} point_t;
#endif

19
src/include/read_file.h Normal file
View File

@ -0,0 +1,19 @@
/*
* Copyright 2018 Christopher Cromer
* Copyright 2018 Rodolfo Cuevas
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _POINTS_READ_FILE
#define _POINTS_READ_FILE
int read_file(char *filename, point_t **points, unsigned int *n);
#endif

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;

251
src/read_file.c Normal file
View File

@ -0,0 +1,251 @@
/*
* Copyright 2018 Christopher Cromer
* Copyright 2018 Rodolfo Cuevas
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <sys/stat.h>
#include "points.h"
/**
* Leer el buffer y guardar el valor si es un float
* @param variable Donde se guarda el valor del buffer
* @param buffer El buffer a leer
* @return Retorna 1 si es exitosa ó 0 si falla
*/
int read_float_buffer(float *variable, char *buffer) {
char *check;
errno = 0;
float input = strtof(buffer, &check);
if (buffer == check) {
// Empty
#ifdef DEBUG
fprintf(stderr, "Error: Empty!\n");
#endif
return 0;
}
else if (errno == ERANGE && input == INFINITY) {
// Overflow
#ifdef DEBUG
fprintf(stderr, "Error: Overflow!\n");
#endif
return 0;
}
else if (errno == ERANGE && input == -INFINITY) {
// Underflow
#ifdef DEBUG
fprintf(stderr, "Error: Underflow!\n");
#endif
return 0;
}
else if (errno == EINVAL) { /* not in all c99 implementations - gcc OK */
// Base contains unsupported value
// This check is not in all c99 implementations, but does exist in gcc
#ifdef DEBUG
fprintf(stderr, "Error: Unsupported value!\n");
#endif
return 0;
}
else if (errno != 0 && input == 0) {
// Unspecified error
#ifdef DEBUG
fprintf(stderr, "Error: Non specified error!\n");
#endif
return 0;
}
else if (errno == 0 && !*check) {
// Valid number
*variable = input;
return 1;
}
else if (errno == 0 && *check != 0) {
// Contains non numeric characters
#ifdef DEBUG
fprintf(stderr, "Error: Non numeric characters!\n");
#endif
return 0;
}
else {
#ifdef DEBUG
fprintf(stderr, "Error: Unknown error!\n");
#endif
return 0;
}
}
/**
* Leer el buffer y guardar el valor si es un unsigned int
* @param variable Donde se guarda el valor del buffer
* @param buffer El buffer a leer
* @return Retorna 1 si es exitosa ó 0 si falla
*/
int read_int_buffer(unsigned int *variable, char *buffer) {
char *check;
errno = 0;
long input = strtol(buffer, &check, 10);
if (buffer == check) {
// Empty
#ifdef DEBUG
fprintf(stderr, "Error: Empty!\n");
#endif
return 0;
}
else if (errno == ERANGE && input == LONG_MIN) {
// Overflow
#ifdef DEBUG
fprintf(stderr, "Error: Overflow!\n");
#endif
return 0;
}
else if (errno == ERANGE && input == LONG_MAX) {
// Underflow
#ifdef DEBUG
fprintf(stderr, "Error: Underflow!\n");
#endif
return 0;
}
else if (errno == EINVAL) { /* not in all c99 implementations - gcc OK */
// Base contains unsupported value
// This check is not in all c99 implementations, but does exist in gcc
#ifdef DEBUG
fprintf(stderr, "Error: Unsupported value!\n");
#endif
return 0;
}
else if (errno != 0 && input == 0) {
// Unspecified error
#ifdef DEBUG
fprintf(stderr, "Error: Non specified error!\n");
#endif
return 0;
}
else if (errno == 0 && !*check) {
// Valid number
*variable = (unsigned int) input;
return 1;
}
else if (errno == 0 && *check != 0) {
// Contains non numeric characters
#ifdef DEBUG
fprintf(stderr, "Error: Non numeric characters!\n");
#endif
return 0;
}
else {
#ifdef DEBUG
fprintf(stderr, "Error: Unknown error!\n");
#endif
return 0;
}
}
/**
* Leer el archivo de puntos
* @param filename El nombre del archivo
* @param points Un arreglo de puntos
* @param n La cantidad de puntos en el archivo
* @return Retorna 0 con exito o un numero de error al contrario
*/
int read_file(char *filename, point_t **points, unsigned int *n) {
FILE *file = NULL;
ssize_t chars = 0;
char *buffer = NULL;
char *token;
size_t size = 0;
float coord;
int i;
int j = 0;
struct stat statbuff;
stat(filename, &statbuff);
if (access(filename, F_OK) || !S_ISREG(statbuff.st_mode)) {
fprintf(stderr, "Error: El archivo \"%s\" no existe!\n", filename);
return 1;
}
if (access(filename, R_OK)) {
fprintf(stderr, "Error: No se puede leer el archivo \"%s\"!\n", filename);
return 2;
}
file = fopen(filename, "r");
if (file == NULL) {
perror("fopen");
return 4;
}
while ((chars = getline(&buffer, &size, file)) != -1) {
if (*n == 0) {
for (i = 0; i < chars; i++) {
if (buffer[i] == ' ' || buffer[i] == '\n' || buffer[i] == '\r' || buffer[i] == '\t') {
buffer[i] = '\0';
}
}
if (!read_int_buffer(n, buffer)) {
fprintf(stderr, "Error: Numero de puntos invalido!\n");
return 5;
}
if (*n > UINT_MAX || *n < 1) {
if (sizeof(unsigned int) == 4) {
fprintf(stderr, "Error: La cantidad de puntos tiene que ser menor de 4,294,967,295 y mayor de 0\n");
}
else {
fprintf(stderr, "Error: La cantidad de puntos tiene que ser menor de 65,535 y mayor de 0\n");
}
return 6;
}
*points = realloc(*points, sizeof(point_t) * *n);
}
else {
for (i = 0; i < chars; i++) {
if (buffer[i] == '\n' || buffer[i] == '\r' || buffer[i] == '\t') {
buffer[i] = '\0';
}
}
i = 0;
token = strtok(buffer, " ");
while (token != NULL) {
if (i == 0) {
if (!read_float_buffer(&coord, token)) {
fprintf(stderr, "Error: Coordinado invalido!\n");
return 7;
}
(*points)[j].x = coord;
}
else if (i == 1) {
if (!read_float_buffer(&coord, token)) {
fprintf(stderr, "Error: Coordinado invalido!\n");
return 7;
}
(*points)[j].y = coord;
j++;
}
else {
break;
}
i++;
token = strtok(NULL, " ");
}
}
free(buffer);
buffer = NULL;
size = 0;
}
return 0;
}

View File

@ -3,7 +3,7 @@ CFLAGS=-Wall -I../src/include -DDEBUG -g
SRC=test.c
OBJ=$(SRC:.c=.o)
OBJ+=../src/random.o
OBJ+=../src/random.o ../src/read_file.o
all: test

View File

@ -22,7 +22,7 @@
* El programa de test
*/
int main(int argc, char **argv) {
int pass;
//int pass;
int passed = 0;
int failed = 0;