c++ - How to write strings and integers in a ring buffer? -
how write strings , integers in ring buffer? write multiple strings , integers ring buffer c++ knowledge limited. if need more info, please let me know. appreciate can provide.
here integer , string variables want write , write function of ring buffer:
string payload; int byte_pos; size_t ringbuffer::write(u_char *data, size_t bytes) { if (bytes == 0) return 0; size_t capacity = capacity_; size_t bytes_to_write = std::min(bytes, capacity - size_); // write in single step if (bytes_to_write <= capacity - end_index_) { memcpy(data_ + end_index_, data, bytes_to_write); end_index_ += bytes_to_write; if (end_index_ == capacity) end_index_ = 0; } // write in 2 steps else { size_t size_1 = capacity - end_index_; memcpy(data_ + end_index_, data, size_1); size_t size_2 = bytes_to_write - size_1; memcpy(data_, data + size_1, size_2); end_index_ = size_2; } size_ += bytes_to_write; return bytes_to_write; }
you have convert std::string
variable c-style pointer char:
string payload; char* cpayload = payload.c_str(); int len = strlen(cpayload); ringbuffer::write(cpayload, len*sizeof(char));
Comments
Post a Comment