-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
262 lines (226 loc) · 6.6 KB
/
main.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "utshp.h"
typedef struct point {
double x;
double y;
} Point;
typedef struct pointM {
double x;
double y;
double m;
} PointM;
typedef struct polyline {
double box[4];
int num_parts;
int num_points;
int *parts;
Point *points;
} PolyLine;
Record *parse_record(FILE *fp);
PolyLine *parse_polyline(unsigned char *buf);
Point *parse_points(unsigned char *buf, int num_points);
PointM *parse_pointM(unsigned char *buf);
int *parse_parts(unsigned char *buf, int num_parts);
void record_nth_print(Record *head, int n);
void record_free(Record *head, int shape_type);
int main(int argc, char **argv)
{
char *filename = argv[1];
FILE *fp = fopen(filename, "rb");
if (fp == NULL)
{
printf("Filename not found\n");
return -1;
}
Record *record = NULL;
char buf[256];
int n;
int len = 0;
int shape_type = parse_header(fp);
while (record_prepend(&record, parse_record(fp)) == 0)
len++;
record_reverse(&record);
printf("Total number of record\t%d\n", len);
while (1)
{
printf("\n>>> Enter record number ('q' to quit): ");
scanf("%s", buf);
n = atoi(buf);
if (n == 0)
break;
record_nth_print(record, n);
}
record_free(record, shape_type);
fclose(fp);
}
Record *parse_record(FILE *fp)
{
unsigned char header[8];
int num_len[2];
int nitems = fread(header, sizeof(header), 1, fp);
if (nitems != 1)
return NULL;
parse_int32(header, num_len, 2, 1); // Parse record number, length
unsigned char content[num_len[1] * 2];
int shape_type;
fread(content, sizeof(content), 1, fp); // Read content of record
parse_int32(content, &shape_type, 1, 0); // Parse shapetype
Record *record = malloc(sizeof(Record));
record->num = num_len[0];
record->len = num_len[1] * 2;
record->shape_type = shape_type;
record->next = NULL;
switch (shape_type)
{
case 1:
record->shape = parse_points(content + 4, 1); // Parse Point
break;
case 3:
case 5: // Parse Polygon
record->shape = parse_polyline(content + 4); // Parse PolyLine
break;
case 21:
record->shape = parse_pointM(content + 4); // Parse PointM
break;
default:
printf("Unknown shape type\n");
break;
}
return record;
}
PolyLine *parse_polyline(unsigned char *buf)
{
PolyLine *polyline = malloc(sizeof(PolyLine));
double box[4];
int parts_points[2];
int i;
parse_double(buf, box, 4);
parse_int32(buf + 32, parts_points, 2, 0);
for (i = 0; i < 4; i++)
polyline->box[i] = box[i];
polyline->num_parts = parts_points[0];
polyline->num_points = parts_points[1];
polyline->parts = parse_parts(buf + 40, polyline->num_parts);
polyline->points = parse_points(buf + 40 + (4 * polyline->num_parts),
polyline->num_points);
return polyline;
}
int *parse_parts(unsigned char *buf, int num_parts)
{
int *parts = malloc(sizeof(int) * num_parts);
parse_int32(buf, parts, num_parts, 0);
return parts;
}
Point *parse_points(unsigned char *buf, int num_points)
{
Point *points = malloc(sizeof(Point) * num_points);
#if 1
// Almost safe. Not exactly portable: only works on 64-bit machines
parse_double(buf, (double *)points, num_points * 2);
#else
for (int i = 0; i < num_points; i++, buf += 16)
{
double xy[2];
parse_double(buf, xy, 2);
points[i].x = xy[0];
points[i].y = xy[1];
}
#endif
return points;
}
PointM *parse_pointM(unsigned char *buf)
{
PointM *pointm = malloc(sizeof(PointM));
#if 1
// Only works on 64-bit machines
parse_double(buf, (double *)pointm, 3);
#else
double xym[3];
parse_double(buf, xym, 3);
pointm->x = xym[0];
pointm->y = xym[1];
pointm->m = xym[2];
#endif
return pointm;
}
void record_nth_print(Record *head, int n)
{
Record *record = head;
Point *point;
PolyLine *polyline;
PointM *pointm;
int i;
if (n > record_length(head))
{
printf("This record doesn't exist\n");
return;
}
for (i = 0; i < n - 1; i++)
record = record->next;
printf("\nRecord number\t%d\n", record->num);
printf("Record length\t%d\n", record->len);
printf("Shape type \t%d (%s)\n", record->shape_type,
shape_type(record->shape_type));
switch (record->shape_type)
{
case 1:
point = record->shape;
printf("Point\t X %f\t Y %f\n", point->x, point->y);
break;
case 3:
case 5:
polyline = record->shape;
printf("Box Xmin, Ymin, Xmax, Ymax\n");
for (i = 0; i < 4; i++)
printf("%f\t", polyline->box[i]);
printf("\n");
printf("NumParts\t%d\n", polyline->num_parts);
printf("NumPoints\t%d\n", polyline->num_points);
printf("Parts \t");
for (i = 0; i < polyline->num_parts; i++)
printf("%d ", polyline->parts[i]);
printf("\n");
for (i = 0; i < polyline->num_points; i++)
{
printf("Point[%d]\tX %f\tY %f\n", i, polyline->points[i].x,
polyline->points[i].y);
}
break;
case 21:
pointm = record->shape;
if (pointm->m < - pow(10, 38))
printf("PointM\tX %f\t\tY %f\t M NaN\n", pointm->x, pointm->y);
else
{
printf("PointM\tX %f\t\tY %f\tM %f\n", pointm->x, pointm->y,
pointm->m);
}
break;
}
}
void record_free(Record *head, int shape_type)
{
PolyLine *polyline;
for (Record *p = head; p != NULL; p = head)
{
head = head->next;
switch (shape_type)
{
case 1:
case 21:
free(p->shape);
free(p);
break;
case 3:
case 5:
polyline = p->shape;
free(polyline->points);
free(polyline->parts);
free(polyline);
free(p);
break;
}
}
}