| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This tool converts Hunspell .aff/.dic pairs to a combined binary dictionary | 5 // This tool converts Hunspell .aff/.dic pairs to a combined binary dictionary |
| 6 // format (.bdic). This format is more compact, and can be more efficiently | 6 // format (.bdic). This format is more compact, and can be more efficiently |
| 7 // read by the client application. | 7 // read by the client application. |
| 8 // | 8 // |
| 9 // We do this conversion manually before publishing dictionary files. It is not | 9 // We do this conversion manually before publishing dictionary files. It is not |
| 10 // part of any build process. | 10 // part of any build process. |
| 11 // | 11 // |
| 12 // See PrintHelp() below for usage. | 12 // See PrintHelp() below for usage. |
| 13 | 13 |
| 14 #include <stdio.h> | 14 #include <stdio.h> |
| 15 | 15 |
| 16 #include "base/at_exit.h" | 16 #include "base/at_exit.h" |
| 17 #include "base/file_path.h" |
| 17 #include "base/file_util.h" | 18 #include "base/file_util.h" |
| 18 #include "base/i18n/icu_util.h" | 19 #include "base/i18n/icu_util.h" |
| 19 #include "base/logging.h" | 20 #include "base/logging.h" |
| 20 #include "base/process_util.h" | 21 #include "base/process_util.h" |
| 21 #include "base/string_util.h" | 22 #include "base/string_util.h" |
| 22 #include "third_party/hunspell/google/bdict_reader.h" | 23 #include "third_party/hunspell/google/bdict_reader.h" |
| 23 #include "third_party/hunspell/google/bdict_writer.h" | 24 #include "third_party/hunspell/google/bdict_writer.h" |
| 24 #include "chrome/tools/convert_dict/aff_reader.h" | 25 #include "chrome/tools/convert_dict/aff_reader.h" |
| 25 #include "chrome/tools/convert_dict/dic_reader.h" | 26 #include "chrome/tools/convert_dict/dic_reader.h" |
| 26 | 27 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 int PrintHelp() { | 76 int PrintHelp() { |
| 76 printf("Usage: convert_dict <dicfile base name>\n\n"); | 77 printf("Usage: convert_dict <dicfile base name>\n\n"); |
| 77 printf("Example:\n"); | 78 printf("Example:\n"); |
| 78 printf(" convert_dict en-US\nwill read en-US.dic / en-US.aff and\n"); | 79 printf(" convert_dict en-US\nwill read en-US.dic / en-US.aff and\n"); |
| 79 printf("generate en-US.bdic\n\n"); | 80 printf("generate en-US.bdic\n\n"); |
| 80 return 1; | 81 return 1; |
| 81 } | 82 } |
| 82 | 83 |
| 83 } // namespace | 84 } // namespace |
| 84 | 85 |
| 86 #if defined(OS_WIN) |
| 87 int wmain(int argc, wchar_t* argv[]) { |
| 88 #else |
| 85 int main(int argc, char* argv[]) { | 89 int main(int argc, char* argv[]) { |
| 90 #endif |
| 86 base::EnableTerminationOnHeapCorruption(); | 91 base::EnableTerminationOnHeapCorruption(); |
| 87 if (argc != 2) | 92 if (argc != 2) |
| 88 return PrintHelp(); | 93 return PrintHelp(); |
| 89 | 94 |
| 90 base::AtExitManager exit_manager; | 95 base::AtExitManager exit_manager; |
| 91 icu_util::Initialize(); | 96 icu_util::Initialize(); |
| 92 | 97 |
| 93 std::string file_base = argv[1]; | 98 FilePath file_base = FilePath(argv[1]); |
| 94 | 99 |
| 95 std::string aff_name = file_base + ".aff"; | 100 FilePath aff_path = file_base.ReplaceExtension(FILE_PATH_LITERAL(".aff")); |
| 96 printf("Reading %s ...\n", aff_name.c_str()); | 101 printf("Reading %" PRFilePath " ...\n", aff_path.value().c_str()); |
| 97 convert_dict::AffReader aff_reader(aff_name.c_str()); | 102 convert_dict::AffReader aff_reader(aff_path); |
| 98 if (!aff_reader.Read()) { | 103 if (!aff_reader.Read()) { |
| 99 printf("Unable to read the aff file.\n"); | 104 printf("Unable to read the aff file.\n"); |
| 100 return 1; | 105 return 1; |
| 101 } | 106 } |
| 102 | 107 |
| 103 std::string dic_name = file_base + ".dic"; | 108 FilePath dic_path = file_base.ReplaceExtension(FILE_PATH_LITERAL(".dic")); |
| 104 printf("Reading %s ...\n", dic_name.c_str()); | 109 printf("Reading %" PRFilePath " ...\n", dic_path.value().c_str()); |
| 105 convert_dict::DicReader dic_reader(dic_name.c_str()); | 110 convert_dict::DicReader dic_reader(dic_path); |
| 106 if (!dic_reader.Read(&aff_reader)) { | 111 if (!dic_reader.Read(&aff_reader)) { |
| 107 printf("Unable to read the dic file.\n"); | 112 printf("Unable to read the dic file.\n"); |
| 108 return 1; | 113 return 1; |
| 109 } | 114 } |
| 110 | 115 |
| 111 hunspell::BDictWriter writer; | 116 hunspell::BDictWriter writer; |
| 112 writer.SetComment(aff_reader.comments()); | 117 writer.SetComment(aff_reader.comments()); |
| 113 writer.SetAffixRules(aff_reader.affix_rules()); | 118 writer.SetAffixRules(aff_reader.affix_rules()); |
| 114 writer.SetAffixGroups(aff_reader.GetAffixGroups()); | 119 writer.SetAffixGroups(aff_reader.GetAffixGroups()); |
| 115 writer.SetReplacements(aff_reader.replacements()); | 120 writer.SetReplacements(aff_reader.replacements()); |
| 116 writer.SetOtherCommands(aff_reader.other_commands()); | 121 writer.SetOtherCommands(aff_reader.other_commands()); |
| 117 writer.SetWords(dic_reader.words()); | 122 writer.SetWords(dic_reader.words()); |
| 118 | 123 |
| 119 printf("Serializing...\n"); | 124 printf("Serializing...\n"); |
| 120 std::string serialized = writer.GetBDict(); | 125 std::string serialized = writer.GetBDict(); |
| 121 | 126 |
| 122 printf("Verifying...\n"); | 127 printf("Verifying...\n"); |
| 123 if (!VerifyWords(dic_reader.words(), serialized)) { | 128 if (!VerifyWords(dic_reader.words(), serialized)) { |
| 124 printf("ERROR converting, the dictionary does not check out OK."); | 129 printf("ERROR converting, the dictionary does not check out OK."); |
| 125 return 1; | 130 return 1; |
| 126 } | 131 } |
| 127 | 132 |
| 128 std::string out_name = file_base + ".bdic"; | 133 FilePath out_path = file_base.ReplaceExtension(FILE_PATH_LITERAL(".bdic")); |
| 129 printf("Writing %s ...\n", out_name.c_str()); | 134 printf("Writing %" PRFilePath " ...\n", out_path.value().c_str()); |
| 130 FILE* out_file = file_util::OpenFile(out_name, "wb"); | 135 FILE* out_file = file_util::OpenFile(out_path, "wb"); |
| 131 if (!out_file) { | 136 if (!out_file) { |
| 132 printf("ERROR writing file\n"); | 137 printf("ERROR writing file\n"); |
| 133 return 1; | 138 return 1; |
| 134 } | 139 } |
| 135 size_t written = fwrite(&serialized[0], 1, serialized.size(), out_file); | 140 size_t written = fwrite(&serialized[0], 1, serialized.size(), out_file); |
| 136 CHECK(written == serialized.size()); | 141 CHECK(written == serialized.size()); |
| 137 file_util::CloseFile(out_file); | 142 file_util::CloseFile(out_file); |
| 138 | 143 |
| 139 return 0; | 144 return 0; |
| 140 } | 145 } |
| OLD | NEW |