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 command-line program converts an effective-TLD data file in UTF-8 from | 5 // This command-line program converts an effective-TLD data file in UTF-8 from |
6 // the format provided by Mozilla to the format expected by Chrome. Any errors | 6 // the format provided by Mozilla to the format expected by Chrome. Any errors |
7 // or warnings are recorded in tld_cleanup.log. | 7 // or warnings are recorded in tld_cleanup.log. |
8 // | 8 // |
9 // In particular, it | 9 // In particular, it |
10 // * Strips blank lines and comments, as well as notes for individual rules. | 10 // * Strips blank lines and comments, as well as notes for individual rules. |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "googleurl/src/gurl.h" | 31 #include "googleurl/src/gurl.h" |
32 #include "googleurl/src/url_parse.h" | 32 #include "googleurl/src/url_parse.h" |
33 | 33 |
34 typedef std::set<std::string> StringSet; | 34 typedef std::set<std::string> StringSet; |
35 | 35 |
36 // Writes the list of domain rules contained in the 'rules' set to the | 36 // Writes the list of domain rules contained in the 'rules' set to the |
37 // 'outfile', with each rule terminated by a LF. The file must already have | 37 // 'outfile', with each rule terminated by a LF. The file must already have |
38 // been created with write access. | 38 // been created with write access. |
39 bool WriteRules(const StringSet& rules, FilePath outfile) { | 39 bool WriteRules(const StringSet& rules, FilePath outfile) { |
40 std::string data; | 40 std::string data; |
| 41 data.append( |
| 42 "// Copyright (c) 2009 The Chromium Authors. All rights reserved.\n" |
| 43 "// Use of this source code is governed by a BSD-style license that\n" |
| 44 "// can be found in the LICENSE file.\n\n" |
| 45 "// This file is generated by net/tools/tld_cleanup/.\n" |
| 46 "// DO NOT MANUALLY EDIT!\n" |
| 47 "#include \"net/base/registry_controlled_domain.h\"\n\n" |
| 48 "const char net::RegistryControlledDomainService::kDomainData[] =\n" |
| 49 ); |
| 50 |
41 for (StringSet::const_iterator iter = rules.begin(); | 51 for (StringSet::const_iterator iter = rules.begin(); |
42 iter != rules.end(); | 52 iter != rules.end(); |
43 ++iter) { | 53 ++iter) { |
| 54 data.append(" \""); |
44 data.append(*iter); | 55 data.append(*iter); |
45 data.append("\n"); | 56 data.append("\\n\"\n"); |
46 } | 57 } |
47 | 58 |
| 59 data.append(";\n"); |
| 60 |
48 int written = file_util::WriteFile(outfile.ToWStringHack(), data.data(), | 61 int written = file_util::WriteFile(outfile.ToWStringHack(), data.data(), |
49 data.size()); | 62 data.size()); |
50 | 63 |
51 return written == static_cast<int>(data.size()); | 64 return written == static_cast<int>(data.size()); |
52 } | 65 } |
53 | 66 |
54 // These result codes should be in increasing order of severity. | 67 // These result codes should be in increasing order of severity. |
55 typedef enum { | 68 typedef enum { |
56 kSuccess, | 69 kSuccess, |
57 kWarning, | 70 kWarning, |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 rule->insert(0, 1, '!'); | 136 rule->insert(0, 1, '!'); |
124 else if (wildcard) | 137 else if (wildcard) |
125 rule->insert(0, "*."); | 138 rule->insert(0, "*."); |
126 | 139 |
127 return result; | 140 return result; |
128 } | 141 } |
129 | 142 |
130 // Loads the file described by 'in_filename', converts it to the desired format | 143 // Loads the file described by 'in_filename', converts it to the desired format |
131 // (see the file comments above), and saves it into 'out_filename'. Returns | 144 // (see the file comments above), and saves it into 'out_filename'. Returns |
132 // the most severe of the result codes encountered when normalizing the rules. | 145 // the most severe of the result codes encountered when normalizing the rules. |
133 NormalizeResult NormalizeFile(const std::wstring& in_filename, | 146 NormalizeResult NormalizeFile(const FilePath& in_filename, |
134 const std::wstring& out_filename) { | 147 const FilePath& out_filename) { |
135 std::string data; | 148 std::string data; |
136 if (!file_util::ReadFileToString(in_filename, &data)) { | 149 if (!file_util::ReadFileToString(in_filename, &data)) { |
137 fwprintf(stderr, L"Unable to read file %ls\n", in_filename.c_str()); | 150 LOG(ERROR) << "Unable to read file"; |
138 // We return success since we've already reported the error. | 151 // We return success since we've already reported the error. |
139 return kSuccess; | 152 return kSuccess; |
140 } | 153 } |
141 | 154 |
142 // We do a lot of string assignment during parsing, but simplicity is more | 155 // We do a lot of string assignment during parsing, but simplicity is more |
143 // important than performance here. | 156 // important than performance here. |
144 std::string rule; | 157 std::string rule; |
145 NormalizeResult result = kSuccess; | 158 NormalizeResult result = kSuccess; |
146 size_t line_start = 0; | 159 size_t line_start = 0; |
147 size_t line_end = 0; | 160 size_t line_end = 0; |
(...skipping 26 matching lines...) Expand all Loading... |
174 | 187 |
175 // Find beginning of next non-empty line. | 188 // Find beginning of next non-empty line. |
176 line_start = data.find_first_of("\r\n", line_end); | 189 line_start = data.find_first_of("\r\n", line_end); |
177 if (line_start == std::string::npos) | 190 if (line_start == std::string::npos) |
178 line_start = data.size(); | 191 line_start = data.size(); |
179 line_start = data.find_first_not_of("\r\n", line_start); | 192 line_start = data.find_first_not_of("\r\n", line_start); |
180 if (line_start == std::string::npos) | 193 if (line_start == std::string::npos) |
181 line_start = data.size(); | 194 line_start = data.size(); |
182 } | 195 } |
183 | 196 |
184 if (!WriteRules(rules, FilePath::FromWStringHack(out_filename))) { | 197 if (!WriteRules(rules, out_filename)) { |
185 LOG(ERROR) << "Error(s) writing " << out_filename; | 198 LOG(ERROR) << "Error(s) writing output file"; |
186 result = kError; | 199 result = kError; |
187 } | 200 } |
188 | 201 |
189 return result; | 202 return result; |
190 } | 203 } |
191 | 204 |
192 int main(int argc, const char* argv[]) { | 205 int main(int argc, const char* argv[]) { |
193 base::EnableTerminationOnHeapCorruption(); | 206 base::EnableTerminationOnHeapCorruption(); |
194 if (argc != 3) { | 207 if (argc != 1) { |
195 fprintf(stderr, "Normalizes and verifies UTF-8 TLD data files\n"); | 208 fprintf(stderr, "Normalizes and verifies UTF-8 TLD data files\n"); |
196 fprintf(stderr, "Usage: %s <input> <output>\n", argv[0]); | 209 fprintf(stderr, "Usage: %s\n", argv[0]); |
197 return 1; | 210 return 1; |
198 } | 211 } |
199 | 212 |
200 // Manages the destruction of singletons. | 213 // Manages the destruction of singletons. |
201 base::AtExitManager exit_manager; | 214 base::AtExitManager exit_manager; |
202 | 215 |
203 // Only use OutputDebugString in debug mode. | 216 // Only use OutputDebugString in debug mode. |
204 #ifdef NDEBUG | 217 #ifdef NDEBUG |
205 logging::LoggingDestination destination = logging::LOG_ONLY_TO_FILE; | 218 logging::LoggingDestination destination = logging::LOG_ONLY_TO_FILE; |
206 #else | 219 #else |
207 logging::LoggingDestination destination = | 220 logging::LoggingDestination destination = |
208 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG; | 221 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG; |
209 #endif | 222 #endif |
210 | 223 |
211 CommandLine::Init(argc, argv); | 224 CommandLine::Init(argc, argv); |
212 | 225 |
213 FilePath log_filename; | 226 FilePath log_filename; |
214 PathService::Get(base::DIR_EXE, &log_filename); | 227 PathService::Get(base::DIR_EXE, &log_filename); |
215 log_filename = log_filename.AppendASCII("tld_cleanup.log"); | 228 log_filename = log_filename.AppendASCII("tld_cleanup.log"); |
216 logging::InitLogging(log_filename.value().c_str(), | 229 logging::InitLogging(log_filename.value().c_str(), |
217 destination, | 230 destination, |
218 logging::LOCK_LOG_FILE, | 231 logging::LOCK_LOG_FILE, |
219 logging::DELETE_OLD_LOG_FILE); | 232 logging::DELETE_OLD_LOG_FILE); |
220 | 233 |
221 icu_util::Initialize(); | 234 icu_util::Initialize(); |
222 | 235 |
223 NormalizeResult result = NormalizeFile(UTF8ToWide(argv[1]), | 236 FilePath input_file; |
224 UTF8ToWide(argv[2])); | 237 PathService::Get(base::DIR_SOURCE_ROOT, &input_file); |
| 238 input_file = input_file.Append(FILE_PATH_LITERAL("net")) |
| 239 .Append(FILE_PATH_LITERAL("base")) |
| 240 .Append(FILE_PATH_LITERAL("effective_tld_names.dat")); |
| 241 FilePath output_file; |
| 242 PathService::Get(base::DIR_SOURCE_ROOT, &output_file); |
| 243 output_file = output_file.Append(FILE_PATH_LITERAL("net")) |
| 244 .Append(FILE_PATH_LITERAL("base")) |
| 245 .Append(FILE_PATH_LITERAL("effective_tld_names.cc")); |
| 246 NormalizeResult result = NormalizeFile(input_file, output_file); |
225 if (result != kSuccess) { | 247 if (result != kSuccess) { |
226 fprintf(stderr, | 248 fprintf(stderr, |
227 "Errors or warnings processing file. See log in tld_cleanup.log."); | 249 "Errors or warnings processing file. See log in tld_cleanup.log."); |
228 } | 250 } |
229 | 251 |
230 if (result == kError) | 252 if (result == kError) |
231 return 1; | 253 return 1; |
232 return 0; | 254 return 0; |
233 } | 255 } |
OLD | NEW |