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
53
54
55
56
57
58
59
60
61
62
|
// Copyright (c) 2009 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 "chrome/browser/sync/util/character_set_converters.h"
#include <windows.h>
#include <string>
using std::string;
namespace browser_sync {
// Converts input_string to UTF8 and appends the result into to output_string.
void AppendPathStringToUTF8(const PathChar* wide, int size,
string* output_string) {
CHECK(output_string);
if (0 == size)
return;
int needed_space = ::WideCharToMultiByte(CP_UTF8, 0, wide, size, 0, 0, 0, 0);
// TODO(sync): This should flag an error when we move to an api that can let
// utf-16 -> utf-8 fail.
CHECK(0 != needed_space);
string::size_type current_size = output_string->size();
output_string->resize(current_size + needed_space);
CHECK(0 != ::WideCharToMultiByte(CP_UTF8, 0, wide, size,
&(*output_string)[current_size], needed_space, 0, 0));
}
bool AppendUTF8ToPathString(const char* utf8, size_t size,
PathString* output_string) {
CHECK(output_string);
if (0 == size)
return true;
// TODO(sync): Do we want to force precomposed characters here?
int needed_wide_chars = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
utf8, size, 0, 0);
if (0 == needed_wide_chars) {
DWORD err = ::GetLastError();
if (MB_ERR_INVALID_CHARS == err)
return false;
CHECK(0 == err);
}
PathString::size_type current_length = output_string->size();
output_string->resize(current_length + needed_wide_chars);
CHECK(0 != ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8, size,
&(*output_string)[current_length], needed_wide_chars));
return true;
}
void TrimPathStringToValidCharacter(PathString* string) {
CHECK(string);
// Constants from http://en.wikipedia.org/wiki/UTF-16
if (string->empty())
return;
if (0x0dc00 == (string->at(string->length() - 1) & 0x0fc00))
string->resize(string->length() - 1);
}
} // namespace browser_sync
|