Browse Source

add read file functionality

Chris Cromer 7 months ago
parent
commit
a917b68766
9 changed files with 451 additions and 107 deletions
  1. 10
    0
      .gitignore
  2. 100
    100
      100.pto
  3. 1
    1
      Makefile
  4. 22
    0
      src/include/points.h
  5. 19
    0
      src/include/read_file.h
  6. 46
    4
      src/points.c
  7. 251
    0
      src/read_file.c
  8. 1
    1
      test/Makefile
  9. 1
    1
      test/test.c

+ 10
- 0
.gitignore View File

@@ -0,0 +1,10 @@
1
+Informe.pdf
2
+doc/*.aux
3
+doc/*.out
4
+doc/*.log
5
+doc/*.synctex.gz
6
+doc/*.toc
7
+test/test
8
+*.o
9
+points
10
+core

+ 100
- 100
100.pto View File

@@ -1,101 +1,101 @@
1 1
 100
2
-4.70399 2.61546 
3
-4.11505 5.40782 
4
-5.38695 7.04457 
5
-7.28393 6.57592 
6
-7.22851 4.6192 
7
-9.30246 1.96946 
8
-3.27011 4.08827 
9
-4.38336 2.60827 
10
-3.58596 1.25516 
11
-2.83329 1.84097 
12
-1.57041 7.29407 
13
-1.08953 8.75504 
14
-6.11049 6.26775 
15
-4.42073 4.45547 
16
-2.51608 4.17832 
17
-7.5074 7.86617 
18
-7.39983 8.79166 
19
-8.37735 9.50368 
20
-8.28499 9.42695 
21
-8.54374 7.33608 
22
-9.44417 7.20781 
23
-8.43204 4.21497 
24
-8.43641 0.312691 
25
-8.99538 1.95157 
26
-9.00337 3.45095 
27
-1.18444 1.77786 
28
-0.619263 2.57976 
29
-1.1813 1.11385 
30
-2.43991 1.47131 
31
-2.72304 0.350919 
32
-3.56181 0.339801 
33
-4.39165 1.2697 
34
-3.30581 1.2275 
35
-2.79924 1.89947 
36
-3.74776 5.40936 
37
-4.26677 3.85951 
38
-4.34214 5.17149 
39
-3.46184 4.94453 
40
-2.15366 4.32258 
41
-1.98295 3.55355 
42
-1.30004 3.26112 
43
-1.66377 3.69903 
44
-1.707 4.2865 
45
-1.07164 7.35545 
46
-1.60081 6.9651 
47
-0.305693 7.65618 
48
-1.66486 7.6809 
49
-0.0521513 7.69084 
50
-0.488579 9.24692 
51
-1.66901 9.25729 
52
-1.35521 9.09874 
53
-1.34367 7.98094 
54
-2.70188 8.11911 
55
-2.11048 8.532 
56
-2.89328 9.40763 
57
-4.26524 7.32178 
58
-3.27553 6.72305 
59
-2.50356 6.35372 
60
-2.37225 6.40923 
61
-3.1371 5.97335 
62
-3.19697 5.73857 
63
-3.36636 6.2637 
64
-4.52454 6.5996 
65
-6.18448 5.30826 
66
-5.78067 5.81179 
67
-4.72225 6.57439 
68
-4.84385 9.4136 
69
-4.49361 9.59739 
70
-5.35009 8.95877 
71
-5.86514 8.11254 
72
-5.69065 6.91125 
73
-8.91671 9.17126 
74
-8.81438 9.74364 
75
-9.95562 9.5857 
76
-9.11793 9.58001 
77
-9.79628 9.12007 
78
-9.18419 8.51274 
79
-8.99504 6.68699 
80
-6.70575 5.62865 
81
-8.45891 6.25965 
82
-8.67494 5.68547 
83
-8.66115 3.10929 
84
-8.77804 4.7744 
85
-9.93093 3.45255 
86
-8.46596 3.4775 
87
-8.30869 4.83072 
88
-7.58965 4.04073 
89
-5.67133 2.20668 
90
-7.47257 1.87875 
91
-7.89039 2.16117 
92
-5.7009 1.01409 
93
-5.31577 1.66729 
94
-4.41841 0.812257 
95
-5.98034 0.690348 
96
-6.44768 0.604096 
97
-7.35945 0.788557 
98
-7.40586 0.891187 
99
-8.45234 1.65136 
100
-7.911 1.36198 
101
-9.56373 0.684329 
2
+4.70399 2.61546
3
+4.11505 5.40782
4
+5.38695 7.04457
5
+7.28393 6.57592
6
+7.22851 4.6192
7
+9.30246 1.96946
8
+3.27011 4.08827
9
+4.38336 2.60827
10
+3.58596 1.25516
11
+2.83329 1.84097
12
+1.57041 7.29407
13
+1.08953 8.75504
14
+6.11049 6.26775
15
+4.42073 4.45547
16
+2.51608 4.17832
17
+7.5074 7.86617
18
+7.39983 8.79166
19
+8.37735 9.50368
20
+8.28499 9.42695
21
+8.54374 7.33608
22
+9.44417 7.20781
23
+8.43204 4.21497
24
+8.43641 0.312691
25
+8.99538 1.95157
26
+9.00337 3.45095
27
+1.18444 1.77786
28
+0.619263 2.57976
29
+1.1813 1.11385
30
+2.43991 1.47131
31
+2.72304 0.350919
32
+3.56181 0.339801
33
+4.39165 1.2697
34
+3.30581 1.2275
35
+2.79924 1.89947
36
+3.74776 5.40936
37
+4.26677 3.85951
38
+4.34214 5.17149
39
+3.46184 4.94453
40
+2.15366 4.32258
41
+1.98295 3.55355
42
+1.30004 3.26112
43
+1.66377 3.69903
44
+1.707 4.2865
45
+1.07164 7.35545
46
+1.60081 6.9651
47
+0.305693 7.65618
48
+1.66486 7.6809
49
+0.0521513 7.69084
50
+0.488579 9.24692
51
+1.66901 9.25729
52
+1.35521 9.09874
53
+1.34367 7.98094
54
+2.70188 8.11911
55
+2.11048 8.532
56
+2.89328 9.40763
57
+4.26524 7.32178
58
+3.27553 6.72305
59
+2.50356 6.35372
60
+2.37225 6.40923
61
+3.1371 5.97335
62
+3.19697 5.73857
63
+3.36636 6.2637
64
+4.52454 6.5996
65
+6.18448 5.30826
66
+5.78067 5.81179
67
+4.72225 6.57439
68
+4.84385 9.4136
69
+4.49361 9.59739
70
+5.35009 8.95877
71
+5.86514 8.11254
72
+5.69065 6.91125
73
+8.91671 9.17126
74
+8.81438 9.74364
75
+9.95562 9.5857
76
+9.11793 9.58001
77
+9.79628 9.12007
78
+9.18419 8.51274
79
+8.99504 6.68699
80
+6.70575 5.62865
81
+8.45891 6.25965
82
+8.67494 5.68547
83
+8.66115 3.10929
84
+8.77804 4.7744
85
+9.93093 3.45255
86
+8.46596 3.4775
87
+8.30869 4.83072
88
+7.58965 4.04073
89
+5.67133 2.20668
90
+7.47257 1.87875
91
+7.89039 2.16117
92
+5.7009 1.01409
93
+5.31577 1.66729
94
+4.41841 0.812257
95
+5.98034 0.690348
96
+6.44768 0.604096
97
+7.35945 0.788557
98
+7.40586 0.891187
99
+8.45234 1.65136
100
+7.911 1.36198
101
+9.56373 0.684329

+ 1
- 1
Makefile View File

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

+ 22
- 0
src/include/points.h View File

@@ -0,0 +1,22 @@
1
+/*
2
+ * Copyright 2018 Christopher Cromer
3
+ * Copyright 2018 Rodolfo Cuevas
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+ *
9
+ * 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.
10
+ *
11
+ * 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.
12
+ *
13
+ * 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.
14
+ */
15
+
16
+#ifndef _POINTS_POINTS
17
+	#define _POINTS_POINTS
18
+	typedef struct Point {
19
+		float x;
20
+		float y;
21
+	} point_t;
22
+#endif

