Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / cld_2 / src / internal / cld2_dynamic_data_tool.cc
1 // Copyright 2014 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 #include <assert.h>
16 #include <stdio.h>
17 #include <iostream>
18 #include <fstream>
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/mman.h>
23
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"
31
32 // We need these in order to set up a real data object to pass around.                              
33 namespace CLD2 {
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;
51 }
52
53 int main(int argc, char** argv) {
54   if (!CLD2DynamicData::isLittleEndian()) {
55     std::cerr << "System is big-endian: currently not supported." << std::endl;
56     return -1;
57   }
58   if (!CLD2DynamicData::coreAssumptionsOk()) {
59     std::cerr << "Core assumptions violated, unsafe to continue." << std::endl;
60     return -2;
61   }
62
63   // Get command-line flags
64   int flags = 0;
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\
70 \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\
73 \n\
74 IMPORTANT: The files output by this tool WILL NOT work on big-endian platforms.\n\
75 \n\
76 Usage:\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\
80                     '--verify'.\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\
86 ";
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);
92     }
93     else if (strcmp(argv[i], "--dump") == 0
94               || strcmp(argv[i], "--verify") == 0
95               || strcmp(argv[i], "--head") == 0) {
96
97       // set mode flag properly
98       if (strcmp(argv[i], "--dump") == 0) mode=1;
99       else if (strcmp(argv[i], "--verify") == 0) mode=2;
100       else mode=3;
101       if (i < argc - 1) {
102         fileName = argv[++i];
103       } else {
104         std::cerr << "missing file name argument" << std::endl << std::endl;
105         std::cerr << USAGE;
106         return -1;
107       }
108     } else if (strcmp(argv[i], "--help") == 0) {
109       std::cout << USAGE;
110       return 0;
111     } else {
112       std::cerr << "Unsupported option: " << argv[i] << std::endl << std::endl;
113       std::cerr << USAGE;
114       return -1;
115     }
116   }
117
118   if (mode == 0) {
119     std::cerr << USAGE;
120     return -1;
121   }
122
123   CLD2::ScoringTables realData = {
124     &CLD2::cld_generated_CjkUni_obj,
125     &CLD2::kCjkCompat_obj,
126     &CLD2::kCjkDeltaBi_obj,
127     &CLD2::kDistinctBiTable_obj,
128     &CLD2::kQuad_obj,
129     &CLD2::kQuad_obj2,
130     &CLD2::kDeltaOcta_obj,
131     &CLD2::kDistinctOcta_obj,
132     CLD2::kAvgDeltaOctaScore,
133   };
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
142   };
143   const CLD2DynamicData::Supplement supplement = {
144     CLD2::kAvgDeltaOctaScoreSize,
145     indirectTableSizes
146   };
147   if (mode == 1) { // dump
148     CLD2DynamicDataExtractor::writeDataFile(
149       static_cast<const CLD2::ScoringTables*>(&realData),
150       &supplement,
151       fileName);
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;
156       return -1;
157     }
158     CLD2DynamicData::dumpHeader(header);
159     delete header->tableHeaders;
160     delete header;
161   }
162   
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);
167
168     if (loadedData == NULL) {
169       std::cerr << "Failed to read data file: " << fileName << std::endl;
170       return -1;
171     }
172     bool result = CLD2DynamicData::verify(
173       static_cast<const CLD2::ScoringTables*>(&realData),
174       &supplement,
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;
179     }
180     if (!result) {
181       std::cerr << "Verification failed!" << std::endl;
182       return -1;
183     }
184   }
185 }