summaryrefslogtreecommitdiffstats
path: root/net/tools
diff options
context:
space:
mode:
authormbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-07 02:53:49 +0000
committermbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-07 02:53:49 +0000
commit1f070f059c645668e6b0446b726f5a08613c20d0 (patch)
tree4c6dfc36a895c1b22b964e225a0814cb6255dd43 /net/tools
parent308379a5ec38a9cf25a91aeaa3f11f9f7af33e95 (diff)
downloadchromium_src-1f070f059c645668e6b0446b726f5a08613c20d0.zip
chromium_src-1f070f059c645668e6b0446b726f5a08613c20d0.tar.gz
chromium_src-1f070f059c645668e6b0446b726f5a08613c20d0.tar.bz2
Fix the UrlToFilenameEncoder to be more cross platform.
FilePath::StringType on linux is a std::string, while on Windows it is a std::wstring. The old implementation assumed wstrings. Rework the class to deal entirely with narrow strings (since wide-strings are not relevant for this utility). Note: This file is only used in some tools. BUG=none TEST=none Review URL: http://codereview.chromium.org/261001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@28224 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/tools')
-rw-r--r--net/tools/dump_cache/url_to_filename_encoder.h59
1 files changed, 30 insertions, 29 deletions
diff --git a/net/tools/dump_cache/url_to_filename_encoder.h b/net/tools/dump_cache/url_to_filename_encoder.h
index e71d7a0..4b9e6c5 100644
--- a/net/tools/dump_cache/url_to_filename_encoder.h
+++ b/net/tools/dump_cache/url_to_filename_encoder.h
@@ -5,6 +5,8 @@
#ifndef NET_TOOLS_DUMP_CACHE_URL_TO_FILE_ENCODER_H_
#define NET_TOOLS_DUMP_CACHE_URL_TO_FILE_ENCODER_H_
+#include <string>
+
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/string_util.h"
@@ -24,22 +26,22 @@ class UrlToFilenameEncoder {
GURL gurl(clean_url);
FilePath filename(base_path);
- filename = filename.Append(ASCIIToWide(gurl.host()));
+ filename = filename.AppendASCII(gurl.host());
- std::wstring url_filename = ASCIIToWide(gurl.PathForRequest());
+ std::string url_filename = gurl.PathForRequest();
// Strip the leading '/'
- if (url_filename[0] == L'/')
+ if (url_filename[0] == '/')
url_filename = url_filename.substr(1);
// replace '/' with '\'
- ConvertToSlashes(url_filename);
+ ConvertToSlashes(&url_filename);
// strip double slashes ("\\")
- StripDoubleSlashes(url_filename);
+ StripDoubleSlashes(&url_filename);
// Save path as filesystem-safe characters
url_filename = Escape(url_filename);
- filename = filename.Append(url_filename);
+ filename = filename.AppendASCII(url_filename);
return filename;
}
@@ -49,18 +51,16 @@ class UrlToFilenameEncoder {
// Technically, we shouldn't need to do this, but I found that
// even with long-filename support, windows had trouble creating
// long subdirectories, and making them shorter helps.
- static const int kMaximumSubdirectoryLength = 128;
+ static const size_t kMaximumSubdirectoryLength = 128;
// Escape the given input |path| and chop any individual components
// of the path which are greater than kMaximumSubdirectoryLength characters
// into two chunks.
- static std::wstring Escape(const std::wstring& path) {
- std::wstring output;
+ static std::string Escape(const std::string& path) {
+ std::string output;
int last_slash = 0;
for (size_t index = 0; index < path.length(); index++) {
- wchar_t wide_char = path[index];
- DCHECK((wide_char & 0xff) == wide_char);
- char ch = static_cast<char>(wide_char & 0xff);
+ char ch = path[index];
if (ch == 0x5C)
last_slash = index;
if ((ch == 0x2D) || // hyphen
@@ -70,8 +70,8 @@ class UrlToFilenameEncoder {
((0x61 <= ch) && (ch <= 0x7A))) { // Lowercase [a-z]
output.append(&path[index],1);
} else {
- wchar_t encoded[3];
- encoded[0] = L'x';
+ char encoded[3];
+ encoded[0] = 'x';
encoded[1] = ch / 16;
encoded[1] += (encoded[1] >= 10) ? 'A' - 10 : '0';
encoded[2] = ch % 16;
@@ -79,7 +79,7 @@ class UrlToFilenameEncoder {
output.append(encoded, 3);
}
if (index - last_slash > kMaximumSubdirectoryLength) {
- wchar_t backslash = L'\\';
+ char backslash = '\\';
output.append(&backslash, 1);
last_slash = index;
}
@@ -88,28 +88,29 @@ class UrlToFilenameEncoder {
}
// Replace all instances of |from| within |str| as |to|.
- static void ReplaceAll(std::wstring& str, const std::wstring& from,
- const std::wstring& to) {
- std::wstring::size_type pos(0);
- while((pos = str.find(from, pos)) != std::wstring::npos) {
- str.replace(pos, from.size(), to);
+ static void ReplaceAll(const std::string& from,
+ const std::string& to,
+ std::string* str) {
+ std::string::size_type pos(0);
+ while((pos = str->find(from, pos)) != std::string::npos) {
+ str->replace(pos, from.size(), to);
pos += from.size();
}
}
// Replace all instances of "/" with "\" in |path|.
- static void ConvertToSlashes(std::wstring& path) {
- std::wstring slash(L"/");
- std::wstring backslash(L"\\");
- ReplaceAll(path, slash, backslash);
+ static void ConvertToSlashes(std::string* path) {
+ static const char slash[] = { '/', '\0' };
+ static const char backslash[] = { '\\', '\0' };
+ ReplaceAll(slash, backslash, path);
}
// Replace all instances of "\\" with "%5C%5C" in |path|.
- static void StripDoubleSlashes(std::wstring& path) {
- std::wstring::size_type pos(0);
- std::wstring doubleslash(L"\\\\");
- std::wstring escaped_doubleslash(L"%5C%5C");
- ReplaceAll(path, doubleslash, escaped_doubleslash);
+ static void StripDoubleSlashes(std::string* path) {
+ static const char doubleslash[] = { '\\', '\\', '\0' };
+ static const char escaped_doubleslash[] =
+ { '%', '5', 'C', '%', '5', 'C','\0' };
+ ReplaceAll(doubleslash, escaped_doubleslash, path);
}
};