Debian Codegraph @

src/uk/me/parabola/imgfmt/app/lbl/LBLHeader.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 Steve Ratcliffe
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 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: Dec 14, 2007
16 */
17 package uk.me.parabola.imgfmt.app.lbl;
18
19 import java.nio.charset.StandardCharsets;
20
21 import uk.me.parabola.imgfmt.app.CommonHeader;
22 import uk.me.parabola.imgfmt.app.ImgFileReader;
23 import uk.me.parabola.imgfmt.app.ImgFileWriter;
24 import uk.me.parabola.imgfmt.app.labelenc.CodeFunctions;
25 import uk.me.parabola.imgfmt.app.srt.Sort;
26
27 /**
28 * The header for the LBL file.
29 *
30 * @author Steve Ratcliffe
31 */
32 public class LBLHeader extends CommonHeader {
33 public static final int HEADER_LEN = 196; // Other lengths are possible
34
35 private static final char UNK3_REC_LEN = 0;
36
37 private int labelStart; // Start of labels.
38 private int labelSize; // Size of file.
39
40 private int offsetMultiplier;
41
42 // Code page and sorting info.
43 private Sort sort;
44
45 private int sortDescriptionLength;
46
47 // The type of encoding employed. This is not a length.
48 private int encodingType = CodeFunctions.ENCODING_FORMAT6;
49
50 // The label section also contains all kinds of records related to place,
51 // so these have all been put in their own class.
52 private final PlacesHeader placeHeader;
53
54 public LBLHeader() {
55 super(HEADER_LEN, "GARMIN LBL");
56 placeHeader = new PlacesHeader();
57 }
58
59 public int getSortDescriptionLength() {
60 return sortDescriptionLength;
61 }
62
63 /**
64 * Read the rest of the header. Specific to the given file. It is guaranteed
65 * that the file position will be set to the correct place before this is
66 * called.
67 *
68 * @param reader The header is read from here.
69 */
70 protected void readFileHeader(ImgFileReader reader) {
71 labelStart = reader.get4();
72 labelSize = reader.get4();
73 offsetMultiplier = 1 << reader.get();
74 encodingType = reader.get();
75
76 // Read the places part of the header.
77 placeHeader.readFileHeader(reader);
78
79 int codepage = reader.get2u();
80 int id1 = reader.get2u();
81 int id2 = reader.get2u();
82 int descOff = reader.get4();
83 int descLen = reader.get4();
84
85 reader.position(descOff);
86 byte[] bytes = reader.get(descLen);
87 String description;
88 description = new String(bytes, StandardCharsets.US_ASCII);
89
90 sort = new Sort();
91 sort.setCodepage(codepage);
92 sort.setId1(id1);
93 sort.setId2(id2);
94 sort.setDescription(description);
95
96 // more to do but not needed yet... Just set position
97 reader.position(labelStart);
98 }
99
100 /**
101 * Write the rest of the header. It is guaranteed that the writer will be set
102 * to the correct position before calling.
103 *
104 * @param writer The header is written here.
105 */
106 protected void writeFileHeader(ImgFileWriter writer) {
107 // LBL1 section, these are regular labels
108 writer.put4(HEADER_LEN + sortDescriptionLength);
109 writer.put4(getLabelSize());
110
111 writer.put1u(offsetMultiplier);
112 writer.put1u(encodingType);
113
114 placeHeader.writeFileHeader(writer);
115
116 writer.put2u(getCodePage());
117
118 // Identifying the sort
119 int id1 = sort.getId1();
120 writer.put2u(id1);
121
122 int id2 = sort.getId2();
123 if (id1 != 0 && id2 != 0)
124 id2 |= 0x8000;
125 writer.put2u(id2);
126
127 writer.put4(HEADER_LEN);
128 writer.put4(sortDescriptionLength);
129
130 writer.put4(placeHeader.getLastPos());
131 writer.put4(0);
132 writer.put2u(UNK3_REC_LEN);
133 writer.put2u(0);
134 }
135
136 protected int getEncodingType() {
137 return encodingType;
138 }
139
140 public void setEncodingType(int type) {
141 this.encodingType = type;
142 }
143
144 protected int getLabelSize() {
145 return labelSize;
146 }
147
148 public void setLabelSize(int labelSize) {
149 this.labelSize = labelSize;
150 placeHeader.setLabelEnd(HEADER_LEN + sortDescriptionLength + labelSize);
151 }
152
153 protected int getCodePage() {
154 return sort.getCodepage();
155 }
156
157 public void setSort(Sort sort) {
158 sortDescriptionLength = sort.getDescription().length() + 1;
159 this.sort = sort;
160 }
161
162 public int getSortOrderId() {
163 return sort.getSortOrderId();
164 }
165
166 public int getLabelStart() {
167 return labelStart;
168 }
169
170 public int getOffsetMultiplier() {
171 return offsetMultiplier;
172 }
173
174 public PlacesHeader getPlaceHeader() {
175 return placeHeader;
176 }
177
178 public void setOffsetMultiplier(int offsetMultiplier) {
179 this.offsetMultiplier = offsetMultiplier;
180 }
181 }