blob: c9cc1ab0ca7e2c1f9286791a7c9f6db4e462df66 [file] [log] [blame]
Elliott Hughesc1fd4922015-11-11 18:02:29 +00001/*
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
Elliott Hughes54c72aa2016-03-23 15:04:52 -070017#ifndef ANDROID_BASE_UTF8_H
18#define ANDROID_BASE_UTF8_H
Elliott Hughesc1fd4922015-11-11 18:02:29 +000019
20#ifdef _WIN32
21#include <string>
Spencer Lowd21dc822015-11-12 15:20:15 -080022#else
23// Bring in prototypes for standard APIs so that we can import them into the utf8 namespace.
24#include <fcntl.h> // open
Renaud Paquaye3e78132017-05-22 15:24:35 -070025#include <stdio.h> // fopen
26#include <sys/stat.h> // mkdir
Spencer Lowd21dc822015-11-12 15:20:15 -080027#include <unistd.h> // unlink
Elliott Hughesc1fd4922015-11-11 18:02:29 +000028#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);
Renaud Paquaye3e78132017-05-22 15:24:35 -070058
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);
Elliott Hughesc1fd4922015-11-11 18:02:29 +000071#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
Renaud Paquaye3e78132017-05-22 15:24:35 -070091FILE* fopen(const char* name, const char* mode);
92int mkdir(const char* name, mode_t mode);
Elliott Hughesc1fd4922015-11-11 18:02:29 +000093int open(const char* name, int flags, ...);
94int unlink(const char* name);
95#else
Renaud Paquaye3e78132017-05-22 15:24:35 -070096using ::fopen;
97using ::mkdir;
Elliott Hughesc1fd4922015-11-11 18:02:29 +000098using ::open;
99using ::unlink;
100#endif
101
102} // namespace utf8
103} // namespace base
104} // namespace android
105
Elliott Hughes54c72aa2016-03-23 15:04:52 -0700106#endif // ANDROID_BASE_UTF8_H