| 1 | /* This file is part of Aard Dictionary for Android <http://aarddict.org>. |
| 2 | * |
| 3 | * This program is free software: you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 3 |
| 5 | * as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License <http://www.gnu.org/licenses/gpl-3.0.txt> |
| 11 | * for more details. |
| 12 | * |
| 13 | * Copyright (C) 2010 Igor Tkach |
| 14 | */ |
| 15 | |
| 16 | package aarddict; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Map; |
| 21 | import java.util.UUID; |
| 22 | |
| 23 | public final class Header { |
| 24 | |
| 25 | public final String signature; |
| 26 | public final String sha1sum; |
| 27 | public final UUID uuid; |
| 28 | public final int version; |
| 29 | public final int volume; |
| 30 | public final int of; |
| 31 | public final long metaLength; |
| 32 | public final long indexCount; |
| 33 | public final long articleOffset; |
| 34 | public final String index1ItemFormat; |
| 35 | public final String keyLengthFormat; |
| 36 | public final String articleLengthFormat; |
| 37 | public final long index1Offset; |
| 38 | public final long index2Offset; |
| 39 | public final int index1ItemSize; |
| 40 | public final char keyPointerSpec; |
| 41 | public final char articlePointerSpec; |
| 42 | public final char keyLengthSpec; |
| 43 | public final char articleLengthSpec; |
| 44 | |
| 45 | Header(RandomAccessFile file) throws IOException { |
| 46 | int specLen = 0; |
| 47 | this.signature = file.readUTF8(4); |
| 48 | specLen += 4; |
| 49 | |
| 50 | this.sha1sum = file.readUTF8(40); |
| 51 | specLen += 40; |
| 52 | |
| 53 | this.version = file.readUnsignedShort(); |
| 54 | specLen += 2; |
| 55 | |
| 56 | this.uuid = file.readUUID(); |
| 57 | specLen += 16; |
| 58 | |
| 59 | this.volume = file.readUnsignedShort(); |
| 60 | specLen += 2; |
| 61 | |
| 62 | this.of = file.readUnsignedShort(); |
| 63 | specLen += 2; |
| 64 | |
| 65 | this.metaLength = file.readUnsignedInt(); |
| 66 | specLen += 4; |
| 67 | |
| 68 | this.indexCount = file.readUnsignedInt(); |
| 69 | specLen += 4; |
| 70 | |
| 71 | this.articleOffset = file.readUnsignedInt(); |
| 72 | specLen += 4; |
| 73 | |
| 74 | this.index1ItemFormat = file.readUTF8(4); |
| 75 | specLen += 4; |
| 76 | |
| 77 | this.keyLengthFormat = file.readUTF8(2); |
| 78 | specLen += 2; |
| 79 | this.keyLengthSpec = this.keyLengthFormat.charAt(1); |
| 80 | |
| 81 | this.articleLengthFormat = file.readUTF8(2); |
| 82 | specLen += 2; |
| 83 | this.articleLengthSpec = this.articleLengthFormat.charAt(1); |
| 84 | |
| 85 | this.index1ItemSize = calcSize(this.index1ItemFormat); |
| 86 | |
| 87 | this.index1Offset = specLen + this.metaLength; |
| 88 | this.index2Offset = this.index1Offset + this.indexCount*this.index1ItemSize; |
| 89 | this.keyPointerSpec = this.index1ItemFormat.charAt(1); |
| 90 | this.articlePointerSpec = this.index1ItemFormat.charAt(2); |
| 91 | } |
| 92 | |
| 93 | static Map<Character, Integer> structSizes = new HashMap<Character, Integer>() { |
| 94 | { |
| 95 | put('h', 2); |
| 96 | put('H', 2); |
| 97 | put('i', 4); |
| 98 | put('I', 4); |
| 99 | put('l', 4); |
| 100 | put('L', 4); |
| 101 | put('q', 8); |
| 102 | put('Q', 8); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | static int calcSize(String structSpec) { |
| 107 | int size = 0; |
| 108 | int length = structSpec.length(); |
| 109 | //ignore byte order spec at index 0 |
| 110 | for (int i = 1; i < length; i++) { |
| 111 | char c = structSpec.charAt(i); |
| 112 | Integer unitSize = structSizes.get(c); |
| 113 | if (unitSize != null) { |
| 114 | size += unitSize; |
| 115 | } |
| 116 | } |
| 117 | return size; |
| 118 | } |
| 119 | } |