summaryrefslogtreecommitdiffstats
path: root/base/value_conversions.cc
blob: 64513d070dc9fa14a1a3e983e47e089584df8b5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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(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 = UTF8ToFilePath(str);
  return true;
}

}  // namespace base