Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr2x.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.text.Collator;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import uk.me.parabola.imgfmt.app.ImgFileWriter;
20
21 /**
22 * Common code for 20, 21, 22 which are all lists of streets ordered in
23 * different ways.
24 *
25 * @author Steve Ratcliffe
26 */
27 public abstract class Mdr2x extends MdrMapSection implements HasHeaderFlags {
28 protected List<Mdr7Record> streets = new ArrayList<>();
29 protected static final int HAS_LABEL = 0x02;
30 protected static final int HAS_NAME_OFFSET = 0x04;
31
32 /**
33 * Write out the contents of this section.
34 *
35 * @param writer Where to write it.
36 */
37 public void writeSectData(ImgFileWriter writer) {
38 Mdr7Record prev = null;
39 Collator collator = getConfig().getSort().getCollator();
40 collator.setStrength(Collator.SECONDARY);
41
42 int size = getSizes().getStreetSizeFlagged();
43 int magic = getExtraValue();
44 boolean writeLabel = (magic & HAS_LABEL) != 0; // A guess
45 boolean writeNameOffset = (magic & HAS_NAME_OFFSET) != 0; // A guess, but less so
46 int partialInfoSize = ((magic >> 3) & 0x7);
47
48 int recordNumber = 0;
49 for (Mdr7Record street : streets) {
50 assert street.getMapIndex() == street.getCity().getMapIndex() : street.getMapIndex() + "/" + street.getCity().getMapIndex();
51 addIndexPointer(street.getMapIndex(), ++recordNumber);
52
53 int repeat = 1;
54 if (writeLabel) {
55 int rr = street.checkRepeat(prev, collator);
56 putMapIndex(writer, street.getMapIndex());
57 int offset = street.getLabelOffset();
58 if (rr == 3 && sameGroup(street, prev))
59 repeat = 0;
60 if (repeat != 0)
61 offset |= 0x800000;
62
63 writer.put3u(offset);
64 if (writeNameOffset)
65 writer.put1u(street.getOutNameOffset());
66
67 if (partialInfoSize > 0) {
68 int trailingFlags = ((rr & 1) == 0) ? 1 : 0;
69 writer.putNu(partialInfoSize, trailingFlags);
70 }
71 } else {
72 int rr = street.checkFullRepeat(prev, collator);
73 if (rr == 2 && sameGroup(street, prev))
74 repeat = 0;
75
76 int index = street.getIndex();
77 writer.putNu(size, (index << 1) | repeat);
78 }
79
80 prev = street;
81 }
82 }
83
84 /**
85 * The size of a record in the section.
86 *
87 * For these sections there is one field that is an index into the
88 * streets with an extra bit for a flag.
89 *
90 * In the device configuration, then there is a label and a flag, just like
91 * for mdr7.
92 */
93 public int getItemSize() {
94 int size;
95 int magic = getExtraValue();
96 int partialInfoSize = ((magic >> 3) & 0x7);
97
98 if (isForDevice()) {
99 // map-index, label, name-offset, 1byte flag
100 size = getSizes().getMapSize() + 3 + 1 + partialInfoSize;
101
102 } else {
103 size = getSizes().getStreetSizeFlagged();
104 }
105
106 return size;
107 }
108
109 /**
110 * The number of records in this section.
111 *
112 * @return The number of items in the section.
113 */
114 protected int numberOfItems() {
115 return streets.size();
116 }
117
118 @Override
119 protected void releaseMemory() {
120 streets = null;
121 }
122
123 /**
124 * These sections are divided into groups based on city, region or country. This routine is
125 * implemented to return true if the two streets are in the same group.
126 *
127 * It is not clear if this is needed for region or country.
128 * @param street1 The first street.
129 * @param street2 The street to compare against.
130 * @return True if the streets are in the same group (city, region etc).
131 */
132 protected abstract boolean sameGroup(Mdr7Record street1, Mdr7Record street2);
133 }