1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is Mozilla IPC.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Chris Jones <jones.chris.g@gmail.com>.
18 : * Portions created by the Initial Developer are Copyright (C) 2009
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #include "base/string_util.h"
38 :
39 : // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
40 : // Use of this source code is governed by a BSD-style license that can be
41 : // found in the LICENSE file.
42 :
43 : #include "base/sys_string_conversions.h"
44 :
45 : #include "base/string_piece.h"
46 : #include "base/string_util.h"
47 :
48 : #include "build/build_config.h"
49 :
50 : // FIXME/cjones: these really only pertain to the linux sys string
51 : // converters.
52 : #ifdef WCHAR_T_IS_UTF16
53 : # define ICONV_WCHAR_T_ENCODING "UTF-16"
54 : #else
55 : # define ICONV_WCHAR_T_ENCODING "WCHAR_T"
56 : #endif
57 :
58 : // FIXME/cjones: BIG assumption here that std::string is a good
59 : // container of UTF8-encoded strings. this is probably wrong, as its
60 : // API doesn't really make sense for UTF8.
61 :
62 : namespace base {
63 : string16 SysWideToUTF16(const std::wstring& wide);
64 :
65 : // FIXME/cjones: as its name implies, this function is a hack.
66 : template<typename FromType, typename ToType>
67 : ToType
68 0 : GhettoStringConvert(const FromType& in)
69 : {
70 : // FIXME/cjones: assumes no non-ASCII characters in |in|
71 0 : ToType out;
72 0 : out.resize(in.length());
73 0 : for (int i = 0; i < static_cast<int>(in.length()); ++i)
74 0 : out[i] = static_cast<typename ToType::value_type>(in[i]);
75 : return out;
76 : }
77 : }
78 :
79 : // Implement functions that were in the chromium ICU library, which
80 : // we're not taking.
81 :
82 : std::string
83 0 : WideToUTF8(const std::wstring& wide)
84 : {
85 0 : return base::SysWideToUTF8(wide);
86 : }
87 :
88 : string16
89 0 : UTF8ToUTF16(const std::string& utf8)
90 : {
91 : // FIXME/cjones: do proper conversion
92 0 : return base::GhettoStringConvert<std::string, string16>(utf8);
93 : }
94 :
95 : std::wstring
96 0 : UTF8ToWide(const StringPiece& utf8)
97 : {
98 0 : return base::SysUTF8ToWide(utf8);
99 : }
100 :
101 : string16
102 0 : WideToUTF16(const std::wstring& wide)
103 : {
104 0 : return base::SysWideToUTF16(wide);
105 : }
106 :
107 : std::string
108 0 : UTF16ToUTF8(const string16& utf16)
109 : {
110 : // FIXME/cjones: do proper conversion
111 0 : return base::GhettoStringConvert<string16, std::string>(utf16);
112 : }
113 :
114 : namespace base {
115 :
116 : // FIXME/cjones: here we're entirely replacing the linux string
117 : // converters, and implementing the one that doesn't exist for OS X
118 : // and Windows.
119 :
120 : #if !defined(OS_MACOSX) && !defined(OS_WIN)
121 0 : std::string SysWideToUTF8(const std::wstring& wide) {
122 : // FIXME/cjones: do this with iconv
123 0 : return GhettoStringConvert<std::wstring, std::string>(wide);
124 : }
125 : #endif
126 :
127 0 : string16 SysWideToUTF16(const std::wstring& wide)
128 : {
129 : #if defined(WCHAR_T_IS_UTF16)
130 : return wide;
131 : #else
132 : // FIXME/cjones: do this with iconv for linux, other for OS X
133 0 : return GhettoStringConvert<std::wstring, string16>(wide);
134 : #endif
135 : }
136 :
137 : #if !defined(OS_MACOSX) && !defined(OS_WIN)
138 0 : std::wstring SysUTF8ToWide(const StringPiece& utf8) {
139 : // FIXME/cjones: do this with iconv
140 0 : return GhettoStringConvert<StringPiece, std::wstring>(utf8);
141 : }
142 :
143 0 : std::string SysWideToNativeMB(const std::wstring& wide) {
144 : // TODO(evanm): we can't assume Linux is UTF-8.
145 0 : return SysWideToUTF8(wide);
146 : }
147 :
148 0 : std::wstring SysNativeMBToWide(const StringPiece& native_mb) {
149 : // TODO(evanm): we can't assume Linux is UTF-8.
150 0 : return SysUTF8ToWide(native_mb);
151 : }
152 : #endif
153 :
154 : } // namespace base
|