Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr5.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2009.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.imgfmt.app.mdr;
14
15 import java.text.Collator;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 import uk.me.parabola.imgfmt.Utils;
23 import uk.me.parabola.imgfmt.app.ImgFileWriter;
24 import uk.me.parabola.imgfmt.app.srt.MultiSortKey;
25 import uk.me.parabola.imgfmt.app.srt.Sort;
26 import uk.me.parabola.imgfmt.app.srt.SortKey;
27
28 /**
29 * Section containing cities.
30 *
31 * We need: map number, city index in map, offset in LBL, flags
32 * and pointer into MDR 15 for the string name.
33 *
34 * @author Steve Ratcliffe
35 */
36 public class Mdr5 extends MdrMapSection {
37 private ArrayList<Mdr5Record> allCities = new ArrayList<>();
38 private ArrayList<Mdr5Record> cities;
39 private int maxCityIndex;
40 private int localCitySize;
41 private int mdr20PointerSize = 0; // bytes for mdr20 pointer, or 0 if no mdr20
42
43 // We need a common area to save the mdr20 values, since there can be multiple
44 // city records with the same global city index
45 private int[] mdr20s;
46
47 public Mdr5(MdrConfig config) {
48 setConfig(config);
49 }
50
51 public void addCity(Mdr5Record record) {
52 assert record.getMapIndex() != 0;
53 allCities.add(record);
54 if (record.getCityIndex() > maxCityIndex)
55 maxCityIndex = record.getCityIndex();
56 }
57
58 /**
59 * Called after all cities to sort and number them.
60 */
61 @Override
62 public void preWriteImpl() {
63 allCities.trimToSize();
64 localCitySize = Utils.numberToPointerSize(maxCityIndex + 1);
65 Sort sort = getConfig().getSort();
66 genCitiesAndMdr20s(sort);
67 // calculate positions for the different road indexes
68 calcMdr20SortPos(sort);
69 calcMdr21SortPos(sort);
70 calcMdr22SortPos(sort);
71 }
72
73 private void genCitiesAndMdr20s(Sort sort) {
74 List<SortKey<Mdr5Record>> sortKeys = new ArrayList<>(allCities.size());
75 Map<String, byte[]> regionCache = new HashMap<>();
76 Map<String, byte[]> countryCache = new HashMap<>();
77 for (Mdr5Record m : allCities) {
78 if (m.getName() == null)
79 continue;
80
81 // Sort by city name, region name, country name and map index.
82 SortKey<Mdr5Record> sortKey = sort.createSortKey(m, m.getName());
83 SortKey<Mdr5Record> regionKey = sort.createSortKey(null, m.getRegionName(), 0, regionCache);
84 SortKey<Mdr5Record> countryKey = sort.createSortKey(null, m.getCountryName(), m.getMapIndex(), countryCache);
85 sortKey = new MultiSortKey<>(sortKey, regionKey, countryKey);
86 sortKeys.add(sortKey);
87 }
88 regionCache = null;
89 countryCache = null;
90 sortKeys.sort(null);
91
92 cities = new ArrayList<>(sortKeys.size());
93 Collator collator = sort.getCollator();
94
95 int count = 0;
96 Mdr5Record lastCity = null;
97
98 mdr20s = new int[sortKeys.size()+1];
99 int mdr20count = 0;
100
101 for (SortKey<Mdr5Record> key : sortKeys) {
102 Mdr5Record c = key.getObject();
103 c.setMdr20set(mdr20s);
104
105 if (!c.isSameByName(collator, lastCity))
106 mdr20count++;
107 c.setMdr20Index(mdr20count);
108
109 if (c.isSameByMapAndName(collator, lastCity)) {
110 c.setGlobalCityIndex(count);
111 } else {
112 count++;
113 c.setGlobalCityIndex(count);
114 cities.add(c);
115
116 lastCity = c;
117 }
118 }
119 //cities.trimToSize(); // it might be a fraction smaller than allocated, but not worth the cost of reallocation
120 }
121
122 /**
123 * Calculate a position when sorting by name, region, and country- This is used for MDR20.
124 */
125 private void calcMdr20SortPos(Sort sort) {
126 List<SortKey<Mdr5Record>> sortKeys = new ArrayList<>(allCities.size());
127 Map<String, byte[]> regionCache = new HashMap<>();
128 Map<String, byte[]> countryCache = new HashMap<>();
129 for (Mdr5Record m : allCities) {
130 if (m.getName() == null)
131 continue;
132
133 // Sort by city name, region name, and country name .
134 SortKey<Mdr5Record> sortKey = sort.createSortKey(m, m.getName());
135 SortKey<Mdr5Record> regionKey = sort.createSortKey(null, m.getRegionName(), 0, regionCache);
136 SortKey<Mdr5Record> countryKey = sort.createSortKey(null, m.getCountryName(), 0, countryCache);
137 sortKey = new MultiSortKey<>(sortKey, regionKey, countryKey);
138 sortKeys.add(sortKey);
139 }
140 regionCache = null;
141 countryCache = null;
142 sortKeys.sort(null);
143
144 SortKey<Mdr5Record> lastKey = null;
145 int pos = 0;
146 for (SortKey<Mdr5Record> key : sortKeys) {
147 Mdr5Record c = key.getObject();
148 if (lastKey == null || key.compareTo(lastKey) != 0)
149 pos++;
150 c.setMdr20SortPos(pos);
151 lastKey = key;
152 }
153 }
154
155 /**
156 * Calculate a position when sorting by region- This is used for MDR21.
157 */
158 private void calcMdr21SortPos(Sort sort) {
159 List<SortKey<Mdr5Record>> sortKeys = new ArrayList<>(allCities.size());
160 for (Mdr5Record m : allCities) {
161 if (m.getRegionName() == null)
162 continue;
163
164 // Sort by region name.
165 sortKeys.add(sort.createSortKey(m, m.getRegionName()));
166 }
167 sortKeys.sort(null);
168
169 SortKey<Mdr5Record> lastKey = null;
170 int pos = 0;
171 for (SortKey<Mdr5Record> key : sortKeys) {
172 Mdr5Record c = key.getObject();
173 if (lastKey == null || key.compareTo(lastKey) != 0)
174 pos++;
175 c.setMdr21SortPos(pos);
176 lastKey = key;
177 }
178 }
179
180 /**
181 * Calculate a position when sorting by country- This is used for MDR22.
182 */
183
184 private void calcMdr22SortPos(Sort sort) {
185 List<SortKey<Mdr5Record>> sortKeys = new ArrayList<>(allCities.size());
186 for (Mdr5Record m : allCities) {
187 if (m.getCountryName() == null)
188 continue;
189
190 // Sort by country name .
191 SortKey<Mdr5Record> countryKey = sort.createSortKey(m, m.getCountryName());
192 sortKeys.add(countryKey);
193 }
194 sortKeys.sort(null);
195
196 SortKey<Mdr5Record> lastKey = null;
197 int pos = 0;
198 for (SortKey<Mdr5Record> key : sortKeys) {
199 Mdr5Record c = key.getObject();
200 if (lastKey == null || key.compareTo(lastKey) != 0)
201 pos++;
202 c.setMdr22SortPos(pos);
203 lastKey = key;
204 }
205 }
206
207 public void writeSectData(ImgFileWriter writer) {
208 Mdr5Record lastCity = null;
209 boolean hasString = hasFlag(0x8);
210 boolean hasRegion = hasFlag(0x4);
211 Collator collator = getConfig().getSort().getCollator();
212 for (Mdr5Record city : cities) {
213 int gci = city.getGlobalCityIndex();
214 addIndexPointer(city.getMapIndex(), gci);
215
216 // Work out if the name is the same as the previous one and set
217 // the flag if so.
218 int flag = 0;
219 int mapIndex = city.getMapIndex();
220 int region = city.getRegionIndex();
221
222 // Set the no-repeat flag if the name/region is different
223 if (!city.isSameByName(collator, lastCity)) {
224 flag = 0x800000;
225 lastCity = city;
226 }
227
228 // Write out the record
229 putMapIndex(writer, mapIndex);
230 putLocalCityIndex(writer, city.getCityIndex());
231 writer.put3u(flag | city.getLblOffset());
232 if (hasRegion)
233 writer.put2u(region);
234 if (hasString)
235 putStringOffset(writer, city.getStringOffset());
236 if (mdr20PointerSize > 0)
237 writer.putNu(mdr20PointerSize, city.getMdr20());
238 }
239 }
240
241 /**
242 * Put the map city index. This is the index within the individual map
243 * and not the global city index used in mdr11.
244 */
245 private void putLocalCityIndex(ImgFileWriter writer, int cityIndex) {
246 writer.putNu(localCitySize, cityIndex);
247 }
248
249 /**
250 * Base size of 8, plus enough bytes to represent the map number
251 * and the city number.
252 * @return The size of a record in this section.
253 */
254 public int getItemSize() {
255 PointerSizes sizes = getSizes();
256 int size = sizes.getMapSize()
257 + localCitySize
258 + 3
259 + mdr20PointerSize;
260 if (hasFlag(0x4))
261 size += 2;
262 if (hasFlag(0x8))
263 size += sizes.getStrOffSize();
264 return size;
265 }
266
267 protected int numberOfItems() {
268 return cities.size();
269 }
270
271 /**
272 * Known structure bits/masks:
273 * 0x0003 size of local city index - 1 (all values appear to work)
274 * 0x0004 has region/country
275 * 0x0008 has string
276 * 0x0010 ? set unconditionally ?
277 * 0x0040 mdr17 sub section
278 * 0x0100 mdr20 present
279 * 0x0400 28_29 offset
280 * 0x0800 mdr20 offset
281 * @return The value to be placed in the header.
282 */
283 public int getExtraValue() {
284 int val = (localCitySize - 1);
285 // String offset is only included for a mapsource index.
286 if (isForDevice() ) {
287 if (!getConfig().getSort().isMulti())
288 val |= 0x40; // mdr17 sub section present (not with unicode)
289 } else {
290 val |= 0x04; // region
291 val |= 0x08; // string
292 }
293 val |= 0x10;
294 if (getSizes().getNumberOfItems(20) > 0) {
295 mdr20PointerSize = getSizes().getMdr20Size();
296 val |= 0x100; // mdr20 present
297 }
298 return val;
299 }
300
301 @Override
302 protected void releaseMemory() {
303 allCities = null;
304 cities = null;
305 mdr20s = null;
306 }
307
308 /**
309 * Get a list of all the cities, including duplicate named ones.
310 * @return All cities.
311 */
312 public List<Mdr5Record> getCities() {
313 return Collections.unmodifiableList(allCities);
314 }
315
316 public List<Mdr5Record> getSortedCities() {
317 return Collections.unmodifiableList(cities);
318 }
319 }