-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapReader.c
72 lines (60 loc) · 1.65 KB
/
mapReader.c
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
/**
* @file
*
* @author Christopher Blöcker
*/
/* -------------------------------------------------------------------------- */
#include <stdio.h>
/* -------------------------------------------------------------------------- */
#include "mapReader.h"
/* -------------------------------------------------------------------------- */
/**
* Read the samples, i.e. city positions, from the given file name and return
* them.
*
* @param[in] filename File that contains the samples.
*
* @return SampleMap.
*/
extern SampleMap mapReaderRead(char * filename)
{
/* Error and number of samples to read */
int error = 0
, n
;
/* Sample position */
Vector p;
/* Dummies for parsing */
char space
, newline
;
/* Resulting sample map */
SampleMap res;
FILE * f = fopen(filename, "r");
error = !f;
/* Read number of samples */
if (error)
fprintf(stderr, "mapReaderRead :: Error opening file %s\n.", filename);
else
error = fscanf(f, "%i%c", &n, &newline) != 2 || newline != '\n';
/* Create sample map for the given number of samples */
if (error)
fprintf(stderr, "mapReaderRead :: Error reading sample count.\n");
else
res = sampleMapMake(n);
/* Read the samples and put them into the sample map */
for (int i = 0; i < n && !error; ++i)
{
error = fscanf(f, "%lf%c%lf%c", &p.x, &space, &p.y, &newline) != 4
|| space != ' '
|| newline != '\n';
if (error)
fprintf(stderr, "mapReaderRead :: Error reading sample %i.\n", i);
else
res = sampleMapPut(res, p);
}
/* Clear allocated memory in case of an error */
if (error)
res = sampleMapFree(res);
return res;
}