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