diff options
author | nick@chromium.org <nick@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-25 17:38:11 +0000 |
---|---|---|
committer | nick@chromium.org <nick@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-25 17:38:11 +0000 |
commit | 1549593d29cb99d623d2c81bb83a5f01f60f7f0f (patch) | |
tree | 51f1cd8c47fec8f34fe8db4d71b8be1acf946162 /chrome/browser/sync | |
parent | 1dd7417d4753a462b6dbf2b0eb83ead2a7d6b97e (diff) | |
download | chromium_src-1549593d29cb99d623d2c81bb83a5f01f60f7f0f.zip chromium_src-1549593d29cb99d623d2c81bb83a5f01f60f7f0f.tar.gz chromium_src-1549593d29cb99d623d2c81bb83a5f01f60f7f0f.tar.bz2 |
Remove string.h/.cc, xml_parse_helpers.h/.cc, and date- parsing code
from notifier/base. These were unused.
Review URL: http://codereview.chromium.org/219006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27212 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/sync')
-rw-r--r-- | chrome/browser/sync/notifier/base/posix/time_posix.cc | 14 | ||||
-rw-r--r-- | chrome/browser/sync/notifier/base/string.cc | 409 | ||||
-rw-r--r-- | chrome/browser/sync/notifier/base/string.h | 394 | ||||
-rw-r--r-- | chrome/browser/sync/notifier/base/string_unittest.cc | 361 | ||||
-rw-r--r-- | chrome/browser/sync/notifier/base/time.cc | 330 | ||||
-rw-r--r-- | chrome/browser/sync/notifier/base/time.h | 57 | ||||
-rw-r--r-- | chrome/browser/sync/notifier/base/time_unittest.cc | 56 | ||||
-rw-r--r-- | chrome/browser/sync/notifier/base/win/time_win32.cc | 55 | ||||
-rw-r--r-- | chrome/browser/sync/notifier/communicator/xml_parse_helpers-inl.h | 24 | ||||
-rw-r--r-- | chrome/browser/sync/notifier/communicator/xml_parse_helpers.cc | 183 | ||||
-rw-r--r-- | chrome/browser/sync/notifier/communicator/xml_parse_helpers.h | 76 |
11 files changed, 1 insertions, 1958 deletions
diff --git a/chrome/browser/sync/notifier/base/posix/time_posix.cc b/chrome/browser/sync/notifier/base/posix/time_posix.cc index cda2769..b3020a7 100644 --- a/chrome/browser/sync/notifier/base/posix/time_posix.cc +++ b/chrome/browser/sync/notifier/base/posix/time_posix.cc @@ -37,18 +37,4 @@ bool Time64ToTm(time64 t, struct tm* tm) { return true; } -bool UtcTimeToLocalTime(struct tm* tm) { - assert(tm != NULL); - time_t t = timegm(tm); - localtime_r(&t, tm); - return true; -} - -bool LocalTimeToUtcTime(struct tm* tm) { - assert(tm != NULL); - time_t t = mktime(tm); - gmtime_r(&t, tm); - return true; -} - } // namespace notifier diff --git a/chrome/browser/sync/notifier/base/string.cc b/chrome/browser/sync/notifier/base/string.cc deleted file mode 100644 index 49ede9d..0000000 --- a/chrome/browser/sync/notifier/base/string.cc +++ /dev/null @@ -1,409 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifdef OS_MACOSX -#include <CoreFoundation/CoreFoundation.h> -#endif - -#include <float.h> -#include <string.h> - -#include "base/format_macros.h" -#include "base/string_util.h" -#include "chrome/browser/sync/notifier/base/string.h" -#include "talk/base/common.h" -#include "talk/base/logging.h" -#include "talk/base/stringencode.h" - -#ifdef OS_WIN -using base::snprintf; -#endif - -namespace notifier { - -std::string HtmlEncode(const std::string& src) { - size_t max_length = src.length() * 6 + 1; - std::string dest; - dest.resize(max_length); - size_t new_size = talk_base::html_encode(&dest[0], max_length, - src.data(), src.length()); - dest.resize(new_size); - return dest; -} - -std::string HtmlDecode(const std::string& src) { - size_t max_length = src.length() + 1; - std::string dest; - dest.resize(max_length); - size_t new_size = talk_base::html_decode(&dest[0], max_length, - src.data(), src.length()); - dest.resize(new_size); - return dest; -} - -std::string UrlEncode(const std::string& src) { - size_t max_length = src.length() * 6 + 1; - std::string dest; - dest.resize(max_length); - size_t new_size = talk_base::url_encode(&dest[0], max_length, - src.data(), src.length()); - dest.resize(new_size); - return dest; -} - -std::string UrlDecode(const std::string& src) { - size_t max_length = src.length() + 1; - std::string dest; - dest.resize(max_length); - size_t new_size = talk_base::url_decode(&dest[0], max_length, - src.data(), src.length()); - dest.resize(new_size); - return dest; -} - -int CharToHexValue(char hex) { - if (hex >= '0' && hex <= '9') { - return hex - '0'; - } else if (hex >= 'A' && hex <= 'F') { - return hex - 'A' + 10; - } else if (hex >= 'a' && hex <= 'f') { - return hex - 'a' + 10; - } else { - return -1; - } -} - -// Template function to convert a string to an int/int64. If strict is true, -// check for the validity and overflow -template<typename T> -bool ParseStringToIntTemplate(const char* str, - T* value, - bool strict, - T min_value) { - ASSERT(str); - ASSERT(value); - - // Skip spaces. - while (*str == ' ') { - ++str; - } - - // Process sign. - int c = static_cast<int>(*str++); // current char - int possible_sign = c; // save sign indication - if (c == '-' || c == '+') { - c = static_cast<int>(*str++); - } - - // Process numbers. - T total = 0; - while (c && (c = CharToDigit(static_cast<char>(c))) != -1) { - // Check for overflow. - if (strict && (total < min_value / 10 || - (total == min_value / 10 && - c > ((-(min_value + 10)) % 10)))) { - return false; - } - - // Accumulate digit. - // Note that we accumulate in the negative direction so that we will not - // blow away with the largest negative number - total = 10 * total - c; - - // Get next char. - c = static_cast<int>(*str++); - } - - // Fail if encountering non-numeric character. - if (strict && c == -1) { - return false; - } - - // Negate the number if needed. - if (possible_sign == '-') { - *value = total; - } else { - // Check for overflow. - if (strict && total == min_value) { - return false; - } - - *value = -total; - } - - return true; -} - -// Convert a string to an int. If strict is true, check for the validity and -// overflow. -bool ParseStringToInt(const char* str, int* value, bool strict) { - return ParseStringToIntTemplate<int>(str, value, strict, kint32min); -} - -// Convert a string to an int. This version does not check for the validity and -// overflow -int StringToInt(const char* str) { - int value = 0; - ParseStringToInt(str, &value, false); - return value; -} - -// Convert a string to an unsigned int. If strict is true, check for the -// validity and overflow -bool ParseStringToUint(const char* str, uint32* value, bool strict) { - ASSERT(str); - ASSERT(value); - - int64 int64_value; - if (!ParseStringToInt64(str, &int64_value, strict)) { - return false; - } - if (int64_value < 0 || int64_value > kuint32max) { - return false; - } - - *value = static_cast<uint32>(int64_value); - return true; -} - -// Convert a string to an int. This version does not check for the validity and -// overflow. -uint32 StringToUint(const char* str) { - uint32 value = 0; - ParseStringToUint(str, &value, false); - return value; -} - -// Convert a string to an int64. If strict is true, check for the validity and -// overflow. -bool ParseStringToInt64(const char* str, int64* value, bool strict) { - return ParseStringToIntTemplate<int64>(str, value, strict, kint64min); -} - -// Convert a string to an int64. This version does not check for the validity -// and overflow. -int64 StringToInt64(const char* str) { - int64 value = 0; - ParseStringToInt64(str, &value, false); - return value; -} - -// Convert a string to a double. If strict is true, check for the validity and -// overflow. -bool ParseStringToDouble(const char* str, double* value, bool strict) { - ASSERT(str); - ASSERT(value); - - // Skip spaces. - while (*str == ' ') { - ++str; - } - - // Process sign. - int c = static_cast<int>(*str++); // Current char. - int sign = c; // save sign indication - if (c == '-' || c == '+') { - c = static_cast<int>(*str++); - } - - // Process numbers before ".". - double total = 0.0; - while (c && (c != '.') && (c = CharToDigit(static_cast<char>(c))) != -1) { - // Check for overflow. - if (strict && total >= DBL_MAX / 10) { - return false; - } - - // Accumulate digit. - total = 10.0 * total + c; - - // Get next char. - c = static_cast<int>(*str++); - } - - // Process ".". - if (c == '.') { - c = static_cast<int>(*str++); - } else { - // Fail if encountering non-numeric character. - if (strict && c == -1) { - return false; - } - } - - // Process numbers after ".". - double power = 1.0; - while ((c = CharToDigit(static_cast<char>(c))) != -1) { - // Check for overflow. - if (strict && total >= DBL_MAX / 10) { - return false; - } - - // Accumulate digit. - total = 10.0 * total + c; - power *= 10.0; - - // Get next char. - c = static_cast<int>(*str++); - } - - // Get the final number. - *value = total / power; - if (sign == '-') { - *value = -(*value); - } - - return true; -} - -// Convert a string to a double. This version does not check for the validity -// and overflow. -double StringToDouble(const char* str) { - double value = 0; - ParseStringToDouble(str, &value, false); - return value; -} - -// Convert a float to a string. -std::string FloatToString(float f) { - char buf[80]; - snprintf(buf, sizeof(buf), "%f", f); - return std::string(buf); -} - -std::string DoubleToString(double d) { - char buf[160]; - snprintf(buf, sizeof(buf), "%.17g", d); - return std::string(buf); -} - -std::string UIntToString(uint32 i) { - char buf[80]; -#ifdef OS_LINUX - snprintf(buf, sizeof(buf), "%u", i); -#else - snprintf(buf, sizeof(buf), "%lu", i); -#endif - return std::string(buf); -} - -// Convert an int to a string -std::string IntToString(int i) { - char buf[80]; - snprintf(buf, sizeof(buf), "%d", i); - return std::string(buf); -} - -// Convert an int64 to a string -std::string Int64ToString(int64 i64) { - char buf[80]; - snprintf(buf, sizeof(buf), "%" PRId64 "d", i64); - return std::string(buf); -} - -std::string UInt64ToString(uint64 i64) { - char buf[80]; - snprintf(buf, sizeof(buf), "%" PRId64 "u", i64); - return std::string(buf); -} - -std::string Int64ToHexString(int64 i64) { - char buf[80]; - snprintf(buf, sizeof(buf), "%" PRId64 "x", i64); - return std::string(buf); -} - -// Parse a single "delim" delimited string from "*source". Modify *source to -// point after the delimiter. If no delimiter is present after the string, set -// *source to NULL. -// -// Mainly a stringified wrapper around strpbrk(). -std::string SplitOneStringToken(const char** source, const char* delim) { - ASSERT(source); - ASSERT(delim); - - if (!*source) { - return std::string(); - } - const char* begin = *source; - *source = strpbrk(*source, delim); - if (*source) { - return std::string(begin, (*source)++); - } else { - return std::string(begin); - } -} - -std::string LowerWithUnderToPascalCase(const char* lower_with_under) { - ASSERT(lower_with_under); - - std::string pascal_case; - bool make_upper = true; - for (; *lower_with_under != '\0'; lower_with_under++) { - char current_char = *lower_with_under; - if (current_char == '_') { - ASSERT(!make_upper); - make_upper = true; - continue; - } - if (make_upper) { - current_char = toupper(current_char); - make_upper = false; - } - pascal_case.append(1, current_char); - } - return pascal_case; -} - -std::string PascalCaseToLowerWithUnder(const char* pascal_case) { - ASSERT(pascal_case); - - std::string lower_with_under; - bool previous_was_upper = true; - for(; *pascal_case != '\0'; pascal_case++) { - char current_char = *pascal_case; - if (isupper(current_char)) { - // DNSName should be dns_name. - if ((islower(pascal_case[1]) && !lower_with_under.empty()) || - !previous_was_upper) { - lower_with_under.append(1, '_'); - } - current_char = tolower(current_char); - } else if (previous_was_upper) { - previous_was_upper = false; - } - lower_with_under.append(1, current_char); - } - return lower_with_under; -} -void StringReplace(std::string* s, - const char* old_sub, - const char* new_sub, - bool replace_all) { - ASSERT(s); - - // If old_sub is empty, nothing to do. - if (!old_sub || !*old_sub) { - return; - } - - int old_sub_size = strlen(old_sub); - std::string res; - std::string::size_type start_pos = 0; - - do { - std::string::size_type pos = s->find(old_sub, start_pos); - if (pos == std::string::npos) { - break; - } - res.append(*s, start_pos, pos - start_pos); - res.append(new_sub); - start_pos = pos + old_sub_size; // start searching again after the "old". - } while (replace_all); - res.append(*s, start_pos, s->length() - start_pos); - - *s = res; -} - -} // namespace notifier diff --git a/chrome/browser/sync/notifier/base/string.h b/chrome/browser/sync/notifier/base/string.h deleted file mode 100644 index b40a60c..0000000 --- a/chrome/browser/sync/notifier/base/string.h +++ /dev/null @@ -1,394 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_SYNC_NOTIFIER_BASE_STRING_H_ -#define CHROME_BROWSER_SYNC_NOTIFIER_BASE_STRING_H_ - -#ifdef COMPILER_MSVC -#include <xhash> -#elif defined(__GNUC__) -#include <ext/hash_map> -#endif - -#include <ctype.h> -#include <string> - -#include "chrome/browser/sync/notifier/base/fastalloc.h" -#include "talk/base/basictypes.h" - -namespace notifier { - -// Does html encoding of strings. -std::string HtmlEncode(const std::string& src); - -// Does html decoding of strings. -std::string HtmlDecode(const std::string& src); - -// Does utl encoding of strings. -std::string UrlEncode(const std::string& src); - -// Does url decoding of strings. -std::string UrlDecode(const std::string& src); - -// Convert a character to a digit. If the character is not a digit return -1 -// (same as CRT). -inline int CharToDigit(char c) { - return ((c) >= '0' && (c) <= '9' ? (c) - '0' : -1); -} - -int CharToHexValue(char hex); - -// ---------------------------------------------------------------------- -// ParseStringToInt() -// ParseStringToUint() -// ParseStringToInt64() -// ParseStringToDouble() -// -// Convert a string to an int/int64/double. -// If strict is true, check for the validity and overflow. -// ---------------------------------------------------------------------- - -bool ParseStringToInt(const char* str, int* value, bool strict); - -bool ParseStringToUint(const char* str, uint32* value, bool strict); - -bool ParseStringToInt64(const char* str, int64* value, bool strict); - -bool ParseStringToDouble(const char* str, double* value, bool strict); - -// ---------------------------------------------------------------------- -// StringToInt() -// StringToUint() -// StringToInt64() -// StringToDouble() -// -// Convert a string to an int/int64/double. -// Note that these functions do not check for the validity or overflow. -// ---------------------------------------------------------------------- - -int StringToInt(const char* str); - -uint32 StringToUint(const char* str); - -int64 StringToInt64(const char* str); - -double StringToDouble(const char* str); - -// ---------------------------------------------------------------------- -// FloatToString() -// DoubleToString() -// IntToString() -// UIntToString() -// Int64ToString() -// UInt64ToString() -// -// Convert various types to their string representation. These all do -// the obvious, trivial thing. -// ---------------------------------------------------------------------- - -std::string FloatToString(float f); -std::string DoubleToString(double d); - -std::string IntToString(int i); -std::string UIntToString(uint32 i); - -std::string Int64ToString(int64 i64); -std::string UInt64ToString(uint64 i64); - -std::string Int64ToHexString(int64 i64); - -// ---------------------------------------------------------------------- -// StringStartsWith() -// StringEndsWith() -// -// Check if a string starts or ends with a pattern. -// ---------------------------------------------------------------------- - -inline bool StringStartsWith(const std::string& s, const char* p) { - return s.find(p) == 0; -} - -inline bool StringEndsWith(const std::string& s, const char* p) { - return s.rfind(p) == (s.length() - strlen(p)); -} - -// ---------------------------------------------------------------------- -// MakeStringEndWith() -// -// If the string does not end with a pattern, make it end with it. -// ---------------------------------------------------------------------- - -inline std::string MakeStringEndWith(const std::string& s, const char* p) { - if (StringEndsWith(s, p)) { - return s; - } else { - std::string ns(s); - ns += p; - return ns; - } -} - -// Convert a lower_case_string to LowerCaseString. -std::string LowerWithUnderToPascalCase(const char* lower_with_under); - -// Convert a PascalCaseString to pascal_case_string. -std::string PascalCaseToLowerWithUnder(const char* pascal_case); - -// ---------------------------------------------------------------------- -// LowerString() -// LowerStringToBuf() -// -// Convert the characters in "s" to lowercase. -// Changes contents of "s". LowerStringToBuf copies at most "n" -// characters (including the terminating '\0') from "s" to another -// buffer. -// ---------------------------------------------------------------------- - -inline void LowerString(char* s) { - for (; *s; ++s) { - *s = tolower(*s); - } -} - -inline void LowerString(std::string* s) { - std::string::iterator end = s->end(); - for (std::string::iterator i = s->begin(); i != end; ++i) { - *i = tolower(*i); - } -} - -inline void LowerStringToBuf(const char* s, char* buf, int n) { - for (int i = 0; i < n - 1; ++i) { - char c = s[i]; - buf[i] = tolower(c); - if (c == '\0') { - return; - } - } - buf[n - 1] = '\0'; -} - -// ---------------------------------------------------------------------- -// UpperString() -// UpperStringToBuf() -// -// Convert the characters in "s" to uppercase. -// UpperString changes "s". UpperStringToBuf copies at most "n" -// characters (including the terminating '\0') from "s" to another -// buffer. -// ---------------------------------------------------------------------- - -inline void UpperString(char* s) { - for (; *s; ++s) { - *s = toupper(*s); - } -} - -inline void UpperString(std::string* s) { - for (std::string::iterator iter = s->begin(); iter != s->end(); ++iter) { - *iter = toupper(*iter); - } -} - -inline void UpperStringToBuf(const char* s, char* buf, int n) { - for (int i = 0; i < n - 1; ++i) { - char c = s[i]; - buf[i] = toupper(c); - if (c == '\0') { - return; - } - } - buf[n - 1] = '\0'; -} - -// ---------------------------------------------------------------------- -// TrimStringLeft -// -// Removes any occurrences of the characters in 'remove' from the start -// of the string. Returns the number of chars trimmed. -// ---------------------------------------------------------------------- -inline int TrimStringLeft(std::string* s, const char* remove) { - int i = 0; - for (; i < static_cast<int>(s->size()) && strchr(remove, (*s)[i]); ++i); - if (i > 0) s->erase(0, i); - return i; -} - -// ---------------------------------------------------------------------- -// TrimStringRight -// -// Removes any occurrences of the characters in 'remove' from the end of -// the string. Returns the number of chars trimmed. -// ---------------------------------------------------------------------- -inline int TrimStringRight(std::string* s, const char* remove) { - int size = static_cast<int>(s->size()); - int i = size; - for (; i > 0 && strchr(remove, (*s)[i - 1]); --i); - if (i < size) { - s->erase(i); - } - return size - i; -} - -// ---------------------------------------------------------------------- -// TrimString -// -// Removes any occurrences of the characters in 'remove' from either end -// of the string. -// ---------------------------------------------------------------------- -inline int TrimString(std::string* s, const char* remove) { - return TrimStringRight(s, remove) + TrimStringLeft(s, remove); -} - -// ---------------------------------------------------------------------- -// StringReplace() -// -// Replace the "old" pattern with the "new" pattern in a string. If -// replace_all is false, it only replaces the first instance of "old." -// ---------------------------------------------------------------------- - -void StringReplace(std::string* s, - const char* old_sub, - const char* new_sub, - bool replace_all); - -inline size_t HashString(const std::string &value) { -#ifdef COMPILER_MSVC - return stdext::hash_value(value); -#elif defined(__GNUC__) - __gnu_cxx::hash<const char*> h; - return h(value.c_str()); -#else - // Compile time error because we don't return a value. -#endif -} - -// ---------------------------------------------------------------------- -// SplitOneStringToken() -// -// Parse a single "delim" delimited string from "*source" -// Modify *source to point after the delimiter. -// If no delimiter is present after the string, set *source to NULL. -// -// If the start of *source is a delimiter, return an empty string. -// If *source is NULL, return an empty string. -// ---------------------------------------------------------------------- -std::string SplitOneStringToken(const char** source, const char* delim); - -//---------------------------------------------------------------------- -// CharTraits provides wrappers with common function names for -// char/wchar_t specific CRT functions -//---------------------------------------------------------------------- - -template <class CharT> struct CharTraits { -}; - -template <> -struct CharTraits<char> { - static inline size_t length(const char* s) { - return strlen(s); - } - static inline bool copy(char* dst, size_t dst_size, const char* s) { - if (s == NULL || dst == NULL) - return false; - else - return copy_num(dst, dst_size, s, strlen(s)); - } - static inline bool copy_num(char* dst, size_t dst_size, const char* s, - size_t s_len) { - if (dst_size < (s_len + 1)) - return false; - memcpy(dst, s, s_len); - dst[s_len] = '\0'; - return true; - } -}; - -template <> -struct CharTraits<wchar_t> { - static inline size_t length(const wchar_t* s) { - return wcslen(s); - } - static inline bool copy(wchar_t* dst, size_t dst_size, const wchar_t* s) { - if (s == NULL || dst == NULL) - return false; - else - return copy_num(dst, dst_size, s, wcslen(s)); - } - static inline bool copy_num(wchar_t* dst, size_t dst_size, const wchar_t* s, - size_t s_len) { - if (dst_size < (s_len + 1)) { - return false; - } - memcpy(dst, s, s_len * sizeof(wchar_t)); - dst[s_len] = '\0'; - return true; - } -}; - -//---------------------------------------------------------------------- -// This class manages a fixed-size, null-terminated string buffer. It -// is meant to be allocated on the stack, and it makes no use of the -// heap internally. In most cases you'll just want to use a -// std::(w)string, but when you need to avoid the heap, you can use this -// class instead. -// -// Methods are provided to read the null-terminated buffer and to append -// data to the buffer, and once the buffer fills-up, it simply discards -// any extra append calls. -//---------------------------------------------------------------------- - -template <class CharT, int MaxSize> -class FixedString { - public: - typedef CharTraits<CharT> char_traits; - - FixedString() : index_(0), truncated_(false) { - buf_[0] = CharT(0); - } - - ~FixedString() { - memset(buf_, 0xCC, sizeof(buf_)); - } - - // Returns true if the Append ever failed. - bool was_truncated() const { return truncated_; } - - // Returns the number of characters in the string, excluding the null - // terminator. - size_t size() const { return index_; } - - // Returns the null-terminated string. - const CharT* get() const { return buf_; } - CharT* get() { return buf_; } - - // Append an array of characters. The operation is bounds checked, and if - // there is insufficient room, then the was_truncated() flag is set to true. - void Append(const CharT* s, size_t n) { - if (char_traits::copy_num(buf_ + index_, arraysize(buf_) - index_, s, n)) { - index_ += n; - } else { - truncated_ = true; - } - } - - // Append a null-terminated string. - void Append(const CharT* s) { - Append(s, char_traits::length(s)); - } - - // Append a single character. - void Append(CharT c) { - Append(&c, 1); - } - - private: - CharT buf_[MaxSize]; - size_t index_; - bool truncated_; -}; - -} // namespace notifier - -#endif // CHROME_BROWSER_SYNC_NOTIFIER_BASE_STRING_H_ diff --git a/chrome/browser/sync/notifier/base/string_unittest.cc b/chrome/browser/sync/notifier/base/string_unittest.cc deleted file mode 100644 index 9c5c918..0000000 --- a/chrome/browser/sync/notifier/base/string_unittest.cc +++ /dev/null @@ -1,361 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/sync/notifier/base/string.h" - -namespace notifier { - -TEST_NOTIFIER_F(StringTest); - -TEST_F(StringTest, StringToInt) { - ASSERT_EQ(StringToInt("625"), 625); - ASSERT_EQ(StringToInt("6"), 6); - ASSERT_EQ(StringToInt("0"), 0); - ASSERT_EQ(StringToInt(" 122"), 122); - ASSERT_EQ(StringToInt("a"), 0); - ASSERT_EQ(StringToInt(" a"), 0); - ASSERT_EQ(StringToInt("2147483647"), 2147483647); - ASSERT_EQ(StringToInt("-2147483648"), - static_cast<int>(0x80000000)); // Hex constant avoids gcc warning. - - int value = 0; - ASSERT_FALSE(ParseStringToInt("62.5", &value, true)); - ASSERT_FALSE(ParseStringToInt("625e", &value, true)); - ASSERT_FALSE(ParseStringToInt("2147483648", &value, true)); - ASSERT_FALSE(ParseStringToInt("-2147483649", &value, true)); - ASSERT_FALSE(ParseStringToInt("-4857004031", &value, true)); -} - -TEST_F(StringTest, StringToUint) { - ASSERT_EQ(StringToUint("625"), 625); - ASSERT_EQ(StringToUint("6"), 6); - ASSERT_EQ(StringToUint("0"), 0); - ASSERT_EQ(StringToUint(" 122"), 122); - ASSERT_EQ(StringToUint("a"), 0); - ASSERT_EQ(StringToUint(" a"), 0); - ASSERT_EQ(StringToUint("4294967295"), static_cast<uint32>(0xffffffff)); - - uint32 value = 0; - ASSERT_FALSE(ParseStringToUint("62.5", &value, true)); - ASSERT_FALSE(ParseStringToUint("625e", &value, true)); - ASSERT_FALSE(ParseStringToUint("4294967296", &value, true)); - ASSERT_FALSE(ParseStringToUint("-1", &value, true)); -} - -TEST_F(StringTest, StringToInt64) { - ASSERT_EQ(StringToInt64("119600064000000000"), - INT64_C(119600064000000000)); - ASSERT_EQ(StringToInt64(" 119600064000000000"), - INT64_C(119600064000000000)); - ASSERT_EQ(StringToInt64("625"), 625); - ASSERT_EQ(StringToInt64("6"), 6); - ASSERT_EQ(StringToInt64("0"), 0); - ASSERT_EQ(StringToInt64(" 122"), 122); - ASSERT_EQ(StringToInt64("a"), 0); - ASSERT_EQ(StringToInt64(" a"), 0); - ASSERT_EQ(StringToInt64("9223372036854775807"), INT64_C(9223372036854775807)); - ASSERT_EQ(StringToInt64("-9223372036854775808I64"), - static_cast<int64>(INT64_C(0x8000000000000000))); - - int64 value = 0; - ASSERT_FALSE(ParseStringToInt64("62.5", &value, true)); - ASSERT_FALSE(ParseStringToInt64("625e", &value, true)); - ASSERT_FALSE(ParseStringToInt64("9223372036854775808", &value, true)); - ASSERT_FALSE(ParseStringToInt64("-9223372036854775809", &value, true)); -} - -TEST_F(StringTest, StringToDouble) { - ASSERT_DOUBLE_EQ(StringToDouble("625"), 625); - ASSERT_DOUBLE_EQ(StringToDouble("-625"), -625); - ASSERT_DOUBLE_EQ(StringToDouble("-6.25"), -6.25); - ASSERT_DOUBLE_EQ(StringToDouble("6.25"), 6.25); - ASSERT_DOUBLE_EQ(StringToDouble("0.00"), 0); - ASSERT_DOUBLE_EQ(StringToDouble(" 55.1"), 55.1); - ASSERT_DOUBLE_EQ(StringToDouble(" 55.001"), 55.001); - ASSERT_DOUBLE_EQ(StringToDouble(" 1.001"), 1.001); - - double value = 0.0; - ASSERT_FALSE(ParseStringToDouble("62*5", &value, true)); -} - -TEST_F(StringTest, Int64ToHexString) { - ASSERT_STREQ("1a8e79fe1d58000", - Int64ToHexString(INT64_C(119600064000000000)).c_str()); - ASSERT_STREQ("271", Int64ToHexString(625).c_str()); - ASSERT_STREQ("0", Int64ToHexString(0).c_str()); -} - -TEST_F(StringTest, StringStartsWith) { - { std::string s(""); ASSERT_TRUE(StringStartsWith(s, "")); } - { std::string s("abc"); ASSERT_TRUE(StringStartsWith(s, "ab")); } - { std::string s("abc"); ASSERT_FALSE(StringStartsWith(s, "bc")); } -} - -TEST_F(StringTest, StringEndsWith) { - { std::string s(""); ASSERT_TRUE(StringEndsWith(s, "")); } - { std::string s("abc"); ASSERT_TRUE(StringEndsWith(s, "bc")); } - { std::string s("abc"); ASSERT_FALSE(StringEndsWith(s, "ab")); } -} - -TEST_F(StringTest, MakeStringEndWith) { - { - std::string s(""); - std::string t(MakeStringEndWith(s, "")); - ASSERT_STREQ(t.c_str(), ""); - } - { - std::string s("abc"); - std::string t(MakeStringEndWith(s, "def")); - ASSERT_STREQ(t.c_str(), "abcdef"); - } - { - std::string s("abc"); - std::string t(MakeStringEndWith(s, "bc")); - ASSERT_STREQ(t.c_str(), "abc"); - } -} - -TEST_F(StringTest, LowerString) { - { std::string s(""); LowerString(&s); ASSERT_STREQ(s.c_str(), ""); } - { std::string s("a"); LowerString(&s); ASSERT_STREQ(s.c_str(), "a"); } - { std::string s("A"); LowerString(&s); ASSERT_STREQ(s.c_str(), "a"); } - { std::string s("abc"); LowerString(&s); ASSERT_STREQ(s.c_str(), "abc"); } - { std::string s("ABC"); LowerString(&s); ASSERT_STREQ(s.c_str(), "abc"); } -} - -TEST_F(StringTest, UpperString) { - { std::string s(""); UpperString(&s); ASSERT_STREQ(s.c_str(), ""); } - { std::string s("A"); UpperString(&s); ASSERT_STREQ(s.c_str(), "A"); } - { std::string s("a"); UpperString(&s); ASSERT_STREQ(s.c_str(), "A"); } - { std::string s("ABC"); UpperString(&s); ASSERT_STREQ(s.c_str(), "ABC"); } - { std::string s("abc"); UpperString(&s); ASSERT_STREQ(s.c_str(), "ABC"); } -} - -TEST_F(StringTest, TrimString) { - const char* white = " \n\t"; - std::string s, c; - - // TrimStringLeft. - s = ""; // empty - c = ""; - ASSERT_EQ(TrimStringLeft(&s, white), 0); - ASSERT_STREQ(s.c_str(), c.c_str()); - - s = " \n\t"; // all bad - c = ""; - ASSERT_EQ(TrimStringLeft(&s, white), 3); - ASSERT_STREQ(s.c_str(), c.c_str()); - - s = "dog"; // nothing bad - c = "dog"; - ASSERT_EQ(TrimStringLeft(&s, white), 0); - ASSERT_STREQ(s.c_str(), c.c_str()); - - s = " dog "; // some bad - c = "dog "; - ASSERT_EQ(TrimStringLeft(&s, white), 1); - ASSERT_STREQ(s.c_str(), c.c_str()); - - s = " \n\t\t I love my little dog \n\t "; - c = "I love my little dog \n\t "; - ASSERT_EQ(TrimStringLeft(&s, white), 5); - ASSERT_STREQ(s.c_str(), c.c_str()); - - // TrimStringRight. - s = ""; - c = ""; - ASSERT_EQ(TrimStringRight(&s, white), 0); - ASSERT_STREQ(s.c_str(), c.c_str()); - - s = " \n\t"; - c = ""; - ASSERT_EQ(TrimStringRight(&s, white), 3); - ASSERT_STREQ(s.c_str(), c.c_str()); - - s = "dog"; - c = "dog"; - ASSERT_EQ(TrimStringRight(&s, white), 0); - ASSERT_STREQ(s.c_str(), c.c_str()); - - s = " dog "; - c = " dog"; - ASSERT_EQ(TrimStringRight(&s, white), 1); - ASSERT_STREQ(s.c_str(), c.c_str()); - - s = " \n\t\t I love my little dog \n\t "; - c = " \n\t\t I love my little dog"; - ASSERT_EQ(TrimStringRight(&s, white), 4); - ASSERT_STREQ(s.c_str(), c.c_str()); - - // TrimString. - s = ""; - c = ""; - ASSERT_EQ(TrimString(&s, white), 0); - ASSERT_STREQ(s.c_str(), c.c_str()); - - s = " \n\t"; - c = ""; - ASSERT_EQ(TrimString(&s, white), 3); - ASSERT_STREQ(s.c_str(), c.c_str()); - - s = "dog"; - c = "dog"; - ASSERT_EQ(TrimString(&s, white), 0); - ASSERT_STREQ(s.c_str(), c.c_str()); - - s = " dog "; - c = "dog"; - ASSERT_EQ(TrimString(&s, white), 2); - ASSERT_STREQ(s.c_str(), c.c_str()); - - s = " \n\t\t I love my little dog \n\t "; - c = "I love my little dog"; - ASSERT_EQ(TrimString(&s, white), 9); - ASSERT_STREQ(s.c_str(), c.c_str()); -} - -TEST_F(StringTest, SplitOneStringToken) { - const char* teststrings[] = { - "alongword", - "alongword ", - "alongword ", - "alongword anotherword", - " alongword", - "", - }; - const char* source = NULL; - - source = teststrings[0]; - ASSERT_STREQ(SplitOneStringToken(&source, " ").c_str(), "alongword"); - ASSERT_STREQ(source, NULL); - - source = teststrings[1]; - ASSERT_STREQ(SplitOneStringToken(&source, " ").c_str(), "alongword"); - ASSERT_STREQ(source, teststrings[1] + strlen("alongword") + 1); - - source = teststrings[2]; - ASSERT_STREQ(SplitOneStringToken(&source, " ").c_str(), "alongword"); - ASSERT_STREQ(source, teststrings[2] + strlen("alongword") + 1); - - source = teststrings[3]; - ASSERT_STREQ(SplitOneStringToken(&source, " ").c_str(), "alongword"); - ASSERT_STREQ(source, teststrings[3] + strlen("alongword") + 1); - - source = teststrings[4]; - ASSERT_STREQ(SplitOneStringToken(&source, " ").c_str(), ""); - ASSERT_STREQ(source, teststrings[4] + 1); - - source = teststrings[5]; - ASSERT_STREQ(SplitOneStringToken(&source, " ").c_str(), ""); - ASSERT_STREQ(source, NULL); -} - -TEST_F(StringTest, FixedString) { - // Test basic operation. - const wchar_t kData[] = L"hello world"; - FixedString<wchar_t, 40> buf; - - buf.Append(kData); - EXPECT_EQ(arraysize(kData)-1, buf.size()); - EXPECT_EQ(0, wcscmp(kData, buf.get())); - - buf.Append(' '); - buf.Append(kData); - const wchar_t kExpected[] = L"hello world hello world"; - EXPECT_EQ(arraysize(kExpected)-1, buf.size()); - EXPECT_EQ(0, wcscmp(kExpected, buf.get())); - EXPECT_EQ(false, buf.was_truncated()); - - // Test overflow. - FixedString<wchar_t, 5> buf2; - buf2.Append(L"hello world"); - EXPECT_EQ(static_cast<size_t>(0), buf2.size()); - EXPECT_EQ(0, buf2.get()[0]); - EXPECT_EQ(true, buf2.was_truncated()); -} - -TEST_F(StringTest, LowerToPascalCase) { - EXPECT_STREQ("", LowerWithUnderToPascalCase("").c_str()); - EXPECT_STREQ("A", LowerWithUnderToPascalCase("a").c_str()); - EXPECT_STREQ("TestS", LowerWithUnderToPascalCase("test_s").c_str()); - EXPECT_STREQ("XQ", LowerWithUnderToPascalCase("x_q").c_str()); - EXPECT_STREQ("XQDNS", LowerWithUnderToPascalCase("x_qDNS").c_str()); -} - -TEST_F(StringTest, PascalCaseToLower) { - EXPECT_STREQ("", PascalCaseToLowerWithUnder("").c_str()); - EXPECT_STREQ("a", PascalCaseToLowerWithUnder("A").c_str()); - EXPECT_STREQ("test_s", PascalCaseToLowerWithUnder("TestS").c_str()); - EXPECT_STREQ("xq", PascalCaseToLowerWithUnder("XQ").c_str()); - EXPECT_STREQ("dns_name", PascalCaseToLowerWithUnder("DNSName").c_str()); - EXPECT_STREQ("xqdns", PascalCaseToLowerWithUnder("XQDNS").c_str()); - EXPECT_STREQ("xqdn_sa", PascalCaseToLowerWithUnder("XQDNSa").c_str()); - EXPECT_STREQ("dns1", PascalCaseToLowerWithUnder("DNS1").c_str()); -} - -TEST_F(StringTest, HtmlEncode) { - EXPECT_STREQ("dns", HtmlEncode("dns").c_str()); - EXPECT_STREQ("&", HtmlEncode("&").c_str()); - EXPECT_STREQ("&amp;", HtmlEncode("&").c_str()); - EXPECT_STREQ("<!>", HtmlEncode("<!>").c_str()); -} - -TEST_F(StringTest, HtmlDecode) { - EXPECT_STREQ("dns", HtmlDecode("dns").c_str()); - EXPECT_STREQ("&", HtmlDecode("&").c_str()); - EXPECT_STREQ("&", HtmlDecode("&amp;").c_str()); - EXPECT_STREQ("<!>", HtmlDecode("<!>").c_str()); -} - -TEST_F(StringTest, UrlEncode) { - EXPECT_STREQ("%26", UrlEncode("&").c_str()); - EXPECT_STREQ("%3f%20", UrlEncode("? ").c_str()); - EXPECT_STREQ("as%20dfdsa", UrlEncode("as dfdsa").c_str()); - EXPECT_STREQ("%3c!%3e", UrlEncode("<!>").c_str()); - EXPECT_STREQ("!%23!", UrlEncode("!#!").c_str()); - EXPECT_STREQ("!!", UrlEncode("!!").c_str()); -} - -TEST_F(StringTest, UrlDecode) { - EXPECT_STREQ("&", UrlDecode("%26").c_str()); - EXPECT_STREQ("? ", UrlDecode("%3f%20").c_str()); - EXPECT_STREQ("as dfdsa", UrlDecode("as%20dfdsa").c_str()); - EXPECT_STREQ("<!>", UrlDecode("%3c!%3e").c_str()); - EXPECT_STREQ("&", UrlDecode("&").c_str()); -} - -TEST_F(StringTest, StringReplace) { - // Test StringReplace core functionality. - std::string s = "<attribute name=abcd/>"; - StringReplace(&s, "=", " = ", false); - EXPECT_STREQ(s.c_str(), "<attribute name = abcd/>"); - - // Test for negative case. - s = "<attribute name=abcd/>"; - StringReplace(&s, "-", "=", false); - EXPECT_STREQ(s.c_str(), "<attribute name=abcd/>"); - - // Test StringReplace core functionality with replace_all flag set. - s = "<attribute name==abcd/>"; - StringReplace(&s, "=", " = ", true); - EXPECT_STREQ(s.c_str(), "<attribute name = = abcd/>"); - - // Input is an empty string. - s = ""; - StringReplace(&s, "=", " = ", false); - EXPECT_STREQ(s.c_str(), ""); - - // Input is an empty string and this is a request for repeated string - // replaces. - s = ""; - StringReplace(&s, "=", " = ", true); - EXPECT_STREQ(s.c_str(), ""); - - // Input and string to replace is an empty string. - s = ""; - StringReplace(&s, "", " = ", false); - EXPECT_STREQ(s.c_str(), ""); -} - -} // namespace notifier diff --git a/chrome/browser/sync/notifier/base/time.cc b/chrome/browser/sync/notifier/base/time.cc index 139ca88..fa67fef 100644 --- a/chrome/browser/sync/notifier/base/time.cc +++ b/chrome/browser/sync/notifier/base/time.cc @@ -7,19 +7,12 @@ #include <string> #include <time.h> -#include "chrome/browser/sync/notifier/base/string.h" #include "chrome/browser/sync/notifier/base/utils.h" #include "talk/base/common.h" #include "talk/base/logging.h" namespace notifier { -// Get the current time represented in 100NS granularity since epoch -// (Jan 1, 1970). -time64 GetCurrent100NSTimeSinceEpoch() { - return GetCurrent100NSTime() - kStart100NsTimeToEpoch; -} - char* GetLocalTimeAsString() { time64 long_time = GetCurrent100NSTime(); struct tm now; @@ -34,327 +27,4 @@ char* GetLocalTimeAsString() { return time_string; } -// Parses RFC 822 Date/Time format -// 5. DATE AND TIME SPECIFICATION -// 5.1. SYNTAX -// -// date-time = [ day "," ] date time ; dd mm yy -// ; hh:mm:ss zzz -// day = "Mon" / "Tue" / "Wed" / "Thu" -// / "Fri" / "Sat" / "Sun" -// -// date = 1*2DIGIT month 2DIGIT ; day month year -// ; e.g. 20 Jun 82 -// -// month = "Jan" / "Feb" / "Mar" / "Apr" -// / "May" / "Jun" / "Jul" / "Aug" -// / "Sep" / "Oct" / "Nov" / "Dec" -// -// time = hour zone ; ANSI and Military -// -// hour = 2DIGIT ":" 2DIGIT [":" 2DIGIT] -// ; 00:00:00 - 23:59:59 -// -// zone = "UT" / "GMT" ; Universal Time -// ; North American : UT -// / "EST" / "EDT" ; Eastern: - 5/ - 4 -// / "CST" / "CDT" ; Central: - 6/ - 5 -// / "MST" / "MDT" ; Mountain: - 7/ - 6 -// / "PST" / "PDT" ; Pacific: - 8/ - 7 -// / 1ALPHA ; Military: Z = UT; -// ; A:-1; (J not used) -// ; M:-12; N:+1; Y:+12 -// / ( ("+" / "-") 4DIGIT ) ; Local differential -// ; hours+min. (HHMM) -// Return local time if ret_local_time == true, return UTC time otherwise -const int kNumOfDays = 7; -const int kNumOfMonth = 12; -// Note: RFC822 does not include '-' as a separator, but Http Cookies use -// it in the date field, like this: Wdy, DD-Mon-YYYY HH:MM:SS GMT -// This differs from RFC822 only by those dashes. It is legacy quirk from -// old Netscape cookie specification. So it makes sense to expand this -// parser rather then add another one. -// See http://wp.netscape.com/newsref/std/cookie_spec.html -const char kRFC822_DateDelimiters[] = " ,:-"; - -const char kRFC822_TimeDelimiter[] = ": "; -const char* kRFC822_Day[] = { - "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" -}; -const char* kRFC822_Month[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" -}; - -struct TimeZoneInfo { - const char* zone_name; - int hour_dif; -}; - -const TimeZoneInfo kRFC822_TimeZone[] = { - { "UT", 0 }, - { "GMT", 0 }, - { "EST", -5 }, - { "EDT", -4 }, - { "CST", -6 }, - { "CDT", -5 }, - { "MST", -7 }, - { "MDT", -6 }, - { "PST", -8 }, - { "PDT", -7 }, - { "A", -1 }, // Military time zones. - { "B", -2 }, - { "C", -3 }, - { "D", -4 }, - { "E", -5 }, - { "F", -6 }, - { "G", -7 }, - { "H", -8 }, - { "I", -9 }, - { "K", -10 }, - { "L", -11 }, - { "M", -12 }, - { "N", 1 }, - { "O", 2 }, - { "P", 3 }, - { "Q", 4 }, - { "R", 5 }, - { "S", 6 }, - { "T", 7 }, - { "U", 8 }, - { "V", 9 }, - { "W", 10 }, - { "X", 11 }, - { "Y", 12 }, - { "Z", 0 }, -}; - -bool ParseRFC822DateTime(const char* str, struct tm* time, - bool ret_local_time) { - ASSERT(str && *str); - ASSERT(time); - - std::string str_date(str); - std::string str_token; - const char* str_curr = str_date.c_str(); - - str_token = SplitOneStringToken(&str_curr, kRFC822_DateDelimiters); - if (str_token == "") { - return false; - } - - for (int i = 0; i < kNumOfDays; ++i) { - if (str_token == kRFC822_Day[i]) { - // Skip spaces after ','. - while (*str_curr == ' ' && *str_curr != '\0') { - str_curr++; - } - - str_token = SplitOneStringToken(&str_curr, kRFC822_DateDelimiters); - if (str_token == "") { - return false; - } - break; - } - } - - int day = 0; - if (!ParseStringToInt(str_token.c_str(), &day, true) || day < 0 || day > 31) { - return false; - } - - str_token = SplitOneStringToken(&str_curr, kRFC822_DateDelimiters); - if (str_token == "") { - return false; - } - - int month = -1; - for (int i = 0; i < kNumOfMonth; ++i) { - if (str_token == kRFC822_Month[i]) { - month = i; // Month is 0 based number. - break; - } - } - if (month == -1) { // Month not found. - return false; - } - - str_token = SplitOneStringToken(&str_curr, kRFC822_DateDelimiters); - if (str_token == "") { - return false; - } - - int year = 0; - if (!ParseStringToInt(str_token.c_str(), &year, true)) { - return false; - } - if (year < 100) { // Two digit year format, convert to 1950 - 2050 range. - if (year < 50) { - year += 2000; - } else { - year += 1900; - } - } - - str_token = SplitOneStringToken(&str_curr, kRFC822_TimeDelimiter); - if (str_token == "") { - return false; - } - - int hour = 0; - if (!ParseStringToInt(str_token.c_str(), &hour, true) || - hour < 0 || hour > 23) { - return false; - } - - str_token = SplitOneStringToken(&str_curr, kRFC822_TimeDelimiter); - if (str_token == "") { - return false; - } - - int minute = 0; - if (!ParseStringToInt(str_token.c_str(), &minute, true) || - minute < 0 || minute > 59) { - return false; - } - - str_token = SplitOneStringToken(&str_curr, kRFC822_TimeDelimiter); - if (str_token == "") { - return false; - } - - int second = 0; - // Distingushed between XX:XX and XX:XX:XX time formats. - if (str_token.size() == 2 && isdigit(str_token[0]) && isdigit(str_token[1])) { - second = 0; - if (!ParseStringToInt(str_token.c_str(), &second, true) || - second < 0 || second > 59) { - return false; - } - - str_token = SplitOneStringToken(&str_curr, kRFC822_TimeDelimiter); - if (str_token == "") { - return false; - } - } - - int bias = 0; - if (str_token[0] == '+' || str_token[0] == '-' || isdigit(str_token[0])) { - // Numeric format. - int zone = 0; - if (!ParseStringToInt(str_token.c_str(), &zone, true)) { - return false; - } - - // Zone is in HHMM format, need to convert to the number of minutes. - bias = (zone / 100) * 60 + (zone % 100); - } else { // Text format. - for (size_t i = 0; i < sizeof(kRFC822_TimeZone) / sizeof(TimeZoneInfo); - ++i) { - if (str_token == kRFC822_TimeZone[i].zone_name) { - bias = kRFC822_TimeZone[i].hour_dif * 60; - break; - } - } - } - - SetZero(*time); - time->tm_year = year - 1900; - time->tm_mon = month; - time->tm_mday = day; - time->tm_hour = hour; - time->tm_min = minute; - time->tm_sec = second; - - time64 time_64 = TmToTime64(*time); - time_64 = time_64 - bias * kMinsTo100ns; - - if (!Time64ToTm(time_64, time)) { - return false; - } - - if (ret_local_time) { - if (!UtcTimeToLocalTime(time)) { - return false; - } - } - - return true; -} - -// Parse a string to time span. -// -// A TimeSpan value can be represented as -// [d.]hh:mm:ss -// -// d = days (optional) -// hh = hours as measured on a 24-hour clock -// mm = minutes -// ss = seconds -bool ParseStringToTimeSpan(const char* str, time64* time_span) { - ASSERT(str); - ASSERT(time_span); - - const char kColonDelimitor[] = ":"; - const char kDotDelimitor = '.'; - - std::string str_span(str); - time64 span = 0; - - int idx = str_span.find(kDotDelimitor); - if (idx != -1) { - std::string str_day = str_span.substr(0, idx); - int day = 0; - if (!ParseStringToInt(str_day.c_str(), &day, true) || - day < 0 || day > 365) { - return false; - } - span = day; - - str_span = str_span.substr(idx + 1); - } - - const char* str_curr = str_span.c_str(); - std::string str_token; - - str_token = SplitOneStringToken(&str_curr, kColonDelimitor); - if (str_token == "") { - return false; - } - - int hour = 0; - if (!ParseStringToInt(str_token.c_str(), &hour, true) || - hour < 0 || hour > 23) { - return false; - } - span = span * 24 + hour; - - str_token = SplitOneStringToken(&str_curr, kColonDelimitor); - if (str_token == "") { - return false; - } - - int minute = 0; - if (!ParseStringToInt(str_token.c_str(), &minute, true) || - minute < 0 || minute > 59) { - return false; - } - span = span * 60 + minute; - - str_token = SplitOneStringToken(&str_curr, kColonDelimitor); - if (str_token == "") { - return false; - } - - int second = 0; - if (!ParseStringToInt(str_token.c_str(), &second, true) || - second < 0 || second > 59) { - return false; - } - - *time_span = (span * 60 + second) * kSecsTo100ns; - - return true; -} - } // namespace notifier diff --git a/chrome/browser/sync/notifier/base/time.h b/chrome/browser/sync/notifier/base/time.h index 4ea303f..338ad71 100644 --- a/chrome/browser/sync/notifier/base/time.h +++ b/chrome/browser/sync/notifier/base/time.h @@ -43,73 +43,16 @@ namespace notifier { // the value since Jan 1, 1601. time64 GetCurrent100NSTime(); -// Get the current time represented in 100NS granularity since epoch -// (Jan 1, 1970). -time64 GetCurrent100NSTimeSinceEpoch(); - // Convert from struct tm to time64. time64 TmToTime64(const struct tm& tm); // Convert from time64 to struct tm. bool Time64ToTm(time64 t, struct tm* tm); -// Convert from UTC time to local time. -bool UtcTimeToLocalTime(struct tm* tm); - -// Convert from local time to UTC time. -bool LocalTimeToUtcTime(struct tm* tm); - // Returns the local time as a string suitable for logging. // Note: This is *not* threadsafe, so only call it from the main thread. char* GetLocalTimeAsString(); -// Parses RFC 822 Date/Time format -// 5. DATE AND TIME SPECIFICATION -// 5.1. SYNTAX -// -// date-time = [ day "," ] date time ; dd mm yy -// ; hh:mm:ss zzz -// day = "Mon" / "Tue" / "Wed" / "Thu" -// / "Fri" / "Sat" / "Sun" -// -// date = 1*2DIGIT month 2DIGIT ; day month year -// ; e.g. 20 Jun 82 -// -// month = "Jan" / "Feb" / "Mar" / "Apr" -// / "May" / "Jun" / "Jul" / "Aug" -// / "Sep" / "Oct" / "Nov" / "Dec" -// -// time = hour zone ; ANSI and Military -// -// hour = 2DIGIT ":" 2DIGIT [":" 2DIGIT] -// ; 00:00:00 - 23:59:59 -// -// zone = "UT" / "GMT" ; Universal Time -// ; North American : UT -// / "EST" / "EDT" ; Eastern: - 5/ - 4 -// / "CST" / "CDT" ; Central: - 6/ - 5 -// / "MST" / "MDT" ; Mountain: - 7/ - 6 -// / "PST" / "PDT" ; Pacific: - 8/ - 7 -// / 1ALPHA ; Military: Z = UT; -// ; A:-1; (J not used) -// ; M:-12; N:+1; Y:+12 -// / ( ("+" / "-") 4DIGIT ) ; Local differential -// ; hours+min. (HHMM) -// Return local time if ret_local_time == true, return UTC time otherwise -bool ParseRFC822DateTime(const char* str, struct tm* time, - bool ret_local_time); - -// Parse a string to time span. -// -// A TimeSpan value can be represented as -// [d.]hh:mm:ss -// -// d = days (optional) -// hh = hours as measured on a 24-hour clock -// mm = minutes -// ss = seconds -bool ParseStringToTimeSpan(const char* str, time64* time_span); - } // namespace notifier #endif // CHROME_BROWSER_SYNC_NOTIFIER_BASE_TIME_H_ diff --git a/chrome/browser/sync/notifier/base/time_unittest.cc b/chrome/browser/sync/notifier/base/time_unittest.cc index 9054798..fc80f77 100644 --- a/chrome/browser/sync/notifier/base/time_unittest.cc +++ b/chrome/browser/sync/notifier/base/time_unittest.cc @@ -8,62 +8,6 @@ namespace notifier { TEST_NOTIFIER_F(TimeTest); -TEST_F(TimeTest, ParseRFC822DateTime) { - struct tm t = {0}; - - EXPECT_TRUE(ParseRFC822DateTime("Mon, 16 May 2005 15:44:18 -0700", - &t, false)); - EXPECT_EQ(t.tm_year, 2005 - 1900); - EXPECT_EQ(t.tm_mon, 4); - EXPECT_EQ(t.tm_mday, 16); - EXPECT_EQ(t.tm_hour, 22); - EXPECT_EQ(t.tm_min, 44); - EXPECT_EQ(t.tm_sec, 18); - - EXPECT_TRUE(ParseRFC822DateTime("Mon, 16 May 2005 15:44:18 -0700", &t, true)); - EXPECT_EQ(t.tm_year, 2005 - 1900); - EXPECT_EQ(t.tm_mon, 4); - EXPECT_EQ(t.tm_mday, 16); - EXPECT_TRUE(t.tm_hour == 15 || t.tm_hour == 14); // Daylight saving time. - EXPECT_EQ(t.tm_min, 44); - EXPECT_EQ(t.tm_sec , 18); - - EXPECT_TRUE(ParseRFC822DateTime("Tue, 17 May 2005 02:56:18 +0400", - &t, false)); - EXPECT_EQ(t.tm_year, 2005 - 1900); - EXPECT_EQ(t.tm_mon, 4); - EXPECT_EQ(t.tm_mday, 16); - EXPECT_EQ(t.tm_hour, 22); - EXPECT_EQ(t.tm_min, 56); - EXPECT_EQ(t.tm_sec , 18); - - EXPECT_TRUE(ParseRFC822DateTime("Tue, 17 May 2005 02:56:18 +0400", &t, true)); - EXPECT_EQ(t.tm_year, 2005 - 1900); - EXPECT_EQ(t.tm_mon, 4); - EXPECT_EQ(t.tm_mday, 16); - EXPECT_TRUE(t.tm_hour == 15 || t.tm_hour == 14); // Daylight saving time. - EXPECT_EQ(t.tm_min, 56); - EXPECT_EQ(t.tm_sec, 18); -} - -TEST_F(TimeTest, ParseStringToTimeSpan) { - time64 time_span = 0; - - EXPECT_TRUE(ParseStringToTimeSpan("0:0:4", &time_span)); - EXPECT_EQ(time_span, 4 * kSecsTo100ns); - - EXPECT_TRUE(ParseStringToTimeSpan("0:3:4", &time_span)); - EXPECT_EQ(time_span, (3 * 60 + 4) * kSecsTo100ns); - - EXPECT_TRUE(ParseStringToTimeSpan("2:3:4", &time_span)); - EXPECT_EQ(time_span, (2 * 3600 + 3 * 60 + 4) * kSecsTo100ns); - - EXPECT_TRUE(ParseStringToTimeSpan("1.2:3:4", &time_span)); - EXPECT_EQ(time_span, (1 * 86400 + 2 * 60 * 60 + 3 * 60 + 4) * kSecsTo100ns); - - EXPECT_FALSE(ParseStringToTimeSpan("2:invalid:4", &time_span)); -} - TEST_F(TimeTest, UseLocalTimeAsString) { // Just call it to ensure that it doesn't assert. GetLocalTimeAsString(); diff --git a/chrome/browser/sync/notifier/base/win/time_win32.cc b/chrome/browser/sync/notifier/base/win/time_win32.cc index e5c5713..8b7bd90c 100644 --- a/chrome/browser/sync/notifier/base/win/time_win32.cc +++ b/chrome/browser/sync/notifier/base/win/time_win32.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // -// Windows specific yime functions. +// Windows specific time functions. #include <time.h> #include <windows.h> @@ -101,57 +101,4 @@ bool Time64ToTm(time64 t, struct tm* tm) { return true; } -bool UtcTimeToLocalTime(struct tm* tm) { - ASSERT(tm); - - SYSTEMTIME utc_time; - TmTimeToSystemTime(*tm, &utc_time); - - TIME_ZONE_INFORMATION time_zone; - if (::GetTimeZoneInformation(&time_zone) == TIME_ZONE_ID_INVALID) { - return false; - } - - SYSTEMTIME local_time; - if (!::SystemTimeToTzSpecificLocalTime(&time_zone, &utc_time, &local_time)) { - return false; - } - - SystemTimeToTmTime(local_time, tm); - - return true; -} - -bool LocalTimeToUtcTime(struct tm* tm) { - ASSERT(tm); - - SYSTEMTIME local_time; - TmTimeToSystemTime(*tm, &local_time); - - // Get the bias, which when added to local, gives UTC. - TIME_ZONE_INFORMATION time_zone; - if (::GetTimeZoneInformation(&time_zone) == TIME_ZONE_ID_INVALID) { - return false; - } - - // By negating the biases, we can get translation from UTC to local. - time_zone.Bias *= -1; - time_zone.DaylightBias *= -1; - time_zone.StandardBias *= -1; // This is 0 but negating for completness. - - // We'll tell SystemTimeToTzSpecificLocalTime that the local time is actually - // UTC. With the negated bias, the "local" time that the API returns will - // actually be UTC. Casting the const off because - // SystemTimeToTzSpecificLocalTime's definition requires it, although the - // value is not modified. - SYSTEMTIME utc_time; - if (!::SystemTimeToTzSpecificLocalTime(&time_zone, &local_time, &utc_time)) { - return false; - } - - SystemTimeToTmTime(utc_time, tm); - - return true; -} - } // namespace notifier diff --git a/chrome/browser/sync/notifier/communicator/xml_parse_helpers-inl.h b/chrome/browser/sync/notifier/communicator/xml_parse_helpers-inl.h deleted file mode 100644 index b400218..0000000 --- a/chrome/browser/sync/notifier/communicator/xml_parse_helpers-inl.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_SYNC_NOTIFIER_COMMUNICATOR_XML_PARSE_HELPERS_INL_H_ -#define CHROME_BROWSER_SYNC_NOTIFIER_COMMUNICATOR_XML_PARSE_HELPERS_INL_H_ - -#include <sstream> - -#include "chrome/browser/sync/notifier/communicator/xml_parse_helpers.h" -#include "talk/xmllite/xmlelement.h" - -namespace notifier { - -template<class T> -void SetAttr(buzz::XmlElement* xml, const buzz::QName& name, const T& data) { - std::ostringstream ost; - ost << data; - xml->SetAttr(name, ost.str()); -} - -} // namespace notifier - -#endif // CHROME_BROWSER_SYNC_NOTIFIER_COMMUNICATOR_XML_PARSE_HELPERS_INL_H_ diff --git a/chrome/browser/sync/notifier/communicator/xml_parse_helpers.cc b/chrome/browser/sync/notifier/communicator/xml_parse_helpers.cc deleted file mode 100644 index e4d27cf..0000000 --- a/chrome/browser/sync/notifier/communicator/xml_parse_helpers.cc +++ /dev/null @@ -1,183 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/sync/notifier/communicator/xml_parse_helpers.h" -#include "chrome/browser/sync/notifier/communicator/xml_parse_helpers-inl.h" - -#include <string> - -#include "chrome/browser/sync/notifier/base/string.h" -#include "talk/base/basicdefs.h" -#include "talk/base/stream.h" -#include "talk/xmllite/xmlbuilder.h" -#include "talk/xmllite/xmlelement.h" -#include "talk/xmllite/xmlparser.h" -#include "talk/xmllite/xmlprinter.h" -#include "talk/xmpp/jid.h" - -namespace notifier { - -buzz::XmlElement* ReadXmlFromStream(talk_base::StreamInterface* stream) { - buzz::XmlBuilder builder; - buzz::XmlParser parser(&builder); - - const int kBufferSize = 4 * 1024; - char buf[kBufferSize]; - - talk_base::StreamResult result = talk_base::SR_SUCCESS; - while(true) { - size_t read = 0; - - // Read a chunk. - result = stream->Read(buf, kBufferSize, &read, NULL); - if (result != talk_base::SR_SUCCESS) - break; - - // Pass it to the parser. - parser.Parse(buf, read, false); - } - - if (result == talk_base::SR_EOS) { - parser.Parse(NULL, 0, true); - return builder.CreateElement(); - } - - return NULL; -} - -bool ParseInt64Attr(const buzz::XmlElement* element, - const buzz::QName& attribute, int64* result) { - if (!element->HasAttr(attribute)) - return false; - std::string text = element->Attr(attribute); - char* error = NULL; -#ifdef POSIX - *result = atoll(text.c_str()); -#else - *result = _strtoi64(text.c_str(), &error, 10); -#endif - return text.c_str() != error; -} - -bool ParseIntAttr(const buzz::XmlElement* element, const buzz::QName& attribute, - int* result) { - if (!element->HasAttr(attribute)) - return false; - std::string text = element->Attr(attribute); - char* error = NULL; - *result = static_cast<int>(strtol(text.c_str(), &error, 10)); - return text.c_str() != error; -} - -bool ParseBoolAttr(const buzz::XmlElement* element, - const buzz::QName& attribute, bool* result) { - int int_value = 0; - if (!ParseIntAttr(element, attribute, &int_value)) - return false; - *result = int_value != 0; - return true; -} - -bool ParseStringAttr(const buzz::XmlElement* element, - const buzz::QName& attribute, std::string* result) { - if (!element->HasAttr(attribute)) - return false; - *result = element->Attr(attribute); - return true; -} - -void WriteXmlToStream(talk_base::StreamInterface* stream, - const buzz::XmlElement* xml) { - // Save it all to a string and then write that string out to disk. - // - // This is probably really inefficient in multiple ways. We probably have an - // entire string copy of the XML in memory twice -- once in the stream and - // once in the string. There is probably a way to get the data directly out - // of the stream but I don't have the time to decode the stream classes right - // now. - std::ostringstream s; - buzz::XmlPrinter::PrintXml(&s, xml); - std::string output_string = s.str(); - stream->WriteAll(output_string.data(), output_string.length(), NULL, NULL); -} - -bool SetInt64Attr(buzz::XmlElement* element, const buzz::QName& attribute, - int64 value) { - if (!element->HasAttr(attribute)) - return false; - element->AddAttr(attribute, Int64ToString(value).c_str()); - return true; -} - -bool SetIntAttr(buzz::XmlElement* element, const buzz::QName& attribute, - int value) { - if (!element->HasAttr(attribute)) - return false; - element->AddAttr(attribute, IntToString(value).c_str()); - return true; -} - -bool SetBoolAttr(buzz::XmlElement* element, const buzz::QName& attribute, - bool value) { - int int_value = 0; - if (value) { - int_value = 1; - } - return SetIntAttr(element, attribute, int_value); -} - -bool SetStringAttr(buzz::XmlElement* element, const buzz::QName& attribute, - const std::string& value) { - if (!element->HasAttr(attribute)) - return false; - element->AddAttr(attribute, value); - return true; -} - -// XmlStream. -XmlStream::XmlStream() - : state_(talk_base::SS_OPEN), - builder_(new buzz::XmlBuilder()), - parser_(new buzz::XmlParser(builder_.get())) { -} - -XmlStream::~XmlStream() { -} - -buzz::XmlElement* XmlStream::CreateElement() { - if (talk_base::SS_OPEN == state_) { - Close(); - } - return builder_->CreateElement(); -} - -talk_base::StreamResult XmlStream::Read(void* buffer, size_t buffer_len, - size_t* read, int* error) { - if (error) - *error = -1; - return talk_base::SR_ERROR; -} - -talk_base::StreamResult XmlStream::Write(const void* data, size_t data_len, - size_t* written, int* error) { - if (talk_base::SS_OPEN != state_) { - if (error) - *error = -1; - return talk_base::SR_ERROR; - } - parser_->Parse(static_cast<const char*>(data), data_len, false); - if (written) - *written = data_len; - return talk_base::SR_SUCCESS; -} - -void XmlStream::Close() { - if (talk_base::SS_OPEN != state_) - return; - - parser_->Parse(NULL, 0, true); - state_ = talk_base::SS_CLOSED; -} - -} // namespace buzz diff --git a/chrome/browser/sync/notifier/communicator/xml_parse_helpers.h b/chrome/browser/sync/notifier/communicator/xml_parse_helpers.h deleted file mode 100644 index ae5ed35..0000000 --- a/chrome/browser/sync/notifier/communicator/xml_parse_helpers.h +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_SYNC_NOTIFIER_COMMUNICATOR_XML_PARSE_HELPERS_H_ -#define CHROME_BROWSER_SYNC_NOTIFIER_COMMUNICATOR_XML_PARSE_HELPERS_H_ - -#include <string> - -#include "talk/base/basictypes.h" -#include "talk/base/scoped_ptr.h" -#include "talk/base/stream.h" - -namespace buzz { -class XmlBuilder; -class XmlElement; -class XmlParser; -class QName; -} - -namespace notifier { - -buzz::XmlElement* ReadXmlFromStream(talk_base::StreamInterface* stream); -bool ParseInt64Attr(const buzz::XmlElement* element, - const buzz::QName& attribute, int64* result); -bool ParseIntAttr(const buzz::XmlElement* element, - const buzz::QName& attribute, int* result); -bool ParseBoolAttr(const buzz::XmlElement* element, - const buzz::QName& attribute, bool* result); -bool ParseStringAttr(const buzz::XmlElement* element, - const buzz::QName& attribute, std::string* result); - -void WriteXmlToStream(talk_base::StreamInterface* stream, - const buzz::XmlElement* xml); -bool SetInt64Attr(buzz::XmlElement* element, const buzz::QName& attribute, - int64 result); -bool SetIntAttr(buzz::XmlElement* element, const buzz::QName& attribute, - int result); -bool SetBoolAttr(buzz::XmlElement* element, const buzz::QName& attribute, - bool result); -bool SetStringAttr(buzz::XmlElement* element, const buzz::QName& attribute, - const std::string& result); - -template<class T> -void SetAttr(buzz::XmlElement* xml, const buzz::QName& name, const T& data); - -/////////////////////////////////////////////////////////////////////////////// -// XmlStream -/////////////////////////////////////////////////////////////////////////////// - -class XmlStream : public talk_base::StreamInterface { - public: - XmlStream(); - virtual ~XmlStream(); - - buzz::XmlElement* CreateElement(); - - virtual talk_base::StreamState GetState() const { return state_; } - - virtual talk_base::StreamResult Read(void* buffer, size_t buffer_len, - size_t* read, int* error); - virtual talk_base::StreamResult Write(const void* data, size_t data_len, - size_t* written, int* error); - virtual void Close(); - - private: - talk_base::StreamState state_; - scoped_ptr<buzz::XmlBuilder> builder_; - scoped_ptr<buzz::XmlParser> parser_; -}; - -/////////////////////////////////////////////////////////////////////////////// - -} // namespace buzz - -#endif // CHROME_BROWSER_SYNC_NOTIFIER_COMMUNICATOR_XML_PARSE_HELPERS_H_ |