Debian Codegraph @

src/uk/me/parabola/imgfmt/app/mdr/Mdr28.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.Collections;
17 import java.util.List;
18
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 * One of these per region name. There are pointers into the other sections
25 * that are sorted by region to the first record that has the given name.
26 *
27 * @author Steve Ratcliffe
28 */
29 public class Mdr28 extends MdrSection implements HasHeaderFlags {
30 private final List<Mdr28Record> index = new ArrayList<>();
31
32 public Mdr28(MdrConfig config) {
33 setConfig(config);
34 }
35
36 public void buildFromRegions(List<Mdr13Record> regions) {
37 Sort sort = getConfig().getSort();
38 List<SortKey<Mdr13Record>> keys = MdrUtils.sortList(sort, regions);
39
40 int record = 0;
41 Mdr28Record mdr28 = null;
42 String lastName = null;
43 for (SortKey<Mdr13Record> key : keys) {
44 Mdr13Record region = key.getObject();
45
46 String name = region.getName();
47 if (!name.equals(lastName)) {
48 record++;
49 mdr28 = new Mdr28Record();
50 mdr28.setIndex(record);
51 mdr28.setName(name);
52 mdr28.setStrOffset(region.getStrOffset());
53 mdr28.setMdr14(region.getMdr14());
54
55 index.add(mdr28);
56 lastName = name;
57 }
58
59 assert mdr28 != null;
60 region.setMdr28(mdr28);
61 }
62 }
63
64 /**
65 * Write out the contents of this section.
66 *
67 * @param writer Where to write it.
68 */
69 public void writeSectData(ImgFileWriter writer) {
70 PointerSizes sizes = getSizes();
71 int size21 = sizes.getSize(21);
72 int size23 = sizes.getSize(23);
73 int size27 = sizes.getSize(27);
74
75 for (Mdr28Record mdr28 : index) {
76 writer.putNu(size23, mdr28.getMdr23());
77 putStringOffset(writer, mdr28.getStrOffset());
78 writer.putNu(size21, mdr28.getMdr21());
79 writer.putNu(size27, mdr28.getMdr27());
80 }
81 }
82
83 /**
84 * The size of a record in the section. This is not a constant and might vary
85 * on various factors, such as the file version, if we are preparing for a
86 * device, the number of maps etc.
87 *
88 * @return The size of a record in this section.
89 */
90 public int getItemSize() {
91 PointerSizes sizes = getSizes();
92
93 return sizes.getSize(23)
94 + sizes.getStrOffSize()
95 + sizes.getSize(21)
96 + sizes.getSize(27);
97 }
98
99 /**
100 * The number of records in this section.
101 *
102 * @return The number of items in the section.
103 */
104 protected int numberOfItems() {
105 return index.size();
106 }
107
108 /**
109 * Flag purposes are not known.
110 */
111 public int getExtraValue() {
112 return 0x7;
113 }
114
115 public List<Mdr28Record> getIndex() {
116 return Collections.unmodifiableList(index);
117 }
118 }