Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr23.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 * Regions sorted by name. Same number of records as mdr13.
24 *
25 * @author Steve Ratcliffe
26 */
27 public class Mdr23 extends MdrSection {
28 private final List<Mdr13Record> regions = new ArrayList<>();
29
30 public Mdr23(MdrConfig config) {
31 setConfig(config);
32 }
33
34 /**
35 * Takes the region list and sorts it according to the name.
36 * @param list Original region list.
37 */
38 public void sortRegions(List<Mdr13Record> list) {
39 Sort sort = getConfig().getSort();
40 List<SortKey<Mdr13Record>> keys = MdrUtils.sortList(sort, list);
41
42 String lastName = null;
43 int lastMapIndex = 0;
44 int record = 0;
45 for (SortKey<Mdr13Record> key : keys) {
46 Mdr13Record reg = key.getObject();
47
48 // Only add if different name or map
49 String name = reg.getName();
50 if (reg.getMapIndex() != lastMapIndex || !name.equals(lastName)) {
51 record++;
52 reg.getMdr28().setMdr23(record);
53 regions.add(reg);
54
55 lastName = name;
56 lastMapIndex = reg.getMapIndex();
57 }
58 }
59 }
60
61 /**
62 * Write out the contents of this section.
63 *
64 * @param writer Where to write it.
65 */
66 public void writeSectData(ImgFileWriter writer) {
67 String lastName = null;
68 for (Mdr13Record reg : regions) {
69 putMapIndex(writer, reg.getMapIndex());
70
71 int flag = 0;
72 String name = reg.getName();
73 if (!name.equals(lastName)) {
74 flag = 0x800000;
75 lastName = name;
76 }
77
78 writer.put2u(reg.getRegionIndex());
79 writer.put2u(reg.getCountryIndex());
80 writer.put3u(reg.getLblOffset() | flag);
81 }
82 }
83
84 /**
85 * There is a map index followed by file region and country indexes.
86 * Then there is a label offset for the name, which strangely appears
87 * to be three bytes always, although that many would rarely (or never?) be
88 * required.
89 */
90 public int getItemSize() {
91 PointerSizes sizes = getSizes();
92 return sizes.getMapSize() + 2 + 2 + 3;
93 }
94
95 /**
96 * The number of records in this section.
97 *
98 * @return The number of items in the section.
99 */
100 protected int numberOfItems() {
101 return regions.size();
102 }
103 }