X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;ds=sidebyside;f=libs%2Futil%2Fbuffer.h;h=a621f4920db076594ba8fdac241f573760156908;hb=550b552a0b1912ac0ef62ffc611e23860749df50;hp=9ce31d62a472b0242cff24137c1b7c5bf409cca1;hpb=a3d27eedb7107d7dd964fcc9d25c6464e3280b98;p=xonotic%2Fnetradiant.git diff --git a/libs/util/buffer.h b/libs/util/buffer.h index 9ce31d62..a621f492 100644 --- a/libs/util/buffer.h +++ b/libs/util/buffer.h @@ -1,6 +1,10 @@ #ifndef INCLUDED_BUFFER_H #define INCLUDED_BUFFER_H +#include +#include +#include + namespace u { using byte = char; @@ -59,9 +63,44 @@ namespace u { return this->data(); } - operator byte *() { + operator cstring() const { + return c_str(); + } + + std::size_t strlen() const { + return ::strlen(c_str()); + } + + byte *mut() { return this->data(); } + +// operator byte *() { +// return mut(); +// } + + void terminate(long offset = -1) { + if (offset < 0) { + offset += this->size(); + } + mut()[offset] = '\0'; + } + + void copy(cstring str, unsigned int offset = 0, unsigned int limit = 0) { + if (!limit) { + limit = this->size() - offset; + } + strncpy(mut() + offset, str, limit); + } + + __attribute__((format(printf, 2, 3))) + void sprintf(const char *format, ...) { + // todo: check for overflow + va_list argptr; + va_start(argptr, format); + vsprintf(this->data(), format, argptr); + va_end(argptr); + } }; template