Open Chinese Convert: BinaryDict.hpp Source File

Open Chinese Convert 1.4.2+g71621a0e1.dirty A project for conversion between Traditional and Simplified Chinese

Loading...
Searching...
No Matches
BinaryDict.hpp
1/*
2 * Open Chinese Convert
3 *
4 * Copyright 2010-2014 Carbo Kuo <byvoid@byvoid.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#pragma once
20
21#include <cstdint>
22#include <cstring>
23
24#include "Common.hpp"
25#include "SerializableDict.hpp"
26
27namespace opencc {
28// Returns true if the 8 bytes at `bytes` look like an 8-byte integer field
29// from the legacy 64-bit OCD layout rather than two fixed-width uint32_t
30// fields. Legacy files were written with native size_t fields in native byte
31// order, so the probe is loaded as a native uint64_t: a legacy field has
32// zero high 32 bits, while for a fixed-width file the high half is one of
33// the two uint32 fields (or the start of the darts array), non-zero for any
34// realistic dictionary. Works on both little- and big-endian hosts; legacy
35// files themselves remain readable only on hosts of the byte order that
36// wrote them.
37inline bool LooksLikeLegacy64Field(const void* bytes) {
38 uint64_t word;
39 std::memcpy(&word, bytes, sizeof(word));
40 return (word >> 32) == 0;
41}
42
47 class OPENCC_EXPORT BinaryDict : public SerializableDict {
48public:
49 BinaryDict(const LexiconPtr& _lexicon) : lexicon(_lexicon) {}
50
51 virtual ~BinaryDict() {}
52
53 virtual void SerializeToFile(FILE* fp) const;
54
55 static BinaryDictPtr NewFromFile(FILE* fp);
56 static BinaryDictPtr NewFromBuffer(const char* data, size_t size);
57
58 const LexiconPtr& GetLexicon() const { return lexicon; }
59
60 size_t KeyMaxLength() const;
61
62private:
63 LexiconPtr lexicon;
64 std::string keyBuffer;
65 std::string valueBuffer;
66
67 void ConstructBuffer(std::string& keyBuffer, std::vector<size_t>& keyOffset,
68 size_t& keyTotalLength, std::string& valueBuffer,
69 std::vector<size_t>& valueOffset,
70 size_t& valueTotalLength) const;
71};
72} // namespace opencc
opencc::BinaryDict::SerializeToFile
virtual void SerializeToFile(FILE *fp) const
Serializes the dictionary and writes in to a file.
Definition BinaryDict.cpp:87
opencc::SerializableDict
Serializable dictionary interface.
Definition SerializableDict.hpp:32