Elliott Hughes | c1fd492 | 2015-11-11 18:02:29 +0000 | [diff] [blame] | 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 | #ifndef BASE_UTF8_H |
| 18 | #define BASE_UTF8_H |
| 19 | |
| 20 | #ifdef _WIN32 |
| 21 | #include <string> |
Spencer Low | d21dc82 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 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 <unistd.h> // unlink |
Elliott Hughes | c1fd492 | 2015-11-11 18:02:29 +0000 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | namespace android { |
| 29 | namespace base { |
| 30 | |
| 31 | // Only available on Windows because this is only needed on Windows. |
| 32 | #ifdef _WIN32 |
| 33 | // Convert size number of UTF-16 wchar_t's to UTF-8. Returns whether the |
| 34 | // conversion was done successfully. |
| 35 | bool WideToUTF8(const wchar_t* utf16, const size_t size, std::string* utf8); |
| 36 | |
| 37 | // Convert a NULL-terminated string of UTF-16 characters to UTF-8. Returns |
| 38 | // whether the conversion was done successfully. |
| 39 | bool WideToUTF8(const wchar_t* utf16, std::string* utf8); |
| 40 | |
| 41 | // Convert a UTF-16 std::wstring (including any embedded NULL characters) to |
| 42 | // UTF-8. Returns whether the conversion was done successfully. |
| 43 | bool WideToUTF8(const std::wstring& utf16, std::string* utf8); |
| 44 | |
| 45 | // Convert size number of UTF-8 char's to UTF-16. Returns whether the conversion |
| 46 | // was done successfully. |
| 47 | bool UTF8ToWide(const char* utf8, const size_t size, std::wstring* utf16); |
| 48 | |
| 49 | // Convert a NULL-terminated string of UTF-8 characters to UTF-16. Returns |
| 50 | // whether the conversion was done successfully. |
| 51 | bool UTF8ToWide(const char* utf8, std::wstring* utf16); |
| 52 | |
| 53 | // Convert a UTF-8 std::string (including any embedded NULL characters) to |
| 54 | // UTF-16. Returns whether the conversion was done successfully. |
| 55 | bool UTF8ToWide(const std::string& utf8, std::wstring* utf16); |
| 56 | #endif |
| 57 | |
| 58 | // The functions in the utf8 namespace take UTF-8 strings. For Windows, these |
| 59 | // are wrappers, for non-Windows these just expose existing APIs. To call these |
| 60 | // functions, use: |
| 61 | // |
| 62 | // // anonymous namespace to avoid conflict with existing open(), unlink(), etc. |
| 63 | // namespace { |
| 64 | // // Import functions into anonymous namespace. |
| 65 | // using namespace android::base::utf8; |
| 66 | // |
| 67 | // void SomeFunction(const char* name) { |
| 68 | // int fd = open(name, ...); // Calls android::base::utf8::open(). |
| 69 | // ... |
| 70 | // unlink(name); // Calls android::base::utf8::unlink(). |
| 71 | // } |
| 72 | // } |
| 73 | namespace utf8 { |
| 74 | |
| 75 | #ifdef _WIN32 |
| 76 | int open(const char* name, int flags, ...); |
| 77 | int unlink(const char* name); |
| 78 | #else |
| 79 | using ::open; |
| 80 | using ::unlink; |
| 81 | #endif |
| 82 | |
| 83 | } // namespace utf8 |
| 84 | } // namespace base |
| 85 | } // namespace android |
| 86 | |
| 87 | #endif // BASE_UTF8_H |