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