1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_STRINGS_SYS_STRING_CONVERSIONS_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_STRINGS_SYS_STRING_CONVERSIONS_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker // Provides system-dependent string type conversions for cases where it's 9*635a8641SAndroid Build Coastguard Worker // necessary to not use ICU. Generally, you should not need this in Chrome, 10*635a8641SAndroid Build Coastguard Worker // but it is used in some shared code. Dependencies should be minimal. 11*635a8641SAndroid Build Coastguard Worker 12*635a8641SAndroid Build Coastguard Worker #include <stdint.h> 13*635a8641SAndroid Build Coastguard Worker 14*635a8641SAndroid Build Coastguard Worker #include <string> 15*635a8641SAndroid Build Coastguard Worker 16*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h" 17*635a8641SAndroid Build Coastguard Worker #include "base/strings/string16.h" 18*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_piece.h" 19*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h" 20*635a8641SAndroid Build Coastguard Worker 21*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX) 22*635a8641SAndroid Build Coastguard Worker #include <CoreFoundation/CoreFoundation.h> 23*635a8641SAndroid Build Coastguard Worker #ifdef __OBJC__ 24*635a8641SAndroid Build Coastguard Worker @class NSString; 25*635a8641SAndroid Build Coastguard Worker #else 26*635a8641SAndroid Build Coastguard Worker class NSString; 27*635a8641SAndroid Build Coastguard Worker #endif 28*635a8641SAndroid Build Coastguard Worker #endif // OS_MACOSX 29*635a8641SAndroid Build Coastguard Worker 30*635a8641SAndroid Build Coastguard Worker namespace base { 31*635a8641SAndroid Build Coastguard Worker 32*635a8641SAndroid Build Coastguard Worker // Converts between wide and UTF-8 representations of a string. On error, the 33*635a8641SAndroid Build Coastguard Worker // result is system-dependent. 34*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string SysWideToUTF8(const std::wstring& wide); 35*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::wstring SysUTF8ToWide(StringPiece utf8); 36*635a8641SAndroid Build Coastguard Worker 37*635a8641SAndroid Build Coastguard Worker // Converts between wide and the system multi-byte representations of a string. 38*635a8641SAndroid Build Coastguard Worker // DANGER: This will lose information and can change (on Windows, this can 39*635a8641SAndroid Build Coastguard Worker // change between reboots). 40*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string SysWideToNativeMB(const std::wstring& wide); 41*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::wstring SysNativeMBToWide(StringPiece native_mb); 42*635a8641SAndroid Build Coastguard Worker 43*635a8641SAndroid Build Coastguard Worker // Windows-specific ------------------------------------------------------------ 44*635a8641SAndroid Build Coastguard Worker 45*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN) 46*635a8641SAndroid Build Coastguard Worker 47*635a8641SAndroid Build Coastguard Worker // Converts between 8-bit and wide strings, using the given code page. The 48*635a8641SAndroid Build Coastguard Worker // code page identifier is one accepted by the Windows function 49*635a8641SAndroid Build Coastguard Worker // MultiByteToWideChar(). 50*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::wstring SysMultiByteToWide(StringPiece mb, uint32_t code_page); 51*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string SysWideToMultiByte(const std::wstring& wide, 52*635a8641SAndroid Build Coastguard Worker uint32_t code_page); 53*635a8641SAndroid Build Coastguard Worker 54*635a8641SAndroid Build Coastguard Worker #endif // defined(OS_WIN) 55*635a8641SAndroid Build Coastguard Worker 56*635a8641SAndroid Build Coastguard Worker // Mac-specific ---------------------------------------------------------------- 57*635a8641SAndroid Build Coastguard Worker 58*635a8641SAndroid Build Coastguard Worker #if defined(OS_MACOSX) 59*635a8641SAndroid Build Coastguard Worker 60*635a8641SAndroid Build Coastguard Worker // Converts between STL strings and CFStringRefs/NSStrings. 61*635a8641SAndroid Build Coastguard Worker 62*635a8641SAndroid Build Coastguard Worker // Creates a string, and returns it with a refcount of 1. You are responsible 63*635a8641SAndroid Build Coastguard Worker // for releasing it. Returns NULL on failure. 64*635a8641SAndroid Build Coastguard Worker BASE_EXPORT CFStringRef SysUTF8ToCFStringRef(const std::string& utf8); 65*635a8641SAndroid Build Coastguard Worker BASE_EXPORT CFStringRef SysUTF16ToCFStringRef(const string16& utf16); 66*635a8641SAndroid Build Coastguard Worker 67*635a8641SAndroid Build Coastguard Worker // Same, but returns an autoreleased NSString. 68*635a8641SAndroid Build Coastguard Worker BASE_EXPORT NSString* SysUTF8ToNSString(const std::string& utf8); 69*635a8641SAndroid Build Coastguard Worker BASE_EXPORT NSString* SysUTF16ToNSString(const string16& utf16); 70*635a8641SAndroid Build Coastguard Worker 71*635a8641SAndroid Build Coastguard Worker // Converts a CFStringRef to an STL string. Returns an empty string on failure. 72*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string SysCFStringRefToUTF8(CFStringRef ref); 73*635a8641SAndroid Build Coastguard Worker BASE_EXPORT string16 SysCFStringRefToUTF16(CFStringRef ref); 74*635a8641SAndroid Build Coastguard Worker 75*635a8641SAndroid Build Coastguard Worker // Same, but accepts NSString input. Converts nil NSString* to the appropriate 76*635a8641SAndroid Build Coastguard Worker // string type of length 0. 77*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::string SysNSStringToUTF8(NSString* ref); 78*635a8641SAndroid Build Coastguard Worker BASE_EXPORT string16 SysNSStringToUTF16(NSString* ref); 79*635a8641SAndroid Build Coastguard Worker 80*635a8641SAndroid Build Coastguard Worker #endif // defined(OS_MACOSX) 81*635a8641SAndroid Build Coastguard Worker 82*635a8641SAndroid Build Coastguard Worker } // namespace base 83*635a8641SAndroid Build Coastguard Worker 84*635a8641SAndroid Build Coastguard Worker #endif // BASE_STRINGS_SYS_STRING_CONVERSIONS_H_ 85