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 ::strncmp(_data, rhs_, _len) == 0 && 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 strncmp(_data, rhs_, _len) != 0 || 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;
369 size_t len = field_.
len();
371 if (isTimestamp(field_))
376 for ( ; i <
len; ++i)
378 if (!isdigit(data[i]))
383 publisherId_ += (amps_uint64_t)(data[i] -
'0');
386 if (i < len && data[i] !=
'|')
388 publisherId_ = sequenceNumber_ = (amps_uint64_t)0;
391 for (i = i + 1; i <
len; ++i)
393 if (!isdigit(data[i]))
397 sequenceNumber_ *= 10;
398 sequenceNumber_ += (amps_uint64_t)(data[i] -
'0');
404 class BookmarkRange :
public Field 407 static bool isRange(
const Field& bookmark_)
409 return memchr(bookmark_.
data(),
':', bookmark_.
len()) != NULL;
413 :
Field(), _start(), _end(), _open(AMPS_UNSET_INDEX)
414 , _capacity(AMPS_UNSET_INDEX)
419 BookmarkRange(
const Field& field_)
420 :
Field(), _start(), _end(), _open(AMPS_UNSET_INDEX)
421 , _capacity(field_.
len())
426 void set(
const Field& field_)
438 bool foundSeparator =
false;
439 bool foundClose =
false;
440 for (
size_t i = 0; i < _len; ++i)
448 if (foundClose || _open != AMPS_UNSET_INDEX)
473 if (foundClose || _open == AMPS_UNSET_INDEX)
478 else if (foundSeparator)
482 _end.assign(_data + i, 0);
485 else if (!_start.data())
487 _start.assign(_data + i, 0);
494 if (foundSeparator || foundClose || _open == AMPS_UNSET_INDEX
500 foundSeparator =
true;
502 if (_start.len() == 0)
505 _start.assign(_start.data(), i - (size_t)(_start.data() - _data));
513 if (foundClose || _open == AMPS_UNSET_INDEX || !_end.data())
524 _end.assign(_end.data(), i - (size_t)(_end.data() - _data));
531 if (_end.data() && _end.len() == 0)
534 _end.assign(_end.data(), i - (size_t)(_end.data() - _data));
537 else if (_start.data() && _start.len() == 0)
540 _start.assign(_start.data(), i - (size_t)(_start.data() - _data));
552 if (_start.empty() || _end.empty())
570 _open = AMPS_UNSET_INDEX;
583 bool isStartInclusive()
const 585 return _data[_open] ==
'[';
590 void makeStartExclusive()
592 const_cast<char*
>(_data)[_open] =
'(';
597 bool isEndInclusive()
const 599 return _data[_len - 1] ==
']';
604 void makeEndExclusive()
606 const_cast<char*
>(_data)[_len - 1] =
')';
611 const Field& getStart()
const 616 const Field& getEnd()
const 623 void replaceStart(
const Field& start_,
bool makeExclusive_ =
false)
627 if (_capacity >= (start_.
len() + _end.len() + 3))
629 char*
data =
const_cast<char*
>(_data);
636 data[0] = _data[_open];
639 if ((
size_t)(_end.data() - _data - 2) < start_.
len())
641 size_t newLen = start_.
len() + _end.len() + 3;
645 for (
size_t dest = newLen - 1, src = _len - 1;
646 src > _start.len(); --src, --dest)
648 data[dest] = _data[src];
650 if (data[src] ==
':')
652 _end.assign(data + dest + 1, _end.len());
659 ::memcpy(static_cast<void*>(data + 1), start_.
data(), start_.
len());
660 _start.assign(data + 1, start_.
len());
662 if ((
size_t)(_end.data() - _start.data()) > _start.len() + 1UL)
665 for (
size_t dest = _start.len() + 1, src = (size_t)(_end.data() - data - 1);
666 src < _len; ++dest, ++src)
668 data[dest] = data[src];
669 if (data[src] ==
']' || data[src] ==
')')
671 _end.assign(data + _start.len() + 2, _end.len());
675 _len = _start.len() + _end.len() + 3;
682 _capacity = (4UL * AMPS_MAX_BOOKMARK_LEN + 6UL
683 >= start_.
len() + _end.len() + 3UL)
684 ? (4UL * AMPS_MAX_BOOKMARK_LEN + 6UL)
685 : (start_.
len() + _end.len() + 3UL);
686 char*
data =
new char[_capacity];
693 data[0] = _data[_open];
696 ::memcpy(static_cast<void*>(data + 1), start_.
data(), start_.
len());
697 _start.assign(data + 1, start_.
len());
698 data[start_.
len() + 1] =
':';
699 ::memcpy(static_cast<void*>(data + start_.
len() + 2), _end.data(),
701 _end.assign(data + start_.
len() + 2, _end.len());
702 size_t len = start_.
len() + 3 + _end.len();
703 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:106
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