OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "base/utf_string_conversions.h" |
5 #include "chrome/browser/sync/util/character_set_converters.h" | 6 #include "chrome/browser/sync/util/character_set_converters.h" |
6 | 7 |
7 #include <windows.h> | |
8 | |
9 #include <string> | |
10 | |
11 using std::string; | |
12 | |
13 namespace browser_sync { | 8 namespace browser_sync { |
14 | 9 |
15 // Converts input_string to UTF8 and appends the result into to output_string. | 10 // Returns UTF8 string from the given FilePath. |
16 void AppendPathStringToUTF8(const PathChar* wide, int size, | 11 std::string FilePathToUTF8(const FilePath& file_path) { |
17 string* output_string) { | 12 return WideToUTF8(file_path.value()); |
18 CHECK(output_string); | |
19 if (0 == size) | |
20 return; | |
21 | |
22 int needed_space = ::WideCharToMultiByte(CP_UTF8, 0, wide, size, 0, 0, 0, 0); | |
23 // TODO(sync): This should flag an error when we move to an api that can let | |
24 // utf-16 -> utf-8 fail. | |
25 CHECK(0 != needed_space); | |
26 string::size_type current_size = output_string->size(); | |
27 output_string->resize(current_size + needed_space); | |
28 CHECK(0 != ::WideCharToMultiByte(CP_UTF8, 0, wide, size, | |
29 &(*output_string)[current_size], needed_space, 0, 0)); | |
30 } | 13 } |
31 | 14 |
32 bool AppendUTF8ToPathString(const char* utf8, size_t size, | 15 // Returns FilePath from the given UTF8 string. |
33 PathString* output_string) { | 16 FilePath UTF8ToFilePath(const std::string& utf8) { |
34 CHECK(output_string); | 17 return FilePath(UTF8ToWide(utf8)); |
35 if (0 == size) | |
36 return true; | |
37 // TODO(sync): Do we want to force precomposed characters here? | |
38 int needed_wide_chars = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, | |
39 utf8, size, 0, 0); | |
40 if (0 == needed_wide_chars) { | |
41 DWORD err = ::GetLastError(); | |
42 if (MB_ERR_INVALID_CHARS == err) | |
43 return false; | |
44 CHECK(0 == err); | |
45 } | |
46 PathString::size_type current_length = output_string->size(); | |
47 output_string->resize(current_length + needed_wide_chars); | |
48 CHECK(0 != ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8, size, | |
49 &(*output_string)[current_length], needed_wide_chars)); | |
50 return true; | |
51 } | |
52 | |
53 void TrimPathStringToValidCharacter(PathString* string) { | |
54 CHECK(string); | |
55 // Constants from http://en.wikipedia.org/wiki/UTF-16 | |
56 if (string->empty()) | |
57 return; | |
58 if (0x0dc00 == (string->at(string->length() - 1) & 0x0fc00)) | |
59 string->resize(string->length() - 1); | |
60 } | 18 } |
61 | 19 |
62 } // namespace browser_sync | 20 } // namespace browser_sync |
OLD | NEW |