+ 19
- 0
src/include/read_file.h View File

@@ -0,0 +1,19 @@
1
+/*
2
+ * Copyright 2018 Christopher Cromer
3
+ * Copyright 2018 Rodolfo Cuevas
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+ *
9
+ * 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.
10
+ *
11
+ * 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.
12
+ *
13
+ * 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.
14
+ */
15
+
16
+#ifndef _POINTS_READ_FILE
17
+	#define _POINTS_READ_FILE
18
+	int read_file(char *filename, point_t **points, unsigned int *n);
19
+#endif

+ 46
- 4
src/points.c View File

@@ -17,9 +17,9 @@
17 17
 #include <stdlib.h>
18 18
 #include <getopt.h>
19 19
 #include <string.h>
20
-#include <errno.h>
21
-#include <limits.h>
22 20
 #include "timer.h"
21
+#include "points.h"
22
+#include "read_file.h"
23 23
 
24 24
 #define POINTS_VERSION "1.0.0"
25 25
 
@@ -28,6 +28,7 @@
28 28
  */
29 29
 void print_usage() {
30 30
 	fprintf(stdout, "uso: points [OPCIÓN]\n");
31
+	fprintf(stdout, "  -f, --file                 el archivo de puntos a correr\n");
31 32
 	fprintf(stdout, "  -a, --all                  usar todos los algoritmos de ordenamentio\n");
32 33
 	fprintf(stdout, "  -b, --brute                usar brute force\n");
33 34
 	fprintf(stdout, "  -d, --divide               usar divide and conquer\n");
@@ -38,14 +39,19 @@ void print_usage() {
38 39
 /**
39 40
  * La entrada del programa
40 41
  * @param argc La cantidad de argumentos pasado al programa
42
+ * @param argv Los argumento pasado al programa
41 43
  * @return Retorna el codigo de error o 0 por exito
42 44
  */
43 45
 int main (int argc, char **argv) {
46
+	point_t *points = NULL;
47
+	char *filename = NULL;
44 48
 	int brute = 0;
45 49
 	int divide = 0;
46 50
 	int opt;
47 51
 	int long_index = 0;
52
+	unsigned int n = 0;
48 53
 	static struct option long_options[] = {
54
+		{"file",		required_argument,	0,	'f'},
49 55
 		{"all",			no_argument,		0,	'a'},
50 56
 		{"brute",		no_argument,		0,	'b'},
51 57
 		{"divide",		no_argument,		0,	'd'},
@@ -59,8 +65,16 @@ int main (int argc, char **argv) {
59 65
 		return 0;
60 66
 	}
61 67
 
62
-	while ((opt = getopt_long(argc, argv, "abdhv", long_options, &long_index)) != -1) {
68
+	while ((opt = getopt_long(argc, argv, "f:abdhv", long_options, &long_index)) != -1) {
63 69
 		switch (opt) {
70
+			case 'f':
71
+				if (filename != NULL) {
72
+					free(filename);
73
+					filename = NULL;
74
+				}
75
+				filename = malloc(sizeof(optarg) * strlen(optarg));
76
+				strcpy(filename, optarg);
77
+				break;
64 78
 			case 'a':
65 79
 				brute = 1;
66 80
 				divide = 1;
@@ -73,13 +87,22 @@ int main (int argc, char **argv) {
73 87
 				break;
74 88
 			case 'h':
75 89
 				print_usage();
90
+				if (filename != NULL) {
91
+					free(filename);
92
+				}
76 93
 				return 0;
77 94
 			case 'v':
78 95
 				printf("points versión: %s\n", POINTS_VERSION);
96
+				if (filename != NULL) {
97
+					free(filename);
98
+				}
79 99
 				return 0;
80 100
 				break;
81 101
 			default:
82 102
 				print_usage();
103
+				if (filename != NULL) {
104
+					free(filename);
105
+				}
83 106
 				return 1;
84 107
 		}
85 108
 	}
@@ -87,7 +110,26 @@ int main (int argc, char **argv) {
87 110
 	if (!brute && !divide) {
88 111
 		fprintf(stderr, "Error: No se seleccionó un algoritmo valido!\n");
89 112
 		print_usage();
90
-		return 4;
113
+		if (filename != NULL) {
114
+			free(filename);
115
+		}
116
+		return 2;
117
+	}
118
+
119
+	points = malloc(sizeof(point_t));
120
+	if ((opt = read_file(filename, &points, &n)) == 1) {
121
+		if (filename != NULL) {
122
+			free(filename);
123
+		}
124
+		if (opt > 3) {
125
+			// Only print this error if the file was able to be opened
126
+			fprintf(stderr, "Error: El archivo es corrupto!\n");
127
+		}
128
+		return 3;
129
+	}
130
+
131
+	if (filename != NULL) {
132
+		free(filename);
91 133
 	}
92 134
 
93 135
 	return 0;

+ 251
- 0
src/read_file.c View File

@@ -0,0 +1,251 @@
1
+/*
2
+ * Copyright 2018 Christopher Cromer
3
+ * Copyright 2018 Rodolfo Cuevas
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+ *
9
+ * 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.
10
+ *
11
+ * 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.
12
+ *
13
+ * 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.
14
+ */
15
+
16
+#include <stdio.h>
17
+#include <stdlib.h>
18
+#include <unistd.h>
19
+#include <limits.h>
20
+#include <string.h>
21
+#include <errno.h>
22
+#include <math.h>
23
+#include <sys/stat.h>
24
+#include "points.h"
25
+
26
+/**
27
+ * Leer el buffer y guardar el valor si es un float
28
+ * @param variable Donde se guarda el valor del buffer
29
+ * @param buffer El buffer a leer
30
+ * @return Retorna 1 si es exitosa ó 0 si falla
31
+ */
32
+int read_float_buffer(float *variable, char *buffer) {
33
+	char *check;
34
+	errno = 0;
35
+	float input = strtof(buffer, &check);
36
+	if (buffer == check) {
37
+		// Empty
38
+		#ifdef DEBUG
39
+		fprintf(stderr, "Error: Empty!\n");
40
+		#endif
41
+		return 0;
42
+	}
43
+	else if (errno == ERANGE && input == INFINITY) {
44
+		// Overflow
45
+		#ifdef DEBUG
46
+		fprintf(stderr, "Error: Overflow!\n");
47
+		#endif
48
+		return 0;
49
+	}
50
+	else if (errno == ERANGE && input == -INFINITY) {
51
+		// Underflow
52
+		#ifdef DEBUG
53
+		fprintf(stderr, "Error: Underflow!\n");
54
+		#endif
55
+		return 0;
56
+	}
57
+	else if (errno == EINVAL) {  /* not in all c99 implementations - gcc OK */
58
+		// Base contains unsupported value
59
+		// This check is not in all c99 implementations, but does exist in gcc
60
+		#ifdef DEBUG
61
+		fprintf(stderr, "Error: Unsupported value!\n");
62
+		#endif
63
+		return 0;
64
+	}
65
+	else if (errno != 0 && input == 0) {
66
+		// Unspecified error
67
+		#ifdef DEBUG
68
+		fprintf(stderr, "Error: Non specified error!\n");
69
+		#endif
70
+		return 0;
71
+	}
72
+	else if (errno == 0 && !*check) {
73
+		// Valid number
74
+		*variable = input;
75
+		return 1;
76
+	}
77
+	else if (errno == 0 && *check != 0) {
78
+		// Contains non numeric characters
79
+		#ifdef DEBUG
80
+		fprintf(stderr, "Error: Non numeric characters!\n");
81
+		#endif
82
+		return 0;
83
+	}
84
+	else {
85
+		#ifdef DEBUG
86
+		fprintf(stderr, "Error: Unknown error!\n");
87
+		#endif
88
+		return 0;
89
+	}
90
+}
91
+
92
+/**
93
+ * Leer el buffer y guardar el valor si es un unsigned int
94
+ * @param variable Donde se guarda el valor del buffer
95
+ * @param buffer El buffer a leer
96
+ * @return Retorna 1 si es exitosa ó 0 si falla
97
+ */
98
+int read_int_buffer(unsigned int *variable, char *buffer) {
99
+	char *check;
100
+	errno = 0;
101
+	long input = strtol(buffer, &check, 10);
102
+	if (buffer == check) {
103
+		// Empty
104
+		#ifdef DEBUG
105
+		fprintf(stderr, "Error: Empty!\n");
106
+		#endif
107
+		return 0;
108
+	}
109
+	else if (errno == ERANGE && input == LONG_MIN) {
110
+		// Overflow
111
+		#ifdef DEBUG
112
+		fprintf(stderr, "Error: Overflow!\n");
113
+		#endif
114
+		return 0;
115
+	}
116
+	else if (errno == ERANGE && input == LONG_MAX) {
117
+		// Underflow
118
+		#ifdef DEBUG
119
+		fprintf(stderr, "Error: Underflow!\n");
120
+		#endif
121
+		return 0;
122
+	}
123
+	else if (errno == EINVAL) {  /* not in all c99 implementations - gcc OK */
124
+		// Base contains unsupported value
125
+		// This check is not in all c99 implementations, but does exist in gcc
126
+		#ifdef DEBUG
127
+		fprintf(stderr, "Error: Unsupported value!\n");
128
+		#endif
129
+		return 0;
130
+	}
131
+	else if (errno != 0 && input == 0) {
132
+		// Unspecified error
133
+		#ifdef DEBUG
134
+		fprintf(stderr, "Error: Non specified error!\n");
135
+		#endif
136
+		return 0;
137
+	}
138
+	else if (errno == 0 && !*check) {
139
+		// Valid number
140
+		*variable = (unsigned int) input;
141
+		return 1;
142
+	}
143
+	else if (errno == 0 && *check != 0) {
144
+		// Contains non numeric characters
145
+		#ifdef DEBUG
146
+		fprintf(stderr, "Error: Non numeric characters!\n");
147
+		#endif
148
+		return 0;
149
+	}
150
+	else {
151
+		#ifdef DEBUG
152
+		fprintf(stderr, "Error: Unknown error!\n");
153
+		#endif
154
+		return 0;
155
+	}
156
+}
157
+
158
+/**
159
+ * Leer el archivo de puntos
160
+ * @param filename El nombre del archivo
161
+ * @param points Un arreglo de puntos
162
+ * @param n La cantidad de puntos en el archivo
163
+ * @return Retorna 0 con exito o un numero de error al contrario
164
+ */
165
+int read_file(char *filename, point_t **points, unsigned int *n) {
166
+	FILE *file = NULL;
167
+	ssize_t chars = 0;
168
+	char *buffer = NULL;
169
+	char *token;
170
+	size_t size = 0;
171
+	float coord;
172
+	int i;
173
+	int j = 0;
174
+    struct stat statbuff;
175
+
176
+    stat(filename, &statbuff);
177
+	if (access(filename, F_OK) || !S_ISREG(statbuff.st_mode)) {
178
+		fprintf(stderr, "Error: El archivo \"%s\" no existe!\n", filename);
179
+		return 1;
180
+	}
181
+	if (access(filename, R_OK)) {
182
+		fprintf(stderr, "Error: No se puede leer el archivo \"%s\"!\n", filename);
183
+		return 2;
184
+	}
185
+
186
+    file = fopen(filename, "r");
187
+    if (file == NULL) {
188
+        perror("fopen");
189
+        return 4;
190
+    }
191
+
192
+	while ((chars = getline(&buffer, &size, file)) != -1) {
193
+		if (*n == 0) {
194
+			for (i = 0; i < chars; i++) {
195
+				if (buffer[i] == ' ' || buffer[i] == '\n' || buffer[i] == '\r' || buffer[i] == '\t') {
196
+					buffer[i] = '\0';
197
+				}
198
+			}
199
+			if (!read_int_buffer(n, buffer)) {
200
+				fprintf(stderr, "Error: Numero de puntos invalido!\n");
201
+				return 5;
202
+			}
203
+			if (*n > UINT_MAX || *n < 1) {
204
+				if (sizeof(unsigned int) == 4) {
205
+					fprintf(stderr, "Error: La cantidad de puntos tiene que ser menor de 4,294,967,295 y mayor de 0\n");
206
+				}
207
+				else {
208
+					fprintf(stderr, "Error: La cantidad de puntos tiene que ser menor de 65,535 y mayor de 0\n");
209
+				}
210
+				return 6;
211
+			}
212
+			*points = realloc(*points, sizeof(point_t) * *n);
213
+		}
214
+		else {
215
+			for (i = 0; i < chars; i++) {
216
+				if (buffer[i] == '\n' || buffer[i] == '\r' || buffer[i] == '\t') {
217
+					buffer[i] = '\0';
218
+				}
219
+			}
220
+			i = 0;
221
+			token = strtok(buffer, " ");
222
+			while (token != NULL) {
223
+				if (i == 0) {
224
+					if (!read_float_buffer(&coord, token)) {
225
+						fprintf(stderr, "Error: Coordinado invalido!\n");
226
+						return 7;
227
+					}
228
+					(*points)[j].x = coord;
229
+				}
230
+				else if (i == 1) {
231
+					if (!read_float_buffer(&coord, token)) {
232
+						fprintf(stderr, "Error: Coordinado invalido!\n");
233
+						return 7;
234
+					}
235
+					(*points)[j].y = coord;
236
+					j++;
237
+				}
238
+				else {
239
+					break;
240
+				}
241
+				i++;
242
+				token = strtok(NULL, " ");
243
+			}
244
+		}
245
+		free(buffer);
246
+		buffer = NULL;
247
+		size = 0;
248
+	}
249
+
250
+	return 0;
251
+}

+ 1
- 1
test/Makefile View File

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

+ 1
- 1
test/test.c View File

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

Loading…
Cancel
Save