1 // Copyright 2014 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
24 #include "cld2_dynamic_data.h"
25 #include "cld2_dynamic_data_extractor.h"
26 #include "cld2_dynamic_data_loader.h"
27 #include "integral_types.h"
28 #include "cld2tablesummary.h"
29 #include "utf8statetable.h"
30 #include "scoreonescriptspan.h"
32 // We need these in order to set up a real data object to pass around.
34 extern const UTF8PropObj cld_generated_CjkUni_obj;
35 extern const CLD2TableSummary kCjkCompat_obj;
36 extern const CLD2TableSummary kCjkDeltaBi_obj;
37 extern const CLD2TableSummary kDistinctBiTable_obj;
38 extern const CLD2TableSummary kQuad_obj;
39 extern const CLD2TableSummary kQuad_obj2;
40 extern const CLD2TableSummary kDeltaOcta_obj;
41 extern const CLD2TableSummary kDistinctOcta_obj;
42 extern const short kAvgDeltaOctaScore[];
43 extern const uint32 kAvgDeltaOctaScoreSize;
44 extern const uint32 kCompatTableIndSize;
45 extern const uint32 kCjkDeltaBiIndSize;
46 extern const uint32 kDistinctBiTableIndSize;
47 extern const uint32 kQuadChromeIndSize;
48 extern const uint32 kQuadChrome2IndSize;
49 extern const uint32 kDeltaOctaIndSize;
50 extern const uint32 kDistinctOctaIndSize;
53 int main(int argc, char** argv) {
54 if (!CLD2DynamicData::isLittleEndian()) {
55 std::cerr << "System is big-endian: currently not supported." << std::endl;
58 if (!CLD2DynamicData::coreAssumptionsOk()) {
59 std::cerr << "Core assumptions violated, unsafe to continue." << std::endl;
63 // Get command-line flags
65 bool get_vector = false;
66 char* fileName = NULL;
67 const char* USAGE = "\
68 CLD2 Dynamic Data Tool:\n\
69 Dump, verify or print summaries of scoring tables for CLD2.\n\
71 The files output by this tool are suitable for all little-endian platforms,\n\
72 and should work on both 32- and 64-bit platforms.\n\
74 IMPORTANT: The files output by this tool WILL NOT work on big-endian platforms.\n\
77 --dump [FILE] Dump the scoring tables that this tool was linked against\n\
78 to the specified file. The tables are automatically verified\n\
79 after writing, just as if the tool was run again with\n\
81 --verify [FILE] Verify that a given file precisely matches the scoring\n\
82 tables that this tool was linked against. This can be used\n\
83 to verify that a file is compatible.\n\
84 --head [FILE] Print headers from the specified file to stdout.\n\
85 --verbose Be verbose.\n\
87 int mode = 0; //1=dump, 2=verify, 3=head
88 for (int i = 1; i < argc; ++i) {
89 if (strcmp(argv[i], "--verbose") == 0) {
90 CLD2DynamicDataExtractor::setDebug(1);
91 CLD2DynamicData::setDebug(1);
93 else if (strcmp(argv[i], "--dump") == 0
94 || strcmp(argv[i], "--verify") == 0
95 || strcmp(argv[i], "--head") == 0) {
97 // set mode flag properly
98 if (strcmp(argv[i], "--dump") == 0) mode=1;
99 else if (strcmp(argv[i], "--verify") == 0) mode=2;
102 fileName = argv[++i];
104 std::cerr << "missing file name argument" << std::endl << std::endl;
108 } else if (strcmp(argv[i], "--help") == 0) {
112 std::cerr << "Unsupported option: " << argv[i] << std::endl << std::endl;
123 CLD2::ScoringTables realData = {
124 &CLD2::cld_generated_CjkUni_obj,
125 &CLD2::kCjkCompat_obj,
126 &CLD2::kCjkDeltaBi_obj,
127 &CLD2::kDistinctBiTable_obj,
130 &CLD2::kDeltaOcta_obj,
131 &CLD2::kDistinctOcta_obj,
132 CLD2::kAvgDeltaOctaScore,
134 const CLD2::uint32 indirectTableSizes[7] = {
135 CLD2::kCompatTableIndSize,
136 CLD2::kCjkDeltaBiIndSize,
137 CLD2::kDistinctBiTableIndSize,
138 CLD2::kQuadChromeIndSize,
139 CLD2::kQuadChrome2IndSize,
140 CLD2::kDeltaOctaIndSize,
141 CLD2::kDistinctOctaIndSize
143 const CLD2DynamicData::Supplement supplement = {
144 CLD2::kAvgDeltaOctaScoreSize,
147 if (mode == 1) { // dump
148 CLD2DynamicDataExtractor::writeDataFile(
149 static_cast<const CLD2::ScoringTables*>(&realData),
152 } else if (mode == 3) { // head
153 CLD2DynamicData::FileHeader* header = CLD2DynamicDataLoader::loadHeaderFromFile(fileName);
154 if (header == NULL) {
155 std::cerr << "Cannot read header from file: " << fileName << std::endl;
158 CLD2DynamicData::dumpHeader(header);
159 delete header->tableHeaders;
163 if (mode == 1 || mode == 2) { // dump || verify (so perform verification)
164 void* mmapAddress = NULL;
165 uint32_t mmapLength = 0;
166 CLD2::ScoringTables* loadedData = CLD2DynamicDataLoader::loadDataFile(fileName, &mmapAddress, &mmapLength);
168 if (loadedData == NULL) {
169 std::cerr << "Failed to read data file: " << fileName << std::endl;
172 bool result = CLD2DynamicData::verify(
173 static_cast<const CLD2::ScoringTables*>(&realData),
175 static_cast<const CLD2::ScoringTables*>(loadedData));
176 CLD2DynamicDataLoader::unloadDataFile(&loadedData, &mmapAddress, &mmapLength);
177 if (loadedData != NULL || mmapAddress != NULL || mmapLength != 0) {
178 std::cerr << "Warning: failed to clean up memory for ScoringTables." << std::endl;
181 std::cerr << "Verification failed!" << std::endl;