Debian Codegraph @

src/uk/me/parabola/imgfmt/mps/MpsFileReader.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 19, 2007
16 */
17 package uk.me.parabola.imgfmt.mps;
18
19 import java.io.Closeable;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import uk.me.parabola.imgfmt.app.BufferedImgFileReader;
25 import uk.me.parabola.imgfmt.app.ImgFileReader;
26 import uk.me.parabola.imgfmt.app.labelenc.CharacterDecoder;
27 import uk.me.parabola.imgfmt.app.labelenc.CodeFunctions;
28 import uk.me.parabola.imgfmt.fs.ImgChannel;
29
30 /**
31 * This file is a description of the map set that is loaded into the
32 * gmapsupp.img file and an index of the maps that it contains.
33 *
34 * It is different than all the other files that fit inside the gmapsupp file
35 * in that it doesn't contain the common header. So it does not extend ImgFile.
36 *
37 * @author Steve Ratcliffe
38 */
39 public class MpsFileReader implements Closeable {
40
41 private final List<MapBlock> maps = new ArrayList<>();
42 private final List<ProductBlock> products = new ArrayList<>();
43
44 private final ImgChannel chan;
45 private final ImgFileReader reader;
46 private final CharacterDecoder decoder;
47 private final int codePage;
48
49 public MpsFileReader(ImgChannel chan, int codePage) {
50 this.chan = chan;
51 this.reader = new BufferedImgFileReader(chan);
52
53 this.codePage = codePage;
54 CodeFunctions funcs = CodeFunctions.createEncoderForLBL(0, codePage);
55 decoder = funcs.getDecoder();
56
57 readBlocks();
58 }
59
60 private void readBlocks() {
61 byte type;
62 while ((type = reader.get()) > 0) {
63 int len = reader.get2u();
64
65 switch (type) {
66 case 0x4c:
67 readMapBlock();
68 break;
69 case 0x46:
70 readProductBlock();
71 break;
72 default:
73 // We always know the length, so just read over it
74 reader.get(len);
75 break;
76 }
77 }
78 }
79
80 private void readMapBlock() {
81 MapBlock block = new MapBlock(codePage);
82 int val = reader.get4();
83 block.setIds(val >>> 16, val & 0xffff);
84 block.setMapNumber(reader.get4());
85
86 byte[] zString = reader.getZString();
87 block.setSeriesName(decodeToString(zString));
88 block.setMapDescription(decodeToString(reader.getZString()));
89 block.setAreaName(decodeToString(reader.getZString()));
90 block.setHexNumber(reader.get4());
91 reader.get4();
92 maps.add(block);
93 }
94
95 private void readProductBlock() {
96 ProductBlock block = new ProductBlock(codePage);
97 block.setProductId(reader.get2u());
98 block.setFamilyId(reader.get2u());
99 block.setDescription(decodeToString(reader.getZString()));
100 products.add(block);
101 }
102
103 private String decodeToString(byte[] zString) {
104 decoder.reset();
105 for (byte b : zString)
106 decoder.addByte(b);
107
108 return decoder.getText().getText();
109 }
110
111 public List<MapBlock> getMaps() {
112 return maps;
113 }
114
115 public List<ProductBlock> getProducts() {
116 return products;
117 }
118
119 public void close() throws IOException {
120 chan.close();
121 }
122 }