-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeos_perf_read_data.c
176 lines (148 loc) · 5.54 KB
/
geos_perf_read_data.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <zlib.h>
#include "geos_perf.h"
/* CHUNK_SIZE is the size of the memory chunk used by the zlib routines. */
#define CHUNK_SIZE 0x4000
/* The following macro calls a zlib routine and checks the return
value. If the return value ("status") is not OK, it prints an error
message and exits the program. Zlib's error statuses are all less
than zero. */
#define CALL_ZLIB(x) { \
int status; \
status = x; \
if (status < 0) { \
fprintf (stderr, \
"%s:%d: %s returned a bad status of %d.\n", \
__FILE__, __LINE__, #x, status); \
exit (EXIT_FAILURE); \
} \
}
/* if "test" is true, print an error message and halt execution. */
#define FAIL(test,file_name,message) { \
if (test) { \
inflateEnd (& strm); \
fprintf (stderr, "%s:%d: " message \
" file '%s' failed: %s\n", \
__FILE__, __LINE__, file_name, \
strerror (errno)); \
exit (EXIT_FAILURE); \
} \
}
#define TFAIL(test,message) { \
if (test) { \
fprintf (stderr, "%s:%d: " message \
" file '%s' failed: %s\n", \
__FILE__, __LINE__, file_name, \
strerror (errno)); \
exit (EXIT_FAILURE); \
} \
}
/* These are parameters to inflateInit2. See
http://zlib.net/manual.html for the exact meanings. */
#define INFLATE_WINDOW_BITS 15
#define ENABLE_ZLIB_GZIP 32
#define TMPFILE "/tmp/geos_perf_tmp"
static int
decompress_data_file(const char* file_name, const char* out_file_name)
{
FILE * file;
FILE * outfile;
z_stream strm = {0};
unsigned char in[CHUNK_SIZE];
unsigned char out[CHUNK_SIZE];
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = in;
strm.avail_in = 0;
CALL_ZLIB (inflateInit2(&strm, INFLATE_WINDOW_BITS | ENABLE_ZLIB_GZIP));
/* Open the file. */
file = fopen(file_name, "rb");
FAIL (!file, file_name, "open input");
outfile = fopen(out_file_name, "w");
FAIL (!outfile, out_file_name, "open output");
while (1)
{
int bytes_read;
int zlib_status;
bytes_read = fread (in, sizeof (char), sizeof (in), file);
FAIL (ferror (file), file_name, "read");
strm.avail_in = bytes_read;
strm.next_in = in;
do {
unsigned have;
strm.avail_out = CHUNK_SIZE;
strm.next_out = out;
zlib_status = inflate (& strm, Z_NO_FLUSH);
switch (zlib_status)
{
case Z_OK:
case Z_STREAM_END:
case Z_BUF_ERROR:
break;
default:
inflateEnd (& strm);
fprintf (stderr, "Gzip error %d in '%s'.\n", zlib_status, file_name);
return -1;
}
have = CHUNK_SIZE - strm.avail_out;
/* Copy the decompressed output to the landing location */
fwrite (out, sizeof(unsigned char), have, outfile);
} while (strm.avail_out == 0);
if (feof(file)) {
inflateEnd(&strm);
break;
}
}
FAIL (fclose (file), file_name, "close input");
FAIL (fclose (outfile), out_file_name, "close output");
return 0;
}
int
read_data_file(const char* file_name, GEOSGeometryList* geoms)
{
char full_file_name[MAXSTRLEN];
snprintf(full_file_name, MAXSTRLEN, "%s/%s", DATA_DIR, file_name);
int read_rv = decompress_data_file(full_file_name, TMPFILE);
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
FILE * file = fopen(TMPFILE, "r");
TFAIL (!file, "open tmp input");
GEOSWKTReader* reader = GEOSWKTReader_create();
while ((linelen = getline(&line, &linecap, file)) > 0)
{
GEOSGeometry* g = GEOSWKTReader_read(reader, line);
if (g)
{
size_t glsz = geomlist_push(geoms, g);
}
}
GEOSWKTReader_destroy(reader);
TFAIL (fclose (file), "close input");
return 0;
}
GEOSGeometry *
read_geometry_file(const char* file_name)
{
char full_file_name[MAXSTRLEN];
snprintf(full_file_name, MAXSTRLEN, "%s/%s", DATA_DIR, file_name);
int read_rv = decompress_data_file(full_file_name, TMPFILE);
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
FILE * file = fopen(TMPFILE, "r");
TFAIL (!file, "open tmp input");
GEOSGeometry *g;
GEOSWKTReader* reader = GEOSWKTReader_create();
if ((linelen = getline(&line, &linecap, file)) > 0)
{
g = GEOSWKTReader_read(reader, line);
}
GEOSWKTReader_destroy(reader);
TFAIL (fclose (file), "close input");
return g;
}