25 #ifndef __AMPS_FIELD_HPP__ 26 #define __AMPS_FIELD_HPP__ 28 #define _AMPS_SKIP_AMPSPLUSPLUS 30 #undef _AMPS_SKIP_AMPSPLUSPLUS 35 #define AMPS_UNSET_INDEX (size_t)-1 36 #define AMPS_TIMESTAMP_LEN 16 37 #define AMPS_TIMESTAMP_LEN_LONG 24 38 #define AMPS_MAX_BOOKMARK_LEN 42 40 #define AMPS_MAX_SUBID_LEN 1048576 // 1MB, NOT FIXED IN AMPS 42 #if (_MSC_VER >= 1400) // VS2005 or higher 43 #define AMPS_snprintf(buf_, sz_, ...) _snprintf_s(buf_, sz_, _TRUNCATE, __VA_ARGS__) 44 #define AMPS_snprintf_amps_uint64_t(buf,sz,val) sprintf_s(buf,sz,"%I64u",val) 46 #define AMPS_snprintf_sizet(buf,sz,val) sprintf_s(buf,sz,"%lu",val) 48 #define AMPS_snprintf_sizet(buf,sz,val) sprintf_s(buf,sz,"%u",val) 50 #else // VS2003 or older 51 #define AMPS_snprintf _snprintf 53 #define AMPS_snprintf_sizet(buf,sz,val) _sprintf(buf,sz,"%lu",val) 55 #define AMPS_snprintf_sizet(buf,sz,val) _sprintf(buf,sz,"%u",val) 59 #define AMPS_snprintf snprintf 61 #define AMPS_snprintf_amps_uint64_t(buf,sz,val) snprintf(buf,sz,"%lu", (unsigned long)val) 62 #define AMPS_snprintf_sizet(buf,sz,val) snprintf(buf,sz,"%lu",val) 64 #define AMPS_snprintf_amps_uint64_t(buf,sz,val) snprintf(buf,sz,"%llu",val) 65 #define AMPS_snprintf_sizet(buf,sz,val) snprintf(buf,sz,"%u",val) 90 Field() : _data(NULL), _len(0) {;}
91 Field(
const char *data_)
94 _len = ::strlen(data_);
96 Field(
const char *data_,
size_t len_)
112 Field(
const std::string& string_)
114 _data = string_.c_str();
115 _len = string_.length();
118 bool contains(
const char* searchString,
size_t len)
const 120 const char *dataEnd = _data+_len;
121 return std::search(_data,dataEnd,searchString,searchString+len) != dataEnd;
134 operator std::string ()
const 136 return _len?std::string(_data, _len):std::string();
146 if ( _len == rhs_._len )
147 return ::memcmp(_data, rhs_._data, _len) == 0;
160 return (!_data && !rhs_);
162 return ::strncmp(_data, rhs_, _len) == 0 && rhs_[_len] ==
'\0';
165 bool operator<(
const Field& rhs)
const;
174 if ( _len == rhs_._len )
175 return ::memcmp(_data, rhs_._data, _len) != 0;
186 return strncmp(_data, rhs_, _len) != 0 || rhs_[_len] !=
'\0';
196 return rhs_.compare(0, rhs_.length(), _data, _len) != 0;
206 return rhs_.compare(0, rhs_.length(), _data, _len) == 0;
217 _data =
new char[orig_._len];
218 ::memcpy(static_cast<void*>(const_cast<char*>(_data)),
219 orig_._data, orig_._len);
243 if (!_data || !_len)
return;
264 void assign(
const char*ptr,
size_t len)
271 inline size_t hash_function()
const 274 const char* p_ = _data;
278 c = (
unsigned long)*p_;
287 size_t operator()(
const Field& f)
const 289 return f.hash_function();
292 bool operator()(
const Field& f1,
const Field& f2)
const 294 if (f1.
len() < f2.
len())
return true;
295 if (f1.
len() > f2.
len())
return false;
297 if (f1.
len() == 0)
return true;
303 static bool isTimestamp(
const Field& field_)
305 return (field_.
len() >= AMPS_TIMESTAMP_LEN
306 && field_.
len() <= AMPS_TIMESTAMP_LEN_LONG
307 && field_.
data()[field_.
len()-1] ==
'Z' 308 && field_.
data()[8] ==
'T');
312 static void parseBookmark(
const Field& field_,
313 amps_uint64_t& publisherId_,
314 amps_uint64_t& sequenceNumber_)
316 publisherId_ = sequenceNumber_ = (amps_uint64_t)0;
318 size_t len = field_.
len();
320 if (isTimestamp(field_))
return;
329 publisherId_ += (amps_uint64_t)(data[i] -
'0');
331 for(i=i+1; i<
len; ++i)
337 sequenceNumber_ *= 10;
338 sequenceNumber_ += (amps_uint64_t)(data[i] -
'0');
344 class BookmarkRange :
public Field 347 static bool isRange(
const Field& bookmark_)
349 return memchr(bookmark_.
data(),
':', bookmark_.
len()) != NULL;
353 :
Field(), _start(), _end(), _open(AMPS_UNSET_INDEX)
354 , _capacity(AMPS_UNSET_INDEX)
359 BookmarkRange(
const Field& field_)
360 :
Field(), _start(), _end(), _open(AMPS_UNSET_INDEX)
361 , _capacity(field_.
len())
366 void set(
const Field& field_)
378 bool foundSeparator =
false;
379 bool foundClose =
false;
380 for (
size_t i=0; i<_len; ++i)
388 if (foundClose || _open != AMPS_UNSET_INDEX)
413 if (foundClose || _open==AMPS_UNSET_INDEX)
418 else if (foundSeparator)
422 _end.assign(_data + i, 0);
425 else if (!_start.data())
427 _start.assign(_data + i, 0);
434 if (foundSeparator || foundClose || _open==AMPS_UNSET_INDEX
440 foundSeparator =
true;
442 if (_start.len() == 0)
445 _start.assign(_start.data(), i-(size_t)(_start.data()-_data));
453 if (foundClose || _open==AMPS_UNSET_INDEX || !_end.data())
464 _end.assign(_end.data(), i-(size_t)(_end.data()-_data));
471 if (_end.data() && _end.len() == 0)
474 _end.assign(_end.data(), i-(size_t)(_end.data()-_data));
477 else if (_start.data() && _start.len() == 0)
480 _start.assign(_start.data(), i-(size_t)(_start.data()-_data));
492 if (_start.empty() || _end.empty())
501 if (!_data || !_len)
return;
507 _open = AMPS_UNSET_INDEX;
513 bool isValid()
const {
return !
empty(); }
517 bool isStartInclusive()
const {
return _data[_open]==
'['; }
521 void makeStartExclusive() {
const_cast<char*
>(_data)[_open]=
'('; }
525 bool isEndInclusive()
const {
return _data[_len-1]==
']'; }
529 void makeEndExclusive() {
const_cast<char*
>(_data)[_len-1]=
')'; }
533 const Field& getStart()
const {
return _start; }
535 const Field& getEnd()
const {
return _end; }
539 void replaceStart(
const Field& start_,
bool makeExclusive_ =
false)
543 if (_capacity >= (start_.
len() + _end.len() + 3))
545 char*
data =
const_cast<char*
>(_data);
546 if (makeExclusive_) data[_open] =
'(';
549 data[0] = _data[_open];
552 if ((
size_t)(_end.data() - _data - 2) < start_.
len())
554 size_t newLen = start_.
len() + _end.len() + 3;
558 for (
size_t dest = newLen-1, src = _len-1;
559 src > _start.len(); --src, --dest)
561 data[dest] = _data[src];
563 if (data[src] ==
':')
565 _end.assign(data+dest+1, _end.len());
572 ::memcpy(static_cast<void*>(data+1), start_.
data(), start_.
len());
573 _start.assign(data+1, start_.
len());
579 _capacity = (4UL * AMPS_MAX_BOOKMARK_LEN + 6UL
580 >= start_.
len() + _end.len() + 3UL)
581 ? (4UL * AMPS_MAX_BOOKMARK_LEN + 6UL)
582 : (start_.
len() + _end.len() + 3UL);
583 char*
data =
new char[_capacity];
584 if (makeExclusive_) data[0] =
'(';
585 else data[0] = _data[_open];
587 ::memcpy(static_cast<void*>(data+1), start_.
data(), start_.
len());
588 _start.assign(data+1, start_.
len());
589 data[start_.
len() + 1] =
':';
590 ::memcpy(static_cast<void*>(data+start_.
len()+2), _end.data(),
592 _end.assign(data+start_.
len()+2, _end.len());
593 size_t len = start_.
len()+3+_end.len();
594 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:156
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:194
bool operator!=(const Field &rhs_) const
Comparison operator Returns true if self and rhs are not equivalent.
Definition: Field.hpp:172
bool operator==(const Field &rhs_) const
Comparison operator Returns `true' if self and rhs are equivalent, `false' otherwise.
Definition: Field.hpp:144
void clear()
Deletes the data associated with this Field, should only be used on Fields that were created as deepC...
Definition: Field.hpp:241
const char * data() const
Returns the (non-null-terminated) data underlying this field.
Definition: Field.hpp:251
bool empty() const
Returns 'true' if empty, 'false' otherwise.
Definition: Field.hpp:126
size_t len() const
Returns the length of the data underlying this field.
Definition: Field.hpp:258
bool operator!=(const char *rhs_) const
String comparison operator Returns true if self and rhs are not equivalent.
Definition: Field.hpp:184
Field represents the value of a single field in a Message.
Definition: Field.hpp:84
void deepCopy(const Field &orig_)
Makes self a deep copy of the original field.
Definition: Field.hpp:212
Definition: ampsplusplus.hpp:103
bool operator==(const std::string &rhs_) const
String comparison operator Returns `true' if self and rhs are equivalent.
Definition: Field.hpp:204
Field deepCopy() const
Makes a deep copy of self, returns it.
Definition: Field.hpp:231