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/value_conversions.cc | |
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/value_conversions.cc')
-rw-r--r-- | base/value_conversions.cc | 52 |
1 files changed, 52 insertions, 0 deletions
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 |