25 #ifndef __AMPS_FIELD_HPP__ 26 #define __AMPS_FIELD_HPP__ 28 #define _AMPS_SKIP_AMPSPLUSPLUS 30 #undef _AMPS_SKIP_AMPSPLUSPLUS 36 #define AMPS_UNSET_INDEX (size_t)-1 37 #define AMPS_TIMESTAMP_LEN 16 38 #define AMPS_TIMESTAMP_LEN_LONG 23 39 #define AMPS_MAX_BOOKMARK_LEN 42 41 #define AMPS_MAX_SUBID_LEN 1048576 // 1MB, NOT FIXED IN AMPS 43 #if (_MSC_VER >= 1400) // VS2005 or higher 44 #define AMPS_snprintf(buf_, sz_, ...) _snprintf_s(buf_, sz_, _TRUNCATE, __VA_ARGS__) 45 #define AMPS_snprintf_amps_uint64_t(buf,sz,val) sprintf_s(buf,sz,"%I64u",val) 47 #define AMPS_snprintf_sizet(buf,sz,val) sprintf_s(buf,sz,"%lu",val) 49 #define AMPS_snprintf_sizet(buf,sz,val) sprintf_s(buf,sz,"%u",val) 51 #else // VS2003 or older 52 #define AMPS_snprintf _snprintf 54 #define AMPS_snprintf_sizet(buf,sz,val) _sprintf(buf,sz,"%lu",val) 56 #define AMPS_snprintf_sizet(buf,sz,val) _sprintf(buf,sz,"%u",val) 60 #define AMPS_snprintf snprintf 61 #if defined(__x86_64__) || defined(__aarch64__) 62 #define AMPS_snprintf_amps_uint64_t(buf,sz,val) snprintf(buf,sz,"%lu", (unsigned long)val) 63 #define AMPS_snprintf_sizet(buf,sz,val) snprintf(buf,sz,"%lu",val) 65 #define AMPS_snprintf_amps_uint64_t(buf,sz,val) snprintf(buf,sz,"%llu",val) 66 #define AMPS_snprintf_sizet(buf,sz,val) snprintf(buf,sz,"%u",val) 92 Field() : _data(NULL), _len(0) {;}
93 Field(
const char* data_)
96 _len = ::strlen(data_);
98 Field(
const char* data_,
size_t len_)
114 Field(
const std::string& string_)
116 _data = string_.c_str();
117 _len = string_.length();
120 bool contains(
const char* searchString,
size_t len)
const 122 const char* dataEnd = _data + _len;
123 return std::search(_data, dataEnd, searchString, searchString + len) != dataEnd;
136 operator std::string ()
const 138 return _len ? std::string(_data, _len) : std::string();
148 if ( _len == rhs_._len )
150 return ::memcmp(_data, rhs_._data, _len) == 0;
164 return (!_data && !rhs_);
166 return (_len == strlen(rhs_)) && (::strncmp(_data, rhs_, _len) == 0);
169 bool operator<(
const Field& rhs)
const;
178 if ( _len == rhs_._len )
180 return ::memcmp(_data, rhs_._data, _len) != 0;
192 return (_len != strlen(rhs_)) || (::memcmp(_data, rhs_, _len) != 0);
202 return rhs_.compare(0, rhs_.length(), _data, _len) != 0;
212 return rhs_.compare(0, rhs_.length(), _data, _len) == 0;
223 _data =
new char[orig_._len];
224 ::memcpy(static_cast<void*>(const_cast<char*>(_data)),
225 orig_._data, orig_._len);
273 void assign(
const char* ptr,
size_t len)
280 inline size_t hash_function()
const 283 const char* p_ = _data;
287 c = (
unsigned long) * p_;
296 size_t operator()(
const Field& f)
const 298 return f.hash_function();
301 bool operator()(
const Field& f1,
const Field& f2)
const 316 return ::memcmp(f1.
data(), f2.
data(), f2.
len()) < 0;
321 static bool isTimestamp(
const Field& field_)
323 return (field_.
len() >= AMPS_TIMESTAMP_LEN
324 && field_.
len() <= AMPS_TIMESTAMP_LEN_LONG
325 && field_.
data()[8] ==
'T');
328 static std::vector<Field> parseBookmarkList(
const Field& field_)
330 std::vector<Field> list;
331 const char* start = field_.
data();
332 size_t remain = field_.
len();
333 const char* comma = (
const char*)memchr((
const void*)start,
337 size_t len = (size_t)(comma - start);
340 #ifdef AMPS_USE_EMPLACE 341 list.emplace_back(
Field(start, len));
343 list.push_back(
Field(start, len));
347 remain = field_.
len() - (size_t)(start - field_.
data());
348 comma = (
const char*)memchr((
const void*)start,
353 #ifdef AMPS_USE_EMPLACE 354 list.emplace_back(
Field(start, remain));
356 list.push_back(
Field(start, remain));
363 static void parseBookmark(
const Field& field_,
364 amps_uint64_t& publisherId_,
365 amps_uint64_t& sequenceNumber_)
367 publisherId_ = sequenceNumber_ = (amps_uint64_t)0;
373 size_t len = field_.
len();
375 if (isTimestamp(field_))
380 for ( ; i <
len; ++i)
382 if (!isdigit(data[i]))
387 publisherId_ += (amps_uint64_t)(data[i] -
'0');
390 if (i < len && data[i] !=
'|')
392 publisherId_ = sequenceNumber_ = (amps_uint64_t)0;
395 for (i = i + 1; i <
len; ++i)
397 if (!isdigit(data[i]))
401 sequenceNumber_ *= 10;
402 sequenceNumber_ += (amps_uint64_t)(data[i] -
'0');
408 class BookmarkRange :
public Field 411 static bool isRange(
const Field& bookmark_)
413 return memchr(bookmark_.
data(),
':', bookmark_.
len()) != NULL;
417 :
Field(), _start(), _end(), _open(AMPS_UNSET_INDEX)
418 , _capacity(AMPS_UNSET_INDEX)
423 BookmarkRange(
const Field& field_)
424 :
Field(), _start(), _end(), _open(AMPS_UNSET_INDEX)
425 , _capacity(field_.
len())
430 void set(
const Field& field_)
442 bool foundSeparator =
false;
443 bool foundClose =
false;
444 for (
size_t i = 0; i < _len; ++i)
452 if (foundClose || _open != AMPS_UNSET_INDEX)
477 if (foundClose || _open == AMPS_UNSET_INDEX)
482 else if (foundSeparator)
486 _end.assign(_data + i, 0);
489 else if (!_start.data())
491 _start.assign(_data + i, 0);
498 if (foundSeparator || foundClose || _open == AMPS_UNSET_INDEX
504 foundSeparator =
true;
506 if (_start.len() == 0)
509 _start.assign(_start.data(), i - (size_t)(_start.data() - _data));
517 if (foundClose || _open == AMPS_UNSET_INDEX || !_end.data())
528 _end.assign(_end.data(), i - (size_t)(_end.data() - _data));
535 if (_end.data() && _end.len() == 0)
538 _end.assign(_end.data(), i - (size_t)(_end.data() - _data));
541 else if (_start.data() && _start.len() == 0)
544 _start.assign(_start.data(), i - (size_t)(_start.data() - _data));
556 if (_start.empty() || _end.empty())
574 _open = AMPS_UNSET_INDEX;
587 bool isStartInclusive()
const 589 return _data[_open] ==
'[';
594 void makeStartExclusive()
596 const_cast<char*
>(_data)[_open] =
'(';
601 bool isEndInclusive()
const 603 return _data[_len - 1] ==
']';
608 void makeEndExclusive()
610 const_cast<char*
>(_data)[_len - 1] =
')';
615 const Field& getStart()
const 620 const Field& getEnd()
const 627 void replaceStart(
const Field& start_,
bool makeExclusive_ =
false)
631 if (_capacity >= (start_.
len() + _end.len() + 3))
633 char*
data =
const_cast<char*
>(_data);
640 data[0] = _data[_open];
643 if ((
size_t)(_end.data() - _data - 2) < start_.
len())
645 size_t newLen = start_.
len() + _end.len() + 3;
649 for (
size_t dest = newLen - 1, src = _len - 1;
650 src > _start.len(); --src, --dest)
652 data[dest] = _data[src];
654 if (data[src] ==
':')
656 _end.assign(data + dest + 1, _end.len());
663 ::memcpy(static_cast<void*>(data + 1), start_.
data(), start_.
len());
664 _start.assign(data + 1, start_.
len());
666 if ((
size_t)(_end.data() - _start.data()) > _start.len() + 1UL)
669 for (
size_t dest = _start.len() + 1, src = (size_t)(_end.data() - data - 1);
670 src < _len; ++dest, ++src)
672 data[dest] = data[src];
673 if (data[src] ==
']' || data[src] ==
')')
675 _end.assign(data + _start.len() + 2, _end.len());
679 _len = _start.len() + _end.len() + 3;
686 _capacity = (4UL * AMPS_MAX_BOOKMARK_LEN + 6UL
687 >= start_.
len() + _end.len() + 3UL)
688 ? (4UL * AMPS_MAX_BOOKMARK_LEN + 6UL)
689 : (start_.
len() + _end.len() + 3UL);
690 char*
data =
new char[_capacity];
697 data[0] = _data[_open];
700 ::memcpy(static_cast<void*>(data + 1), start_.
data(), start_.
len());
701 _start.assign(data + 1, start_.
len());
702 data[start_.
len() + 1] =
':';
703 ::memcpy(static_cast<void*>(data + start_.
len() + 2), _end.data(),
705 _end.assign(data + start_.
len() + 2, _end.len());
706 size_t len = start_.
len() + 3 + _end.len();
707 data[len - 1] = _data[_len - 1];
bool operator==(const char *rhs_) const
String comparison operator Returns `true' if self and rhs are equivalent, `false' otherwise...
Definition: Field.hpp:160
Core type and function declarations for the AMPS C client.
bool operator!=(const std::string &rhs_) const
String comparison operator Returns `true' if self and rhs are not equivalent.
Definition: Field.hpp:200
bool operator!=(const Field &rhs_) const
Comparison operator Returns true if self and rhs are not equivalent.
Definition: Field.hpp:176
bool operator==(const Field &rhs_) const
Comparison operator Returns `true' if self and rhs are equivalent, `false' otherwise.
Definition: Field.hpp:146
void clear()
Deletes the data associated with this Field, should only be used on Fields that were created as deepC...
Definition: Field.hpp:247
const char * data() const
Returns the (non-null-terminated) data underlying this field.
Definition: Field.hpp:260
bool empty() const
Returns 'true' if empty, 'false' otherwise.
Definition: Field.hpp:128
size_t len() const
Returns the length of the data underlying this field.
Definition: Field.hpp:267
bool operator!=(const char *rhs_) const
String comparison operator Returns true if self and rhs are not equivalent.
Definition: Field.hpp:190
Field represents the value of a single field in a Message.
Definition: Field.hpp:86
void deepCopy(const Field &orig_)
Makes self a deep copy of the original field.
Definition: Field.hpp:218
Definition: ampsplusplus.hpp:102
bool operator==(const std::string &rhs_) const
String comparison operator Returns `true' if self and rhs are equivalent.
Definition: Field.hpp:210
Field deepCopy() const
Makes a deep copy of self, returns it.
Definition: Field.hpp:237