Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr29.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.ArrayList;
16 import java.util.List;
17
18 import uk.me.parabola.imgfmt.Utils;
19 import uk.me.parabola.imgfmt.app.ImgFileWriter;
20 import uk.me.parabola.imgfmt.app.srt.Sort;
21 import uk.me.parabola.imgfmt.app.srt.SortKey;
22
23 /**
24 * An index of countries sorted by name with pointers to the other country related sections.
25 * There is only one per name, not per name and map.
26 *
27 * @author Steve Ratcliffe
28 */
29 public class Mdr29 extends MdrSection implements HasHeaderFlags {
30 private final List<Mdr29Record> index = new ArrayList<>();
31 private int max17;
32
33 public Mdr29(MdrConfig config) {
34 setConfig(config);
35 }
36
37 public void buildFromCountries(List<Mdr14Record> countries) {
38 Sort sort = getConfig().getSort();
39 List<SortKey<Mdr14Record>> keys = MdrUtils.sortList(sort, countries);
40
41 // Sorted by name, for every new name we allocate a new 29 record and set the same one in every
42 // country with the same name.
43 String lastName = null;
44 Mdr29Record mdr29 = null;
45 for (SortKey<Mdr14Record> key : keys) {
46 Mdr14Record country = key.getObject();
47
48 String name = country.getName();
49 if (!name.equals(lastName)) {
50 mdr29 = new Mdr29Record();
51 mdr29.setName(name);
52 mdr29.setStrOffset(country.getStrOff());
53 index.add(mdr29);
54 lastName = name;
55 }
56
57 assert mdr29 != null;
58 country.setMdr29(mdr29);
59 }
60 }
61
62 @Override
63 protected void preWriteImpl() {
64 if (!index.isEmpty()) {
65 for (Mdr29Record r : index) {
66 this.max17 = Math.max(max17, r.getMdr17());
67 }
68 }
69 }
70
71 /**
72 * Write out the contents of this section.
73 *
74 * @param writer Where to write it.
75 */
76 public void writeSectData(ImgFileWriter writer) {
77 int magic = getExtraValue();
78
79 boolean hasString = (magic & 1) != 0;
80 boolean has26 = (magic & 0x8) != 0;
81 boolean has17 = (magic & 0x30) != 0;
82
83 PointerSizes sizes = getSizes();
84 int size24 = sizes.getSize(24);
85 int size22 = sizes.getSize(22);
86 int size25 = sizes.getSize(5); // NB appears to be size of 5 (cities), not 25 (cities with country).
87 int size26 = has26? sizes.getSize(26): 0;
88 int size17 = Utils.numberToPointerSize(max17);
89 for (Mdr29Record record : index) {
90 writer.putNu(size24, record.getMdr24());
91 if (hasString)
92 putStringOffset(writer, record.getStrOffset());
93 writer.putNu(size22, record.getMdr22());
94 writer.putNu(size25, record.getMdr25());
95 if (has26)
96 writer.putNu(size26, record.getMdr26());
97 if (has17)
98 writer.putNu(size17, record.getMdr17());
99 }
100 }
101
102 /**
103 * The size of a record in the section. This is not a constant and might vary
104 * on various factors, such as the file version, if we are preparing for a
105 * device, the number of maps etc.
106 *
107 * @return The size of a record in this section.
108 */
109 public int getItemSize() {
110 PointerSizes sizes = getSizes();
111 int size = sizes.getSize(24)
112 + sizes.getSize(22)
113 + sizes.getSize(5) // NB: not 25
114 ;
115 if (isForDevice()) {
116 if (!getConfig().getSort().isMulti())
117 size += Utils.numberToPointerSize(max17);
118 } else {
119 size += sizes.getStrOffSize();
120 size += sizes.getSize(26);
121 }
122 return size;
123 }
124
125 /**
126 * The number of records in this section.
127 *
128 * @return The number of items in the section.
129 */
130 protected int numberOfItems() {
131 return index.size();
132 }
133
134 /**
135 * Unknown flags.
136 *
137 * As there are 4 bits set and 4 extra fields, that might be it. Compare
138 * to mdr28 where there are 3 extra fields and 3 bits set. Just a guess...
139 */
140 public int getExtraValue() {
141 if (isForDevice()) {
142 int magic = 0x6; // 22 and 25
143 if (!getConfig().getSort().isMulti())
144 magic |= Utils.numberToPointerSize(max17) << 4;
145 return magic; // +17, -26, -strings
146 }
147 else
148 return 0xf; // strings, 22, 25 and 26
149 }
150 }