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