53 lines
1.1 KiB
C++
53 lines
1.1 KiB
C++
#ifndef BUFFER_H
|
|
#define BUFFER_H
|
|
|
|
#include <cstring>
|
|
|
|
class Buffer {
|
|
private:
|
|
char* data_;
|
|
size_t size_;
|
|
size_t currentLength_;
|
|
const char separator_;
|
|
boolean overflow=false;
|
|
|
|
public:
|
|
Buffer(size_t size, const char separator = '\0')
|
|
: size_(size), currentLength_(0), separator_(separator) {
|
|
data_ = new char[size_];
|
|
data_[0] = '\0'; // Ensure the buffer is null-terminated
|
|
}
|
|
|
|
~Buffer() {
|
|
delete[] data_;
|
|
}
|
|
|
|
bool add(const char* str) {
|
|
size_t strLength = strlen(str);
|
|
if (currentLength_ + strLength + 1 + (currentLength_ > 0 && separator_ != '\0') < size_) {
|
|
if (currentLength_ > 0 && separator_ != '\0') {
|
|
strcat(data_, &separator_);
|
|
++currentLength_;
|
|
}
|
|
strcat(data_, str);
|
|
currentLength_ += strLength;
|
|
return true;
|
|
} else {
|
|
overflow=true;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void clear() {
|
|
data_[0] = '\0';
|
|
currentLength_ = 0;
|
|
overflow=false;
|
|
}
|
|
|
|
const char* get() const {
|
|
return data_;
|
|
}
|
|
};
|
|
|
|
#endif
|