Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/MdrSection.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 uk.me.parabola.imgfmt.Utils;
16 import uk.me.parabola.imgfmt.app.ImgFileWriter;
17
18 /**
19 * Super class of all sections.
20 * @author Steve Ratcliffe
21 */
22 public abstract class MdrSection extends ConfigBase {
23 private PointerSizes sizes;
24 private boolean released;
25 protected int nItems;
26 private boolean sizeValid;
27
28 /**
29 * Write out the contents of this section.
30 * @param writer Where to write it.
31 */
32 public abstract void writeSectData(ImgFileWriter writer);
33
34 /**
35 * The size of a record in the section. This is not a constant and
36 * might vary on various factors, such as the file version, if we are
37 * preparing for a device, the number of maps etc.
38 *
39 * @return The size of a record in this section.
40 */
41 public abstract int getItemSize();
42
43 protected PointerSizes getSizes() {
44 return sizes;
45 }
46
47 public void setSizes(PointerSizes sizes) {
48 this.sizes = sizes;
49 }
50
51 protected void putMapIndex(ImgFileWriter writer, int mapIndex) {
52 writer.putNu(sizes.getMapSize(), mapIndex);
53 }
54
55 protected void putStringOffset(ImgFileWriter writer, int strOff) {
56 writer.putNu(sizes.getStrOffSize(), strOff);
57 }
58
59 /**
60 * The number of records in this section.
61 * @return The number of items in the section.
62 */
63 public final int getNumberOfItems() {
64 assert sizeValid;
65 if (released)
66 return nItems;
67 else
68 return numberOfItems();
69 }
70
71 /**
72 * Method to be implemented by subclasses to return the number of items in the section.
73 * This will only be valid after the section is completely finished etc.
74 * @return The number of items in the section.
75 */
76 protected abstract int numberOfItems();
77
78 /**
79 * Get the size of an integer that is sufficient to store a record number
80 * from this section. If the pointer has a flag(s) then this must be
81 * taken into account too.
82 * @return A number between 1 and 4 giving the number of bytes required
83 * to store the largest record number in this section.
84 */
85 public int getSizeForRecord() {
86 return Utils.numberToPointerSize(getNumberOfItems());
87 }
88
89 /**
90 * Called before the section is written and before the actual size of the section
91 * is required.
92 *
93 * Calling it more than once is ok.
94 *
95 * The actual work is implemented in the subclass via the {@link #preWriteImpl()} method.
96 */
97 public final void preWrite() {
98 if (!sizeValid)
99 preWriteImpl();
100 sizeValid = true;
101 }
102
103 /**
104 * Prepare the final list of items to be written.
105 * Used to de-duplicate or remove invalid entries from the raw data that was
106 * saved.
107 *
108 * In particular after this call the number of items must not change.
109 */
110 protected void preWriteImpl() {
111 }
112
113 public final void release() {
114 nItems = numberOfItems();
115 releaseMemory();
116 released = true;
117 }
118
119 protected void releaseMemory() {
120 throw new UnsupportedOperationException();
121 }
122
123 /**
124 * Provides the pointer sizes required to hold record of offset values
125 * in the various sections.
126 */
127 static class PointerSizes {
128
129 private final MdrSection[] sections;
130
131 public PointerSizes(MdrSection[] sections) {
132 this.sections = sections;
133 }
134
135 public int getMapSize() {
136 return sections[1].getSizeForRecord();
137 }
138
139 public int getCitySize() {
140 return sections[5].getSizeForRecord();
141 }
142
143 /**
144 * Get the number of bytes required to represent a city when there is
145 * one bit reserved for a flag.
146 * There is a minimum size of 2.
147 * @return Number of bytes to represent a city record number and a
148 * one bit flag.
149 */
150 public int getCitySizeFlagged() {
151 return Math.max(2, Utils.numberToPointerSize(sections[5].getNumberOfItems() << 1));
152 }
153
154 public int getCityFlag() {
155 return flagForSize(getCitySizeFlagged());
156 }
157
158 public int getStreetSize() {
159 return sections[7].getSizeForRecord();
160 }
161
162 public int getStreetSizeFlagged() {
163 return Utils.numberToPointerSize(sections[7].getNumberOfItems() << 1);
164 }
165
166 public int getPoiSize() {
167 return sections[11].getSizeForRecord();
168 }
169
170 public int getZipSize() {
171 //return Math.max(2, sections[6].getSizeForRecord());
172 return sections[6].getSizeForRecord();
173 }
174
175 /**
176 * The number of bytes required to represent a POI (mdr11) record number
177 * and a flag bit.
178 */
179 public int getPoiSizeFlagged() {
180 return Utils.numberToPointerSize(sections[11].getNumberOfItems() << 1);
181 }
182
183 public int getPoiFlag() {
184 return flagForSize(getPoiSizeFlagged());
185 }
186
187 /**
188 * Size of the pointer required to index a byte offset into mdr15 (strings).
189 * There is a minimum of 3 for this value.
190 * @return Pointer size required for the string offset value.
191 */
192 public int getStrOffSize() {
193 return Math.max(3, sections[15].getSizeForRecord());
194 }
195
196 public int getMdr20Size() {
197 return sections[20].getSizeForRecord();
198 }
199
200 private static int flagForSize(int size) {
201 int flag;
202 if (size == 1)
203 flag = 0x80;
204 else if (size == 2)
205 flag = 0x8000;
206 else if (size == 3)
207 flag = 0x800000;
208 else if (size == 4)
209 flag = 0x80000000;
210 else
211 flag = 0;
212 return flag;
213 }
214
215 public int getSize(int sect) {
216 return sections[sect].getSizeForRecord();
217 }
218
219 public int getNumberOfItems(int sect) {
220 return sections[sect].getNumberOfItems();
221 }
222 }
223 }