-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.c
More file actions
93 lines (68 loc) · 1.85 KB
/
Copy pathmap.c
File metadata and controls
93 lines (68 loc) · 1.85 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
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
#include "map.h"
#include "string.h"
int parseMap(tspsMap_t *map){
char *file = NULL;
char *weights = NULL;
char *aux = NULL;
int i, j;
if(loadMap(&file) != TSPS_RC_SUCCESS)
return TSPS_RC_FAILURE;
weights = strstr(file, "NODE_COORD_SECTION");
weights += 19;
map->numNodes = NUM_NODES;
map->nodes = (tspsNode*)malloc(sizeof(tspsNode)*map->numNodes);
aux = weights;
i = 0;
while(aux != NULL){
if(i >= map->numNodes)
break;
// skip the node id
if(i==0)
aux = strtok(weights, " ");
else
aux = strtok(NULL, " ");
if(i!=atoi(aux)-1){
printf("Error! i is [%d], should be [%d]\n", i, atoi(aux));
}
aux = strtok(NULL, " ");
map->nodes[i].x = atoi(aux);
aux = strtok(NULL, " \n");
map->nodes[i].y = atoi(aux);
if(*aux == '\n'){
aux++;
}
i++;
}
free(file);
return TSPS_RC_SUCCESS;
}
int loadMap(char **file){
FILE * pFile;
long lSize;
char *buffer;
size_t result;
if((pFile = fopen("maps/berlin52.tsp", "rb")) == NULL &&
(pFile = fopen("../maps/berlin52.tsp", "rb")) == NULL &&
(pFile = fopen("berlin52.tsp", "rb")) == NULL){
return TSPS_RC_FAILURE;
}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL)
return TSPS_RC_FAILURE;
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != lSize)
return TSPS_RC_FAILURE;
// terminate
fclose (pFile);
*file = buffer;
return TSPS_RC_SUCCESS;
}