Home | History | Annotate | Download | only in android-base
      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 ANDROID_BASE_UTF8_H
     18 #define ANDROID_BASE_UTF8_H
     19 
     20 #ifdef _WIN32
     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 
     30 namespace android {
     31 namespace 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.
     37 bool 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.
     41 bool 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.
     45 bool 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.
     49 bool 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.
     53 bool 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.
     57 bool 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.
     70 bool 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 // }
     88 namespace utf8 {
     89 
     90 #ifdef _WIN32
     91 FILE* fopen(const char* name, const char* mode);
     92 int mkdir(const char* name, mode_t mode);
     93 int open(const char* name, int flags, ...);
     94 int unlink(const char* name);
     95 #else
     96 using ::fopen;
     97 using ::mkdir;
     98 using ::open;
     99 using ::unlink;
    100 #endif
    101 
    102 }  // namespace utf8
    103 }  // namespace base
    104 }  // namespace android
    105 
    106 #endif  // ANDROID_BASE_UTF8_H
    107