Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr24.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.app.ImgFileWriter;
19 import uk.me.parabola.imgfmt.app.srt.Sort;
20 import uk.me.parabola.imgfmt.app.srt.SortKey;
21
22 /**
23 * Countries sorted by name. Same number of entries as 14.
24 *
25 * @author Steve Ratcliffe
26 */
27 public class Mdr24 extends MdrSection {
28 private final List<Mdr14Record> countries = new ArrayList<>();
29
30 public Mdr24(MdrConfig config) {
31 setConfig(config);
32 }
33
34 /**
35 * Sort the countries by name. Duplicates are kept.
36 * @param list The full list of countries.
37 */
38 public void sortCountries(List<Mdr14Record> list) {
39 Sort sort = getConfig().getSort();
40 List<SortKey<Mdr14Record>> keys = MdrUtils.sortList(sort, list);
41
42 String lastName = null;
43 int lastMapIndex = 0;
44 int record = 0;
45 for (SortKey<Mdr14Record> key : keys) {
46 Mdr14Record c = key.getObject();
47
48 // If this is a new name, then we prepare a mdr29 record for it.
49 String name = c.getName();
50
51 if (lastMapIndex != c.getMapIndex() || !name.equals(lastName)) {
52 record++;
53 c.getMdr29().setMdr24(record);
54 countries.add(c);
55
56 lastName = name;
57 lastMapIndex = c.getMapIndex();
58 }
59 }
60 }
61
62 /**
63 * Write out the contents of this section.
64 *
65 * @param writer Where to write it.
66 */
67 public void writeSectData(ImgFileWriter writer) {
68 String lastName = null;
69 for (Mdr14Record c : countries) {
70 putMapIndex(writer, c.getMapIndex());
71
72 int flag = 0;
73 String name = c.getName();
74 if (!name.equals(lastName)) {
75 flag = 0x800000;
76 lastName = name;
77 }
78
79 writer.put2u(c.getCountryIndex());
80 writer.put3u(c.getLblOffset() | flag);
81 }
82 }
83
84 /**
85 * The size of a record in the section. This is not a constant and might vary
86 * on various factors, such as the file version, if we are preparing for a
87 * device, the number of maps etc.
88 *
89 * @return The size of a record in this section.
90 */
91 public int getItemSize() {
92 PointerSizes sizes = getSizes();
93 return sizes.getMapSize() + 2 + 3;
94 }
95
96 /**
97 * The number of records in this section.
98 *
99 * @return The number of items in the section.
100 */
101 protected int numberOfItems() {
102 return countries.size();
103 }
104 }