Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr1.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.util.ArrayList;
16 import java.util.List;
17
18 import uk.me.parabola.imgfmt.app.ImgFileWriter;
19
20 /**
21 * The section MDR 1 contains a list of maps and for each map
22 * an offset to a reverse index for that map.
23 *
24 * The reverse index consists of a number of sections, that I call sub-sections
25 * here. The sub-sections are all lists of record numbers in other sections
26 * in the MDR that contain records belonging to more than one map.
27 *
28 * Using the index you could extract records that belong to an individual map
29 * from other MDR sections without having to go through them all and check
30 * which map they belong to.
31 *
32 * The subsections are as follows:
33 *
34 * sub1 points into MDR 11 (POIs)
35 * sub2 points into MDR 10 (POI types)
36 * sub3 points into MDR 7 (street names)
37 * sub4 points into MDR 5 (cities)
38 * sub5 points into MDR 6 (zips)
39 * sub6 points into MDR 20
40 * sub7 points into MDR 21
41 * sub8 points into MDR 22
42 *
43 * @author Steve Ratcliffe
44 */
45 public class Mdr1 extends MdrSection implements HasHeaderFlags {
46 private final List<Mdr1Record> maps = new ArrayList<>();
47
48 public Mdr1(MdrConfig config) {
49 setConfig(config);
50 }
51
52 /**
53 * Add a map. Create an MDR1 record for it and also allocate its reverse
54 * index if this is not for a device.
55 * @param mapNumber The map index number.
56 * @param index The map index.
57 */
58 public void addMap(int mapNumber, int index) {
59 assert index > 0;
60 Mdr1Record rec = new Mdr1Record(mapNumber);
61 rec.setMapIndex(index);
62 maps.add(rec);
63
64 if (!isForDevice()) {
65 Mdr1MapIndex mapIndex = new Mdr1MapIndex();
66 rec.setMdrMapIndex(mapIndex);
67 }
68 }
69
70 public void writeSubSections(ImgFileWriter writer) {
71 if (isForDevice())
72 return;
73 for (Mdr1Record rec : maps) {
74 rec.setIndexOffset(writer.position());
75 Mdr1MapIndex mapIndex = rec.getMdrMapIndex();
76 mapIndex.writeSubSection(writer);
77 }
78 }
79
80 /**
81 * This is written right at the end after we know all the offsets in
82 * the MDR 1 record.
83 * @param writer The mdr 1 records are written out to this writer.
84 */
85 public void writeSectData(ImgFileWriter writer) {
86 boolean revIndex = (getExtraValue() & 1) != 0;
87 for (Mdr1Record rec : maps) {
88 writer.put4(rec.getMapNumber());
89 if (revIndex)
90 writer.put4(rec.getIndexOffset());
91 }
92 }
93
94 public int getItemSize() {
95 return isForDevice()? 4: 8;
96 }
97
98 public void setStartPosition(int sectionNumber) {
99 // nothing to do
100 }
101
102 public void setEndPosition(int sectionNumber) {
103 if (isForDevice())
104 return;
105
106 for (Mdr1Record mi : maps) {
107 mi.getMdrMapIndex().endSection(sectionNumber);
108 }
109 }
110
111 public void setPointerSize(int sectionSize, int recordSize) {
112 for (Mdr1Record mi : maps) {
113 Mdr1MapIndex mapIndex = mi.getMdrMapIndex();
114 mapIndex.setPointerSize(sectionSize, recordSize);
115 }
116 }
117
118 public void addPointer(int mapNumber, int recordNumber) {
119 Mdr1MapIndex mi = maps.get(mapNumber - 1).getMdrMapIndex();
120 mi.addPointer(recordNumber);
121 }
122
123 /**
124 * The number of records in this section.
125 *
126 * @return The number of items in the section.
127 */
128 protected int numberOfItems() {
129 return maps.size();
130 }
131
132 public int getExtraValue() {
133 int magic = 0;
134 if (!isForDevice())
135 magic |= 1;
136 return magic;
137 }
138 }