sys_string_conversions.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
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#pragma once
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"
15#include "base/string16.h"
16
17#if defined(OS_MACOSX)
18#include <CoreFoundation/CoreFoundation.h>
19#ifdef __OBJC__
20@class NSString;
21#else
22class NSString;
23#endif
24#endif  // OS_MACOSX
25
26namespace base {
27
28class StringPiece;
29
30// Converts between wide and UTF-8 representations of a string. On error, the
31// result is system-dependent.
32std::string SysWideToUTF8(const std::wstring& wide);
33std::wstring SysUTF8ToWide(const StringPiece& utf8);
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
37// change between reboots).
38std::string SysWideToNativeMB(const std::wstring& wide);
39std::wstring SysNativeMBToWide(const StringPiece& native_mb);
40
41// Windows-specific ------------------------------------------------------------
42
43#if defined(OS_WIN)
44
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
47// MultiByteToWideChar().
48std::wstring SysMultiByteToWide(const StringPiece& mb, uint32 code_page);
49std::string SysWideToMultiByte(const std::wstring& wide, uint32 code_page);
50
51#endif  // defined(OS_WIN)
52
53// Mac-specific ----------------------------------------------------------------
54
55#if defined(OS_MACOSX)
56
57// Converts between STL strings and CFStringRefs/NSStrings.
58
59// Creates a string, and returns it with a refcount of 1. You are responsible
60// for releasing it. Returns NULL on failure.
61CFStringRef SysUTF8ToCFStringRef(const std::string& utf8);
62CFStringRef SysUTF16ToCFStringRef(const string16& utf16);
63CFStringRef SysWideToCFStringRef(const std::wstring& wide);
64
65// Same, but returns an autoreleased NSString.
66NSString* SysUTF8ToNSString(const std::string& utf8);
67NSString* SysUTF16ToNSString(const string16& utf16);
68NSString* SysWideToNSString(const std::wstring& wide);
69
70// Converts a CFStringRef to an STL string. Returns an empty string on failure.
71std::string SysCFStringRefToUTF8(CFStringRef ref);
72string16 SysCFStringRefToUTF16(CFStringRef ref);
73std::wstring SysCFStringRefToWide(CFStringRef ref);
74
75// Same, but accepts NSString input. Converts nil NSString* to the appropriate
76// string type of length 0.
77std::string SysNSStringToUTF8(NSString* ref);
78string16 SysNSStringToUTF16(NSString* ref);
79std::wstring SysNSStringToWide(NSString* ref);
80
81#endif  // defined(OS_MACOSX)
82
83}  // namespace base
84
85#endif  // BASE_SYS_STRING_CONVERSIONS_H_
86