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