Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2011.
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.util.List;
16
17 import uk.me.parabola.imgfmt.app.ImgFileWriter;
18
19 /**
20 * Name indexes consisting of a prefix of the string and the record
21 * number at which that prefix first occurs. So it is like 8 and 12
22 * except that they are all combined together in this one section.
23 *
24 * @author Steve Ratcliffe
25 */
26 public class Mdr17 extends MdrSection {
27 private PrefixIndex streets;
28 private PrefixIndex streetsByCountry;
29 private PrefixIndex cities;
30 private PrefixIndex pois;
31
32 public Mdr17(MdrConfig config) {
33 setConfig(config);
34 streets = new PrefixIndex(getConfig(), 4);
35 streetsByCountry = new PrefixIndex(getConfig(), 4);
36 cities = new PrefixIndex(getConfig(), 2);
37 pois = new PrefixIndex(getConfig(), 4);
38 }
39
40 public void writeSectData(ImgFileWriter writer) {
41 writeSubSect(writer, streets);
42 writeSubSect(writer, cities);
43 writeSubSect(writer, streetsByCountry);
44 writeSubSect(writer, pois);
45 }
46
47 /**
48 * Write one of the subsections that makes up the section. They are all similar and
49 * have a header with the length and the record size and prefix length of the
50 * records in the subsection.
51 */
52 private static void writeSubSect(ImgFileWriter writer, PrefixIndex index) {
53 index.preWrite();
54 int len = index.getItemSize() * index.getNumberOfItems() + 2;
55 if (len == 2)
56 return; // nothing to do
57
58 // The length is a variable length integer with the length indicated by a suffix.
59 len = (len << 1) + 1;
60 int mask = ~0xff;
61 int count = 1;
62 while ((len & mask) != 0) {
63 mask <<= 8;
64 len <<= 1;
65 count++;
66 }
67 writer.putNu(count, len);
68
69 // Calculate the header. This code is unlikely to survive the finding of another example!
70 // Have no idea what the real thinking behind this is.
71 int prefixLength = index.getPrefixLength();
72 int header = (prefixLength - 1) << 8;
73 header += (prefixLength + 1) * (prefixLength + 1);
74 header += (index.getItemSize() - prefixLength - 1) * 0xa;
75
76 writer.put2u(header);
77 index.writeSectData(writer);
78 }
79
80 @Override
81 protected void releaseMemory() {
82 streets = null;
83 cities = null;
84 streetsByCountry = null;
85 pois = null;
86 }
87
88 public void addStreets(List<Mdr7Record> streetList) {
89 streets.createFromList(streetList);
90 }
91
92 public void addCities(List<Mdr5Record> cityList) {
93 cities.createFromList(cityList);
94 }
95
96 public void addStreetsByCountry(List<Mdr7Record> streets) {
97 streetsByCountry.createFromList(streets, true);
98 }
99
100 public void addPois(List<Mdr11Record> poiList) {
101 pois.createFromList(poiList);
102 }
103
104 public int getItemSize() {
105 return 0;
106 }
107
108 protected int numberOfItems() {
109 return 0;
110 }
111 }