-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.c
More file actions
220 lines (165 loc) · 5.05 KB
/
Copy pathreader.c
File metadata and controls
220 lines (165 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Routine for evaluating population members */
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <string.h>
# include "global.h"
# include "rand.h"
/*Función que avanza el puntero del archivo hasta el string def */
void findDef(FILE *f, char *def) {
char word[3000];
/* assumes no word exceeds length of 1023 */
while (fscanf(f, " %1023s", word)) {
if(strcmp(word,def) == 0) break;
}
}
void removeSemicolon(char *line){
strtok(line, ";");
}
/* Básticamente cuenta cuantos espacios hay */
int countWords(char *line){
int words;
char linet[3000], *token;
strcpy(linet, line);
words = 0;
token = strtok(linet, " "); /* Obtenemos la primera palabra */
while( token != NULL ) {
words ++;
token = strtok(NULL, " "); /* Avanzamos a la siguiente palabra */
}
return words;
}
/* FUNCIONES PARA LEER CONJUNTOS: */
void readObjetos(FILE *f, problem_instance *pi){
char *token, id=0;
char line[3000];
fgets(line, sizeof(line), f);
removeSemicolon(line);
pi->nObj = countWords(line);
pi->objetos = malloc(pi->nObj*sizeof(objeto));
/*Ahora asignamos los ids a cada objeto*/
token=strtok(line, " ");
while( token != NULL ) {
pi->objetos[id].id=id;
pi->objetos[id].next = malloc(sizeof(objeto));
token=strtok(NULL, " ");
id++;
}
}
void readTelescopios(FILE *f, problem_instance *pi){
char *token, id=0;
char line[3000];
fgets(line, sizeof(line), f);
removeSemicolon(line);
pi->nTel = countWords(line);
pi->telescopios = malloc(pi->nTel*sizeof(telescopio));
/* Asignamos los ids a cada telescopio */
token=strtok(line, " ");
while( token != NULL ) {
pi->telescopios[id].id=id;
pi->telescopios[id].queue = malloc(sizeof(objeto));
token=strtok(NULL, " ");
id++;
}
}
/* FUNCIONES PARA LEER PARAMETROS */
void readObjetosParams(FILE *f, problem_instance *pi){
char line[3000];
char *token;
/* Nos saltamos la primera linea */
fgets(line, sizeof(line), f);
int i = 0;
while (1){
/*Nos saltamos la primera word, si es ; entonces break*/
fgets(line, sizeof(line), f);
token = strtok(line, " ");
if (strchr(token, ';') != NULL) break;
/* Recorremos todos los parametros de la linea */
token =strtok(NULL, " "); /* Para leer el primer parametro */
int param = 0;
while(token != NULL){
if(param == 0){
pi->objetos[i].l = atoi(token);
}
else if(param == 1){
pi->objetos[i].O = atoi(token);
}
else if(param == 2){
pi->objetos[i].D = atoi(token);
}
else if(param == 3){
pi->objetos[i].start_cost = atoi(token);
}
param++;
token=strtok(NULL, " ");
}
i++;
}
fgets(line, sizeof(line), f);
}
void readGiro(FILE *f, problem_instance *pi){
char line[3000];
char *token;
/* Pedimos memoria para el parámetro giro */
pi->giro = (int **)malloc(pi->nObj * sizeof(int*));
int i;
for(i=0; i < pi->nObj; i++){
pi->giro[i] = (int *)malloc(pi->nObj * sizeof(int));
}
/* Nos saltamos la primera linea */
fgets(line, sizeof(line), f);
int j=0;
i=0;
while(1){
/*Nos saltamos la primera word, si es ; entonces break*/
fgets(line, sizeof(line), f);
token = strtok(line, " ");
if (strchr(token, ';') != NULL) break;
/* Ahora asignamos el costo de giro */
token = strtok(NULL, " "); /* Avanzamos para enrtar al ciclo*/
j=0;
while(token != NULL){
pi->giro[i][j] = atoi(token);
token=strtok(NULL, " ");
j++;
}
i++;
}
}
int readInputFile(char* filePath, problem_instance *pi) {
int debug=0;
FILE* fh=fopen(filePath, "r");
/*check if file exists*/
if( fh==NULL ){
printf("File does not exists %s", filePath);
return 0;
}
if(debug) printf("Reading: %s \n", filePath);
/* Lectura de conjuntos */
findDef(fh, "N:=");
readObjetos(fh, pi);
if(debug) printf("End Objetos! \n");
findDef(fh, "M:=");
readTelescopios(fh, pi);
if(debug) printf("End Telescopios! \n");
/* Lectura de parámetros */
/*(no reconoce "l O D start_cost:=")*/
findDef(fh, "start_cost:=");
readObjetosParams(fh, pi);
if(debug) printf("End objetosParams! \n");
char find[3000];
sprintf(find, "o%d:=", pi->nObj); /*tenemos que buscar o8, donde 8 es el ultimo objeto */
findDef(fh, find);
readGiro(fh, pi);
if(debug) printf("End giro! \n");
/* Finished reading */
fclose(fh);
if(debug) printf("End Reading! \n \n");
/* Mostramos la instancia que leímos */
if (debug) printProblemInstance(pi);
if(debug) {
printf("Finished instance description, press any key to continue \n");
getchar();
}
return 0;
}