Open Chinese Convert: SerializableDict.hpp Source File

Open Chinese Convert 1.0.2 A project for conversion between Traditional and Simplified Chinese

- Main Page
- Modules
- Classes
- Examples

All Classes Functions Typedefs Modules
SerializableDict.hpp
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2010-2014 BYVoid <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 "Dict.hpp"
22
23 namespace opencc {
28 class OPENCC_EXPORT SerializableDict {
29 public:
33 virtual void SerializeToFile(FILE* fp) const = 0;
34
38 virtual void SerializeToFile(const string& fileName) const {
39 FILE* fp = fopen(fileName.c_str(), "wb");
40 if (fp == NULL) {
41 throw FileNotWritable(fileName);
42 }
43 SerializeToFile(fp);
44 fclose(fp);
45 }
46
47 template<typename DICT>
48 static bool TryLoadFromFile(const string& fileName,
49 std::shared_ptr<DICT>* dict) {
50 FILE* fp = fopen(fileName.c_str(), "rb");
51 if (fp == NULL) {
52 return false;
53 }
54 std::shared_ptr<DICT> loadedDict = DICT::NewFromFile(fp);
55 fclose(fp);
56 *dict = loadedDict;
57 return true;
58 }
59
60 template<typename DICT>
61 static std::shared_ptr<DICT> NewFromFile(const string& fileName) {
62 std::shared_ptr<DICT> dict;
63 if (!TryLoadFromFile<DICT>(fileName, &dict)) {
64 throw FileNotFound(fileName);
65 }
66 return dict;
67 }
68 };
69 }
opencc::SerializableDict
Serializable dictionary interface.
Definition: SerializableDict.hpp:28
opencc::SerializableDict::SerializeToFile
virtual void SerializeToFile(const string &fileName) const
Serializes the dictionary and writes in to a file.
Definition: SerializableDict.hpp:38
opencc
Definition: BinaryDict.hpp:24
opencc::FileNotWritable
Definition: Exception.hpp:60