Ready for reading by JAVA.
[timetab.git] / map-extract.c
blob3181ea092a8b66a32303611b20f2374f990b5170
1 /*
2 * IDOS Map Extractor
4 * (c) 2013 Tomas Pokorny <jethro@kam.mff.cuni.cz>
6 * This software can be freely distributed and used according
7 * to the terms of the GNU General Public License.
8 */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <inttypes.h>
13 #include <sys/stat.h>
14 #include <errno.h>
15 #include <stdbool.h>
19 FILE * in;
21 int main(int argc, char * argv[])
23 in = fopen(argv[1], "r");
24 int32_t x;
25 int32_t y;
26 FILE * out;
27 int32_t len;
28 int32_t count;
29 int32_t next;
30 int32_t chunks;
31 int32_t blocks;
32 uint8_t * int_count;
33 char key;
34 int nulls;
36 next=0x89;
37 fclose(fopen("map/chunks.dat","w"));
38 fclose(fopen("map/stations.dat","w"));
40 fseek(in,next,SEEK_SET);
41 printf("Reading header\n");
42 fread(&len,1,sizeof(len),in);
43 fread(&blocks,1,sizeof(blocks),in);
44 int_count = malloc(blocks);
45 for (int i=0;i<blocks;i++)
46 int_count[i]=2;
47 printf("Len: %d, blocks: %d\n",len,blocks);
48 next = ftell(in)+len;
49 if (!(len%blocks==0))
50 printf("Skipping, header had changed.\n");
51 else{
53 int rec_len;
54 rec_len=len/blocks;
55 int32_t buf[rec_len];
56 for (int i=blocks;i>0;i--){
57 fread(buf,1,rec_len,in);
58 for (int j=0;j<rec_len;j++)
59 printf("%x ",buf[j]);
60 printf("\n");
64 // HACK
65 int_count[5]=int_count[6]=int_count[7]=3;
66 // /HACK
68 for (int blkid=0;blkid<blocks;blkid++)
70 fseek(in,next,SEEK_SET);
72 nulls=0;
74 do {
75 fread(&chunks,1,sizeof(chunks),in);
76 nulls++;
77 next+=4;
79 while (chunks==0);
81 printf("Read %d null ints, will read %d chunks\n",nulls,chunks);
82 if (int_count[blkid]==3)
84 next = ftell(in)-4;
85 fseek(in,next,SEEK_SET);
86 printf("Reading stations\n");
87 fread(&len,1,sizeof(len),in);
88 fread(&count,1,sizeof(count),in);
89 next=ftell(in)+len;
91 printf("Base: %x Next: %x, count: %d\n",(uint32_t)ftell(in),len,count);
93 int32_t idx;
94 out = fopen("map/stations.dat","a");
95 for (int i=count;i>0;i--)
97 fread(&x,1,sizeof(x),in);
98 fread(&y,1,sizeof(y),in);
99 fread(&idx,1,sizeof(idx),in);
100 fprintf(out,"%d %d %d\n",x,y,idx);
102 fprintf(out,"\n");
103 fclose(out);
104 } else {
106 printf("Reading %d chunks\n",chunks);
107 for (int j=chunks;j>0;j--)
109 fseek(in,next,SEEK_SET);
110 fread(&len,1,sizeof(len),in);
111 fread(&count,1,sizeof(count),in);
112 next=ftell(in)+len;
114 printf("Base: %x Next: %x, count: %d\n",(uint32_t)ftell(in),len,count);
116 out = fopen("map/chunks.dat","a");
117 for (int i=count;i>0;i--)
119 fread(&x,1,sizeof(x),in);
120 fread(&y,1,sizeof(y),in);
121 fprintf(out,"%d %d\n",x,y);
123 fprintf(out,"\n");
124 fclose(out);
131 printf("At: %x continue?",(uint32_t)ftell(in));
132 scanf("%c",&key);
134 fseek(in,next,SEEK_SET);
135 printf("Reading header\n");
136 do {
137 fread(&len,1,sizeof(len),in);
138 nulls++;
139 next+=4;
141 while (len==0);
142 printf("Read %d null ints, will read header of length %x \n",nulls,len);
144 fread(&blocks,1,sizeof(blocks),in);
145 int_count = malloc(blocks);
146 for (int i=0;i<blocks;i++)
147 int_count[i]=2;
148 printf("Len: %d, blocks: %d\n",len,blocks);
149 next = ftell(in)+len;
150 if (!(len%blocks==0))
151 printf("Skipping, header had changed.\n");
152 else{
154 int rec_len;
155 rec_len=len/blocks;
156 int32_t buf[rec_len];
157 for (int i=blocks;i>0;i--){
158 fread(buf,1,rec_len,in);
159 for (int j=0;j<rec_len;j++)
160 printf("%x ",buf[j]);
161 printf("\n");
165 // HACK
166 int_count[0]=3;
167 // /HACK
169 for (int blkid=0;blkid<blocks;blkid++)
171 fseek(in,next,SEEK_SET);
173 nulls=0;
175 do {
176 fread(&chunks,1,sizeof(chunks),in);
177 nulls++;
178 next+=4;
180 while (chunks==0);
182 printf("Read %d null ints, will read %d chunks\n",nulls,chunks);
183 if (int_count[blkid]==3)
185 next = ftell(in)-4;
186 fseek(in,next,SEEK_SET);
187 printf("Reading stations\n");
188 fread(&len,1,sizeof(len),in);
189 fread(&count,1,sizeof(count),in);
190 next=ftell(in)+len;
192 printf("Base: %x Next: %x, count: %d\n",(uint32_t)ftell(in),len,count);
194 int32_t idx;
195 out = fopen("map/stations.dat","a");
196 for (int i=count;i>0;i--)
198 fread(&x,1,sizeof(x),in);
199 fread(&y,1,sizeof(y),in);
200 fread(&idx,1,sizeof(idx),in);
201 fprintf(out,"%d %d %d\n",x,y,idx);
203 fprintf(out,"\n");
204 fclose(out);
205 } else {
207 printf("Reading %d chunks\n",chunks);
208 for (int j=chunks;j>0;j--)
210 fseek(in,next,SEEK_SET);
211 fread(&len,1,sizeof(len),in);
212 fread(&count,1,sizeof(count),in);
213 next=ftell(in)+len;
215 printf("Base: %x Next: %x, count: %d\n",(uint32_t)ftell(in),len,count);
217 out = fopen("map/chunks.dat","a");
218 for (int i=count;i>0;i--)
220 fread(&x,1,sizeof(x),in);
221 fread(&y,1,sizeof(y),in);
222 fprintf(out,"%d %d\n",x,y);
224 fprintf(out,"\n");
225 fclose(out);
234 printf("At: %x continue?",(uint32_t)ftell(in));