summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-19 10:26:24 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-19 10:26:24 +0000
commit3bf683815a0ab6beaac2fb4c2d5633487dc62009 (patch)
tree714075ca96c44637406abf7b1f2869d732067f9b
parent26a666d76f9182543859bb6f3d4eb22bc45c0e0a (diff)
downloadchromium_src-3bf683815a0ab6beaac2fb4c2d5633487dc62009.zip
chromium_src-3bf683815a0ab6beaac2fb4c2d5633487dc62009.tar.gz
chromium_src-3bf683815a0ab6beaac2fb4c2d5633487dc62009.tar.bz2
FTP: use ICU to parse month abbreviations.
In theory we should be able to parse localized listings in more languages now. This also allows us to get rid of hardcoded special cases for German. BUG=65917 TEST=net_unittests Review URL: http://codereview.chromium.org/6697021 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78826 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--net/ftp/ftp_directory_listing_parser_vms.cc4
-rw-r--r--net/ftp/ftp_util.cc69
-rw-r--r--net/ftp/ftp_util.h23
3 files changed, 49 insertions, 47 deletions
diff --git a/net/ftp/ftp_directory_listing_parser_vms.cc b/net/ftp/ftp_directory_listing_parser_vms.cc
index c74dad0..65a3161 100644
--- a/net/ftp/ftp_directory_listing_parser_vms.cc
+++ b/net/ftp/ftp_directory_listing_parser_vms.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -130,7 +130,7 @@ bool VmsDateListingToTime(const std::vector<string16>& columns,
return false;
if (!base::StringToInt(date_parts[0], &time_exploded.day_of_month))
return false;
- if (!net::FtpUtil::ThreeLetterMonthToNumber(date_parts[1],
+ if (!net::FtpUtil::AbbreviatedMonthToNumber(date_parts[1],
&time_exploded.month))
return false;
if (!base::StringToInt(date_parts[2], &time_exploded.year))
diff --git a/net/ftp/ftp_util.cc b/net/ftp/ftp_util.cc
index 2633e9d..6c7959f 100644
--- a/net/ftp/ftp_util.cc
+++ b/net/ftp/ftp_util.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -12,6 +12,8 @@
#include "base/string_util.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
+#include "unicode/datefmt.h"
+#include "unicode/dtfmtsym.h"
// For examples of Unix<->VMS path conversions, see the unit test file. On VMS
// a path looks differently depending on whether it's a file or directory.
@@ -108,41 +110,40 @@ std::string FtpUtil::VMSPathToUnix(const std::string& vms_path) {
}
// static
-bool FtpUtil::ThreeLetterMonthToNumber(const string16& text, int* number) {
- const static char* months[] = { "jan", "feb", "mar", "apr", "may", "jun",
- "jul", "aug", "sep", "oct", "nov", "dec" };
-
- for (size_t i = 0; i < arraysize(months); i++) {
- if (LowerCaseEqualsASCII(text, months[i])) {
- *number = i + 1;
- return true;
+bool FtpUtil::AbbreviatedMonthToNumber(const string16& text, int* number) {
+ icu::UnicodeString unicode_text(text.data(), text.size());
+
+ int32_t locales_count;
+ const icu::Locale* locales =
+ icu::DateFormat::getAvailableLocales(locales_count);
+
+ // Some FTP servers localize the date listings. To guess the locale,
+ // we loop over all available ones.
+ for (int32_t locale = 0; locale < locales_count; locale++) {
+ UErrorCode status(U_ZERO_ERROR);
+
+ icu::DateFormatSymbols format_symbols(locales[locale], status);
+
+ // If we cannot get format symbols for some locale, it's not a fatal error.
+ // Just try another one.
+ if (U_FAILURE(status))
+ continue;
+
+ int32_t months_count;
+ const icu::UnicodeString* months =
+ format_symbols.getShortMonths(months_count);
+
+ // Loop over all abbreviated month names in given locale.
+ // An alternative solution (to parse |text| in given locale) is more
+ // lenient, and may accept more than we want even with setLenient(false).
+ for (int32_t month = 0; month < months_count; month++) {
+ if (months[month].caseCompare(unicode_text, 0) == 0) {
+ *number = month + 1;
+ return true;
+ }
}
}
- // Special cases for directory listings in German (other three-letter month
- // abbreviations are the same as in English). Note that we don't need to do
- // a case-insensitive compare here. Only "ls -l" style listings may use
- // localized month names, and they will always start capitalized. Also,
- // converting non-ASCII characters to lowercase would be more complicated.
- if (text == UTF8ToUTF16("M\xc3\xa4r")) {
- // The full month name is M-(a-umlaut)-rz (March), which is M-(a-umlaut)r
- // when abbreviated.
- *number = 3;
- return true;
- }
- if (text == ASCIIToUTF16("Mai")) {
- *number = 5;
- return true;
- }
- if (text == ASCIIToUTF16("Okt")) {
- *number = 10;
- return true;
- }
- if (text == ASCIIToUTF16("Dez")) {
- *number = 12;
- return true;
- }
-
return false;
}
@@ -153,7 +154,7 @@ bool FtpUtil::LsDateListingToTime(const string16& month, const string16& day,
base::Time* result) {
base::Time::Exploded time_exploded = { 0 };
- if (!ThreeLetterMonthToNumber(month, &time_exploded.month))
+ if (!AbbreviatedMonthToNumber(month, &time_exploded.month))
return false;
if (!base::StringToInt(day, &time_exploded.day_of_month))
diff --git a/net/ftp/ftp_util.h b/net/ftp/ftp_util.h
index eb7e963..a3189c7 100644
--- a/net/ftp/ftp_util.h
+++ b/net/ftp/ftp_util.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -18,22 +18,23 @@ namespace net {
class FtpUtil {
public:
- // Convert Unix file path to VMS path (must be a file, and not a directory).
+ // Converts Unix file path to VMS path (must be a file, and not a directory).
static std::string UnixFilePathToVMS(const std::string& unix_path);
- // Convert Unix directory path to VMS path (must be a directory).
+ // Converts Unix directory path to VMS path (must be a directory).
static std::string UnixDirectoryPathToVMS(const std::string& unix_path);
- // Convert VMS path to Unix-style path.
+ // Converts VMS path to Unix-style path.
static std::string VMSPathToUnix(const std::string& vms_path);
- // Convert three-letter month abbreviation (like Nov) to its number (in range
- // 1-12).
- static bool ThreeLetterMonthToNumber(const string16& text, int* number);
+ // Converts abbreviated month (like Nov) to its number (in range 1-12).
+ // Note: in some locales abbreviations are more than three letters long,
+ // and this function also handles them correctly.
+ static bool AbbreviatedMonthToNumber(const string16& text, int* number);
- // Convert a "ls -l" date listing to time. The listing comes in three columns.
- // The first one contains month, the second one contains day of month.
- // The first one is either a time (and then we guess the year based
+ // Converts a "ls -l" date listing to time. The listing comes in three
+ // columns. The first one contains month, the second one contains day
+ // of month. The third one is either a time (and then we guess the year based
// on |current_time|), or is a year (and then we don't know the time).
static bool LsDateListingToTime(const string16& month,
const string16& day,
@@ -41,7 +42,7 @@ class FtpUtil {
const base::Time& current_time,
base::Time* result);
- // Skip |columns| columns from |text| (whitespace-delimited), and return the
+ // Skips |columns| columns from |text| (whitespace-delimited), and returns the
// remaining part, without leading/trailing whitespace.
static string16 GetStringPartAfterColumns(const string16& text, int columns);
};