Android-cuttlefish cvd tool
utf8.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#ifdef _WIN32
20#include <sys/types.h>
21#include <string>
22#else
23// Bring in prototypes for standard APIs so that we can import them into the utf8 namespace.
24#include <fcntl.h> // open
25#include <stdio.h> // fopen
26#include <sys/stat.h> // mkdir
27#include <unistd.h> // unlink
28#endif
29
30namespace android {
31namespace base {
32
33// Only available on Windows because this is only needed on Windows.
34#ifdef _WIN32
35// Convert size number of UTF-16 wchar_t's to UTF-8. Returns whether the
36// conversion was done successfully.
37bool WideToUTF8(const wchar_t* utf16, const size_t size, std::string* utf8);
38
39// Convert a NULL-terminated string of UTF-16 characters to UTF-8. Returns
40// whether the conversion was done successfully.
41bool WideToUTF8(const wchar_t* utf16, std::string* utf8);
42
43// Convert a UTF-16 std::wstring (including any embedded NULL characters) to
44// UTF-8. Returns whether the conversion was done successfully.
45bool WideToUTF8(const std::wstring& utf16, std::string* utf8);
46
47// Convert size number of UTF-8 char's to UTF-16. Returns whether the conversion
48// was done successfully.
49bool UTF8ToWide(const char* utf8, const size_t size, std::wstring* utf16);
50
51// Convert a NULL-terminated string of UTF-8 characters to UTF-16. Returns
52// whether the conversion was done successfully.
53bool UTF8ToWide(const char* utf8, std::wstring* utf16);
54
55// Convert a UTF-8 std::string (including any embedded NULL characters) to
56// UTF-16. Returns whether the conversion was done successfully.
57bool UTF8ToWide(const std::string& utf8, std::wstring* utf16);
58
59// Convert a file system path, represented as a NULL-terminated string of
60// UTF-8 characters, to a UTF-16 string representing the same file system
61// path using the Windows extended-lengh path representation.
62//
63// See https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#MAXPATH:
64// ```The Windows API has many functions that also have Unicode versions to
65// permit an extended-length path for a maximum total path length of 32,767
66// characters. To specify an extended-length path, use the "\\?\" prefix.
67// For example, "\\?\D:\very long path".```
68//
69// Returns whether the conversion was done successfully.
70bool UTF8PathToWindowsLongPath(const char* utf8, std::wstring* utf16);
71#endif
72
73// The functions in the utf8 namespace take UTF-8 strings. For Windows, these
74// are wrappers, for non-Windows these just expose existing APIs. To call these
75// functions, use:
76//
77// // anonymous namespace to avoid conflict with existing open(), unlink(), etc.
78// namespace {
79// // Import functions into anonymous namespace.
80// using namespace android::base::utf8;
81//
82// void SomeFunction(const char* name) {
83// int fd = open(name, ...); // Calls android::base::utf8::open().
84// ...
85// unlink(name); // Calls android::base::utf8::unlink().
86// }
87// }
88namespace utf8 {
89
90#ifdef _WIN32
91FILE* fopen(const char* name, const char* mode);
92int mkdir(const char* name, mode_t mode);
93int open(const char* name, int flags, ...);
94int unlink(const char* name);
95#else
96using ::fopen;
100#endif
101
102} // namespace utf8
103} // namespace base
104} // namespace android
uint32_t size
Definition: io.h:2
Definition: map_ptr.h:34
#define unlink
Definition: sysdeps.h:578
#define mkdir
Definition: sysdeps.h:738
#define open
Definition: sysdeps.h:492