diff options
author | bauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-15 09:51:50 +0000 |
---|---|---|
committer | bauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-15 09:51:50 +0000 |
commit | 8703b2b019d923536a9ad4a8eda2053aa7c7472e (patch) | |
tree | be668fd7d482d45f80f6d7b074e7cf442318cb51 /base | |
parent | 2c48f9216fe490c9bfe33b6ed0fa063d6d67e220 (diff) | |
download | chromium_src-8703b2b019d923536a9ad4a8eda2053aa7c7472e.zip chromium_src-8703b2b019d923536a9ad4a8eda2053aa7c7472e.tar.gz chromium_src-8703b2b019d923536a9ad4a8eda2053aa7c7472e.tar.bz2 |
Move FilePath <-> Value conversions into a separate file.
BUG=75276
TEST=none
Review URL: http://codereview.chromium.org/6691011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78182 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gypi | 6 | ||||
-rw-r--r-- | base/value_conversions.cc | 52 | ||||
-rw-r--r-- | base/value_conversions.h | 23 | ||||
-rw-r--r-- | base/values.cc | 33 | ||||
-rw-r--r-- | base/values.h | 4 |
5 files changed, 79 insertions, 39 deletions
diff --git a/base/base.gypi b/base/base.gypi index c265f36..ac8d9acd 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -111,8 +111,8 @@ 'logging.h', 'logging_win.cc', 'mac/cocoa_protocols.h', - 'mac/foundation_util.h', - 'mac/foundation_util.mm', + 'mac/foundation_util.h', + 'mac/foundation_util.mm', 'mac/mac_util.h', 'mac/mac_util.mm', 'mac/os_crash_dumps.cc', @@ -299,6 +299,8 @@ 'utf_string_conversions.h', 'values.cc', 'values.h', + 'value_conversions.cc', + 'value_conversions.h', 'version.cc', 'version.h', 'vlog.cc', diff --git a/base/value_conversions.cc b/base/value_conversions.cc new file mode 100644 index 0000000..09522be --- /dev/null +++ b/base/value_conversions.cc @@ -0,0 +1,52 @@ +// 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. + +#include "base/value_conversions.h" + +#include "base/file_path.h" +#include "base/sys_string_conversions.h" +#include "base/utf_string_conversions.h" +#include "base/values.h" + +namespace base { + +namespace { + +// |Value| internally stores strings in UTF-8, so we have to convert from the +// system native code to UTF-8 and back. + +std::string FilePathToUTF8(const FilePath& file_path) { +#if defined(OS_POSIX) + return WideToUTF8(SysNativeMBToWide(file_path.value())); +#else + return UTF16ToUTF8(file_path.value()); +#endif +} + +FilePath UTF8ToFilePath(const std::string& str) { + FilePath::StringType result; +#if defined(OS_POSIX) + result = SysWideToNativeMB(UTF8ToWide(str)); +#elif defined(OS_WIN) + result = UTF8ToUTF16(str); +#endif + return FilePath(result); +} + +} // namespace + +StringValue* CreateFilePathValue(const FilePath& in_value) { + return new StringValue(internal::FilePathToUTF8(in_value)); +} + +bool GetValueAsFilePath(const Value& value, FilePath* file_path) { + std::string str; + if (!value.GetAsString(&str)) + return false; + if (file_path) + *file_path = internal::UTF8ToFilePath(str); + return true; +} + +} // namespace base diff --git a/base/value_conversions.h b/base/value_conversions.h new file mode 100644 index 0000000..9678fc1 --- /dev/null +++ b/base/value_conversions.h @@ -0,0 +1,23 @@ +// 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. + +#ifndef BASE_VALUE_CONVERSIONS_H_ +#define BASE_VALUE_CONVERSIONS_H_ +#pragma once + +// This file contains methods to convert a |FilePath| to a |Value| and back. + +class FilePath; +class StringValue; +class Value; + +namespace base { + +// The caller takes ownership of the returned value. +StringValue* CreateFilePathValue(const FilePath& in_value); +bool GetValueAsFilePath(const Value& value, FilePath* file_path); + +} // namespace + +#endif // BASE_VALUE_CONVERSIONS_H_ diff --git a/base/values.cc b/base/values.cc index 3340f9b..9c96f26 100644 --- a/base/values.cc +++ b/base/values.cc @@ -4,10 +4,8 @@ #include "base/values.h" -#include "base/file_path.h" #include "base/logging.h" #include "base/string_util.h" -#include "base/sys_string_conversions.h" #include "base/utf_string_conversions.h" namespace { @@ -97,19 +95,6 @@ StringValue* Value::CreateStringValue(const string16& in_value) { } // static -StringValue* Value::CreateFilePathValue(const FilePath& in_value) { -#if defined(OS_POSIX) - // Value::SetString only knows about UTF8 strings, so convert the path from - // the system native value to UTF8. - std::string path_utf8 = WideToUTF8(base::SysNativeMBToWide(in_value.value())); - return new StringValue(path_utf8); -#else - return new StringValue(in_value.value()); -#endif - -} - -// static BinaryValue* Value::CreateBinaryValue(char* buffer, size_t size) { return BinaryValue::Create(buffer, size); } @@ -134,10 +119,6 @@ bool Value::GetAsString(string16* out_value) const { return false; } -bool Value::GetAsFilePath(FilePath* out_value) const { - return false; -} - bool Value::GetAsList(ListValue** out_value) { return false; } @@ -269,20 +250,6 @@ bool StringValue::GetAsString(string16* out_value) const { return true; } -bool StringValue::GetAsFilePath(FilePath* out_value) const { - if (out_value) { - FilePath::StringType result; -#if defined(OS_POSIX) - // We store filepaths as UTF8, so convert it back to the system type. - result = base::SysWideToNativeMB(UTF8ToWide(value_)); -#elif defined(OS_WIN) - result = UTF8ToUTF16(value_); -#endif - *out_value = FilePath(result); - } - return true; -} - StringValue* StringValue::DeepCopy() const { return CreateStringValue(value_); } diff --git a/base/values.h b/base/values.h index 5814a9f..8dc9f25 100644 --- a/base/values.h +++ b/base/values.h @@ -33,7 +33,6 @@ class BinaryValue; class DictionaryValue; -class FilePath; class FundamentalValue; class ListValue; class StringValue; @@ -69,7 +68,6 @@ class Value { static FundamentalValue* CreateDoubleValue(double in_value); static StringValue* CreateStringValue(const std::string& in_value); static StringValue* CreateStringValue(const string16& in_value); - static StringValue* CreateFilePathValue(const FilePath& in_value); // This one can return NULL if the input isn't valid. If the return value // is non-null, the new object has taken ownership of the buffer pointer. @@ -94,7 +92,6 @@ class Value { virtual bool GetAsDouble(double* out_value) const; virtual bool GetAsString(std::string* out_value) const; virtual bool GetAsString(string16* out_value) const; - virtual bool GetAsFilePath(FilePath* out_value) const; virtual bool GetAsList(ListValue** out_value); // This creates a deep copy of the entire Value tree, and returns a pointer @@ -162,7 +159,6 @@ class StringValue : public Value { // Subclassed methods virtual bool GetAsString(std::string* out_value) const; virtual bool GetAsString(string16* out_value) const; - virtual bool GetAsFilePath(FilePath* out_value) const; virtual StringValue* DeepCopy() const; virtual bool Equals(const Value* other) const; |