OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 CHROME_BROWSER_SYNC_UTIL_CHARACTER_SET_CONVERTERS_H_ |
| 6 #define CHROME_BROWSER_SYNC_UTIL_CHARACTER_SET_CONVERTERS_H_ |
| 7 |
| 8 // A pair of classes to convert UTF8 <-> UCS2 character strings. |
| 9 // |
| 10 // Note that the current implementation is limited to UCS2, whereas the |
| 11 // interface is agnostic to the wide encoding used. |
| 12 // |
| 13 // Also note that UCS2 is different from UTF-16, in that UTF-16 can encode all |
| 14 // the code points in the Unicode character set by multi-character encodings, |
| 15 // while UCS2 is limited to encoding < 2^16 code points. |
| 16 // |
| 17 // It appears that Windows support UTF-16, which means we have to be careful |
| 18 // what we feed this class. |
| 19 // |
| 20 // Usage: |
| 21 // string utf8; |
| 22 // CHECK(browser_sync::Append(wide_string, &utf8)); |
| 23 // PathString bob; |
| 24 // CHECK(browser_sync::Append(utf8, &bob)); |
| 25 // PathString fred = bob; |
| 26 |
| 27 #ifdef OS_LINUX |
| 28 #include <glib.h> |
| 29 #endif |
| 30 |
| 31 #include <string> |
| 32 |
| 33 #include "base/basictypes.h" |
| 34 #include "base/logging.h" |
| 35 #include "base/string16.h" |
| 36 #include "chrome/browser/sync/util/sync_types.h" |
| 37 |
| 38 // Need to cast literals (Linux, OSX) |
| 39 #define STRING16_UGLY_DOUBLE_DEFINE_HACK(s) \ |
| 40 reinterpret_cast<const char16*>(L##s) |
| 41 #define STRING16(s) STRING16_UGLY_DOUBLE_DEFINE_HACK(s) |
| 42 |
| 43 using std::string; |
| 44 |
| 45 namespace browser_sync { |
| 46 |
| 47 // These 2 classes are deprecated. Instead, prefer the Append() functions. |
| 48 |
| 49 // A class to convert wide -> UTF8. |
| 50 class ToUTF8 { |
| 51 public: |
| 52 explicit ToUTF8(const PathChar* wide); |
| 53 ToUTF8(const PathChar* wide, PathString::size_type size); |
| 54 explicit ToUTF8(const PathString& wide); |
| 55 |
| 56 // cast operators |
| 57 operator const std::string&() const; |
| 58 operator const char*() const; |
| 59 |
| 60 // accessors |
| 61 const std::string& get_string() const; |
| 62 const char* data() const; |
| 63 std::string::size_type byte_length() const; |
| 64 |
| 65 private: |
| 66 std::string result_; |
| 67 }; |
| 68 |
| 69 // A class to convert UTF8 -> wide. |
| 70 class ToPathString { |
| 71 public: |
| 72 explicit ToPathString(const char*); |
| 73 ToPathString(const char*, size_t size); |
| 74 explicit ToPathString(const std::string&); |
| 75 |
| 76 // true iff UTF-8 to wide conversion succeeded in constructor. |
| 77 bool good() { |
| 78 good_checked_ = true; |
| 79 return good_; |
| 80 } |
| 81 |
| 82 // It's invalid to invoke the accessors or the cast operators unless the |
| 83 // string is good and good() has been invoked at least once. |
| 84 |
| 85 // Implicit casts to const PathString& and const PathChar* |
| 86 operator const PathString&() const; |
| 87 operator const PathChar*() const; |
| 88 |
| 89 // Accessors |
| 90 const PathString& get_string16() const; |
| 91 const PathChar* data() const; |
| 92 PathString::size_type length() const; |
| 93 |
| 94 private: |
| 95 PathString result_; |
| 96 |
| 97 // Conversion succeeded. |
| 98 bool good_; |
| 99 // good() has been invoked at least once. |
| 100 bool good_checked_; |
| 101 }; |
| 102 |
| 103 // Converts the UCS2 string "wide" to UTF8 encoding and stores the result in |
| 104 // output_string. |
| 105 void PathStringToUTF8(const PathChar* wide, int size, |
| 106 std::string* output_string); |
| 107 |
| 108 // Converts UCS2 string wide to UTF8 encoding and appends the result to |
| 109 // output_string. |
| 110 void AppendPathStringToUTF8(const PathChar* wide, int size, |
| 111 std::string* output_string); |
| 112 |
| 113 // Converts the UTF8 encoded string "utf8" to UCS16 and stores the result in |
| 114 // output_string. |
| 115 // |
| 116 // Returns true iff conversion was successful, false otherwise. |
| 117 bool UTF8ToPathString(const char* utf8, size_t size, |
| 118 PathString* output_string); |
| 119 |
| 120 // Converts the UTF8 encoded string "utf8" to UCS2 and appends the result in |
| 121 // output_string. |
| 122 // |
| 123 // Returns true iff conversion was successful, false otherwise. |
| 124 bool AppendUTF8ToPathString(const char* utf8, size_t size, |
| 125 PathString* output_string); |
| 126 |
| 127 // Converts the UTF8 encoded string "utf8" to UCS2 and appends the result in |
| 128 // output_string. |
| 129 // |
| 130 // @returns true iff conversion was successful, false otherwise. |
| 131 inline bool AppendUTF8ToPathString(const std::string& utf8, |
| 132 PathString* output_string) { |
| 133 return AppendUTF8ToPathString(utf8.data(), utf8.length(), output_string); |
| 134 } |
| 135 |
| 136 // Converts UCS2 string wide to UTF8 encoding and appends the result to |
| 137 // output_string. |
| 138 inline void AppendPathStringToUTF8(const PathString& wide, |
| 139 std::string* output_string) { |
| 140 return AppendPathStringToUTF8(wide.data(), wide.length(), output_string); |
| 141 } |
| 142 |
| 143 |
| 144 inline bool Append(const PathChar* wide, int size, |
| 145 std::string* output_string) { |
| 146 AppendPathStringToUTF8(wide, size, output_string); |
| 147 return true; |
| 148 } |
| 149 |
| 150 inline bool Append(const PathChar* wide, std::string* output_string) { |
| 151 AppendPathStringToUTF8(wide, PathLen(wide), output_string); |
| 152 return true; |
| 153 } |
| 154 |
| 155 inline bool Append(const std::string& utf8, PathString* output_string) { |
| 156 return AppendUTF8ToPathString(utf8.data(), utf8.length(), output_string); |
| 157 } |
| 158 |
| 159 #if !PATHSTRING_IS_STD_STRING |
| 160 inline bool Append(const char* utf8, size_t size, PathString* output_string) { |
| 161 return AppendUTF8ToPathString(utf8, size, output_string); |
| 162 } |
| 163 |
| 164 inline bool Append(const char* s, int size, std::string* output_string) { |
| 165 output_string->append(s, size); |
| 166 return true; |
| 167 } |
| 168 |
| 169 inline bool Append(const char* utf8, PathString* output_string) { |
| 170 return AppendUTF8ToPathString(utf8, strlen(utf8), output_string); |
| 171 } |
| 172 |
| 173 inline bool Append(const char* s, std::string* output_string) { |
| 174 output_string->append(s); |
| 175 return true; |
| 176 } |
| 177 |
| 178 inline bool Append(const PathString& wide, std::string* output_string) { |
| 179 return Append(wide.data(), wide.length(), output_string); |
| 180 } |
| 181 |
| 182 inline bool Append(const std::string& s, std::string* output_string) { |
| 183 return Append(s.data(), s.length(), output_string); |
| 184 } |
| 185 #endif |
| 186 |
| 187 inline ToUTF8::operator const std::string&() const { |
| 188 return result_; |
| 189 } |
| 190 |
| 191 inline ToUTF8::operator const char*() const { |
| 192 return result_.c_str(); |
| 193 } |
| 194 |
| 195 inline const std::string& ToUTF8::get_string() const { |
| 196 return result_; |
| 197 } |
| 198 |
| 199 inline const char* ToUTF8::data() const { |
| 200 return result_.data(); |
| 201 } |
| 202 |
| 203 inline std::string::size_type ToUTF8::byte_length() const { |
| 204 return result_.size(); |
| 205 } |
| 206 |
| 207 inline ToPathString::operator const PathString&() const { |
| 208 DCHECK(good_ && good_checked_); |
| 209 return result_; |
| 210 } |
| 211 |
| 212 inline ToPathString::operator const PathChar*() const { |
| 213 DCHECK(good_ && good_checked_); |
| 214 return result_.c_str(); |
| 215 } |
| 216 |
| 217 inline const PathString& ToPathString::get_string16() const { |
| 218 DCHECK(good_ && good_checked_); |
| 219 return result_; |
| 220 } |
| 221 |
| 222 inline const PathChar* ToPathString::data() const { |
| 223 DCHECK(good_ && good_checked_); |
| 224 return result_.data(); |
| 225 } |
| 226 |
| 227 inline PathString::size_type ToPathString::length() const { |
| 228 DCHECK(good_ && good_checked_); |
| 229 return result_.length(); |
| 230 } |
| 231 |
| 232 void TrimPathStringToValidCharacter(PathString* string); |
| 233 |
| 234 } // namespace browser_sync |
| 235 |
| 236 #endif // CHROME_BROWSER_SYNC_UTIL_CHARACTER_SET_CONVERTERS_H_ |
OLD | NEW |