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